[英]Invalid combination of arguments - eq()
I'm using a code shared here to test a CNN image classifier.我正在使用此处共享的代码来测试 CNN 图像分类器。 When I call the test function, I got this error on line 155 :当我调用测试函数时,我在第 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: test
函数的片段:
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 .快速搜索后,我发现错误可能与prediction
和labels
之间的比较有关,就像在这个SO 问题中一样。
Any idea on how to fix this?关于如何解决这个问题的任何想法?
Why do you have .numpy()
here prediction = prediction.cpu().numpy()
?为什么这里有.numpy()
prediction = prediction.cpu().numpy()
.numpy()
prediction = prediction.cpu().numpy()
? That way you convert PyTorch tensor to NumPy array, making it incompatible type to compare with labels.data
.这样你就可以将 PyTorch 张量转换为 NumPy 数组,使其成为与labels.data
比较的不兼容类型。
Removing .numpy()
part should fix the issue.删除.numpy()
部分应该可以解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.