报错:invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number


报错:invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

解决方法:

That's because in PyTorch>=0.5, the index of 0-dim tensor is invalid. The master branch is designed for PyTorch 0.4.1, loss_val.data[0] works well.

Try to change

total_loss += loss_val.data[0] loss_values = [v.data[0] for v in losses]

to

total_loss += loss_val.data
loss_values = [v.data for v in losses]

might fix the problem.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM