PyTorch Error fix-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版本問題
tensor.data[0] 是pytorch0.3.1版本代碼
在0.4-0.5版本的pytorch會出現警告,不會報錯;
在0.5版本以上的pytorch就會報錯。

解決方法:
tensor.data[0]修改爲:tensor.item()

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