欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-13 17:19:23
...

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
train_loss += loss.data[0] 是pytorch0.3.1版本代码
解决方法:

#将原语句:

loss_l.data[0], loss_c.data[0],

loss_l.data[0], loss_c.data[0],

#修改为:

loss_l.item(), loss_c.item(),

loss_l.item(), loss_c.item(),