← Contents Volume VII · Lecture 14

Volume VII · Neural Networks From First Principles

14

The Chain Rule, Applied Systematically

Every gradient in this track so far was derived once, by hand, in closed form, because the hypothesis class was simple enough to allow it. A neural network is a composition of as many differentiable layers as you care to stack, and no one is going to re-derive its gradient by hand for every architecture. Backpropagation is the calculus you already know, organised so a machine can do that derivation for you, automatically, every time.

The mental model

Backpropagation is not a new idea beyond the chain rule. It is the chain rule applied layer by layer, computed from the output backward, so that the gradient signal each layer needs is handed to it already computed rather than re-derived from scratch.

The forward pass, precisely

Fix a network with L layers. Layer l takes the previous layer's activations a(l−1), applies a learned affine map, and passes the result through a nonlinearity g:

Forward pass, layer l z(l) = W(l) a(l−1) + b(l)  ·  a(l) = g(z(l))

The input is a(0) = x. The final layer's activations a(L) are the network's output, and a loss L compares that output against the true label — squared error for regression, cross-entropy for classification, the choice does not matter for what follows. Every quantity in this lecture is defined for one training example at a time; summing or averaging over the training set happens outside the derivation, the same way it did for every gradient earlier in this track.

FORWARD PASS a⁽⁰⁾ = x W⁽¹⁾ a⁽¹⁾ W⁽²⁾ a⁽²⁾ loss L → forward pass →
Figure 14a A three-layer network. Each arrow group is one weight matrix; each column of nodes is one layer's activations. Values flow strictly left to right, ending in a scalar loss.

What the backward pass has to compute

Training the network means adjusting every weight matrix W(l) to reduce L, which requires ∂L/∂W(l) for every layer l. Take the concrete case of a 3-layer network — layers 1, 2 and 3, with layer 3 the output layer — and ask for ∂L/∂W(2), the gradient with respect to the middle layer's weights. W(2) influences L only through the path it lies on: W(2) sets z(2), which sets a(2), which (through W(3)) sets z(3), which sets a(3), which the loss reads directly. The chain rule says the total derivative along that path is a product of the local derivatives at each link:

∂L/∂W⁽²⁾ by the chain rule ∂L/∂W(2)  =  (∂L/∂a(3)) · (∂a(3)/∂z(3)) · (∂z(3)/∂a(2)) · (∂a(2)/∂z(2)) · (∂z(2)/∂W(2))

Each factor is an ordinary derivative you already know how to write down, and none of them is mysterious once named individually:

Nothing here is asserted; every factor is an ordinary partial derivative of one of the two equations in §14.1, evaluated at the values the forward pass already produced. The chain rule is not a metaphor for backpropagation — this product is backpropagation, written out in full for one weight matrix.

Naming the reusable piece: the error at a layer

Group the first two factors of the product above — the derivatives that depend only on what happens at or after layer 3 — into a single quantity, conventionally written δ(3) = ∂L/∂a(3) · ∂a(3)/∂z(3), sometimes called the error at layer 3. Once δ(3) is known, the same quantity — not recomputed, simply reused — is exactly what is needed to start the computation of δ(2):

The error, propagated one layer back δ(2) = ( W(3) )ᵀ δ(3) ⊙ g′(z(2))  ·  ∂L/∂W(2) = δ(2) ( a(1) )ᵀ

This is the entire algorithm, stated in general form: compute δ at the output layer directly from the loss, then at every earlier layer obtain δ(l) from δ(l+1) — the layer just after it — by one matrix multiply and one elementwise product with the local nonlinearity's derivative. Every layer's weight gradient then falls out as δ(l) times the previous layer's activation, exactly as the last factor pair of the chain-rule product in §14.2 showed for W(2) specifically.

Why this is cheap: reuse, not repetition

The efficiency claim in the algorithm's name is precise and worth stating without hand-waving. If ∂L/∂W(1) were derived completely independently from ∂L/∂W(2) — starting over from the loss each time, expanding the full chain rule product from scratch for every layer — the factors belonging to every layer after the one being differentiated would be recomputed again and again, once per weight matrix, in a network with L layers. Computing δ(l) for every layer in a single backward sweep, starting at the output and working toward the input, means each layer's error is computed exactly once and handed to the layer before it as a ready-made partial result. That reuse — not any deeper mathematical trick — is why the cost of computing every gradient in the network is a small constant multiple of the cost of one forward pass, rather than growing with the number of layers the way independent re-derivation would.

BACKWARD PASS loss L δ⁽³⁾ δ⁽²⁾ δ⁽¹⁾ ← backward pass, each δ reused from the layer just after it ←
Figure 14b The gradient signal δ(l) is computed once at each layer boundary, moving right to left, and reused to build the next layer's δ rather than being re-derived independently for every weight matrix.

What this buys the field

Every hypothesis class this track has studied through Volume VI — linear and logistic regression, GDA and Naive Bayes, the SVM, a single decision tree — was simple enough that its gradient (or its closed-form solution) could be derived by hand, once, and written down for good. A neural network of arbitrary depth and arbitrary layer composition has no such luxury: nobody is deriving ∂L/∂W(l) by hand for every new architecture a researcher dreams up. Backpropagation is the general, mechanical procedure — the chain rule applied systematically, in the output-to-input order §14.3 and §14.4 justify — that computes that gradient automatically, for any composition of differentiable layers. That generality, not any single equation in this lecture, is what made deep, many-layered networks trainable at all, including every architecture the sibling track on modern large language models studies.

Read the primary source