leonardofmed
leonardofmed

Reputation: 835

Invalid combination of arguments - eq()

I'm using a code shared here to test a CNN image classifier. When I call the test function, I got this error on line 155:

test_acc += torch.sum(prediction == labels.data)
TypeError: eq() received an invalid combination of arguments - got (numpy.ndarray), but expected one of:
 * (Tensor other)
      didn't match because some of the arguments have invalid types: ([31;1mnumpy.ndarray[0m)
 * (Number other)
      didn't match because some of the arguments have invalid types: ([31;1mnumpy.ndarray[0m)

Fragment of the test function:

def test():
    model.eval()
    test_acc = 0.0
    for i, (images, labels) in enumerate(test_loader):

        if cuda_avail:
                images = Variable(images.cuda())
                labels = Variable(labels.cuda())

        #Predict classes using images from the test set
        outputs = model(images)
        _,prediction = torch.max(outputs.data, 1)
        prediction = prediction.cpu().numpy()
        test_acc += torch.sum(prediction == labels.data) #line 155



    #Compute the average acc and loss over all 10000 test images
    test_acc = test_acc / 10000

return test_acc

After a quick search I see that the error is probably related to the comparison between the prediction and labels, as seem in this SO question.

Any idea on how to fix this?

Upvotes: 0

Views: 1147

Answers (1)

Sergii Dymchenko
Sergii Dymchenko

Reputation: 7209

Why do you have .numpy() here prediction = prediction.cpu().numpy()? That way you convert PyTorch tensor to NumPy array, making it incompatible type to compare with labels.data.

Removing .numpy() part should fix the issue.

Upvotes: 1

Related Questions