Skip to content

Backpropagation

Apply the chain rule backward through a network to find the gradient of every weight

Quick recap
  • Backpropagation is the chain rule on a computational graph.
  • The forward pass stores the intermediate values; the backward pass reuses them.
  • Backprop computes gradients. The optimizer uses them to update the weights.

This lesson follows Neural Networks and the Gradient Descent lesson. For the calculus, review Multivariable Calculus.

1. The forward pass

Use a network with one input, one hidden unit, and one output. The weights and biases are w, c, v, b:

Forward pass, part 1
Forward pass, part 2

Start with x = 1 and w = 1, c = 0, v = 1, b = 0. Then:

z₁ = 1, h = tanh(1) ≈ 0.762, z₂ ≈ 0.762, and p = σ(0.762) ≈ 0.682.

The forward pass does more than produce p. It also stores x, z₁, h, and z₂. Backpropagation will reuse those values instead of recomputing them.

2. The loss

With label y = 1, binary cross-entropy is:

Binary cross-entropy

Here L = −ln(0.682) ≈ 0.383. The loss is lowest when the model gives the correct label probability 1, and grows as that probability falls.

The derivative with respect to the output probability is:

3. The chain rule

The loss depends on p, p depends on z₂, z₂ depends on h, h depends on z₁, and z₁ depends on w. To find how the loss changes with w, multiply the local derivatives along that path:

Chain rule along the graph

Two local derivatives are especially simple. The sigmoid plus cross-entropy gives:

Output gradient

The tanh derivative is 1 − h², and the linear score passes the error back to the input weight:

Hidden gradient

The same pattern repeats in every layer: multiply by the outgoing weight, then by the activation derivative. That is why the algorithm is called back-propagation.

4. Backward by hand

Worked example: every gradient in the tiny network

The forward pass gives p ≈ 0.682 and L ≈ 0.383. The backward pass gives:

quantityvalue
∂L/∂z₂ = p − y-0.318
∂L/∂v = (p − y)h-0.242
∂L/∂b = p − y-0.318
∂L/∂z₁-0.134
∂L/∂w-0.134
∂L/∂c-0.134

The signs say “increase w, v, b, and c to reduce the loss” at this point. The magnitude says how strongly. A learning rate of 0.1 would move w from 1 to about 1.013.

5. Backprop vs optimizer

Backpropagation does not change a single weight. It only computes gradients. Gradient descent, momentum, or Adam then turns those gradients into an update:

The optimizer's job

This separation matters. You can change the optimizer without changing backprop, and you can debug a network by checking whether the gradients are correct before blaming the learning rate.

6. Where it struggles

In a deep network the backward signal is multiplied by many weights and activation derivatives. If those factors are consistently smaller than 1, the gradient can vanish; if they are larger, it can explode. That is why initialization, normalization, and adaptive optimizers matter in practice.

Backprop also needs a differentiable path. A hard step function has zero derivative almost everywhere, so it cannot be used as a hidden activation with this method. ReLU, tanh, and sigmoid are smooth enough (with a convention at the ReLU corner).

The recommended path continues with overfitting and regularization. A network can compute perfect gradients and still memorize the training set.

Next on the recommended path: Overfitting, Bias–Variance & Regularization

7. Going deeper

A computational graph treats every operation as a node. The forward pass evaluates the graph; the backward pass sends a gradient from the output toward the inputs. Each node only needs its local derivative and the gradient arriving from above.

This is reverse-mode automatic differentiation. It computes the gradient of one scalar loss with respect to many parameters in roughly the cost of one forward pass. That efficiency is why backpropagation made large networks practical.

The chain rule is the whole idea. The engineering is in storing intermediate values, handling vector and matrix shapes, and avoiding numerical underflow or overflow.

8. Practice

Try it

Why is ∂L/∂b equal to p − y in this example?

Try it

If p = 0.9 and y = 1, what is the sign of ∂L/∂z₂? What does the optimizer do?

Try it

A network has a step activation in a hidden layer. Why does backpropagation have trouble training it?

9. Resources

The MIT chapter derives backpropagation for a single neuron and a hidden layer. The 3Blue1Brown chapter gives the visual chain-rule tour. Both links were opened and checked for this lesson.

Continue the track

The mathematics behind this topic: Multivariable calculus.

All Machine Learning lessons

Site soundtrack