S1425 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, y=w2tanh⁡(w1x), with input x = 1, target t and squared error E=(y−t)2/2. The error as a function of w1, and the tangent line whose slope backpropagation computes.
  • Move w1 and watch the slope change. Where the tanh saturates (large |w1|), 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?

Name it

Forward, then backward

A forward pass computes each unit's activation aj=∑iwjizi and output zj=h(aj). Define each unit's error signal δj=∂E/∂aj. Then every weight's gradient is local:

∂E∂wji=δjzi

The backward recursion

At an output unit with its matching loss (linear with squared error, sigmoid or softmax with cross-entropy), δk=yk−tk. Hidden units collect the errors of the units they feed:

δj=h′(aj)∑kwkjδk

For tanh, h′=1−z2; 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: 2W for W weights.

Gradient checking

Compare backpropagation against the central difference [E(w+ϵ)−E(w−ϵ)]/2ϵ, whose error falls as ϵ2 (the one-sided difference's falls only as ε). Agreement to about 10−6 relative is the standard test that a backward pass is right.

Vanishing and exploding gradients

Through L layers, δ is multiplied by wh′ at every step. Products below 1 vanish exponentially and above 1 explode, which is why initialization keeps wh′ near 1.

Worked example

For y=w2tanh⁡(w1x) with x = 1, w1 = 0.5, w2 = 2 and t = 0.5, backpropagate the squared error.

Show the solution
  1. Forward pass. z=tanh⁡(0.5)=0.4621y=0.9242E=0.42422/2=0.0900
  2. Output delta. δout=y−t=0.4242∂E∂w2=0.4242×0.4621=0.1960
  3. Hidden delta. Here 1−z2 = 0.7864, and δhidden=(1−z2)w2δout=0.6673
  4. First-layer gradient. ∂E∂w1=0.6673×x=0.6673

Answer E = 0.0900, ∂E/∂w2 = 0.1960 and ∂E/∂w1 = 0.6673.

Worked example

Check that gradient with ε = 0.001.

Show the solution
  1. Central difference. E(w+ϵ)−E(w−ϵ)2ϵ=0.667275 off by 1.2×10−6.
  2. One-sided difference. E(w+ϵ)−E(w)ϵ=0.66820 off by 9.3×10−4.
  3. Halving ε. It would cut the first error by 4 and the second by 2.

Answer the central difference gives 0.667275, within 1.2×10−6 of the backpropagated gradient, and the one-sided difference 0.66820, off by 9.3×10−4.

Worked example

A linear layer computes 𝐲=W𝐱 with W=[120−1] 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
  1. Gradient with respect to W. Each weight Wij multiplies xj into yi, so ∂L/∂Wij=gixj, the outer product: 𝐠𝐱T=[0.51.5−1−3]
  2. Gradient with respect to x. It sums over the outputs each input feeds: WT𝐠=[1×0.5+0×(−1)2×0.5+(−1)(−1)]=(0.5,2)
  3. 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 [0.51.5−1−3], and with respect to x it's (0.5, 2).

Check

For y=w2tanh⁡(w1x) with w1 = 1.2, w2 = 1.5, input 2, target 1 and squared error, what's ∂E/∂w2?

Check

For the same network (w1 = 1.2, w2 = 1.5, x = 2, t = 1), what's ∂E/∂w1?

Check

A network computes y=vReLU(wx+b) with w = 2, b = −0.25, v = −1. For input 1, target 1 and squared error, what's ∂E/∂w?

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 𝐲=W𝐱 has W=[abcd] with a = 1, b = 0.5, c = 2, d = 4. The input is x = (1, −1) and the upstream gradient ∂L/∂𝐲 = (0.5, 2).

(a)

What is y1?

(b)

What is ∂L/∂b (the weight from x2 to y1)?

(c)

What is ∂L/∂x2?

(d)

What is ∂L/∂x1?

Check

A single logistic unit y=σ(wx) 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°.

Explain why

Why does backpropagation compute all the gradients for the price of a few forward passes?

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.