This is a classic result in deep learning that I should derive carefully. Let me work through the mathematics of computing the gradient of cross-entropy loss with respect to the logits (before softmax).
Setup
Let's denote:
- z = logits (raw outputs before softmax)
- a = softmax outputs = softmax(z)
- y = one-hot encoded target labels
- L = cross-entropy loss
Cross-Entropy Loss
The cross-entropy loss is defined as:
L=−∑i=1Cyilo
where C is the number of classes, yi is the one-hot encoded target (1 for the true class, 0 for others), and ai is the softmax output.
Softmax Function
The softmax function is defined as:
aj=∑k=1
Let me denote the denominator as Z=∑k=1Cez for convenience.
Step 1: Chain Rule
We need to find ∂zj∂L:
Step 2: Derivative of Loss with Respect to Softmax Output
∂ai∂L=−
Step 3: Derivative of Softmax with Respect to Logits
This is the tricky part. We need ∂zj∂ai.
ai=Zez
Using the quotient rule, I need to handle two cases separately. When i=j, the numerator's derivative contributes ezi while the denominator's derivative is , giving:
When i=j, only the denominator changes, so:
∂
These combine into the compact form ∂zj∂a.
Now applying the chain rule with the loss gradient:
∂zj
The Kronecker delta isolates the i=j term, leaving −yj+a since the labels sum to 1.
In vector form, this simplifies to ∂z∂L=a−y—the predicted probabilities minus the target. This elegant result means the gradient directly reflects how far predictions deviate from the true label, with negative gradients when the model is overconfident on the correct class. _j + \log\left(\sum_k e^{z_k}\right)$$
Taking the derivative with respect to zj:
∂z
For the general case where y is one-hot, the gradient becomes:
∂zj∂L
This shows the same result: the model adjusts logits based on the difference between predicted and true probabilities, pushing down incorrect class scores while pulling up the correct one. The gradient simplifies to ∂zj∂L=, which is the standard form I should use going forward.
Gradient of Cross-Entropy Loss w.r.t. Softmax Logits
Problem Setup
Let me derive the gradient of cross-entropy loss with respect to logits (raw network outputs).
Notation:
- z = logits (inputs to softmax)
- a=softmax(z) = predictions
- y = one-hot target labels
- L = cross-entropy loss
The Loss Function
L=−∑i=1Cyilog
where ai=∑ke
Derivation
Method 1: Direct Expansion (Cleanest)
Rewrite the loss by expanding the log-softmax:
L=−∑iyilog(
=−∑iyi[z
=−∑iyiz
=−∑iyizi
Now take the derivative with respect to zj:
∂zj∂L=
=−yj+∑k
=−yj+∑k
=aj−yj
Method 2: Via Chain Rule
For completeness, using the chain rule:
∂zj∂L
Step 1: ∂ai∂L=−
Step 2: For softmax derivatives:
∂zj∂a
Step 3: Combine:
∂zj
The Elegant Result
∇zL=a−y
In words: The gradient is simply predictions minus targets.
Interpretation
| Scenario | Gradient | Meaning |
|---|
| Correct class (y=1) | aj−1<0 | Pushes logits down when confident (aj) |
Example: If softmax outputs [0.7,0.2,0.1] for true class [1,0,0]:
∇
The largest error (−0.3) occurs on the correct class where confidence is insufficient.
Why This Matters
- Simple implementation: Backprop is just
(softmax_output - target)
- Well-conditioned gradients: Avoids numerical issues from computing softmax derivatives separately
- Intuitive: Directly encodes the prediction error
- Generalizes: This same elegant form applies to multi-class entropy loss more broadly