What is the difference between loss and loss.item()

Relevant links:

https://pytorch.org/docs/stable/generated/torch.Tensor.item.html

Tensor. item () → number

Returns the value of this tensor as a standard Python number. This only works for tensors with one element.

So, basically loss is one-element PyTorch tensor in your case, and .item() converts its value to a standard Python number.

1 Like