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()


版权声明:本文为weixin_44478378原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。