Understanding the use of classes when training a neural network with Pytorch
I'm currently reading about using the cross-entropy loss to train a neural network in the pytorch documentation. The criterion that is to be used to calculate the loss is called as follows

criterion = nn.CrossEntropyLoss()


According to the torch.nn documentation, CrossEntropyLoss is a class. From my understanding this would mean that criterion is an object of type nn.CrossEntropyLoss. 

When training the neural network, criterion is used to calculate the loss in the following way

loss = criterion(input, target)


This is somewhat confusing to me. If criterion is an object, then how can it be used as a function? What I would expect is something like

loss = criterion.calculate_loss(input, target)


where calculate_loss() would be a method defined in the nn.CrossEntropyLoss class. Furthermore, the documentation also uses the following line of code

running_loss += loss.item()


Where does this item() method come from? I can't find a source mentioning item() online.


  If criterion is an object, then how can it be used as a function?


The criterion object has a forward method that is called in that case. criterion(input, target) is shorthand for criterion.forward(input, target)


  Where does this item() method come from?


This method returns a one dimensional Tensor. The single value can be accessed as a number with item().
