其他分享
首页 > 其他分享> > IndexError: invalid index of a 0-dim tensor.

IndexError: invalid index of a 0-dim tensor.

作者:互联网

代码中的这一句报错

logging.info('iter ' + repr(iteration) + '||Loss: %.4f, lr: %.5f||Timer: %.4f sec.' % (loss.data[0], optimizer.param_groups[0]['lr'], t1 - t0))

错误后有提示:Use tensor.item() to convert a 0-dim tensor to a Python number
所以将代码中的 .data[0] 改为 .item(),即

logging.info('iter ' + repr(iteration) + '||Loss: %.4f, Loss_l: %.4f, Loss_c: %.4f, lr: %.5f||Timer: %.4f sec.' % (loss.item(), loss_l.item(), loss_c.item(), optimizer.param_groups[0]['lr'], t1 - t0))

标签:index,%.,IndexError,tensor,Loss,item,lr,loss,4f
来源: https://blog.csdn.net/qq_44167992/article/details/88135195