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:


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