A reverse-mode automatic differentiation engine written from scratch — no PyTorch, no libraries — training a real neural network live in your browser. Every gradient you see below was computed by ~120 lines of hand-written chain rule.
Every scalar in the network is a Value that remembers how it was created.
Arithmetic (+, *, tanh…) builds a computation graph.
Calling loss.backward() walks the graph in reverse topological order and applies
the chain rule at every node — that's all backpropagation is.
For c = a · b, the engine closes over:
∂c/∂a = b → a.grad += b.data * c.grad∂c/∂b = a → b.grad += a.data * c.gradThe whole engine is src/engine.js — about 120 lines, view source.