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