1.8 — Multi-class cross-entropy and the ŷ − y gradient
Goal: Prove the identity ∂L/∂z = ŷ − y for softmax + cross-entropy and understand why it is the most-used backward identity in deep learning.
1. Why this session exists
Softmax + cross-entropy is the output head of almost every modern classifier and the entire next-token loss of every LLM. Underneath, it admits a single backward identity:
∂z∂L=y^−y.
This is the same identity we saw in 1.2 for sigmoid + BCE (the K=2 case). It is the algebraic miracle that makes softmax + cross-entropy the gold-standard pairing — no σ′ tail, no K×K matrix to multiply during backward, just a single subtraction. This session is the careful derivation.
2. Setup
A single training example. Logits z∈RK, one-hot label y∈{0,1}K with ∑kyk=1. Predicted distribution:
y^k=softmax(z)k=∑jezjezk.
Cross-entropy loss:
L=−k=1∑Kyklogy^k.
Because y is one-hot, L=−logy^c where c is the index of the true class. Equivalently, L=−zc+log∑jezj (the log-sum-exp form from 0.10 / 1.7).
For the first piece, ∂(−zc)/∂zj=−δjc=−yj (since y is one-hot with yc=1).
For the second piece,
∂zj∂logl∑ezl=∑lezlezj=y^j.
(A side identity worth committing to memory: the gradient of log-sum-exp is the softmax.)
Adding,
∂zj∂L=−yj+y^j=y^j−yj.■
The second derivation is shorter because the log-sum-exp form bypasses the softmax Jacobian entirely. Internally, PyTorch's F.cross_entropy uses something equivalent.
6. Gradient w.r.t. the parameters
With logits z=Wx+b in RK, the chain rule extends through the linear layer.
∇WL.zk=wk⊤x+bk, so ∂zk/∂wk=x⊤ (and ∂zk/∂wl=0 for l=k). Therefore
Same "residual × input" pattern as 1.3, just with vector-valued residuals now.
7. Why this identity is the most-used in deep learning
Three reasons.
It is the backward signal at the loss layer of every classifier. Every CNN classifier, every Transformer LM head, every reward model in RLHF (Chapter 9) — they all backpropagate y^−y out of the softmax + CE head.
It composes cleanly with everything upstream. Whatever module produced the logits z — a linear layer, a deep stack of attention blocks — receives a "tell me where you went wrong" signal of the right shape (a K-dim residual) at the right scale (no σ′ damping).
It is interpretable.y^−y is the error in the predicted distribution. Moving z a step in the direction −(y^−y) shifts probability mass from the wrong classes onto the right class. The size of the move is how off the model currently is.
8. Takeaways
∂LCE/∂z=y^−y. Memorise this.
Two derivations: through the softmax Jacobian, or by differentiating the log-sum-exp form.
Gradient w.r.t. the weight matrix is the outer product (y^−y)x⊤, averaged over the batch.
Sigmoid + BCE is the K=2 special case of this identity (1.2).
9. Where we go next
Chapter 0 gave us mathematical fluency. Chapter 1 used it to build the simplest end-to-end trainable models — linear regression, binary logistic regression, the perceptron, multi-class softmax regression — and articulated the central obstacle (XOR) that motivates depth.
Chapter 2 introduces the multilayer perceptron and backpropagation: the same chain rule from 0.3 and the same y^−y gradient sink from this session, run through more than one matrix multiplication. The result will be every neural network in this course, all the way to the Transformer.
Self-test
(a) Redo derivation 1 explicitly for K=2 and check that you recover ∂L/∂z=y^−y as a 2-vector. Compare with the 1.2 scalar version.
(b) Show that the softmax Jacobian J=diag(y^)−y^y^⊤ is symmetric, PSD, and has 1 in its null space. Explain in words why each of these properties is required by the structure of softmax outputs.
(c) Suppose you use label smoothing: replace one-hot y with (1−ε)y+ε1/K. Show that ∂L/∂z=y^−ysmoothed where ysmoothed is the smoothed target. Why does this prevent confident logits from growing without bound (it acts as a stand-in for L2 regularisation on z)?
(d) Compute the Hessian ∇z2L and check that it equals the softmax Jacobian J from §3. Conclude that softmax + CE is convex in z, and hence in (W,b) since z=Wx+b is affine.