FIG. 02 · Guides

Backpropagation, visualised: the chain rule one node at a time

Backpropagation is the chain rule applied in a careful order. This guide computes every gradient of one neuron by hand, then shows why the same few lines of code can train any network.

Published: 4 December 2026 Updated: 4 December 2026 7 min read

Every modern neural network learns the same way. It computes a number that measures how wrong it is, the loss, and then asks, for every parameter, “if I nudged you slightly, how much would the loss change?” That quantity is the gradient. Gradient descent moves each parameter a small step against its gradient, and repeats.

Backpropagation is simply an efficient way to compute all of those gradients at once. It is not a special learning rule; it is the chain rule from calculus, applied to a graph in reverse order. The Neural Network Autograd Lab shows every step live. This guide does the same calculation on paper.

A neuron as a graph

Take one neuron with two inputs, two weights and a bias, followed by a tanh activation. To make it trainable, compare its output with a target y* using the squared error.

n    = x1·w1 + x2·w2 + b
o    = tanh(n)
loss = (o − y*)²

Written as a computational graph, every intermediate result becomes a node: x1·w1, x2·w2, their sum, n, o, the difference o − y* and finally the loss. Each node remembers which nodes produced it and which operation it was. That is all the information backpropagation needs.

The forward pass

Use the same starting values as the lab: x1 = 2, x2 = 0, w1 = −3, w2 = 1, b = 6.8814 and target y* = 1.

Node Expression Value
x1·w1 2 × −3 −6.0000
x2·w2 0 × 1 0.0000
n −6 + 0 + 6.8814 0.8814
o tanh(0.8814) 0.7071
o − y* 0.7071 − 1 −0.2929
loss (−0.2929)² 0.0858

The forward pass is ordinary evaluation, from inputs to output.

Local derivatives

Backpropagation needs one small fact per operation: how its output changes when each input changes, holding the others fixed. These local derivatives are tiny and never change.

Operation Output Local derivative with respect to each input
addition a + b 1 and 1
multiplication a · b b and a
power aᵏ k · aᵏ⁻¹
tanh tanh(a) 1 − tanh(a)²
sigmoid σ(a) σ(a) · (1 − σ(a))
ReLU max(0, a) 1 if a > 0, otherwise 0

The chain rule says that the gradient of the loss with respect to a node equals the gradient flowing in from above, multiplied by the local derivative. If several paths lead from a node to the loss, the contributions add up.

The backward pass

Start at the top. The gradient of the loss with respect to itself is 1. Then walk the graph in reverse.

  1. Square. loss = d² with d = o − y*, so ∂loss/∂d = 2d = 2 × −0.2929 = −0.5858.
  2. Subtraction. d = o − y*, local derivative 1, so ∂loss/∂o = −0.5858.
  3. tanh. Local derivative 1 − o² = 1 − 0.5 = 0.5, so ∂loss/∂n = −0.5858 × 0.5 = −0.2929.
  4. Sum. Addition passes the gradient through unchanged: ∂loss/∂b = −0.2929, and both products receive −0.2929.
  5. Products. For x1·w1 the local derivative with respect to w1 is x1, so ∂loss/∂w1 = 2 × −0.2929 = −0.5858. With respect to x1 it is w1, so ∂loss/∂x1 = −3 × −0.2929 = 0.8787.
  6. The second input. ∂loss/∂w2 = x2 × −0.2929 = 0. Because x2 is zero, w2 has no influence on the loss right now, and its gradient says exactly that.

Every gradient took one multiplication. That is the whole trick.

One gradient step

Gradient descent updates each parameter as p ← p − η · ∂loss/∂p, where η is the learning rate. With η = 0.1:

w1 ← −3.0000 − 0.1 × (−0.5858) = −2.9414
b  ←  6.8814 − 0.1 × (−0.2929) =  6.9107
w2 ←  1.0000 − 0.1 × 0         =  1.0000

Run the forward pass again and the loss falls from 0.0858 to 0.0515. Press 1 gradient step in the lab and you will see exactly these numbers.

Why reverse order is efficient

A network may have millions of parameters but only one loss. Reverse-mode differentiation computes the gradient of that single output with respect to every input in one backward sweep, at a cost close to one extra forward pass. Forward-mode differentiation would need one sweep per parameter. That asymmetry is why reverse mode, under the name backpropagation, is the default for neural networks.

To walk the graph safely, the engine first sorts the nodes topologically, so that every node is processed only after all the nodes that depend on it. The lab’s engine does this iteratively rather than recursively, so it can handle graphs with tens of thousands of nodes without overflowing the call stack.

The bug everybody writes once

If a value is used twice, for example a · a, two separate paths lead back to a, and its gradient must be the sum of both contributions. An engine that assigns gradients with = instead of accumulating them with += silently gets this wrong. The unit tests in this project include exactly that case. Because gradients accumulate, they must also be reset to zero before each new backward pass.

From one neuron to a network

A multilayer perceptron is many neurons arranged in layers, and its graph is just larger. The same engine, with no new rules, trains the networks in the lab’s 2D playground.

  • XOR cannot be separated by a single straight line, so a network without a hidden layer cannot learn it. With a small hidden layer it bends the decision boundary into the right shape within a few hundred steps.
  • tanh versus ReLU. ReLU is cheap and does not saturate for positive inputs, but a ReLU unit whose input is always negative receives zero gradient and stops learning. tanh is smoother on tiny networks.
  • Learning rate. Too small and the loss creeps down; too large and it oscillates or diverges. Move the slider during training and the loss chart shows both failure modes.

The network diagram draws each weight as a line whose thickness is the magnitude and whose colour is the sign, so you can watch the structure form as the loss falls.

Checking the gradients

Analytic gradients can be wrong in subtle ways, so the engine checks itself with central finite differences:

∂f/∂x ≈ (f(x + h) − f(x − h)) / 2h,   with h = 0.000001

The approximation error shrinks with h², so it is accurate enough to catch almost any mistake. The lab shows the largest gap between analytic and numeric gradients live, and the unit tests require agreement within 1e−5 for every operation, every activation and networks up to two hidden layers of eight neurons.

Credit

The engine follows the design of Andrej Karpathy’s micrograd, released under the MIT licence: a scalar Value class, a closure per operation and a topological sort. This version is written in TypeScript, adds sigmoid and ReLU, an iterative sort, a seeded random number generator and finite-difference tests.

Open the Neural Network Autograd Lab and press the step button a few times. Watching the gradients change is the fastest way to make the chain rule feel obvious.