Basis Lab

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 zˉi=pi−ti\bar z_i = p_i - t_i: 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, aa for the correct option and bb 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.

L=−log⁡eaea+eb=−a+log⁡(ea+eb)L = -\log\frac{e^{a}}{e^{a}+e^{b}} = -a + \log\left(e^{a}+e^{b}\right)

Differentiate against aa. The −a-a contributes −1-1. The logarithm contributes the derivative of its argument divided by the argument, which is ea/(ea+eb)e^{a}/(e^{a}+e^{b}), and that is exactly p1p_1, the probability the model put on the correct option. So the derivative against aa is p1−1p_1 - 1. Differentiate against bb and the first term contributes nothing, while the second contributes eb/(ea+eb)=p2e^{b}/(e^{a}+e^{b}) = p_2. 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 nn options and the correct one at index kk, 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.

L=−zk+log⁡∑jezj,∂L∂zi=−1[i=k]+ezi∑jezj=pi−tiL = -z_k + \log\sum_j e^{z_j}, \qquad \frac{\partial L}{\partial z_i} = -\mathbb{1}[i = k] + \frac{e^{z_i}}{\sum_j e^{z_j}} = p_i - t_i

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.

∂pi∂zj=pi(δij−pj)\frac{\partial p_i}{\partial z_j} = p_i\left(\delta_{ij} - p_j\right)
zˉj=∑i∂L∂pi∂pi∂zj=∑i(−tipi)pi(δij−pj)=−tj+pj∑iti=pj−tj\bar z_j = \sum_i \frac{\partial L}{\partial p_i}\frac{\partial p_i}{\partial z_j} = \sum_i \left(-\frac{t_i}{p_i}\right)p_i\left(\delta_{ij} - p_j\right) = -t_j + p_j\sum_i t_i = p_j - t_j

Read the middle of that: the pip_i in front of the Jacobian cancels the pip_i in the denominator of the loss derivative exactly, in every term, which is why no fraction is left. What remains is −tj-t_j plus pjp_j multiplied by the sum of the targets. Since the targets are one for a single option and zero elsewhere, that sum is one, and pjp_j times one is just pjp_j. The Jacobian was never absent. It was multiplied by the one thing that undoes it.

FIG 1What goes backwards at four levels of confidence
stepprobability on the correct optiongradient therelargest wrong-option gradientwhat happened
10.99-0.010.01Confidently right. Almost nothing travels back and the weights barely move.
20.7-0.30.3Right but unsure. A moderate push towards more confidence.
30.5-0.50.5Undecided between two options. A large correction.
40.02-0.980.98Confidently wrong, the largest signal the loss can produce.
4 steps
The correction is the error itself, nothing to tune, and it is bounded: no single example can send back more than one per option.

The numbers are the subtraction, done. A correct option sitting at probability 0.990.99 sends back 0.99−1=−0.010.99 - 1 = -0.01, and a wrong option at 0.010.01 sends 0.01−0=0.010.01 - 0 = 0.01. A confident mistake at 0.020.02 on the correct option sends −0.98-0.98 there and 0.980.98 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, 2∑i(pi−ti) pi(δij−pj)2\sum_i (p_i - t_i)\,p_i(\delta_{ij} - p_j), which does not reduce to a difference. Keep the cross entropy but divide the scores by a temperature TT and the same argument leaves (pi−ti)/T(p_i - t_i)/T: 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 pj∑iti−tjp_j\sum_i t_i - t_j 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 course

Other questions people ask about this

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Read alongside