Lab#DL-0002: Backpropagation by hand

Lab

📘 Lab#DL-0002: Backpropagation by hand

We already have a graph of plus and times. Now we walk it backwards and, at every node, apply the chain rule by hand: first on a tiny expression, then through a single neuron with a \(\tanh\) activation.

This lab follows Andrej Karpathy’s lecture The spelled-out intro to neural networks and backpropagation: building micrograd (playlist). The code lives in AlbertProfe/wikiml-zero-to-hero (z2h-01), derived from karpathy/micrograd (MIT Licence).

Code on GitHub

The cells are live Python in the browser. We still write the gradients ourselves; the engine that does it for us comes in the next lab.

1 The chain rule, one node at a time

The chain rule is less a formula than a habit: each node only knows its local slope; the gradient of the final output is that local slope, multiplied by the gradient already sitting on the node’s output.

If \(L\) depends on \(d\) and \(d\) depends on \(e\), then

\[ \frac{\partial L}{\partial e} = \frac{\partial L}{\partial d} \cdot \frac{\partial d}{\partial e}. \]

You never have to see the whole expression at once. You stand at a node, you ask “if my output wiggles, how does \(L\) wiggle?” (that number has already arrived from above), and “if my input wiggles, how does my output wiggle?” (that is the local derivative, which plus and times know in their sleep). Multiply, and pass the product to the parent.

An everyday picture: a chain of bicycle gears. Turning the first cog a little turns the last wheel by the product of the local ratios. You do not need a drawing of the whole bicycle; you need each ratio, and you multiply as you go.

Karpathy’s first manual example starts at about 32:10.

2 A small expression, filled in by hand

Take the same expression as the previous lab:

\[ e = a \cdot b, \quad d = e + c, \quad L = d \cdot f \]

with \(a=2\), \(b=-3\), \(c=10\), \(f=-2\). Then \(e=-6\), \(d=4\), \(L=-8\).

Start at the end: \(\partial L / \partial L = 1\). For a product \(L = d \cdot f\) the local derivatives swap the other factor, so

\[ \frac{\partial L}{\partial d} = f = -2, \qquad \frac{\partial L}{\partial f} = d = 4. \]

Addition \(d = e + c\) has local derivative \(1\) on each input, so those parents just inherit \(-2\):

\[ \frac{\partial L}{\partial e} = -2, \qquad \frac{\partial L}{\partial c} = -2. \]

And \(e = a \cdot b\) swaps factors again:

\[ \frac{\partial L}{\partial a} = (-2)\cdot b = 6, \qquad \frac{\partial L}{\partial b} = (-2)\cdot a = -4. \]

Ex#DL-2001

Read \(a.\mathrm{grad} = 6\) as advice: if you want \(L\) to go up, increase \(a\). A tiny bump \(\Delta a = 0.01\) should lift \(L\) by about \(0.06\), from \(-8\) to \(-7.94\). You can check that by re-evaluating the expression, exactly as Karpathy does.

flowchart BT
  a["a = 2, grad = 6"] --> e["e = −6, grad = −2"]
  b["b = −3, grad = −4"] --> e
  e --> d["d = 4, grad = −2"]
  c["c = 10, grad = −2"] --> d
  d --> L["L = −8, grad = 1"]
  f["f = −2, grad = 4"] --> L

3 One neuron, and tanh as a single node

A neuron is just a slightly larger expression. Each input \(x_i\) is multiplied by a weight \(w_i\), the products are summed, a bias \(b\) is added, and a squashing function such as \(\tanh\) maps the result into \((-1, 1)\):

\[ n = x_1 w_1 + x_2 w_2 + b, \qquad o = \tanh(n). \]

Karpathy builds this from about 52:52. He treats \(\tanh\) as one node, not as a pile of exponentials, and that is the point: a node can sit at any level of abstraction, so long as you know its local derivative,

\[ \frac{d}{dn}\tanh(n) = 1 - \tanh(n)^2 = 1 - o^2. \]

The numbers in the lecture are chosen so the arithmetic comes out tidy: \(x_1=2\), \(x_2=0\), \(w_1=-3\), \(w_2=1\), and \(b \approx 6.8814\), which makes \(n \approx 0.8814\) and \(o \approx 0.7071\).

Start with \(o.\mathrm{grad} = 1\). Then \(n.\mathrm{grad} = 1 - o^2 \approx 0.5\). Every \(+\) along the way just copies that \(0.5\) backwards. The \(\times\) nodes swap in the other factor, and one of them is zero because \(x_2 = 0\): wiggling \(w_2\) cannot change an input that is silent.

Ex#DL-2002

If you want this neuron’s output to rise, \(w_1\) is the weight to increase (w1.grad is about \(+1\)). \(w_2\) cannot help, not because the weight is bad, but because its input is zero in this example.

Doing this by hand for a two-input neuron is already a little tedious. Doing it for a network is impossible. The next lab lets each operation remember its own local rule, and then walks the graph for us.

4 Summary

Idea In one sentence
Chain rule Local slope, multiplied by the gradient already on the output, is the gradient of the final result.
Plus Passes the incoming gradient through, unchanged.
Times Multiplies the incoming gradient by the other factor.
\(\tanh\) Local derivative \(1 - o^2\), where \(o = \tanh(n)\).
A neuron Weighted sum plus bias, then an activation; backprop is the same walk as on any other expression.
A gradient of zero This input cannot change the output in this example (here, \(w_2\) because \(x_2 = 0\)).

5 FAQ

Why start from the end, with a gradient of \(1\)? Because we are asking for the derivative of \(L\) (or \(o\)) with respect to \(L\) itself, which is \(1\). Every earlier gradient is “how much do I affect that final node?”.

Why can \(\tanh\) be a single node? Because we know its local derivative. We could have built \(\tanh\) from \(\exp\) instead (Karpathy does this later, at 1:27:05). The graph would be deeper and the answer the same. The size of a node is a modelling choice.

What does a negative weight gradient mean? Decrease that weight if you want the output to go up. The sign is the direction; the magnitude is how much the output cares.

Is this already training? Not yet. We have a direction for every slider. Training, in the last lab, is taking a small step in that direction, over and over.

Where is this in the lecture? Manual backprop on a simple expression from 32:10; a brief preview of one optimisation step at 51:10; the neuron and \(\tanh\) from 52:52. Official YouTube chapter markers, checked on the video page (30 November 2026).

6 See also

7 References

Back to top