Learning LabExplorable explanations
← All artifacts
Machine Learning

Backpropagation, by Hand

A tiny network learns XOR while you watch. Step the forward pass neuron by neuron, then trace the gradient backward edge by edge as the chain rule assembles itself.

backpropagationneural-networksgradientsautodiff
LiveInteractive · drag, toggle, run it
Machine Learning, computed live

Backpropagation, by Hand

XOR is the smallest problem a single neuron cannot solve, so it needs a hidden layer, which makes it the perfect place to watch backprop work. This net has two tanh neurons and one sigmoid output; walk the forward pass and the chain rule backward one step at a time, and read every gradient as it forms.

Idle
Example (0, 1) -> 1
INPUTSHIDDEN (tanh)OUTPUT (sigmoid)x00x11h0h1ŷ
Pick example:
Ready

Step the forward pass to push this example through the network, then step the backward pass to watch the chain rule assign a gradient to every weight. Or skip ahead and run a gradient-descent update.

2.5

Larger steps descend faster but can overshoot and oscillate. Too small and the loss crawls. Watch the curve below react when you change it mid-training.

Loss over time0.1352
Run an update or train to plot the loss.
Mean squared error over all 4 examples after 0 updates.
Predictions vs target
(0, 0)
0.58 / 0
(0, 1)
0.50 / 1
(1, 0)
0.41 / 1
(1, 1)
0.39 / 0
Not yet separating the classes. Keep training.
Why we step opposite the gradient

A gradient is the direction of steepest increase in the loss. Each weight's gradient answers one question: if I nudge this weight up a little, does the loss rise or fall, and how sharply? Backprop computes all of those answers in a single backward sweep by reusing the upstream gradient at each layer, which is why it is far cheaper than poking each weight one at a time. To shrink the loss we move every weight a small step in the opposite direction of its gradient, scaled by the learning rate. Repeat over the four examples and the network bends its decision boundary until XOR comes apart.