Backpropagation and optimizers
The chain rule at scale, and how weights get updated
Chapter 3 of 21 · I. Foundations · about 40 minutes. Builds on: 2. Neural networks and the multilayer perceptron.
Backpropagation is the chain rule, organized
Section titled “Backpropagation is the chain rule, organized”A network is a composition of simple functions. To train it you need for every parameter, and the chain rule says how derivatives of a composition multiply. Backpropagation computes all of them in one backward sweep, reusing intermediate results.
A two-layer network, derived
Section titled “A two-layer network, derived”Forward pass, with a ReLU or other activation and squared-error loss on one example:
Work backward. Define , the “error signal” at layer .
Each layer needs only the error from the layer above, its own stored input, and its local derivative. That is the pattern for any depth: reverse-mode automatic differentiation. Two practical consequences:
- The backward pass costs about twice the forward pass (one matrix product for the gradient of the weights, one for the gradient of the input). Together with the forward pass this is the origin of the “6 FLOPs per parameter per token” rule in Chapter 9.
- The forward activations must be stored (or recomputed) for the backward pass. Activation memory is often larger than the parameter memory, which is why frameworks offer activation checkpointing: store a few and recompute the rest.
Gradients through many layers
Section titled “Gradients through many layers”The backward signal passes through a product of per-layer Jacobians. If those matrices tend to shrink vectors, the signal vanishes; if they tend to enlarge them, it explodes. Two standard remedies matter for transformers.
Residual connections
Section titled “Residual connections”Instead of , compute y = x + F(x) . Then . The identity term gives gradients a direct path from the loss to early layers regardless of what does. A transformer is a stack of such residual updates to a single vector (the “residual stream”, Chapter 7).
Initialization
Section titled “Initialization”Initial weights are random, and their scale is chosen so that activations and gradients keep roughly the same variance from layer to layer. For a layer with inputs, a standard choice draws weights with variance proportional to (with a constant of 2 for ReLU). Too large and activations blow up with depth; too small and they vanish. Large models also scale down the output weights of residual branches so the sum of many layers stays bounded.
Optimizers
Section titled “Optimizers”Given the gradient from a minibatch, an optimizer decides the step.
SGD with momentum
Section titled “SGD with momentum”Plain SGD is . Momentum keeps a running velocity that smooths the noise and builds speed along consistent directions:
Adam keeps two running averages per parameter: the mean of the gradient () and the mean of its square (), and divides one by the root of the other, so every parameter takes steps of roughly the same size no matter how big its gradients are:
The bias correction exists because and start at zero, so early on they underestimate the true averages. Typical values are , to 0.999, .
Weight decay shrinks weights toward zero to regularize. Adding an L2 penalty to the loss is not equivalent to decay when the optimizer rescales gradients, so AdamW applies decay directly to the weights, separately from the adaptive step:
Muon and matrix-aware optimizers
Section titled “Muon and matrix-aware optimizers”Adam treats each weight independently, but a weight matrix is a structured object. Muon accumulates momentum as usual, then replaces the momentum matrix by an approximately orthogonalized version (all singular values pushed toward 1, computed with a few Newton–Schulz matrix iterations) before applying it. report that, with modifications such as weight decay and per-matrix update scaling, it scales to large language model training and can be more compute-efficient than AdamW.
Schedules and clipping
Section titled “Schedules and clipping”- Warmup: ramp the learning rate from near zero over the first few thousand steps; early Adam statistics are unreliable.
- Decay: reduce the rate (cosine or linear) toward the end of training.
- Gradient clipping: rescale if exceeds a threshold, which prevents rare huge gradients from wrecking the weights.
Try it: a narrow valley
Section titled “Try it: a narrow valley”The loss below is : curvature is thirty times larger across the valley than along it. Chapter 1’s stability condition limits SGD to , which makes progress along the valley slow. Compare the three optimizers and change the rates.
Try it: train a real transformer
Section titled “Try it: train a real transformer”Everything above applies unchanged to a transformer: the loss is the average negative log-probability of each true next word, backpropagation produces a gradient for every weight matrix, and an optimizer moves the weights. The widget below does exactly that to a copy of the Model Explorer’s two-layer model, in your browser, using hand-written backpropagation that is checked against finite differences. Start from random weights, take a few steps on one sentence, and watch the probability of each true next word climb while the loss falls.
Check yourself
Section titled “Check yourself”In the two-layer derivation, why does δ₁ contain φ′(z₁)?
Because ; by the chain rule, , and elementwise. For ReLU that factor is 1 where and 0 elsewhere, so units that were “off” pass no gradient.
What does the residual connection do to the gradient?
It adds the identity to the Jacobian: . The identity path carries the gradient directly to earlier layers.
Why does Adam need bias correction?
and are initialized to 0 and are exponential averages, so at step they are biased toward 0 by a factor . Dividing by that factor removes the bias.
Papers
Section titled “Papers”- Introduces residual connections.
- Adam.
- AdamW; read for the argument about why L2 regularization and weight decay differ.
- A recent matrix-aware optimizer shown to scale.
- Chapters 8 (optimization) and 6.5 (backpropagation).