Answers · The mathematics underneath
Why does softmax followed by cross entropy have gradient p minus y?
In shortBecause the derivative of the loss through softmax simplifies to the predicted probability minus the target, one number per class, with the exponentials and fractions cancelling out.
The gradient of the pair, taken with respect to the scores that go into the softmax, is : the predicted probability minus the target, one number per option. Neither the exponentials nor the division of the softmax survives into the answer, and there is no special case for the position that happens to be correct. What arrives back at the last layer is a subtraction whose size is the error.
Two options, by hand
Two scores, for the correct option and for the other, is the smallest case that shows the whole thing. The loss is the negative logarithm of the first probability, and splitting that logarithm leaves an expression with no fraction in it.
Differentiate against . The contributes . The logarithm contributes the derivative of its argument divided by the argument, which is , and that is exactly , the probability the model put on the correct option. So the derivative against is . Differentiate against and the first term contributes nothing, while the second contributes . The target for the correct option is one and the target for a wrong one is zero, so both results are the same sentence: probability minus target.
With options and the correct one at index , nothing changes. The loss is minus the correct score plus the logarithm of the sum of all the exponentials, and every option is differentiated the same way.
The matrix that gets cancelled
The reasonable expectation is that something unpleasant survives. Softmax does have an unpleasant derivative: each probability depends on every score, so the matrix of partial derivatives is full rather than diagonal, and a change to one score moves every output. Anyone who differentiates the two operations in turn and stops halfway has a right to expect a mess.
Read the middle of that: the in front of the Jacobian cancels the in the denominator of the loss derivative exactly, in every term, which is why no fraction is left. What remains is plus multiplied by the sum of the targets. Since the targets are one for a single option and zero elsewhere, that sum is one, and times one is just . The Jacobian was never absent. It was multiplied by the one thing that undoes it.
| step | probability on the correct option | gradient there | largest wrong-option gradient | what happened |
|---|---|---|---|---|
| 1 | 0.99 | -0.01 | 0.01 | Confidently right. Almost nothing travels back and the weights barely move. |
| 2 | 0.7 | -0.3 | 0.3 | Right but unsure. A moderate push towards more confidence. |
| 3 | 0.5 | -0.5 | 0.5 | Undecided between two options. A large correction. |
| 4 | 0.02 | -0.98 | 0.98 | Confidently wrong, the largest signal the loss can produce. |
The numbers are the subtraction, done. A correct option sitting at probability sends back , and a wrong option at sends . A confident mistake at on the correct option sends there and on the wrong one, close to the ceiling: with probabilities trapped in the unit interval no single entry of this gradient can exceed one in size.
That the algebra lands this cleanly is why libraries compute the two steps as one operation. Separating them means storing the probabilities for the backward pass, running a full matrix product where a subtraction would do, and taking the logarithm of a probability that may have underflowed to zero, which is not a number at all. The failure lands precisely on the confidently wrong examples, the ones carrying the most signal.
Where the difference stops appearing
Change either half of the pair and the cancellation goes with it. Put a squared error on the softmax output instead of a logarithm and the derivative against the scores keeps the whole Jacobian, , which does not reduce to a difference. Keep the cross entropy but divide the scores by a temperature and the same argument leaves : the shape survives, the scale does not. The identity also leans on the targets summing to one. Normalised targets, smoothed ones included, are fine; a label vector summing to something else leaves with a term that should not be there.
Worth keeping
- The gradient of softmax followed by cross entropy, with respect to the scores, is the predicted probability minus the target, one entry per option.
- The softmax Jacobian is full, but the loss derivative carries a factor of one over the probability that cancels it term by term.
- Because probabilities lie between zero and one, no entry of this gradient can exceed one in size, so a confident correct answer barely moves the weights.
Where this goes next
This question is one step of Backpropagation in Full, a course that works through its subject from the beginning. The page that teaches this part of it is The Layer Where Everything Cancels, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- Why does backpropagation never build the full Jacobian?Backpropagation only needs a vector multiplied by each layer's derivative matrix, so it computes that product directly and never builds the matrix.
- Why do derivatives multiply along a chain but add when the path splits?Along a chain each step scales the effect that arrived, so the scalings multiply; at a split each use contributes its own effect to the same change, so the contributions add.
- What does a computation graph actually let you differentiate?It lets you differentiate a final number with respect to any earlier value in the graph by multiplying local derivatives along each route and adding across routes.
- Why does adding a shortcut connection stop gradients from vanishing?A shortcut gives the gradient a route whose factor is one plus the layer's own factor, so many layers in series no longer multiply the signal down to nothing.
- What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
- How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.