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

报错原因分析:

loss.data[0] 是pytorch0.3.1版本代码,在0.4-0.5版本的pytorch会出现警告,不会报错,但是0.5版本以上的pytorch就会报错

解决方法:

train_loss+=loss.data[0]

修改:

train_loss+=loss.item()
 

发布了302 篇原创文章 · 获赞 161 · 访问量 49万+

猜你喜欢

转载自blog.csdn.net/qq_32146369/article/details/102630400
今日推荐