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

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


  
    
    
    Does .item() automatically move the data to the cpu? 
  
  
    Hi, 
Yes .item() moves the data to CPU. It converts the value into a plain python number. And plain python number can only live on the CPU.
  


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