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

Loss of a single batch is calculated as:

loss = criterion(predictions, target)

What does the function loss.item() specifically do? The values I got after debugging both are the same.

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