Skip to content

3.2 Backprop and autograd

ready≈ 11.5 hoursNeeds: 3.1, 1.3Unlocks: 3.3, 3.4, 3.6, 3.8
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Computation graph stepper)
  4. 4 Do
  5. 5 Check

A computation is a graph. Break any formula into primitive operations: each node computes one output from one or two inputs, and each knows its own local derivative. For c=a×bc = a \times b, ∂c/∂a=b\partial c/\partial a = b and ∂c/∂b=a\partial c/\partial b = a. Nothing else about the graph is needed (CS231n backprop notes).

Reverse mode. The chain rule says ∂L/∂a=∂L/∂c⋅∂c/∂a\partial L/\partial a = \partial L/\partial c \cdot \partial c/\partial a. Start at the loss with ∂L/∂L=1\partial L/\partial L = 1 and walk the graph backwards, multiplying each node’s gradient by its local derivatives and passing the products to its inputs. One backward pass gives the gradient with respect to every input, at about the cost of one forward pass. Forward mode would need one pass per input, which is hopeless for millions of parameters (UDL §7.4; Baydin et al. 2018).

Two rules make it correct. If a value feeds several nodes, its gradient is the sum of what each sends back, so _backward must use +=, never =. And a node may only pass its gradient on once all of its users have sent theirs, so nodes are processed in reverse topological order. The “reuse” expression in the stepper shows both (micrograd).

From scalars to layers. Your Value engine works one number at a time, which is slow but transparent. Grouping the same steps by layer gives the matrix formulas below, which are what NumPy and PyTorch run (UDL §7.4.1). Note how the backward pass reuses the forward cache from 3.1: H\mathbf{H} for dW2\mathrm{d}\mathbf{W}_2 and Z1\mathbf{Z}_1 for the ReLU mask.

Backprop through a 2-layer MLP, layer by layerstep 1 of 6
  1. Z1=XW1+b1,H=ReLU(Z1),Y^=HW2+b2,L=12n∥Y^−Y∥2\mathbf{Z}_1 = \mathbf{X}\mathbf{W}_1 + \mathbf{b}_1,\quad \mathbf{H} = \mathrm{ReLU}(\mathbf{Z}_1),\quad \hat{\mathbf{Y}} = \mathbf{H}\mathbf{W}_2 + \mathbf{b}_2,\quad L = \tfrac{1}{2n}\lVert\hat{\mathbf{Y}} - \mathbf{Y}\rVert^2

★ Computation graph stepper

Backprop, chain rule, gradient accumulation

Static view on small screens. Open this page on a laptop to interact.

x₁data 1.500w₁data -2.000x₂data 0.500w₂data 1.000bdata 2.200x₁w₁ (×)data ?x₂w₂ (×)data ?s (+)data ?n (+)data ?o (tanh)data ?
step 0 of 11Leaves hold the inputs. Step forward to compute each node from its inputs.

Each backward step multiplies the node's own gradient by the local derivative of each input and adds it into that input's gradient. In the "reuse" expression, x and u each feed two nodes, so their gradients arrive in two pieces: that is why _backward uses +=. A node is only processed after every node that uses it, which is what the topological order guarantees.

A computation graph drawn left to right from inputs to output, each node showing its value and, once the backward pass starts, its gradient. Step buttons move one operation at a time through the forward pass and then the backward pass; the readout states the arithmetic of each step, such as “x.grad += 1.5 × 0.42”. Three expressions are available: a single tanh neuron, an expression that reuses its inputs, and the squared error of a ReLU unit. Keyboard: Tab to the step buttons and press Enter or Space; arrow keys adjust input sliders.

Build micrograd: a Value class whose operations record their inputs and a _backward closure, a backward() that sorts the graph, and a small MLP made of Neuron and Layer objects. The tests check every operation against finite differences, check that reused values accumulate, and train your MLP.

Backprop and autograd stubs/m3_2_micrograd.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m3_2_micrograd.py and run mlc check 3.2. The tests are identical; import the results on the progress page.

Hints · __mul__ 0/3 revealed
Hints · backward 0/3 revealed
Hints · Neuron.__call__ 0/3 revealed

Locally: mlc check 3.2.

1. Why does every _backward use += rather than =?
2. A function has a million inputs and one scalar output. How many passes does reverse mode need for the full gradient?
3. Why must backward visit nodes in reverse topological order?
4. For c = a × b with a = 3, b = −2 and c.grad = 5, what does the backward step add to a.grad?
5. In the matrix version, why is dW = Hᵀ dY (a sum over examples)?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.