Neural Networks, lesson 9
Momentum, Adam and learning-rate schedules
How real training runs set the size of every weight's step.
Plain gradient descent, from "Updating weights with gradients", gives every weight the same learning rate. Real loss surfaces are steep in some directions and nearly flat in others, and one rate can't suit both. This lesson builds momentum, Adam and a changing rate on a small valley you can follow by hand.
By the end, you can:
Part 1. A narrow valley
Predict when plain gradient descent zigzags, and when it crawls.
Take two weights, x and y, and the loss 0.5·(x² + 100y²), with its floor at (0, 0). The slope is x in the x direction and 100y in the y direction. So the loss is a long, narrow trench: gentle along x, and 100 times as steep across y.
Example 1.1
Three plain steps
Start at x = 10, y = 1. Take three steps of plain gradient descent with a learning rate of 0.018: each weight minus 0.018 times its slope.
- Step 1x: 10 − 0.018 × 10 = 9.82; y: 1 − 0.018 × 100 = −0.8
y jumps right over the floor to the far wall. x moves only 0.18.
- Step 2x: 9.82 − 0.018 × 9.82 = 9.643; y: −0.8 − 0.018 × (−80) = 0.64
y's slope has flipped sign, so it jumps back.
- Step 3x: 9.470; y: −0.512
Each step keeps 98.2% of x and turns y into −0.8 times itself.
AnswerAfter three steps, x has moved from 10 to 9.47 and y has crossed the floor three times.
Each step multiplies x by 1 − 0.018 = 0.982, and y by 1 − 0.018 × 100 = −0.8. The minus sign is the zigzag. The 0.982 is the crawl: x needs 381 steps to get within 0.01 of the floor.
So raise the rate? At 0.02, y's multiplier is −1 and y bounces between the walls forever; above that, each bounce grows. The steep direction sets the speed limit, and the gentle one pays for it.
Condition number
Measure a direction's steepness by how fast its slope grows: 1 per unit of x, 100 per unit of y. The condition number is the steepest over the gentlest, 100 here. Plain steps need a rate below 2 ÷ the steepest, so the gentlest shrinks by at most 2% a step.
each step: direction × (1 − rate × steepness)
A smaller rate cures the zigzag but not the crawl: at 0.01, y lands on the floor in one step, but x needs 688. A network's loss has a direction for every weight, and some are far steeper than others.
Your turn
A different valley: 0.5·(x² + 25y²). What learning rate must plain gradient descent stay below, and how much of x is left after each step near that limit?
Part 2. Momentum, a running velocity
Run momentum's velocity by hand, and say why β = 0.9 means about 10 steps.
Keep going with Pro
You've done the first part. Pro opens the rest of this lesson, and every other one.
- Momentum, a running velocity
- A step size for each weight
- Warmup and cosine decay
Neural Networks overviewSpotted a mistake? Tell us.