如何修复alexNet中的0暗张量的无效索引。使用tensor.item()将0维的张量转换为Python数字

时间:2020-03-18 11:16:15

标签: python torch mnist

我正在使用MNIST数据集处理AlexNet拱门,下面是以下代码

model = AlexNet()
if torch.cuda.is_available():
    model.cuda()

optimizer = optim.SGD(model.parameters(), lr=0.01)


train_losses = []
test_losses =[]
test_accuracy = []
for epoch in range(1, 15):
    train(epoch)
    test()
plt.figure()
plt.plot(train_losses)
plt.title('train losses')
plt.xlabel('step')
plt.ylabel('percent')

plt.figure()
plt.plot(test_losses)
plt.title('test losses')
plt.xlabel('step')
plt.ylabel('percent')

plt.figure()
plt.plot(test_accuracy)
plt.title('test_accuracy')
plt.xlabel('step')
plt.ylabel('percent')

运行该单元格后,出现以下错误:


IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

任何帮助都会让您很感激

0 个答案:

没有答案