liveBook · Manning

NLLLoss

As explained in Natural Language Processing in Action, Second Edition

NLLLoss is a PyTorch loss function that computes negative log likelihood loss for model outputs and target classes.

NLLLoss measures negative log likelihood during a PyTorch optimization loop. Its log-based score can be converted back into a likelihood, making model confidence easier to inspect and explain. section 8.2.3

Turning log loss into likelihood

For the name “Khalid,” a model reports log losses of −1.17, −1.35, and −1.80 for different nationality predictions. Applying the exponential gives likelihoods of 0.31, 0.26, and 0.17, respectively.

Because the predictions are probabilities, inspecting NLLLoss-related outputs helps communicate how confident a model is to teammates, managers, or users. Treat those confidence values as evidence to examine, not as guarantees. section 8.2.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains the log-to-likelihood transformation and confidence interpretation, but does not detail the loss implementation or tensor interface.
Examples
Uses ranked nationality predictions for “Khalid,” showing log losses alongside exponentiated likelihoods.
What it assumes
Assumes familiarity with natural logarithms and asks readers to understand how exponentiation inverts the log operation.
What you can do after
Evaluate model confidence by converting log-loss outputs into likelihoods and inspecting ranked predictions.
How it connects
Connects negative log likelihood with probability, model confidence, PyTorch, and recurrent neural network predictions.

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.