Build the autograd engine

Implement the Value class: a scalar that records its computation graph and runs backpropagation (backward()) to compute gradients automatically. Ends with a single neuron whose gradients are provably correct.

Python~60 min
Use this templateFree · your own copy on GitHub

What you’ll build

A working automatic-differentiation engine — the same idea behind PyTorch — in about 100 lines. By the end, a real trainable neuron whose gradients you can prove are correct.

engine.pyPython
a = Value(2.0)
b = Value(-3.0)
L = (a * b + Value(10.0)).tanh()

L.backward()      # fills in every gradient automatically
a.grad            # how much L moves when you nudge a

The tests decide when you’re done — not the tutor. When they pass, you’ve built it.

What you’ll learn

  • Implement a Value class that records every operation into a computation graph
  • Write the backward pass — reverse-mode autodiff — so gradients fill in automatically
  • Derive and apply the chain rule by hand through +, *, and tanh
  • Check your gradients against numerical estimates, so you know they're correct
  • Wire it into a single neuron and watch the gradients flow end to end
Automatic differentiationComputation graphBackpropagationChain ruleReverse-mode autodiffGradients

Before you start: Comfortable writing Python classes. A faint memory of derivatives is plenty — you re-derive the chain rule here by building it.

Meet your tutor

It won’t hand you the answer. It asks the one question that makes the next line obvious — and checks your work by running the tests.

you
i don’t get why we even need gradients
tutor
before I answer — picture nudging a up by a hair. what happens to the loss?
you
it goes down a little?
tutor
exactly — that “how much” is the gradient. let’s make the code measure it. open engine.py and find where * is defined…

A taste of the tutoring style

Next project

Build & train a neural net