From Neurons to Networks
Backpropagation
The chain rule that teaches the network
The chain rule, run in reverse: one backward sweep hands every weight in the network its exact share of the blame.
Gradient descent already told us the plan: nudge each weight against its gradient and the loss goes down. It left one enormous question open - with millions of tangled weights, how do you get the gradient for every one of them? Backpropagation is the answer, and it is not magic. It is bookkeeping: run the chain rule backward through the network's computation, and each weight's contribution to the error falls out of a single, orderly sweep.
The idea in one line
Everything is a computational graph
Before we can differentiate a network, we rewrite it as a : break the whole calculation into tiny elementary operations - one multiply, one add, one square - each a node with inputs and one output. Even a giant LLM is just an astronomically large version of this. Our toy example predicts a number from inputs and measures how far off it lands from a target :
The expression, node by node
Here is the prediction and is the loss - a single number for "how wrong." Each node's local derivative (how its output reacts to a tiny wiggle of one input) is elementary calculus:
The chain rule (Lesson 14) is the glue. To learn how the loss depends on a buried input like , you multiply the local derivatives along the path from to :
The chain rule, spelled out
Backprop's trick is to compute these right to left and reuse the shared front part. The quantity is needed by both and - so we compute it once, cache it, and hand it down. That reuse is why one backward sweep is cheap even for a trillion weights.
Forward, then backward - step through it
Press ▶ Forward to fill every node's value left to right, then ◀ Backward to watch the gradient flow right to left, one edge at a time - each edge multiplying in its local derivative (the chain rule, live). Or walk it yourself with Next. Then drag a slider: the graph and the gradient readouts recompute instantly, so you can see exactly which gradients each input controls.
Forward pass. The inputs are known: a = 2, b = -3, c = 10. Now fill every node left to right.
Two rules run the whole show
The same thing, in code
This is exactly what a framework does automatically, but written by hand it fits in a dozen lines. Notice the shape: forward first (top to bottom), then backward (bottom to top), each gradient built from the one just above it.
# inputs
a, b, c, t = 2.0, -3.0, 10.0, 1.0
# --- forward pass: fill every value left to right ---
u = a * b # -6.0
p = u + c # 4.0 (prediction)
L = (p - t) ** 2 # 9.0 (loss)
# --- backward pass: reverse-mode autodiff ---
dL = 1.0 # seed: dL/dL
dp = dL * 2 * (p - t) # 6.0 through the square
du = dp * 1 # 6.0 + passes gradient through
dc = dp * 1 # 6.0
da = du * b # -18.0 * swaps in the OTHER input
db = du * a # 12.0Forward must finish first
Check yourself
When the gradient reaches an add node p = u + c, each input receives:
Why does backpropagation visit the graph nodes in reverse order?
Recall: what are backprop's two passes, and what does each one compute?
Nail down the two-phase structure. Try to state it, then check.
Lock it in
- A network is a computational graph: tiny multiply / add / square nodes, each with a simple local derivative.
- Backprop is the chain rule run right to left, reusing and caching shared partials so one sweep prices out every weight.
- A plus node distributes the gradient unchanged; a times node scales it by the other input.
- Forward first (cache every value), then backward, because the backward pass reads those cached values.
Primary source
micrograd - the demo above is a miniature of it.Ask your teacher