Cross-Entropy Loss › Gradient Behavior and Why Cross-Entropy Works

Michael BrenndoerferFebruary 21, 20263 min read
Language AI HandbookAll chapters
Part 53 of 67Evaluation Fundamentals
Chapter 406Cross-Entropy Loss › Gradient Behavior and Why Cross-Entropy Works
Reading section 7 of 15 · Gradient Behavior and Why Cross-Entropy Works · 3 minSection 7 of 15 · 3 min

Book contents

Section 7 of 15 · 3 min read

Cross-Entropy Loss › Gradient Behavior and Why Cross-Entropy WorksOne section · Sections in this chapter7 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Understanding why cross-entropy produces good gradients requires examining what happens when we differentiate through the softmax and cross-entropy combination. This is one of those places where the mathematical elegance of the formulation reveals why certain design choices are not arbitrary but deeply principled.

The Softmax-Cross-Entropy Gradient

Let zkz_k be the unnormalized logit for class kk, and let y^k=softmax(zk)\hat{y}_k = \text{softmax}(z_k) be the predicted probability after normalization. The gradient of the cross-entropy loss with respect to the logit zjz_j is:

∂L∂zj=y^j−yj\frac{\partial \mathcal{L}}{\partial z_j} = \hat{y}_j - y_j

where:

  • ∂L∂zj\frac{\partial \mathcal{L}}{\partial z_j}: the gradient of the loss with respect to logit zjz_j
  • y^j\hat{y}_j: the model's predicted probability for class jj
  • yjy_j: the true label for class jj (1 for the true class, 0 otherwise)

This remarkably simple result comes from the cancellation of terms when differentiating through softmax and logarithm together. For the true class (where yj=1y_j = 1), the gradient is y^j−1\hat{y}_j - 1: a negative quantity that pushes the logit upward, increasing the true class probability. For all other classes (where yj=0y_j = 0), the gradient is y^j\hat{y}_j: a positive quantity that pushes those logits downward, reducing their probability. The gradient magnitude is proportional to the prediction error: when the model is right with high confidence (y^j≈1\hat{y}_j \approx 1), gradients are near zero. When the model is wrong with high confidence (the model assigned probability near 1 to the wrong class), gradients are large.

This gradient formula has another important property: it is bounded. No matter how wrong the model is, the gradient of cross-entropy with respect to logits is at most 1.0 in magnitude. Compare this to squared error loss, where gradients grow without bound as predictions diverge from targets. The bounded gradients of cross-entropy make training more stable and reduce the need for gradient clipping in practice. As discussed in Gradient Clipping, managing gradient magnitude is important for stable transformer training, and the naturally bounded logit gradients from cross-entropy help.

Why Cross-Entropy Beats Mean Squared Error for Classification

A natural question arises: why not use mean squared error (MSE) as a classification loss? MSE between predicted probabilities and one-hot targets is:

LMSE=1K∑k=1K(yk−y^k)2\mathcal{L}_{\text{MSE}} = \frac{1}{K} \sum_{k=1}^{K} (y_k - \hat{y}_k)^2

The gradient of MSE with respect to logits, after accounting for the softmax, involves terms like (y^j−yj)⋅y^j(1−y^j)(\hat{y}_j - y_j) \cdot \hat{y}_j(1 - \hat{y}_j). When the model is very wrong (say, y^j≈0\hat{y}_j \approx 0 for the true class), this gradient includes the factor y^j(1−y^j)≈0\hat{y}_j(1 - \hat{y}_j) \approx 0, which nearly vanishes. This is the saturation problem: MSE produces tiny gradients exactly when the model is most wrong and needs correction most urgently. Cross-entropy avoids this problem because its gradient is simply y^j−yj\hat{y}_j - y_j, which remains large when the model is wrong. This is why cross-entropy consistently outperforms MSE for training classifiers, and why you will never see MSE used as the training objective for a language model.