欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-17 11:24:46
...
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()

相关标签: Pytorch