The chain rule, run backwards: backpropagation
Functions inside functions differentiate by multiplying the local slopes. Do it from the loss backwards on a computation graph and you have backpropagation.
1. The idea
Networks are functions inside functions: the loss of the activation of a weighted sum. The chain rule says how to differentiate a chain like that: multiply the local derivatives along the way. If z = g(w) and L = f(z), then dL/dw = (dL/dz) · (dz/dw).
Backpropagation is the chain rule run efficiently on a computation graph. The forward pass computes every intermediate value. The backward pass starts at the loss with dL/dL = 1 and walks back, multiplying by each node's local derivative. Adding passes the gradient through unchanged; multiplying sends each input the other input's value; squaring sends back 2z.
Each node only needs its own small rule, and every intermediate gradient is computed once and reused by everything upstream of it. That is why a backward pass costs about as much as a forward pass, however many weights there are.
2. Try it: step through a backward pass
Set x, w and b, then press Next step: three forward steps fill in the values, six backward steps fill in the gradients in red, from L back to the inputs.
3. The math
4. The code (python)
x, w, b = 2.0, -1.5, 1.0
m = w * x # forward pass
z = m + b
L = z ** 2
print(m, z, L) # -3.0 -2.0 4.0
dL_dz = 2 * z # backward pass: each node multiplies by its local derivative
dL_dm = dL_dz * 1.0
dL_db = dL_dz * 1.0
dL_dw = dL_dm * x
dL_dx = dL_dm * w
print(dL_dz, dL_dw, dL_db, dL_dx) # -4.0 -8.0 -4.0 6.0
eps = 1e-6 # check dL/dw by nudging w
print(round((((w + eps) * x + b) ** 2 - L) / eps, 3)) # -8.05. You’ll see this in
- Core Course chapter 5: a tiny autograd engine this graph, built in code
- Module 0: functions and arrows composition, the thing the chain rule differentiates
6. Check yourself
Finished this chapter?
What changed in ML architectures this week
New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.