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