How it works · tiny engine for training neural networks

How micrograd works

Every number remembers how it was made, so the answer can be traced back to say how much each input mattered.

Think of it like this. A receipt for a calculation. Read it backwards and you can see what each item added to the total.

  1. Value A number that also keeps its parents, the operation that made it, and its gradient. micrograd/engine.py
  2. The operations Add, multiply, power, relu. Each makes a new Value and knows how to pass blame back to its inputs. micrograd/engine.py
  3. backward() Orders every Value so parents come first, then walks back from the answer. micrograd/engine.py
  4. The network Neurons, layers and a small network, built from nothing but Values. micrograd/nn.py

Follow one thing through: You compute c = a * b + a, then call c.backward()

  1. a * b makes a new Value that remembers a and b, and a small function for passing blame back.micrograd/engine.py
  2. Adding a makes another Value on top. The calculation is now a graph.
  3. backward() lists every Value in order, so each comes after the ones it was made from.micrograd/engine.py
  4. The answer’s gradient is set to 1: it matters fully to itself.
  5. Walking the list in reverse, each Value hands its gradient to its parents, scaled by how much they affected it.
  6. a was used twice, so it receives blame twice, and the two are added.

The clever bit: Each operation carries its own way back

There is no big table of derivatives. When a * b runs, it stores a tiny function on the result that knows one thing: for multiplication, a’s share of the blame is b’s value, and the other way round. Backpropagation is then just calling those tiny functions in the right order. That is the whole engine, in under 100 lines.

micrograd/engine.py

Words you’ll meet

gradient
How much the answer changes if you nudge this number a little.
backpropagation
Working out every gradient in one pass, starting at the answer and going backwards.
chain rule
If a affects b and b affects c, multiply the two effects to get a’s effect on c.

Good questions to ask next

Open micrograd in Bento and ask them there. It reads the project in your browser, draws its parts as a map you can press, and opens the real code behind each one.

Take micrograd apart yourself.

Free and open source. Nothing to install, no account.

More projects, taken apart

Written by hand from the code as it was on 2026-08-03 (commit 7bc720e). micrograd belongs to its authors; this page is an independent explanation. Spot a mistake? Tell us.