Backpropagate through two layers
Real networks have more than one layer, so the error has to travel back through each of them. The weight in the first layer does not touch the output directly. It changes a hidden value, and the hidden value changes the output. So its gradient is the output's error, passed back through everything in between. One of those things is a ReLU, which can block the path completely.
Learn it
Words to know
- hidden layer
- A layer of neurons between the input and the output. Its values are not seen from outside.
- chain rule
- If A changes B and B changes C, the effect of A on C is the two effects multiplied together.
- ReLU
- The function
max(0, x). It keeps positive numbers and turns everything else into 0. - preactivation
- The value that goes into an activation function: the neuron's weighted sum plus bias, before the bend.
How it works
- Forward:
z = w1 * x, the preactivation; thenh = max(0, z), the hidden value; thenoutput = w2 * h. The loss is(output - target)^2 / 2. - The error is
output - target. - Second weight: the output changes by
hfor each unit ofw2. Gradient:error * h. - First weight: go back through
w2, through the ReLU, and throughx. Gradient:error * w2 * xwhenzis above 0. - When
zis 0 or below, the ReLU outputs a flat 0. Nothing gets through, and the gradient forw1is 0.
Worked example
Input x = 2 w1 = 1 w2 = 3 target = 4 Output [12, 4]
| What | Calculation | Value |
|---|---|---|
| z | w1 × x = 1 × 2 | 2 |
| h | max(0, 2) | 2 |
| Output | w2 × h = 3 × 2 | 6 |
| Error | 6 - 4 | 2 |
| Gradient for w2 | error × h = 2 × 2 | 4 |
| Gradient for w1 | error × w2 × x = 2 × 3 × 2 | 12 |
The output 6 is above the target 4. With a positive input and positive weights, both gradients are positive, so both weights should go down.
Why it matters
The same rule carries the error back through every layer of a deep network. Units whose ReLU blocks the path come back in chapter 4 as dead units.
Your turn
Work out the gradients for both weights of a two-layer model: h = ReLU(w1 * x), output = w2 * h, loss (output - target)^2 / 2.
x- the input, one number
w1- the first-layer weight
w2- the second-layer weight
target- the output we want
- Returns
- The list
[dL/dw1, dL/dw2]. Whenw1 * xis 0 or below, treat the slope of the ReLU as 0.
Example 1
Input x = 2 w1 = 1 w2 = 3 target = 4 Output [12, 4]
The error is 2. The gradient for w2 is 2 × 2 = 4, and for w1 it is 2 × 3 × 2 = 12.
Example 2
Input x = 2 w1 = -1 w2 = 3 target = 4 Output [0, 0]
w1 * x is -2, so the ReLU outputs 0. Both gradients are 0: the hidden value is 0, and the path back to w1 is blocked.
Example 3
Input x = 0 w1 = 1 w2 = 3 target = 4 Output [0, 0]
The input is 0, so z is 0 and the hidden value is 0. Again both gradients are 0.
Example 4
Input x = -2 w1 = -1 w2 = 3 target = 4 Output [-12, 4]
w1 * x is (-1) × (-2) = 2, above 0, so the path is open although w1 is negative. The gradient for w1 is 2 × 3 × (-2) = -12.
Hints
Each hint gives away a little more. Open one only when you need it.
Hint 1: Where to start
Run the model forward and keep every value on the way:
z,hand the output. The backward pass reads them.Hint 2: The key step
The gradient for
w2usesh. The gradient forw1usesw2andx, and it is 0 unlesszis above 0.Hint 3: The outline
Compute
z = w1 * x,h = max(0, z)anderror = w2 * h - target. The answer is[error * w2 * x if z > 0 else 0, error * h].
Solution
From here on the page gives the method away. If you are still solving, go back to the hints.
Step by step
- Forward, keeping the value from before the ReLU.
zis the value that went into the ReLU. Above 0 the ReLU lets a gradient through; at 0 or below it blocks it. - The second weight is multiplied by h. The output is
w2 * h, so each unit ofw2moves the output byh. - The first weight is reached back through w2 and the ReLU. This is the chain rule: each link multiplies the error.
w2for the second layer, 1 or 0 for the ReLU, andxfor the first layer.
The same numbered steps are the comments in the solution.
Python
def solve(x, w1, w2, target):
# 1. Forward, keeping the value from before the ReLU
z = w1 * x
h = max(0, z)
error = w2 * h - target
# 2. The second weight is multiplied by h
dw2 = error * h
# 3. The first weight is reached back through w2 and the ReLU
dw1 = error * w2 * x if z > 0 else 0
return [dw1, dw2]Watch out
- Decide the ReLU's slope from
z, the value that went into it. The sign ofxor ofw1alone does not tell you. - Do not forget
w2in the first gradient. The first weight can only change the loss by way of the second layer.
Check yourself
Why can the gradient for the first weight be exactly 0?
When the value going into the ReLU is 0 or below, its output is flat. A small change to the weight changes nothing after it.
What does the first weight's gradient include that the second weight's does not?
The second weight w2, the slope of the ReLU, and the input x: every link between the first weight and the output.