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, — 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
For each layer , define
(One column per example in the batched form; we keep working per-example here and re-vectorise in §5.)
In words: is the gradient of the loss with respect to the pre-activation at layer . It is the natural backward-pass variable because once we have it we can compute the gradients w.r.t. and in one step (2.7), and we can propagate to in one step (2.6).
3. The output delta for softmax + CE
Setup. Final-layer logits , predicted distribution , one-hot label , cross-entropy loss
This is the setup of 1.8. By the result there,
The "miracle gradient" of the entire chapter. No damping, no matrix — just a -vector residual.
4. Other common output deltas
Three more output layers we'll need.
Sigmoid + binary cross-entropy (the case, also used per-output for multi-label classification). From 1.2,
Linear + mean-squared error (regression with no output nonlinearity). Loss with . Then
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 multiplier:
You can do it, but the damping makes confident-but-wrong predictions move slowly. Avoid.
5. Batched form
For a mini-batch of examples, define
Here is the average loss over the batch. By linearity,
So the batched for softmax + CE is
(In code we usually keep the factor inside the loss reduction. Whether it lives in or in the final gradient is bookkeeping.)
6. Sanity check via shapes
- , so ✓.
- , so ✓.
- Batched: , so ✓.
All shapes match. This is how we'll spot dimension bugs in 2.6 and 2.7.
7. Takeaways
- is the central backward-pass variable.
- For softmax + CE, (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 multiplier and trains slowly.[2]
8. Where we go next
We have . Session 2.6 propagates it backwards: via the chain rule. Session 2.7 uses each to compute and . Session 2.8 walks through a tiny numerical example end-to-end.
(a) For a regression task with linear output and MSE loss, derive from scratch.
(b) For multi-label classification — multiple independent sigmoid outputs each with its own BCE — write as a vector and check that each entry is .
(c) Compute for the bad pairing sigmoid + MSE and contrast with sigmoid + BCE. For what values of is the BCE delta much larger than the MSE one?
(d) In code, 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]
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]
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).
↩