IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python
Posted 菜鸡小王的技术之路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python相关的知识,希望对你有一定的参考价值。
报错原因分析:
train_loss += loss.data[0] 是pytorch0.3.1版本代码,在0.4-0.5版本的pytorch会出现警告,不会报错,但是0.5版本以上的pytorch就会报错,总的来说是版本更新问题.
解决方法:
将datra[0]换成item() 即可
以上是关于IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python的主要内容,如果未能解决你的问题,请参考以下文章