liveBook · Manning

Negative log-likelihood

As explained in Natural Language Processing in Action, Second Edition

Negative log likelihood is the negative of the logarithm of a model-assigned likelihood, commonly minimized as a probabilistic prediction loss.

Negative log-likelihood (NLL) measures a model’s probabilistic prediction loss: minimizing it rewards assigning higher likelihood to the correct outcome. In PyTorch, NLLLoss appears in the optimization loop. Because logarithms are natural logs here, exponentiating a log-loss recovers the corresponding likelihood and makes model confidence easier to communicate. section 8.2.3

Reading model confidence

For the name Khalid, a log-loss of −1.17 becomes a likelihood of 0.31 after applying exp; the model therefore assigns Algerian as its top prediction with 31% confidence, followed by Moroccan at 26%. section 8.2.3

>>> predictions = topk_predictions(model, 'Khalid', topk=4)
>>> predictions['likelihood'] = np.exp(predictions['log_loss'])
>>> predictions
        text  log_loss nationality  likelihood
rank
0     Khalid     -1.17    Algerian        0.31
1     Khalid     -1.35    Moroccan        0.26
2     Khalid     -1.80   Malaysian        0.17
3     Khalid     -2.40      Arabic        0.09

Inspect ranked predictions, their log-loss values, and the exponentiated likelihoods side by side. section 8.2.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains logarithm inversion and likelihood interpretation, then stops short of a deeper derivation of the loss.
Examples
Uses a nationality-prediction model, the name Khalid, ranked log-loss values, and exponentiated likelihoods.
What it assumes
Assumes familiarity with logarithms, while clarifying that log denotes the natural logarithm.
What you can do after
Evaluate/understand model confidence by converting log-loss outputs into likelihoods and inspecting ranked predictions.
How it connects
Links probabilistic loss to RNNs, LSTMs, PyTorch optimization, and model-output inspection.

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 Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.