S14 · Lesson 25 · Backpropagation
The chain rule organized for networks, errors sent backward from the output, gradients for every weight in one backward pass, checking them with finite differences, the cost compared with numerical gradients, and why gradients vanish through many saturating layers
Training a network needs the gradient of the error with respect to every weight, and a large network has billions. Computing each derivative separately by nudging a weight would take two forward passes per weight. Backpropagation gets all of them for roughly the cost of two or three forward passes, by running the chain rule backward through the network and reusing every intermediate result. It's the same reverse-mode differentiation that circuit simulators use for sensitivity analysis, and every deep-learning framework is built around it.
- A tiny network, , with input x = 1, target t and squared error . The error as a function of , and the tangent line whose slope backpropagation computes.
- Move and watch the slope change. Where the tanh saturates (large ), the error curve flattens and the gradient fades.
- The readout is the backpropagated derivative; the tangent should touch the curve exactly.
Predict
A network has 10 sigmoid layers in a chain, each with weight 1. By at most what factor does the gradient shrink from the output back to the first layer?
Each layer multiplies the backward signal by its weight times the sigmoid's derivative, which is at most 0.25. Ten layers give at most . That's the vanishing gradient problem, and it's why deep networks use ReLU units (derivative 1 where active), careful initialization and residual connections.
Name it
Forward, then backward
A forward pass computes each unit's activation and output . Define each unit's error signal . Then every weight's gradient is local:
The backward recursion
At an output unit with its matching loss (linear with squared error, sigmoid or softmax with cross-entropy), . Hidden units collect the errors of the units they feed:
For tanh, ; for ReLU, h′ is 1 where a > 0 and 0 otherwise.
Cost
One backward pass costs about as much as a forward pass (each weight is used once more for δ and once for its gradient), so the full gradient costs a small multiple of a forward pass. Finite differences need two forward passes per weight: for W weights.
Gradient checking
Compare backpropagation against the central difference , whose error falls as (the one-sided difference's falls only as ε). Agreement to about relative is the standard test that a backward pass is right.
Vanishing and exploding gradients
Through L layers, δ is multiplied by at every step. Products below 1 vanish exponentially and above 1 explode, which is why initialization keeps near 1.
Worked example
For with x = 1, = 0.5, = 2 and t = 0.5, backpropagate the squared error.
Show the solution
- Forward pass.
- Output delta.
- Hidden delta. Here = 0.7864, and
- First-layer gradient.
Answer E = 0.0900, = 0.1960 and = 0.6673.
Worked example
Check that gradient with ε = 0.001.
Show the solution
- Central difference. off by .
- One-sided difference. off by .
- Halving ε. It would cut the first error by 4 and the second by 2.
Answer the central difference gives 0.667275, within of the backpropagated gradient, and the one-sided difference 0.66820, off by .
Worked example
A linear layer computes with and x = (1, 3). The loss's gradient with respect to y is g = (0.5, −1). Find the gradients with respect to W and x.
Show the solution
- Gradient with respect to W. Each weight multiplies into , so , the outer product:
- Gradient with respect to x. It sums over the outputs each input feeds:
- Cost. Every layer's backward pass is these two products, which is why it costs about twice the forward pass.
Answer the gradient with respect to W is , and with respect to x it's (0.5, 2).
Check
For with = 1.2, = 1.5, input 2, target 1 and squared error, what's ?
Check
For the same network ( = 1.2, = 1.5, x = 2, t = 1), what's ?
Check
A network computes with w = 2, b = −0.25, v = −1. For input 1, target 1 and squared error, what's ?
Check
Through each of 20 layers, the backward signal is multiplied by a weight 1 and an activation slope 0.1. By what overall factor is it scaled?
Check
A central-difference gradient check with step 0.001 is off by 1 × 10⁻⁶. What error do you expect with a step 10 times smaller (rounding aside)?
Check
A network has 1000000 weights. Backpropagation computes the gradient for about 2 forward passes' cost. How many times more work does a central-difference gradient take?
Check
A linear layer has with a = 1, b = 0.5, c = 2, d = 4. The input is x = (1, −1) and the upstream gradient = (0.5, 2).
(a)
What is ?
(b)
What is (the weight from to )?
(c)
What is ?
(d)
What is ?
Check
A single logistic unit is trained with cross-entropy against target t.
Step 1
What is its output?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the loss's gradient with respect to w?
Explain why
Why does backpropagation compute all the gradients for the price of a few forward passes?
Every weight's gradient is the product of the error signal δ at the unit it feeds and the activation coming into it. The δ's for a whole layer can be computed from the δ's of the layer above in one sweep, because each unit's error is just its activation slope times the weighted sum of the errors it caused downstream. So one backward pass through the network, using the activations saved from the forward pass, produces every δ, and every gradient follows by one multiplication. Finite differences instead treat the network as a black box, rerunning it for each weight, so they cost two forward passes per weight; backpropagation shares the work across all weights.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.