【问题标题】:Invalid combination of arguments - eq()无效的参数组合 - eq()
【发布时间】: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

快速搜索后,我发现该错误可能与predictionlabels 之间的比较有关,如SO question 所示。

知道如何解决这个问题吗?

【问题讨论】:

    标签: python numpy image-processing machine-learning pytorch


    【解决方案1】:

    为什么这里有.numpy() prediction = prediction.cpu().numpy()? 这样你就可以将 PyTorch 张量转换为 NumPy 数组,使其与 labels.data 比较的类型不兼容。

    删除 .numpy() 部分应该可以解决问题。

    【讨论】:

    • 这解决了问题,谢谢!我希望这对将来的某人有所帮助。
    猜你喜欢
    • 2019-07-26
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多