Why the softmax gradient is prediction minus target
Updated
A classifier assigns probabilities to possible answers. Cross-entropy penalizes it when the correct answer receives little probability. Its derivative has a useful form: the predicted probability minus the target, scaled by how the loss is averaged.
This note develops the manual derivatives in my makemore backpropagation notebook. See backpropagation for the chain-rule foundation.
Derive it for one example
Let be a logit and let the correct class be . Softmax and negative log-likelihood give
Differentiating with respect to yields
The indicator is one only at the target class. For mean loss over a batch of equally weighted examples, divide each row's gradient by . Summed loss, class weighting, ignored targets and other reductions require their own scaling.
An illustrative row
For illustrative probabilities with the middle class correct, the single-example logit gradient is . The entries sum to zero. Adding a constant to all logits leaves the probabilities unchanged, so the loss has no slope in that common-shift direction.
A negative target-logit gradient means a gradient-descent update increases that logit. The other logits decrease in this isolated logit-space picture; changes to shared network parameters can couple examples and classes.
Avoid a tensor-to-scalar shortcut
Only selected entries of the log-probability matrix contribute directly to the loss. Their derivative is ; the others are zero. Replacing that whole matrix with a scalar would incorrectly send gradients into every entry. Shapes and indexing are part of the derivative.
For stable computation, subtract the largest logit before exponentiation or use a fused log-softmax/cross-entropy operation. The mathematical derivative is unchanged, but overflow is avoided. A centered finite-difference check on moderate logits helps detect missing batch factors and wrong target indices.
The source exercise is Karpathy's makemore backpropagation lesson; micrograd's implementation illustrates how local derivative rules compose.