Lesson 7.1 · 10 min read · about 1 h 5 min to master at a very slow pace

The Chain Rule and Computational Graphs

Backpropagation is the chain rule applied backwards through the network, reusing every intermediate result. The chain rule on a small graph, with numbers, and the two rules: multiply along a path, add across paths.

Not startedJump to quiz

The chain rule

If depends on and depends on :

For a longer chain , multiply the local derivatives along the path: .

Two rules for graphs

  • Multiply local derivatives along a path.
  • Add the contributions of all paths when a value feeds several downstream nodes.

Worked example: one weight, with numbers

, , , with , , .

  • Forward: , , .
  • Local derivatives: ; ; .
  • Multiply: .

Negative, so increasing reduces the error, as expected when the output is below the target.

Why "backwards" and why it's efficient

Backpropagation is reverse-mode automatic differentiation. One forward pass computes and stores every intermediate value; one backward pass, from the loss towards the inputs, computes the derivative of the loss with respect to every parameter, reusing shared terms instead of recomputing them. The cost is about the same as one more forward pass, however many parameters there are. That reuse is what makes training large networks feasible.

One training iteration, end to end

Backpropagation is one step of a loop. Every term from the earlier sections has a single job in it. For training examples:

  • Loss : the error on one example.
  • Empirical risk : the average loss on the training data, our stand-in for the error on all future data.
  • Objective : what training actually minimises. With it is just the empirical risk.
  • ERM (empirical risk minimisation) is the principle: pick the that minimises . With the penalty, it is regularised ERM.
  • Optimisation is how we minimise : gradient descent or a variant. It needs , and backpropagation is how we get it.
StepWhat happensResult
1. Forward passInputs flow through the layers; every activation is stored for laterPredictions
2. Loss and objectiveCompare with , average over the batch, add any penalty, one number
3. Backward passChain rule from back through the stored values to every weight, one number per weight
4. Optimiser step (or momentum, Adam)New weights
5. RepeatNext mini-batch; after each epoch, check the validation lossA trained network

So the objective is calculated at the end of every forward pass, the gradient straight after it in the backward pass, and the weights change only in step 4, after the backward pass has finished. With mini-batches, and its gradient are computed on the batch, so both are estimates of the full-data values. In PyTorch: optimizer.zero_grad() clears the old gradients, loss = loss_fn(model(x), y) is steps 1 and 2, loss.backward() is step 3, and optimizer.step() is step 4.

Click through one full iteration on the 2–3–1 network from lesson 3.1: forward, loss, backward, update, and forward again.

Why compute the objective again after the update?

A gradient is only valid at the point where it was computed. Once the weights change, the old and the old gradient are out of date. The next forward pass measures how good the new weights are and gives the gradient at the new point.

Continue the worked example above. The gradient was . Take one step with : . Forward again: and , down from . The error fell, so the step helped. If had gone up, that would be a sign that is too large.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passIs backpropagation the same as gradient descent?

No. Backpropagation computes the gradient. Gradient descent uses it to update the weights. One computes the direction; the other takes the step. PyTorch mirrors this: loss.backward() is backpropagation and optimizer.step() is the update.

First passIs ERM an optimisation algorithm? How is it different from gradient descent?

No. ERM is a principle: it says what to minimise, the average training loss (plus a penalty if regularised). Gradient descent is an algorithm: it says how to minimise it, step by step. ERM defines the target; gradient descent, Adam or a closed-form formula like OLS's gets you there.

First passDo both ordinary and regularised ERM update the weights?

Yes. Both are minimised with the same loop: forward pass, objective, gradient, update. The only difference is the objective. With an L2 penalty, the gradient gains an extra term , so every update also shrinks the weights a little: weight decay (lesson 9.1).

First passWhy does a neural network have so many gradients?

Because it has many parameters, and the gradient has one entry per parameter: how much the objective changes if you nudge that one weight or bias. The 2–3–1 network in lesson 3.1 has 13 parameters, so 13 partial derivatives; a network with a million weights has a million. Backpropagation computes all of them in one backward pass, and each one updates its own parameter.

First passHow do I say ∂L/∂w in words, and how do I write the different gradients?

is how much the loss changes when this weight changes by a tiny amount. If , raising by 0.01 lowers the loss by about 0.015, so gradient descent raises .

SymbolRead it as
Gradient for one weight
or A matrix of gradients, the same shape as
The error signal at a neuron's pre-activation
Gradient for the weights of layer 2 (the superscript is the layer)

Cheatsheet

Chain rule on graphs

Multiply local derivatives along a path; add contributions over all paths from a node to the loss.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculate, , with , , , giving . What is ?
Q2conceptA node's value feeds three different downstream nodes. How do you get the loss's derivative with respect to it?
Q3conceptWhat is the difference between backpropagation and gradient descent?
Q4interpretWhy does backpropagation cost only about as much as one extra forward pass, however many weights the network has?
Q5calculate. Using the chain rule, what is at ?