【发布时间】:2019-08-04 11:05:14
【问题描述】:
我正在使用共享代码here 来测试 CNN 图像分类器。当我调用测试函数时,我在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)
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
快速搜索后,我发现该错误可能与prediction 和labels 之间的比较有关,如SO question 所示。
知道如何解决这个问题吗?
【问题讨论】:
标签: python numpy image-processing machine-learning pytorch