Find a course

Module 3: How Networks LearnLesson 7 of 1116 min

Backpropagation

Use the chain rule to compute gradients through a network, work a full training step by hand, and see why gradients can vanish in deep networks.

Step 1 of 15Learn

The bookkeeping problem

Gradient descent needs the slope of the loss with respect to every weight. How would you get it? The naive approach: nudge one weight by a tiny amount, run a forward pass, see how much the loss changed, and repeat for every weight. For the 101,770-parameter digit network, that's over 100,000 forward passes for a single update step. For a model with a billion parameters, it's hopeless.

Backpropagation (short for backward propagation of errors) solves this. It computes the exact gradient for every weight in roughly the cost of one forward pass plus one backward pass, whatever the number of weights. That efficiency is what makes training large networks possible at all. It was described by several researchers over the years and became widely known through a 1986 paper; today it runs inside every deep learning framework.

The trick is not magic, just careful reuse of intermediate results. A network is a long chain of simple operations (multiply, add, activate), and calculus has a rule for differentiating chains. Backprop applies that rule starting from the loss and working backward, layer by layer, so each layer's gradient is computed from the one after it instead of from scratch.

Step 2 of 15Learn

The chain rule

If one quantity depends on a second, and the second depends on a third, the rates of change multiply. In symbols: if the loss L depends on an activation a, a depends on z, and z depends on a weight w, then

dL/dw = dL/da · da/dz · dz/dw

Think of gears. If gear A turns 3 times for each turn of gear B, and gear B turns 2 times for each turn of gear C, then A turns 3 × 2 = 6 times per turn of C. Each link in the chain contributes its own local ratio, and the overall ratio is the product.

For a neuron, the local ratios are easy:

  • z = w·x + b, so dz/dw = x (the input) and dz/db = 1.
  • For ReLU, da/dz is 1 when z > 0 and 0 when z < 0. For sigmoid, da/dz = σ(z)·(1 − σ(z)).
  • For a squared-error loss L = (a − y)2, dL/da = 2(a − y).

The pleasant consequence: a weight's gradient is proportional to the input flowing through it. If an input was 0 for an example, its weight receives no update from that example, which makes intuitive sense, since the weight had no influence on the output.

Step 3 of 15Diagram

The chain from a weight to the loss

Each arrow has a local rate of change. Backprop multiplies them, right to left.

The chain from a weight to the lossProcess diagram: Weight w → z = w·x + b → a = activation(z) → Loss L.Weight wz = w·x + ba =activation(z)Loss L

Tap a shape with to see more.

dL/dw = dL/da × da/dz × dz/dw.
Weight w
z = w·x + b

Local rate dz/dw = x, the input.

a = activation(z)

Local rate da/dz = the slope of the activation at z.

Loss L

Local rate dL/da, e.g. 2(a − y) for squared error.

Step 4 of 15Practice · Work it out

In a chain where dL/da = 2, da/dz = 0.5 and dz/dw = 3, what is dL/dw?

Step 5 of 15Learn

One training step by hand

Here's a complete step for a single linear neuron (no activation), trained on one example: input x = 2, target y = 3. Current weight w = 0.5, bias b = 0, learning rate η = 0.1. Loss: L = (a − y)2.

Forward pass

  • z = w·x + b = 0.5·2 + 0 = 1
  • a = z = 1 (linear neuron)
  • L = (1 − 3)2 = 4

Backward pass

  • dL/da = 2(a − y) = 2(1 − 3) = −4
  • da/dz = 1
  • dL/dw = −4 · 1 · x = −4 · 2 = −8
  • dL/db = −4 · 1 · 1 = −4

Update

  • w = 0.5 − 0.1·(−8) = 1.3
  • b = 0 − 0.1·(−4) = 0.4

Check: the new output is 1.3·2 + 0.4 = 3.0, exactly the target, and the loss drops from 4 to 0. One step rarely lands this perfectly; it happened here because the numbers were chosen to be friendly. Notice that the forward pass values (z, a, x) were needed in the backward pass. Frameworks store them during the forward pass, which is why training uses much more memory than inference.

Step 6 of 15Practice · Fill in the blanks

Fill in the blanks

For a neuron with z = w·x + b, the derivative dz/dw equals , and the derivative dz/db equals .

Step 7 of 15Tap to reveal

Backpropagation through a whole network, step by step

  1. 1. Forward pass

    Run the input through every layer and store each layer's z values and activations. These cached values are reused in the backward pass.

  2. 2. Compute the loss

    Compare the output with the label using the loss function, for example cross-entropy.

  3. 3. Gradient at the output

    Compute how the loss changes with each output value. For softmax with cross-entropy this is neat: predicted probabilities minus the one-hot label.

  4. 4. Move backward one layer

    Multiply by the derivative of the activation function, then by the transposed weight matrix, to get the gradient with respect to the previous layer's activations.

  5. 5. Weight gradients along the way

    At each layer, the gradient for a weight is the incoming gradient times the input that flowed through that weight in the forward pass.

  6. 6. Update

    Once every gradient is known, the optimizer (gradient descent, Adam) adjusts all weights and biases at once.

Step 8 of 15Practice · Put in order

Put the parts of one training step in the correct order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Compute the loss from predictions and labels
  2. Backpropagate to get the gradient for every parameter
  3. Run the forward pass on a batch of examples
  4. Update the parameters with the optimizer
  5. Move on to the next batch

Step 9 of 15Learn

Vanishing and exploding gradients

The chain rule multiplies one factor per layer. That's fine for three layers, but deep networks multiply dozens of factors, and products of many numbers behave badly.

If each factor is less than 1, the product shrinks toward zero: the vanishing gradient problem. Sigmoid is the classic culprit, because its derivative is at most 0.25. Through 10 sigmoid layers, the gradient can shrink by a factor of 0.2510, which is about one in a million. The early layers then barely learn while the later layers do all the work. This is a major reason deep networks were so hard to train before about 2010.

If the factors are larger than 1, the product grows explosively: the exploding gradient problem, which sends weights to huge values and the loss to NaN.

The fixes are some of the most important ideas in modern deep learning:

  • ReLU activations, whose derivative is exactly 1 for positive inputs.
  • Careful initialization (Xavier or He initialization) that scales random starting weights so signals keep a steady size from layer to layer.
  • Batch or layer normalization, which rescales activations inside the network.
  • Residual connections, which add a layer's input to its output and give gradients a shortcut path backward.
  • Gradient clipping, which caps the size of the gradient to tame explosions.

Step 10 of 15Practice · Work it out

A gradient passes backward through 5 sigmoid layers, and each layer multiplies it by at most 0.25. What is the largest fraction of the original gradient that can reach the first layer? Give the answer as a decimal.

Step 11 of 15Chart

A gradient after passing through many layers

If every layer multiplies the gradient by the same factor, small differences compound fast.

A gradient after passing through many layersLine chart with 7 categories: 0: Factor 0.9 per layer 1, Factor 1.1 per layer 1; 5: Factor 0.9 per layer 0.59, Factor 1.1 per layer 1.61; 10: Factor 0.9 per layer 0.35, Factor 1.1 per layer 2.59; 20: Factor 0.9 per layer 0.12, Factor 1.1 per layer 6.73; 30: Factor 0.9 per layer 0.04, Factor 1.1 per layer 17.45; 40: Factor 0.9 per layer 0.01, Factor 1.1 per layer 45.26; 50: Factor 0.9 per layer 0.005, Factor 1.1 per layer 117.0255075100125gradient size (start = 1)051020304050layers passed
Computed as factor^layers. Real networks vary layer by layer; this isolates the effect.
A gradient after passing through many layers
layers passedFactor 0.9 per layerFactor 1.1 per layer
011
50.591.61
100.352.59
200.126.73
300.0417.45
400.0145.26
500.005117

Step 12 of 15Note

Step 13 of 15Flashcards

Backprop vocabulary

Tap a card to flip it.

Step 14 of 15Practice · True or false

Backpropagation is a learning algorithm that replaces gradient descent.

Step 15 of 15Practice · Choose one

What does backpropagation produce?