TypeError: argument 0 is not a Variable

I got this error when run this model:
class VAMetric(nn.Module):
def init(self):
super(VAMetric, self).init()
self.cnnv1 = nn.Conv2d(1, 128, 5, stride=1, padding=2)
self.cnnv2 = nn.Conv2d(128, 1, 5, stride=1, padding=2)

    self.cnna1 = nn.Conv2d(1, 96, 5, stride=1, padding=2)
    self.cnna2 = nn.Conv2d(96, 1, 5, stride=1, padding=2)

    self.fca = nn.Linear(128, 32)
    self.fcv = nn.Linear(1024, 32)
    self.fc1 = nn.Linear(64, 1)
    self.fc2 = nn.Linear(120, 1)
    self.init_params()

def init_params(self):
    for m in self.modules():
        if isinstance(m, nn.Linear):
            nn.init.xavier_uniform(m.weight)
            nn.init.constant(m.bias, 0)

def forward(self, vfeat, afeat):
    vfeat_1 = torch.FloatTensor(1, 64, 120, 1024).zero_()
    afeat_1 = torch.FloatTensor(1, 64, 120, 128).zero_()

    vfeat_t = Variable(vfeat_1)
    afeat_t = Variable(afeat_1)
    vfeat_t[0] = vfeat
    afeat_t[0] = afeat
    vfeat_t = torch.transpose(vfeat_1, 0, 1)
    afeat_t = torch.transpose(afeat_1, 0, 1)

    vfeat_t = self.cnnv1(vfeat_t)
    afeat_t = self.cnna1(afeat_t)
    vfeat_t = self.cnnv2(vfeat_t)
    afeat_t = self.cnna2(afeat_t)

    vfeat_t = F.sigmoid(self.fcv(vfeat_t))
    afeat_t = F.sigmoid(self.fca(afeat_t))

    feat = F.sigmoid(self.fc1(torch.cat((vfeat_t, afeat_t), 3)))
    feat_ = F.sigmoid(self.fc2(torch.transpose(feat, 2, 3)))
    return feat_[0][0]

I scan the same topic and got the idea to update my pytorch to 0.1.2, but I am on pytorch 0.2.0

that is:
Traceback (most recent call last):
File β€œtrain.py”, line 207, in
main()
File β€œtrain.py”, line 194, in main
train(train_loader, model, criterion, optimizer, epoch, opt)
File β€œtrain.py”, line 116, in train
sim = model(vfeat_var, afeat_var) # inference simialrity
File β€œ/home/srt/kk/local/lib/python2.7/site-packages/torch/nn/modules/module.py”, line 224, in call
result = self.forward(*input, **kwargs)
File β€œ/home/data1/kk15/models.py”, line 39, in forward
vfeat_t = self.cnnv1(vfeat_t)
File β€œ/home/srt/kk/local/lib/python2.7/site-packages/torch/nn/modules/module.py”, line 224, in call
result = self.forward(*input, **kwargs)
File β€œ/home/srt/kk/local/lib/python2.7/site-packages/torch/nn/modules/conv.py”, line 254, in forward
self.padding, self.dilation, self.groups)
File β€œ/home/srt/kk/local/lib/python2.7/site-packages/torch/nn/functional.py”, line 52, in conv2d
return f(input, weight, bias)
TypeError: argument 0 is not a Variable

maybe check this…

input must be Variable. (from torch.autogad import Variable)

input = Variable(x)
model(input)

1 Like