PyTorch loss写法的版本变更 | IndexError: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()` in C++ to convert a 0-dim tensor to a number

自己遇到了这个问题,记录一下

以下写法

print_loss = loss.data[0]

在PyTorch>=0.5时,报错

IndexError: invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item<T>() in C++ to convert a 0-dim tensor to a number

更正写法

print_loss = loss.item()
posted @ 2024-05-11 19:06  Mactor  阅读(58)  评论(0)    收藏  举报