Learn the MachineDansk

Explained with numbers you can follow

Backpropagation, step by step

Backpropagation is how a neural network works out how each weight should change to make the error smaller. It applies the chain rule backward through the calculation, one step at a time. Walk through the steps yourself on a tiny network with one hidden neuron.

The demo needs JavaScript. The text below explains what it shows.

Embed this demo on your site

Copy the code into your page. The demo loads from learnthemachine.com, so it stays up to date, and the link under it shows where it comes from.

Forward: compute the answer and the error

The network in the demo has four parameters: w₁ and b₁ for the hidden neuron, and w₂ and b₂ for the output. The forward pass computes z = w₁·x + b₁, then h = tanh(z), then the output y = w₂·h + b₂, and finally the loss L = ½(y − t)², which measures how far y is from the target t.

Backward: the chain rule

We want to know how much L changes when each parameter changes a little: the gradient ∂L/∂w. The chain rule says you can find it by multiplying the local derivatives together along the path from the parameter to the loss. Backpropagation starts at the loss and works backward, so each gradient reuses the one just computed. First ∂L/∂y, then ∂L/∂h, then ∂L/∂z, and every parameter gets its gradient along the way.

That is why the method is efficient. A network with millions of weights gets all its gradients in one backward pass that costs about the same as the forward pass.

The backpropagation formulas

Written out for the network in the demo, the backward pass is seven lines. Each line multiplies the gradient from the line above by one local derivative:

With the demo’s starting values (x = 1, t = 0.9, w₁ = 0.8, b₁ = 0.1, w₂ = −0.6, b₂ = 0.2), the forward pass gives z = 0.9, h = tanh(0.9) ≈ 0.716, y ≈ −0.230 and L ≈ 0.638. Backward: ∂L/∂y ≈ −1.130, ∂L/∂w₂ ≈ −0.809, ∂L/∂h ≈ 0.678 and ∂L/∂z ≈ 0.330, and since x = 1, ∂L/∂w₁ = ∂L/∂b₁ ≈ 0.330. The negative ∂L/∂w₂ says that a larger w₂ makes the loss smaller: y is below the target, and h is positive.

The general form for a layer

In a real network each layer computes zl = Wlal−1 + bl and al = f(zl), with vectors and weight matrices instead of single numbers. Call δl = ∂L/∂zl the layer’s error. Backpropagation is then four formulas:

⊙ means multiplying element by element. The transposed weight matrix sends the error back along the same connections the signal took forward, and f′ is the activation function’s local derivative: 1 − tanh² for tanh, 0 or 1 for ReLU. In the demo every vector has one element and the output layer has no activation function (f′ = 1), so the four formulas become the seven lines above: the output layer’s δ is ∂L/∂y = y − t, the hidden layer’s δ is w₂ · (y − t) · (1 − h²), and the weight gradients are each δ times the layer’s input (h and x).

Gradient descent: use the gradients

Backpropagation only finds the gradients. The learning happens in the next step: each parameter moves a little in the opposite direction of its gradient, w ← w − η · ∂L/∂w, where η is the learning rate. If the learning rate is too small, progress is slow. If it is too large, the loss can grow instead of shrink. Try it with the slider.

Why tanh matters

The step through tanh multiplies by 1 − h². When the neuron is saturated and h is close to −1 or 1, that factor is close to 0 and the gradient becomes small. In deep networks with many such layers, the gradients can get so small that the first layers barely learn. This is called vanishing gradients, and it is one reason modern networks often use ReLU and residual connections.

The numerical check

You can also find a gradient by changing a parameter by a very small number ε and measuring the change in the loss. That is far too slow for training, because it needs two passes per parameter, but it is a good way to check that backpropagation is implemented correctly. In the demo the two numbers agree to at least five decimal places.

Train a real network and look inside it

In the Neural network lab you train a network with four hidden neurons in the browser on a task a straight line cannot solve, then examine what the hidden neurons do. The lab requires full access. If you would rather start with a single neuron, the Vectors and neurons lab is free.

See the Neural network labStart free with Vectors and neurons

More explainers