01Why it matters
Give gradient descent a memory
Plain gradient descent has no memory: each step measures the slope underfoot, moves against it and forgets it. In the gradient descent lesson that made it zigzag across a narrow valley while it crept along the floor. A network's loss has millions of directions, some far steeper than others, so this is the normal case.
A ball rolling down the valley does better because it keeps its speed: pushes along the floor add up, and pushes from side to side cancel. That is momentum. Shrinking the steps of directions that have been steep lately is RMSProp. Doing both is Adam, the usual default for training neural networks.
02Key parts
The pieces you will see
- velocity v
- Momentum's memory: past gradients added up, each shrunk by β for every step since. Momentum steps against v, as plain descent steps against g.
- decay β
- How much of the memory survives each step. At β = 0.9 it spans about 1/(1 − β) = 10 recent gradients.
- running average
- An average that forgets slowly: m ← βm + (1 − β)g. RMSProp keeps one of g², Adam one of g and one of g².
- bias correction
- Adam's averages start at 0, so they are too small at first. Dividing by 1 − βᵗ at step t scales them back up.
03See it work
Four ways to turn a gradient into a step
The race below runs four optimisers from one start for 60 steps. Each step, each one measures the gradient where it stands (derivatives and gradients shows how) and turns it into a step. Plain descent steps against it, . Momentum shrinks its velocity by , adds , and steps against the velocity:
RMSProp keeps a running average of , coordinate by coordinate (weight by weight, in a network), and divides each coordinate's step by :
Adam keeps a running average of as well as RMSProp's , corrects both for starting at 0, and steps by their ratio:
with and at step ; the tiny only prevents a division by 0. The race uses and , and its own per optimiser and surface, so read it as a picture of behaviour, not a benchmark. On the narrow valley the gradient, , changes 100 times faster across the valley than along it.
Step once: plain descent () jumps 2.7 across the valley and 0.063 along it, while Adam moves exactly its , 0.1, in each coordinate: at the corrections give and , so the step is .
Momentum's velocity is a sum, not an average: on a steady slope it grows towards , ten times at , so momentum gets a smaller (0.03 here). Along the floor the gradient keeps its direction, so momentum builds up speed there: after 60 steps it is at , plain descent at . A gradient that flips sign every step adds and in turn, so the velocity settles at about . The price is inertia: momentum overshoots the floor (its rises at step 3), then swings across, a little less each time.
RMSProp divides by how big each coordinate's gradients have been lately, so while a gradient holds steady, is about and the step about , steep or gentle. Adam's is momentum written as an average, about the size of , so dividing by again makes each step about : Adam walks down the steep wall in steps of about 0.1 instead of leaping across, though its momentum still carries it past the floor in slow swings.
04Predict
Your turn: who leads the race?
On the two-valleys surface, , the left valley is lower than the right, with a ridge near between them. All four start at (1.8, 1.2), on the steep outer slope of the right-hand valley. Predict who will be lowest after step 1 and after every tenth step: think about each first step, and who could gather enough speed to cross the ridge.
Make 6 predictions: step forward and answer each question before the next step plays.
0 of 6 made05Try it
Momentum in code, and what memory costs
Here is momentum on , whose gradient is . With the velocity is just the latest gradient, so one function runs both: momentum with is plain descent. With , and a start at , every stays a whole number.
def descend(label, beta, steps):
x = 8
v = 0
for t in range(steps):
g = 2 * x
v = beta * v + g
x = x - 0.25 * v
print(label + ' ' + str(int(x)))
descend('plain', 0, 3)
descend('momentum', 0.5, 3)
On a round bowl plain steps already point straight at the bottom, so momentum has no zigzag to cancel, and its speed only carries it past.
Real gradients come from small random batches, so they are noisy, and the running averages smooth that too. SGD with momentum is still widely used beside Adam, and sometimes generalises a little better. One setting never goes away.
06Check yourself
Check yourself
Question 1 of 6
Momentum’s β is raised from 0.9 to 0.99. On a long, steady slope, up to about how many times plain descent’s step (at the same η) can its steps grow?
Your answer comes back in Review in a few days, so it sticks.
Question 2 of 6
Plain descent with η = 0.1 zigzags across a narrow valley but still reaches the bottom. Which run is more likely to fly off?
Question 3 of 6
RMSProp has run for a while with η = 0.01. Weight a’s recent gradients have all been about 40, weight b’s about 0.04. Roughly how far does each move next?
Question 4 of 6
Suppose Adam skipped the bias correction. With β₁ = 0.9 and β₂ = 0.999, how long would its first step be in each coordinate?
Question 5 of 6
A model’s loss has two basins. Plain descent ends in the higher one; Adam, started from the same weights, ends in the lower one. What is the best explanation?
Question 6 of 6
A team switches from plain descent at η = 0.5 to Adam and keeps η = 0.5, since Adam “adapts the learning rate anyway”. What happens on the first step?
07Explain it back
Say it in your own words
How do momentum, RMSProp and Adam use past gradients to take better steps than plain gradient descent, and what can none of them do?
08Recap
What to remember
- Plain gradient descent uses only the current gradient; momentum, RMSProp and Adam add a memory of past ones.
- Momentum steps against : steady gradients build it up to about times the gradient (10 at ), flipping ones cancel, and its speed makes it overshoot.
- RMSProp divides each coordinate's step by the root of its running average of , so steep and gentle directions both step about .
- Adam is momentum as an average, plus RMSProp, plus bias correction.
- None of them sees the surface: they can settle in a local minimum, and still has to be chosen.
- Next: neural networks and the neural network playground, which trains with momentum. Backpropagation shows where the gradients come from.