PyTorch Error fix 汇总

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

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

报错原因: pytorch版本问题
loss.data[0] 是pytorch0.3.1版本代码
在0.4-0.5版本的pytorch会出现警告,不会报错;
在0.5版本以上的pytorch就会报错。

解决方法:
loss.data[0]修改为:loss.item()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章