【发布时间】:2020-02-20 08:00:05
【问题描述】:
我正在尝试查看我的图像分类器对每类数据的准确性。但是,我是一个初学者,我正在尝试使用 Pytorch 教程提供的代码来训练分类器。 当我运行代码时:
class_correct = list(0. for i in range(7))
class_total = list(0. for i in range(7))
with torch.no_grad():
for data in test_loader:
images, labels = data
outputs = network(images)
print(outputs.shape)
_, predicted = torch.max(outputs, 1)
c = (predicted == labels.argmax(dim=1)).squeeze()
print(c.shape)
for i in range(5):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
for i in range(7):
print('Accuracy of %5s : %2d %%' % (
classes[i], 100 * class_correct[i] / class_total[i]))
" 出现以下错误:
torch.Size([100, 7])
torch.Size([100])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-73-b4cabf540645> in <module>
11 for i in range(5):
12 label = labels[i]
---> 13 class_correct[label] += c[i].item()
14 class_total[label] += 1
15 for i in range(7):
TypeError: only integer tensors of a single element can be converted to an index
我知道问题出在标签张量上,但我不知道如何解决。 非常感谢任何帮助! 先谢谢大家了
在上一个单元格中,我运行了下面提供的代码以获得有效的整体准确性,但我在获取每个类的准确性时遇到了问题。
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
outputs = network(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels.float().argmax(dim=1)).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (
100 * correct / total))
【问题讨论】:
标签: python classification conv-neural-network