flowchart BT a["a = 2"] --> e["e = a × b = −6"] b["b = −3"] --> e e --> d["d = e + c = 4"] c["c = 10"] --> d d --> L["L = d × f = −8"] f["f = −2"] --> L
Lab#DL-0001: Derivatives and the computation graph
Lab
📘 Lab#DL-0001: Derivatives and the computation graph
A derivative is a sensitivity: if I nudge this input, how much does the output move? We measure that with a tiny step, then we start wrapping numbers in a Value object that remembers how they were made. That memory is the computation graph.
This lab follows Andrej Karpathy’s lecture The spelled-out intro to neural networks and backpropagation: building micrograd (the first video of Neural Networks: Zero to Hero). The code lives in AlbertProfe/wikiml-zero-to-hero (z2h-01), derived from karpathy/micrograd (MIT Licence).
The cells on this page are live Python in the browser. There is no PyTorch here, and no install. The first load takes a few seconds.
1 What a derivative actually says
Karpathy’s opening move is not a long symbolic derivation. It is a measurement.
Take a function of one input, say \(f(x) = 3x^2 - 4x + 5\). At \(x = 2\) the value is \(f(2) = 9\). Now nudge the input by a tiny amount \(h\) and look at how far the output moved:
\[ \frac{f(x + h) - f(x)}{h} \]
That ratio is the derivative at \(x\). In words: how sensitive is the output to this input, right here? If the derivative is \(8\), then a small nudge of \(+0.001\) on \(x\) should lift \(f\) by about \(0.008\). If it is negative, the output falls when the input rises.
An everyday picture: you stand on a hillside in the fog. You cannot see the whole slope, but you can shuffle one foot a centimetre downhill and feel how far you dropped. The derivative is that drop, divided by the shuffle. A steep hill gives a large number; a flat terrace gives almost zero; if the ground rises when you shuffle “forwards”, the sign flips.
The same idea has many names later — gradient, sensitivity, “how much blame this weight deserves” — but it is this measurement.
Karpathy works this through from about 8:08 in the lecture.
The numerical slope sits right next to \(8\). That is all we asked: a number that tells us, at this point, which way \(f\) moves and how strongly.
2 Several inputs at once
Most interesting functions have more than one input. Then we do not have one slope; we have one slope per input, each measured while the others stay still. Those are the partial derivatives. Together they make the gradient.
A useful analogy is a mixing desk. Each slider is an input. You wiggle one slider and listen to how much the output changes; then you put it back and wiggle the next. The gradient is the list of those listenings.
Karpathy does this with a small multi-input expression from about 14:12.
Read the numbers as advice. If you want \(g\) to go up, increase \(b\) (its derivative is \(+2\)) and decrease \(a\) (its derivative is \(-3\)). Changing \(c\) moves \(g\) one-for-one. A neural network will later take exactly this advice, on millions of sliders at once. The idea does not change.
3 A Value that remembers its parents
A calculator throws the recipe away and keeps only the result. To take derivatives automatically we have to keep the recipe.
That is Karpathy’s Value object, started at about 19:09. Each Value stores:
data— the number itself;_prev— the values it was made from;_op— the operation that combined them (+,*, and latertanh).
A label is only there so we can point at a node when we draw the graph.
Nothing here computes a derivative yet. The object has only learned to remember. That memory is already the computation graph: a directed acyclic graph whose leaves are the inputs and whose root is the expression we care about.
Every arrow is a parent pointing to a child that used it. When we later walk backwards along those arrows, we will be doing backpropagation. For now it is enough to see that the graph is just the arithmetic, written down.
The full Value class, with gradients, lives in engine.py of the lab repo. The next lab fills the graph in by hand.
4 Summary
| Idea | In one sentence |
|---|---|
| Derivative | How much the output moves when you nudge one input by a tiny \(h\), divided by \(h\). |
| Sign and size | The sign says which way to move the input; the size says how strongly the output cares. |
| Partial derivative | The same measurement, with the other inputs held still. |
| Gradient | The list of all partial derivatives. |
Value |
A number that also stores its parents and the operation that made it. |
| Computation graph | The directed acyclic graph of those parent links, from inputs to the final expression. |
5 FAQ
Do I need a lot of calculus? No. Karpathy deliberately avoids long symbolic expansions. If you can accept “nudge, measure, divide” and, later, the chain rule as “multiply local slopes”, you can follow the lecture and these labs.
Why is \(h\) so small? Why not \(1\)? The true derivative is the limit as \(h\) goes to zero. A large \(h\) sees the curve bend; a tiny \(h\) stays on an almost straight piece of it. Too tiny, and floating-point noise takes over. \(10^{-4}\) is a comfortable middle.
Why wrap numbers in a class instead of using Python floats? A float is a bare result. A Value is a result plus the recipe. Automatic differentiation needs the recipe.
Is this already a neural network? No. It is the bookkeeping a network will sit on. A network is just a large expression of this kind, with the “sliders” being its weights.
Where is this in the lecture? Overview of micrograd from 0:25; the one-input derivative from 8:08; several inputs from 14:12; the Value object and its graph from 19:09. The official YouTube chapter markers were checked on the video page (30 November 2026).
6 See also
7 References
- [1] Andrej Karpathy, The spelled-out intro to neural networks and backpropagation: building micrograd, 2022. Video in Neural Networks: Zero to Hero.
- [2] Andrej Karpathy, micrograd, GitHub, MIT Licence. https://github.com/karpathy/micrograd
- [3] Lab code for this sequence: AlbertProfe/wikiml-zero-to-hero, tag
z2h-01, also MIT.