AI / ML Theory
Chapter 2 · 16 min

2.5 — Backprop, part 1: the output delta δ⁽ᴸ⁾ = ŷ − y

Goal: Define the backprop delta variable and derive the output-layer delta for softmax + cross-entropy.

1. Why this session exists

Backpropagation is the chain rule from 0.3 applied to the MLP composition from 2.1. To organise the computation we introduce a single variable — the delta, δ(ℓ)\delta^{(\ell)} — that propagates gradient signal backwards through the network. The base case of the backward recursion lives at the loss layer. For softmax + cross-entropy, the answer is the identity we already proved in 1.8.[1]


2. Definition of δ(ℓ)\delta^{(\ell)}

For each layer ℓ\ell, define

 δ(ℓ)  :=  ∂L∂z(ℓ)  ∈  Rdℓ. \boxed{\ \delta^{(\ell)} \;:=\; \frac{\partial L}{\partial z^{(\ell)}} \;\in\; \mathbb{R}^{d_\ell}.\ }

(One column per example in the batched form; we keep working per-example here and re-vectorise in §5.)

In words: δ(ℓ)\delta^{(\ell)} is the gradient of the loss with respect to the pre-activation at layer ℓ\ell. It is the natural backward-pass variable because once we have it we can compute the gradients w.r.t. W(ℓ)W^{(\ell)} and b(ℓ)b^{(\ell)} in one step (2.7), and we can propagate to δ(ℓ−1)\delta^{(\ell-1)} in one step (2.6).


3. The output delta for softmax + CE

Setup. Final-layer logits z(L)∈RKz^{(L)} \in \mathbb{R}^K, predicted distribution y^=softmax⁡(z(L))∈RK\hat y = \operatorname{softmax}(z^{(L)}) \in \mathbb{R}^K, one-hot label y∈{0,1}Ky \in \{0, 1\}^K, cross-entropy loss

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

This is the setup of 1.8. By the result there,

 δ(L)  =  ∂L∂z(L)  =  y^−y. \boxed{\ \delta^{(L)} \;=\; \frac{\partial L}{\partial z^{(L)}} \;=\; \hat y - y.\ }

The "miracle gradient" of the entire chapter. No σ′\sigma' damping, no K×KK \times K matrix — just a KK-vector residual.


4. Other common output deltas

Three more output layers we'll need.

Sigmoid + binary cross-entropy (the K=2K = 2 case, also used per-output for multi-label classification). From 1.2,

δ(L)=y^−y∈R.\delta^{(L)} = \hat y - y \in \mathbb{R}.

Linear + mean-squared error (regression with no output nonlinearity). Loss L=12∥y^−y∥2L = \tfrac{1}{2} \lVert \hat y - y \rVert^2 with y^=z(L)\hat y = z^{(L)}. Then

δ(L)=y^−y∈RdL.\delta^{(L)} = \hat y - y \in \mathbb{R}^{d_L}.

Same shape, same expression — the "match the activation to the loss" principle of 1.2 keeps the output delta clean every time.

Sigmoid + MSE. This is the bad pairing from 1.2 SelfTest (d). It leaves a σ′(z(L))\sigma'(z^{(L)}) multiplier:

δ(L)=(y^−y) ⊙ σ′(z(L)).\delta^{(L)} = (\hat y - y)\, \odot\, \sigma'(z^{(L)}).

You can do it, but the σ′∈(0,1/4]\sigma' \in (0, 1/4] damping makes confident-but-wrong predictions move slowly. Avoid.


5. Batched form

For a mini-batch of BB examples, define

Δ(ℓ)  :=  ∂Lbatch∂Z(ℓ)  ∈  RB×dℓ.\Delta^{(\ell)} \;:=\; \frac{\partial L_{\text{batch}}}{\partial Z^{(\ell)}} \;\in\; \mathbb{R}^{B \times d_\ell}.

Here Lbatch=1B∑i=1BLiL_{\text{batch}} = \tfrac{1}{B} \sum_{i=1}^B L_i is the average loss over the batch. By linearity,

Δi,:(ℓ)  =  1B ∂Li∂zi(ℓ)  =  1B δi(ℓ).\Delta^{(\ell)}_{i, :} \;=\; \frac{1}{B}\, \frac{\partial L_i}{\partial z^{(\ell)}_i} \;=\; \frac{1}{B}\, \delta^{(\ell)}_i.

So the batched Δ(L)\Delta^{(L)} for softmax + CE is

Δ(L)  =  1B (Y^−Y)  ∈  RB×K.\Delta^{(L)} \;=\; \frac{1}{B}\,(\hat Y - Y) \;\in\; \mathbb{R}^{B \times K}.

(In code we usually keep the 1/B1/B factor inside the loss reduction. Whether it lives in Δ\Delta or in the final gradient is bookkeeping.)


6. Sanity check via shapes

  • z(L)∈RdLz^{(L)} \in \mathbb{R}^{d_L}, so δ(L)∈RdL\delta^{(L)} \in \mathbb{R}^{d_L} ✓.
  • y^,y∈RdL\hat y, y \in \mathbb{R}^{d_L}, so y^−y∈RdL\hat y - y \in \mathbb{R}^{d_L} ✓.
  • Batched: Z(L),Y^,Y∈RB×dLZ^{(L)}, \hat Y, Y \in \mathbb{R}^{B \times d_L}, so Y^−Y∈RB×dL\hat Y - Y \in \mathbb{R}^{B \times d_L} ✓.

All shapes match. This is how we'll spot dimension bugs in 2.6 and 2.7.


7. Takeaways

  • δ(ℓ):=∂L/∂z(ℓ)\delta^{(\ell)} := \partial L / \partial z^{(\ell)} is the central backward-pass variable.
  • For softmax + CE, δ(L)=y^−y\delta^{(L)} = \hat y - y (1.8 reused verbatim).
  • Same identity for sigmoid + BCE and for linear + MSE — these are the three "loss-aligned" output heads.
  • Avoid sigmoid + MSE: it leaves a σ′\sigma' multiplier and trains slowly.[2]

8. Where we go next

We have δ(L)\delta^{(L)}. Session 2.6 propagates it backwards: δ(ℓ+1)→δ(ℓ)\delta^{(\ell+1)} \to \delta^{(\ell)} via the chain rule. Session 2.7 uses each δ(ℓ)\delta^{(\ell)} to compute ∂L/∂W(ℓ)\partial L / \partial W^{(\ell)} and ∂L/∂b(ℓ)\partial L / \partial b^{(\ell)}. Session 2.8 walks through a tiny numerical example end-to-end.


Self-test

(a) For a regression task with linear output and MSE loss, derive δ(L)\delta^{(L)} from scratch.

(b) For multi-label classification — multiple independent sigmoid outputs each with its own BCE — write δ(L)\delta^{(L)} as a vector and check that each entry is y^k−yk\hat y_k - y_k.

(c) Compute δ(L)\delta^{(L)} for the bad pairing sigmoid + MSE and contrast with sigmoid + BCE. For what values of (y^,y)(\hat y, y) is the BCE delta much larger than the MSE one?

(d) In code, Δ(L)\Delta^{(L)} would be returned by (logits.softmax(dim=-1) - y_onehot) / B. Verify the units/scale: if you multiply by the learning rate and update, are the magnitudes sensible?


References

  1. [1]

    Rumelhart, D. E., Hinton, G. E., & Williams, R. J. (1986). "Learning representations by back-propagating errors". Nature, 323(6088), 533–536. — Backpropagation (2.5–2.7).

    ↩
  2. [2]

    Korthikanti, V., Casper, J., Lym, S., et al. (2022). "Reducing activation recomputation in large transformer models". arXiv:2205.05198 — Sequence parallelism + selective activation recomputation (7.9 §2.5; also 7.7 §7).

    ↩