AI / ML Theory
Chapter 1 · 22 min

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:

∂L∂z=y^−y.\frac{\partial L}{\partial z} = \hat y - y.

This is the same identity we saw in 1.2 for sigmoid + BCE (the K=2K = 2 case). It is the algebraic miracle that makes softmax + cross-entropy the gold-standard pairing — no σ′\sigma' tail, no K×KK \times K matrix to multiply during backward, just a single subtraction. This session is the careful derivation.


2. Setup

A single training example. Logits z∈RKz \in \mathbb{R}^K, one-hot label y∈{0,1}Ky \in \{0, 1\}^K with ∑kyk=1\sum_k y_k = 1. Predicted distribution:

y^k=softmax⁡(z)k=ezk∑jezj.\hat y_k = \operatorname{softmax}(z)_k = \frac{e^{z_k}}{\sum_j e^{z_j}}.

Cross-entropy loss:

L=−∑k=1Kyk log⁡y^k.L = -\sum_{k=1}^K y_k\, \log \hat y_k.

Because yy is one-hot, L=−log⁡y^cL = -\log \hat y_c where cc is the index of the true class. Equivalently, L=−zc+log⁡∑jezjL = -z_c + \log \sum_j e^{z_j} (the log-sum-exp form from 0.10 / 1.7).


3. The Jacobian of softmax

We need ∂y^k/∂zj\partial \hat y_k / \partial z_j for all k,jk, j. Two cases.

Case j=kj = k:

∂y^k∂zk=∂∂zkezk∑lezl=ezk⋅∑lezl−ezk⋅ezk(∑lezl)2=y^k−y^k2=y^k (1−y^k).\frac{\partial \hat y_k}{\partial z_k} = \frac{\partial}{\partial z_k} \frac{e^{z_k}}{\sum_l e^{z_l}} = \frac{e^{z_k} \cdot \sum_l e^{z_l} - e^{z_k} \cdot e^{z_k}}{\bigl(\sum_l e^{z_l}\bigr)^2} = \hat y_k - \hat y_k^2 = \hat y_k\,(1 - \hat y_k).

Case j≠kj \ne k:

∂y^k∂zj=∂∂zjezk∑lezl=−ezk ezj(∑lezl)2=−y^k y^j.\frac{\partial \hat y_k}{\partial z_j} = \frac{\partial}{\partial z_j} \frac{e^{z_k}}{\sum_l e^{z_l}} = -\frac{e^{z_k}\, e^{z_j}}{\bigl(\sum_l e^{z_l}\bigr)^2} = -\hat y_k\, \hat y_j.

Combining,

 ∂y^k∂zj  =  y^k (δkj−y^j), \boxed{\ \frac{\partial \hat y_k}{\partial z_j} \;=\; \hat y_k\,(\delta_{kj} - \hat y_j),\ }

where δkj\delta_{kj} is the Kronecker delta. The Jacobian matrix is

J=diag⁡(y^)−y^ y^⊤  ∈  RK×K.J = \operatorname{diag}(\hat y) - \hat y\, \hat y^\top \;\in\; \mathbb{R}^{K \times K}.

It is symmetric, PSD, and singular — its null space is 1\mathbf{1}, reflecting the simplex constraint ∑ky^k=1\sum_k \hat y_k = 1.


4. The gradient of cross-entropy: derivation 1 (via the Jacobian)

By the chain rule,

∂L∂zj=∑k=1K∂L∂y^k⋅∂y^k∂zj.\frac{\partial L}{\partial z_j} = \sum_{k=1}^K \frac{\partial L}{\partial \hat y_k} \cdot \frac{\partial \hat y_k}{\partial z_j}.

Each factor:

∂L∂y^k=−yky^k,∂y^k∂zj=y^k (δkj−y^j).\frac{\partial L}{\partial \hat y_k} = -\frac{y_k}{\hat y_k}, \qquad \frac{\partial \hat y_k}{\partial z_j} = \hat y_k\,(\delta_{kj} - \hat y_j).

Multiply and sum:

∂L∂zj=−∑kyky^k⋅y^k (δkj−y^j)=−∑kyk (δkj−y^j).\frac{\partial L}{\partial z_j} = -\sum_k \frac{y_k}{\hat y_k} \cdot \hat y_k\,(\delta_{kj} - \hat y_j) = -\sum_k y_k\,(\delta_{kj} - \hat y_j).

The y^k\hat y_k in numerator and denominator cancel. Distribute the sum:

∂L∂zj=−∑kyk δkj+y^j ∑kyk=−yj+y^j⋅1=y^j−yj.\frac{\partial L}{\partial z_j} = -\sum_k y_k\, \delta_{kj} + \hat y_j\, \sum_k y_k = -y_j + \hat y_j \cdot 1 = \hat y_j - y_j.

Writing this as a vector identity,

 ∂L∂z  =  y^−y  ∈  RK. \boxed{\ \frac{\partial L}{\partial z} \;=\; \hat y - y \;\in\; \mathbb{R}^K. \ }

5. Derivation 2 (via the log-sum-exp form)

Same result, no Jacobian. Start from

L=−zc+log⁡∑jezj.L = -z_c + \log \sum_j e^{z_j}.

For the first piece, ∂(−zc)/∂zj=−δjc=−yj\partial(-z_c) / \partial z_j = -\delta_{jc} = -y_j (since yy is one-hot with yc=1y_c = 1).

For the second piece,

∂∂zjlog⁡∑lezl=ezj∑lezl=y^j.\frac{\partial}{\partial z_j} \log \sum_l e^{z_l} = \frac{e^{z_j}}{\sum_l e^{z_l}} = \hat y_j.

(A side identity worth committing to memory: the gradient of log-sum-exp is the softmax.)

Adding,

∂L∂zj=−yj+y^j=y^j−yj.■\frac{\partial L}{\partial z_j} = -y_j + \hat y_j = \hat y_j - y_j. \quad \blacksquare

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+bz = W x + b in RK\mathbb{R}^K, the chain rule extends through the linear layer.

∇WL\nabla_W L. zk=wk⊤x+bkz_k = w_k^\top x + b_k, so ∂zk/∂wk=x⊤\partial z_k / \partial w_k = x^\top (and ∂zk/∂wl=0\partial z_k / \partial w_l = 0 for l≠kl \ne k). Therefore

∂L∂wk=(y^k−yk) x⊤,\frac{\partial L}{\partial w_k} = (\hat y_k - y_k)\, x^\top,

which stacks into the matrix outer-product

 ∇WL  =  (y^−y) x⊤  ∈  RK×n. \boxed{\ \nabla_W L \;=\; (\hat y - y)\, x^\top \;\in\; \mathbb{R}^{K \times n}.\ }

(Residual column times input row.)

∇bL\nabla_b L. ∂zk/∂bk=1\partial z_k / \partial b_k = 1, so

∇bL=y^−y.\nabla_b L = \hat y - y.

Averaged over a mini-batch of mm examples,

∇WLbatch=1m∑i=1m(y^i−yi) xi⊤,∇bLbatch=1m∑i=1m(y^i−yi).\nabla_W L_{\text{batch}} = \frac{1}{m} \sum_{i=1}^m (\hat y_i - y_i)\, x_i^\top, \qquad \nabla_b L_{\text{batch}} = \frac{1}{m} \sum_{i=1}^m (\hat y_i - y_i).

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.

  1. 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\hat y - y out of the softmax + CE head.
  2. It composes cleanly with everything upstream. Whatever module produced the logits zz — a linear layer, a deep stack of attention blocks — receives a "tell me where you went wrong" signal of the right shape (a KK-dim residual) at the right scale (no σ′\sigma' damping).
  3. It is interpretable. y^−y\hat y - y is the error in the predicted distribution. Moving zz a step in the direction −(y^−y)-(\hat 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\partial L_{\text{CE}} / \partial z = \hat 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⊤(\hat y - y)\, x^\top, averaged over the batch.
  • Sigmoid + BCE is the K=2K = 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\hat 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=2K = 2 and check that you recover ∂L/∂z=y^−y\partial L / \partial z = \hat y - y as a 2-vector. Compare with the 1.2 scalar version.

(b) Show that the softmax Jacobian J=diag⁡(y^)−y^ y^⊤J = \operatorname{diag}(\hat y) - \hat y\, \hat y^\top is symmetric, PSD, and has 1\mathbf{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 yy with (1−ε) y+ε 1/K(1 - \varepsilon)\, y + \varepsilon\, \mathbf{1} / K. Show that ∂L/∂z=y^−ysmoothed\partial L / \partial z = \hat y - y_{\text{smoothed}} where ysmoothedy_{\text{smoothed}} is the smoothed target. Why does this prevent confident logits from growing without bound (it acts as a stand-in for L2 regularisation on zz)?

(d) Compute the Hessian ∇z2L\nabla^2_z L and check that it equals the softmax Jacobian JJ from §3. Conclude that softmax + CE is convex in zz, and hence in (W,b)(W, b) since z=Wx+bz = Wx + b is affine.