IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python
程序员文章站
2024-03-17 11:33:28
...
刚开始学习PyTorch机器学习从入门到实战,运行随书代码,出现的错误,想着整理总结一下,日后可以进行回忆和学习。
报错原因分析:
loss = output.data[0]
是pytorch0.3版本的代码,在0.4-0.5版本的pytorch会出现警告,不会报错,但是0.5版本以上的pytorch就会报错,自己安装的pytorch的版本是1.3.1,总的来说是版本更新问题。
解决方法:
#将原语句:
loss = output.data[0]
#修改为:
loss = output.item()
下一篇: Centos后台运行jar包
推荐阅读
-
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python
-
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python
-
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python
-
invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a python number
-
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python
-
Pytorch报错IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to
-
解决IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Pyth
-
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python