Relevant links:
https://pytorch.org/docs/stable/generated/torch.Tensor.item.html
Tensor.
item
() → numberReturns 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.