Pytorch报错IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to

IndexError                                Traceback (most recent call last)
<ipython-input-23-4ba7a4acb927> in <module>
     19 
     20     if (epoch+1) % 20 == 0:
---> 21         print('Epoch[{}/{}], loss: {:.6f}'.format(epoch+1, num_epochs, loss.data[0]))

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

解决:

loss.data[0 ] 改成loss.item()

发布了165 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44478378/article/details/104313816