liveBook · Manning

Negative log-likelihood

As explained in Deep Learning with PyTorch, Second Edition

A loss function that penalizes low predicted probability assigned to the correct class by taking the negative logarithm of that probability and summing over samples.

Negative log-likelihood (NLL) is a classification loss that penalizes assigning low probability to the correct class. It takes the negative logarithm of that probability and sums across samples. section 7.2.4

For each supervised sample:

  • Run the model forward, convert output scores into probabilities, select the probability assigned to the ground-truth class, take its logarithm, negate it, and add it to the loss. section 7.2.4
  • Low assigned probabilities produce sharply increasing loss, while probabilities above 0.5 reduce the loss more gradually. section 7.2.4
  • PyTorch's nn.NLLLoss expects log probabilities rather than probabilities, typically produced with nn.LogSoftmax. section 7.2.4

PyTorch image classification

A two-class CIFAR model can end with nn.LogSoftmax(dim=1), then use loss = nn.NLLLoss(). Passing the model output and the ground-truth label returns the sample loss.

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Defines the formula, explains probability sensitivity, gives calculation steps, and demonstrates PyTorch's log-probability convention.
Examples
Demonstrates CIFAR bird-versus-airplane classification with nn.LogSoftmax and nn.NLLLoss.
What it assumes
Assumes supervised classification, class labels, model outputs, and softmax probabilities; explains likelihood and the loss workflow.
What you can do after
Apply it via PyTorch by producing log probabilities and passing them with target class labels to nn.NLLLoss.
How it connects
Connects likelihood with softmax, log-softmax, cross-entropy, ground-truth labels, and supervised classification.

Also explained in

Compare how 7 books explain negative log-likelihood →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.