Lab#DL01-2: Backpropagation by hand

Lab

📘 Lab#DL01-2: 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}. \]

Intuitive explanation of the chain rule

The chain rule is a product of rates. If you know how fast \(z\) changes with \(y\), and how fast \(y\) changes with \(x\), then \(z\) changes with \(x\) at the product of those two rates:

\[ \frac{dz}{dx} = \frac{dz}{dy} \cdot \frac{dy}{dx}. \]

A picture from everyday speeds: “If a car travels twice as fast as a bicycle and the bicycle is four times as fast as a walking man, then the car travels \(2 \times 4 = 8\) times as fast as the man.” Let \(z\), \(y\) and \(x\) be the positions of the car, the bicycle and the walker. Then \(dz/dy = 2\), \(dy/dx = 4\), so \(dz/dx = 2 \cdot 4 = 8\).[4]

Diagram of a walker x, then a bicycle y with the rate dy over dx equal to 4, then a car z with the rate dz over dy equal to 2. A bracket under the chain is labelled dz over dx equals 2 times 4 equals 8. Below, the chain-rule formula and a note that in backpropagation the local derivatives along a path get multiplied.

The two local rates multiply: \(dz/dx = 2 \cdot 4 = 8\). In backpropagation the local derivatives along a path are multiplied in the same way. Own diagram, based on Wikipedia’s chain-rule example.

Diagram of a walker x, then a bicycle y with the rate dy over dx equal to 4, then a car z with the rate dz over dy equal to 2. A bracket under the chain is labelled dz over dx equals 2 times 4 equals 8. Below, the chain-rule formula and a note that in backpropagation the local derivatives along a path get multiplied.

The two local rates multiply: \(dz/dx = 2 \cdot 4 = 8\). In backpropagation the local derivatives along a path are multiplied in the same way. Own diagram, based on Wikipedia’s chain-rule example.

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#DL01-4

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, and the diagram below is the same picture Karpathy uses in the lecture. Each input \(x_i\) arrives along an axon and is multiplied by a weight \(w_i\) — the synaptic strength. Those products travel along dendrites into the cell body, where they are summed and a bias \(b\) is added. An activation \(f\) — in micrograd, \(\tanh\) — then squashes the total into \((-1, 1)\), and the result leaves along the output axon:

About the neuron

A biological neuron takes incoming signals on its dendrites, combines them in the cell body, and may send a spike along its axon. The picture we use here is an analogy of that: weights are synaptic strengths, the weighted sum plus bias is the cell body, and \(f\) stands in for whether — and how strongly — the neuron fires.

It is a very rough model. Real dendrites and synapses are far more complicated, and spike timing matters in ways this picture ignores. It is still the right starting point for the arithmetic we need.

Screenshot from Karpathy’s micrograd lecture: a draw_dot graph of neuron inputs. x1 has data 2.0 and w1 has data minus 3.0, going into a times node; w2 has data 1.0 and x2 has data 0.0, going into a times node; every gradient is 0.

Two inputs \(x_1\) and \(x_2\), each multiplied by its weight \(w_1\) and \(w_2\) (\(x_i \cdot w_i\)). Every grad is still 0, because backpropagation has not run yet.

Screenshot from Karpathy’s micrograd lecture: a plus node feeds n with data 0.7000, then a tanh node, then the neuron output with data 0.6044; every gradient is 0.

The weighted sum plus the bias gives \(n = 0.7\), then the activation \(\tanh\) squashes it to the neuron’s output \(0.6044\) (\(\tanh(0.7) \approx 0.6044\)), with grads still 0.

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

Diagram of an artificial neuron from Stanford CS231n: inputs x arrive along an axon, are multiplied by weights w at the synapses, travel along dendrites into the cell body where they are summed and a bias is added, then an activation function f produces the output axon.

Neuron model: inputs \(x_i\), weights \(w_i\) at the synapses, a sum plus bias in the cell body, then the activation \(f\) on the way out. Neuron model diagram from Stanford CS231n (cs231n.github.io/neural-networks-1), as shown in Andrej Karpathy’s micrograd lecture.

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#DL01-5

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