Backpropagation, Step by Step

After reading this you can hand-trace one forward and one backward pass of a small neural network, compute every gradient with the chain rule, and update each weight by gradient descent using numbers you can check with a calculator.

What backpropagation is

Backpropagation is the algorithm that tells you how to change each weight in a neural network to reduce its error. It has two halves. The forward pass sends an input through the network and produces an output and a loss. The backward pass sends the error back through the same network, computing for every weight the partial derivative of the loss with respect to that weight.

Here is the hook. Suppose a single weight w equals 0.5, and the backward pass reports \partial L / \partial w = 0.8. That number means: if you nudge w up by a tiny amount, the loss rises about 0.8 times that amount. So you push w the other way. With a learning rate of 0.1 the update is 0.5 - 0.1 × 0.8 = 0.42. Backprop is the bookkeeping that produces that 0.8 for every weight at once.

The 2→2→1 network in the tool has two inputs, two hidden neurons, and one output. That is small enough to write every value on the diagram and still large enough to show a full gradient path with several chain-rule factors.

When to trace it by hand, and when not

Trace backprop by hand once or twice to understand it. A 2→2→1 network has 9 weights (4 input-to-hidden, 2 biases in the hidden layer, 2 hidden-to-output, 1 output bias). You can follow all 9 gradients on paper in a few minutes.

Do not trace it by hand for real work. A network with a million parameters has a million gradients per pass, and training runs millions of passes. Autodiff libraries compute all of them mechanically. The value of a hand trace is that it removes the mystery: once you have multiplied three local derivatives together and watched them produce a gradient, the phrase "the gradient vanished" stops being magic.

A browser demo running a 9-weight network says nothing about training speed or accuracy at production scale. It shows you the arithmetic of one update exactly. Wall-clock training time, batching, and optimizer tricks are separate questions.

The math, one factor at a time

Each hidden neuron computes a weighted sum, then an activation. For hidden neuron h1:

z_1 = w_{11} x_1 + w_{21} x_2 + b_1, \qquad a_1 = \sigma(z_1)

Here z_1 is the pre-activation sum, a_1 is the activation, and \sigma is either sigmoid or tanh. The output neuron does the same with the hidden activations as its inputs, giving a prediction \hat{y}. The loss for one example is squared error:

L = \tfrac{1}{2}(\hat{y} - y)^2

The factor \tfrac{1}{2} is there so the derivative is clean: \partial L / \partial \hat{y} = \hat{y} - y, with no stray 2.

To find how a weight affects the loss, chain the local derivatives along the path from the loss back to that weight:

\frac{\partial L}{\partial w} = \frac{\partial L}{\partial \hat{y}} \cdot \frac{\partial \hat{y}}{\partial z} \cdot \frac{\partial z}{\partial w}

Read it right to left. \partial z / \partial w is just the input feeding that weight. \partial \hat{y} / \partial z is the activation derivative. \partial L / \partial \hat{y} is the error. The two activation derivatives you need are:

\sigma'(z) = \sigma(z)\,(1 - \sigma(z)), \qquad \tanh'(z) = 1 - \tanh^2(z)

The sigmoid derivative peaks at 0.25 when \sigma(z) = 0.5. The tanh derivative peaks at 1 when z = 0. Every layer a gradient passes through multiplies it by one of these factors, so deep gradients tend to shrink.

A worked example you can reproduce

Use the demo defaults: x_1 = 1, x_2 = 0, target y = 1, sigmoid activation. Fix these weights so the numbers are checkable: input-to-hidden weights w11 = 0.5, w21 = -0.3, w12 = 0.8, w22 = 0.2, hidden biases both 0, hidden-to-output weights v1 = 0.4, v2 = -0.6, output bias 0.

Forward pass, then backward pass

  1. Hidden sums: z_1 = 0.5(1) + (-0.3)(0) = 0.5 and z_2 = 0.8(1) + 0.2(0) = 0.8.
  2. Hidden activations: a_1 = \sigma(0.5) = 0.6225 and a_2 = \sigma(0.8) = 0.6900.
  3. Output sum: z_o = 0.4(0.6225) + (-0.6)(0.6900) = 0.2490 - 0.4140 = -0.1650.
  4. Prediction: \hat{y} = \sigma(-0.1650) = 0.4588.
  5. Loss: L = \tfrac{1}{2}(0.4588 - 1)^2 = \tfrac{1}{2}(0.2929) = 0.1465.
  6. Output error: \partial L / \partial \hat{y} = 0.4588 - 1 = -0.5412.
  7. Output activation derivative: \sigma'(z_o) = 0.4588(1 - 0.4588) = 0.2483.
  8. Combine into the output "delta": \delta_o = -0.5412 \times 0.2483 = -0.1344.
  9. Gradient for v1: \delta_o \cdot a_1 = -0.1344 \times 0.6225 = -0.0837. For v2: -0.1344 \times 0.6900 = -0.0927.
  10. Push the delta back to hidden neuron 1: \delta_1 = \delta_o \cdot v_1 \cdot \sigma'(z_1) = -0.1344 \times 0.4 \times (0.6225)(0.3775) = -0.1344 \times 0.4 \times 0.2350 = -0.01264.
  11. Gradient for w11: \delta_1 \cdot x_1 = -0.01264 \times 1 = -0.01264. Gradient for w21 is \delta_1 \cdot x_2 = 0, because x_2 = 0.
  12. Update v1 with learning rate 0.5: 0.4 - 0.5(-0.0837) = 0.4419. The weight moved up because raising it would lower the loss.

Notice step 11: the gradient for w21 is exactly zero. A weight whose input is zero receives no signal on this example, so it does not change. That is not a bug. It is the chain rule reporting that this input played no part in this prediction.

Reading the values as they change

Two quantities carry the most meaning during a trace.

The delta at a neuron
The error signal already multiplied by that neuron's activation derivative. Every weight feeding the neuron uses the same delta, scaled by its own input. In the example \delta_o = -0.1344 served both output weights.
The shrink factor per layer
Each backward step through a sigmoid multiplies by at most 0.25. In step 10 the gradient passed through v1 = 0.4 and then \sigma'(z_1) = 0.2350, so \delta_1 is about a tenth of \delta_o. Stack ten sigmoid layers and the signal can fall below 0.25^{10} \approx 0.0000010.

Commit several updates and watch the loss. With the numbers above and learning rate 0.5, the loss falls steadily from 0.1465 toward a small value as \hat{y} climbs toward the target of 1. The curve below shows the shape of that decline for this single example.

The loss drops fast at first, then flattens as the prediction nears the target and gradients shrink.

Watch the learning rate reshape the update

The single most instructive knob is the learning rate. Too small and the loss crawls down. Too large and the update overshoots, so the loss can rise instead of fall. Move the slider and watch a single weight step toward or past its target.

With the example gradient -0.0837 for weight v1 = 0.4: a learning rate of 0.5 updates it to 0.442, a rate of 2 to 0.567, and a very large rate of 20 to 2.07, which overshoots and raises the loss on the next pass.

Common mistakes

Do not forget the activation derivative. A frequent error is computing \partial L / \partial w = (\hat{y} - y) \cdot \text{input} and skipping the middle factor. With sigmoid at z_o = -0.1650 that factor is 0.2483, so leaving it out inflates every output-layer gradient by about four times and can make the loss diverge.

Three more traps show up often:

  • Reusing stale activations. The backward pass needs the exact activations from the forward pass. If you update a weight and then keep computing gradients with the old prediction, the numbers stop matching. Finish the whole backward sweep first, then apply all updates.
  • Sign confusion. The update subtracts the gradient: w \leftarrow w - \eta \cdot \partial L / \partial w. A negative gradient means the weight goes up. In the example v1 rose from 0.4 to 0.442 precisely because its gradient was -0.0837.
  • Blaming the algorithm for a dead input. A zero gradient on w21 here comes from x_2 = 0, not from a mistake. Change the input and the gradient reappears.

Related tools

Once the single-pass arithmetic is clear, watch the same math run continuously in the Neural Network Playground, where a small network learns a decision boundary. To see why the shrink factor above matters at depth, train a deep stack in Vanishing & Exploding Gradients and watch per-layer gradients starve. Compare the activation derivatives themselves in the Activation Function Explorer. For a full trained model, try the Handwritten Digit Recognizer, and to fight the overfitting that unconstrained training invites, see the Regularization Demo.

Frequently asked questions

Why is it called backpropagation?

Because the error signal propagates backward through the network, from the loss at the output toward the input. The forward pass goes left to right computing activations; the backward pass goes right to left computing gradients, reusing each stored activation.

What is the difference between the gradient and the delta?

The delta at a neuron is the error already multiplied by that neuron's activation derivative. The gradient for a specific weight is that delta times the input flowing along that weight. One delta produces many weight gradients, which is why backprop is efficient: it computes all gradients in one sweep at roughly the cost of two forward passes.

Why does the tool offer sigmoid and tanh but not ReLU?

Sigmoid and tanh have smooth derivatives that are easy to write out fully (0.25 and 1 maxima) and make the vanishing-gradient effect visible in a single trace. ReLU has a flat derivative of 0 or 1, which is important in practice but less illustrative for a hand calculation of the chain rule.

Why does my loss go up after an update?

Almost always the learning rate is too large. The update moves the weight by \eta times the gradient, and if that step overshoots the bottom of the loss bowl the loss rises. Halve the rate and try again. The widget above shows the exact threshold for the example gradient.

Does the squared-error loss matter, or could I use another?

Only the first factor changes. With squared error \partial L / \partial \hat{y} = \hat{y} - y. With a different loss you substitute a different first factor; every later factor in the chain stays the same. Classification networks usually pair a softmax output with cross-entropy loss, which conveniently gives the same clean \hat{y} - y form.