3.2 Backprop and autograd
1. Learn
Section titled “1. Learn”- Karpathy, The spelled-out intro to neural networks and backpropagation: building microgradwhole video (2:25:52); the core is 00:08:08–01:39:31 (derivatives, manual backprop, a backward function per operation, the bug when a node is used twice, then exp, pow, division and subtraction) and 01:43:55–02:14:03 (an MLP in micrograd and its training loop)
- Understanding Deep Learning (Prince)§7.1–7.4 Problem definitions through Backpropagation algorithm (pp. 96–107)
- CS231n, Backpropagation, intuitionswhole page, especially "Patterns in backward flow" and "Gradients for vectorized operations"
2. Bridge
Section titled “2. Bridge”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 , and . Nothing else about the graph is needed (CS231n backprop notes).
Reverse mode. The chain rule says . Start at the loss with 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: for and for the ReLU mask.
3. Explore
Section titled “3. Explore”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.
stubs/m3_2_micrograd.pyLoading 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.
Locally: mlc check 3.2.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
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.