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
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(),