【问题标题】:Pytorch TypeError: eq() received an invalid combination of argumentsPytorch TypeError: eq() 收到了无效的参数组合
【发布时间】:2019-07-26 18:00:30
【问题描述】:
num_samples = 10
def predict(x):
    sampled_models = [guide(None, None) for _ in range(num_samples)]
    yhats = [model(x).data for model in sampled_models]
    mean = torch.mean(torch.stack(yhats), 0)
    return np.argmax(mean.numpy(), axis=1)

print('Prediction when network is forced to predict')
correct = 0
total = 0
for j, data in enumerate(test_loader):
    images, labels = data
    predicted = predict(images.view(-1,28*28))
    total += labels.size(0)
    correct += (predicted == labels).sum().item()
print("accuracy: %d %%" % (100 * correct / total))

错误

correct += (predicted == labels).sum().item() 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: (!numpy.ndarray!)
* (Number other)
  didn't match because some of the arguments have invalid types: (!numpy.ndarray!)

*

【问题讨论】:

    标签: numpy image-processing machine-learning computer-vision pytorch


    【解决方案1】:

    您正在尝试比较 predictedlabels。但是,您的 predictednp.arraylabelstorch.tensor 因此 eq()== 运算符)无法在它们之间进行比较。
    np.argmax 替换为torch.argmax

     return torch.argmax(mean, dim=1)
    

    你应该没事的。

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 2019-08-04
      • 1970-01-01
      • 2019-06-12
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 2021-08-10
      相关资源
      最近更新 更多