简体   繁体   English

无效的参数组合 - eq()

[英]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 .快速搜索后,我发现错误可能与predictionlabels之间的比较有关,就像在这个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.

相关问题 pytorch中参数的无效组合 - An invalid combination of arguments in pytorch torch.addmm接收到无效的参数组合 - torch.addmm received an invalid combination of arguments nn.LSTM() 收到 arguments 的无效组合 - nn.LSTM() received an invalid combination of arguments conv1d() 收到 arguments 的无效组合 - conv1d() received an invalid combination of arguments $ eq无效语法Mongo DB - $eq invalid syntax Mongo DB Python:BERT Model 池错误 - mean() 收到了无效的 arguments 组合 - 得到(str,int) - Python: BERT Model Pooling Error - mean() received an invalid combination of arguments - got (str, int) torch.Tensor() new() 收到了无效的 arguments 组合 - got (list, dtype=torch.dtype) - torch.Tensor() new() received an invalid combination of arguments - got (list, dtype=torch.dtype) 如何解决“TypeError: max() 收到无效的参数组合 - 得到(线性,int),但预期”? - How can I solve "TypeError: max() received an invalid combination of arguments - got (Linear, int), but expected"? 我如何摆脱这个错误:max() 收到了一个无效的参数组合 - 得到了 (str, int),但需要以下之一:*(张量输入) - How do I get rid of this error: max() received an invalid combination of arguments - got (str, int), but expected one of: * (Tensor input) 具有两个参数的多进程itertool组合 - Multiprocess itertool combination with two arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM