Search code examples
pytorchclassificationloss-function

Understanding of Pytorch NLLLOSS


PyTorch's negative log-likelihood loss, nn.NLLLoss is defined as:

enter image description here

So, if the loss is calculated with the standard weight of one in a single batch the formula for the loss is always:

-1 * (prediction of model for correct class)

Example:

enter image description here

Correct Class = 0

prediction of model for correct class = 0.5

loss = -1 * 0.5

So, why is it called the "negative log-likelihood loss", if there isn't a log function involved in calculating the loss? ​


Solution

  • Indeed no log is being used to compute the result of nn.NLLLoss so this can be a little confusing. However, I believe the reason why it was called this way is because it expects to receive log-probabilities:

    The input given through a forward call is expected to contain log-probabilities of each class. - docs

    In the end it does not make much sense to have it in the name since you might as well want to apply this function on non-log-probabilities...