报错:
running_loss += loss.data[0]
Traceback (most recent call last):
File "/B/Practice/csdncifar-10.py", line 98, in <module>
running_loss += loss.data[0]
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
更改:
running_loss += loss.item ()
本文解决了一个常见的PyTorch编程问题,即在尝试将0维张量用作索引时引发的错误。通过将'running_loss+=loss.data[0]'更改为'running_loss+=loss.item()',有效地避免了IndexError,并确保了代码的正常运行。
4968

被折叠的 条评论
为什么被折叠?



