liveBook · Manning

Cross entropy loss

As explained in Deep Learning with PyTorch, Second Edition

A loss value based on cross-entropy, used here as the quantity minimized during training.

For a classifier, the loss needs to say how well its output matches the correct class. Cross-entropy loss does this by combining softmax with negative log likelihood: softmax turns class scores into probabilities, and negative log likelihood penalizes the probability assigned to the correct class. section 7.5

In PyTorch, pass logits—the class scores before softmax—directly to cross-entropy loss. This gives the same result as passing the output of nn.LogSoftmax to nn.NLLLoss. Do not add a softmax output layer before using the direct-logits approach. section 7.2.4

Choosing a loss for two classes

A bird-versus-airplane network produces two class scores. Those scores can go directly to cross-entropy loss; alternatively, a network ending in nn.LogSoftmax can use nn.NLLLoss with a class index of 0 or 1. In a separate nodule-versus-non-nodule dataset, the classification tensor has two elements, one for each candidate class, because the classifier provides one output per class. section 7.2.4 section 12.5

Figure 7.11 The cross-entropy (left) and MSE between predicted probabilities and the target probability vector (right) as functions of the predicted scores—that is, before the (log-) softmax.
figure

Compare the loss curves against mean squared error (MSE). Cross-entropy retains a useful slope even for nearly correct predictions; MSE can become too flat, including when predictions are substantially wrong. section 7.2.4

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains its equivalence to LogSoftmax plus NLLLoss and compares gradient behavior with MSE, without deriving the loss mathematically.
Examples
Uses bird-versus-airplane predictions, a plotted loss comparison, and a two-class nodule dataset.
What it assumes
Builds from class scores and softmax probabilities rather than assuming familiarity with cross-entropy.
What you can do after
Apply it via PyTorch by supplying logits directly, or use LogSoftmax with NLLLoss instead.
How it connects
Connects logits, softmax, negative log likelihood, class indices, and mean squared error.

Also explained in

Compare how 19 books explain cross entropy loss →

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.