Pytorch TypeError: eq() 接收到无效的参数组合

num_samples = 10def 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('当网络被强制预测时的预测结果')correct = 0total = 0for 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("准确率: %d %%" % (100 * correct / total))

错误:

correct += (predicted == labels).sum().item() TypeError: eq() 接收到无效的参数组合 - 接收到 (numpy.ndarray),但期望的是以下之一:  * (Tensor other)  不匹配,因为某些参数类型无效:(!numpy.ndarray!)* (Number other)  不匹配,因为某些参数类型无效:(!numpy.ndarray!)

*


回答:

您试图比较 predictedlabels。然而,您的 predicted 是一个 np.array,而 labels 是一个 torch.tensor,因此 eq()(即 == 操作符)无法在它们之间进行比较。
np.argmax 替换为 torch.argmax

 return torch.argmax(mean, dim=1)

这样应该就可以了。

Related Posts

如何从数据集中移除EXIF数据?

我在尝试从数据集中的图像中移除EXIF数据(这些数据将…

用于Python中的“智能点”游戏的遗传算法不工作

过去几天我一直在尝试实现所谓的“智能点”游戏。我第一次…

哪个R平方得分更有帮助?

data.drop(‘Movie Title’, ax…

使用线性回归预测GRE分数对录取率的影响

我正在学习线性回归,并尝试在Jupyter笔记本中用P…

使用mlrMBO贝叶斯优化进行SVM超参数调优时出现错误

我试图针对一个分类任务优化SVM,这个方法在许多其他模…

Keras模型的二元交叉熵准确率未发生变化

我在网上看到了很多关于这个问题的提问,但没有找到明确的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注