How do you derive the gradient of the softmax cross-entropy loss with respect to the logits?
Deep learning, Backpropagation
Level L3 Intermediate. Length Short. Nature Mat Mathematical.
What's being asked
Whether you can do a basic and very common derivation. It tests careful use of the chain rule and indices.
Key idea
Let be the logits (the raw scores before softmax), and a one-hot label (1 for the true class, 0 elsewhere). The loss has gradient .
Steps
- Write and .
- Simplify first, because it avoids the full softmax Jacobian: , so (using ).
- Differentiate: .
- Interpret: the gradient is the prediction error. It is bounded, which is one reason this pairing trains well.
- Mention numerical stability: subtract before exponentiating (the log-sum-exp trick). This stops from overflowing and does not change .
Common mistakes
- Working through the full softmax Jacobian and making an index error, when the log-sum-exp route is shorter.
- Forgetting that .
- Implementing softmax and log separately in code, which is numerically unstable.
How long your answer should be
Under 2 minutes; four lines.
Last checked on 4 December 2026.
Read this
Previous in Deep learning: A tiny network has input , two hidden units with and , and a linear output . Here . Compute the hidden values, the output , and the squared error for the target ?
Next in Deep learning: Suppose you train a binary classification network with a sigmoid output using mean-squared error (MSE) instead of cross-entropy. Is training still useful, judging by the gradient of each loss with respect to the logit (the input to the sigmoid)? How do the two losses look as functions of for a positive example?