Eq received an invalid combination of arguments - got (Variable), but expected one of (int value) didn't match because some of the arguments have invalid types: (Variable)

In process of calculate the correct rate,always occuring the error:
eq received an invalid combination of arguments - got (Variable), but expected one of:

  • (int value)
    didn’t match because some of the arguments have invalid types: (Variable)
  • (torch.cuda.LongTensor other)
    didn’t match because some of the arguments have invalid types: (Variable)

my code like this:
for step_test, (test_x, test_y) in enumerate(test_loader):
test_x = Variable(test_x,volatile=True).cuda()
test_y=Variable(test_y).cuda()
test_output = densenet(test_x)
pred =output.data.max(1)[1]
correct +=(pred==test_y).cpu().sum()
total=len(test_loader.dataset)
accuracy=100.*correct/float(total)
like correct +=(pred==test_y).cpu().sum(),have error?
Help me!!!
THANK YOU VERY MUCH!!!

The problem is that pred is a Tensor and test_y is a Variable.

Extract the Tensor data from test_y:

correct +=(pred==test_y.data).cpu().sum()
7 Likes

Thank you very much!I have solved it

Thank you!!! I’ve been trying to figure this out for entire day/two.

I’m dealing with NLP, the accuracy is very off from 28% to 200% and it fluctuates from these ranges.

I’m just starting off and I have no idea how to fix it other than maybe the inputs are wrong ?

if you are summing over LongTensor, converting it float before sum may help. Something like below:

            non_padding = target.ne(pad)
            correct = seqlist[step].view(-1).eq(target).masked_select(non_padding).float().sum().data[0]
            match += correct
            total += non_padding.float().sum().data[0]