Lesson 1.3 · 9 min read · about 1 h 15 min to master at a very slow pace
Gradient Descent and Momentum
Training means minimising an objective. Gradient descent repeatedly steps against the gradient. A worked example, what the learning rate does, why a zero gradient is not proof of a minimum, and how momentum helps.
Training is optimisation
Learning means choosing parameters that make an objective (a cost or loss) as small as possible:
Here is the vector of parameters and the objective. At an interior minimum of a differentiable function, the gradient is zero: . That condition is necessary but not sufficient: the gradient is also zero at a maximum, at a saddle point and on a flat plateau.
The gradient descent update
The gradient points in the direction of steepest local increase, so step the other way:
(eta) is the learning rate, a hyperparameter you choose.
Worked example
Minimise , whose minimum is at . The derivative is . Start at with :
Each step multiplies the distance to the minimum by : .
What the learning rate does
- Too small: progress is very slow.
- Too large: updates overshoot, oscillate or diverge.
- Narrow valleys (steep one way, shallow the other) make plain gradient descent zig-zag.
- Local minima, saddle points and plateaus can stall it.
Momentum
Momentum adds a fraction of the previous step:
Steps in a consistent direction build up speed, while zig-zag components cancel out. This helps on badly conditioned objectives. RMSprop and Adam go further by adapting the step size for each parameter; Adam is a common default in practice.
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
First passWhy do we subtract the gradient instead of adding it?
The gradient points uphill: it's the direction in which increases fastest. We want to decrease, so we move the opposite way. In the example, at the derivative is (the function rises to the right), so we move left, towards 3.
Going deeperIf the gradient is zero, have we found the best parameters?
Not necessarily. A zero gradient only says the surface is locally flat. It could be a minimum, a maximum, a saddle point (up in one direction, down in another) or a plateau. In high-dimensional neural network losses, saddle points and plateaus are common, which is one reason momentum and Adam help.
First passWhat exactly is an objective function?
It is the single number that training tries to make as small as possible, written as a function of the parameters. The data is held fixed; only the parameters change. In deep learning it is usually the average loss over the training examples, sometimes plus a penalty on the weights.
In the worked example, is the objective and is the parameter. At its value is ; at it is , the best possible.
First passWhat is a gradient?
With one parameter, it is the derivative: the slope of the objective at the current point, meaning how much changes for a small change in . At , , so nudging up by raises by about .
With many parameters, the gradient is the vector of partial derivatives, one per parameter. Together they point in the direction of steepest increase, and the length of the vector says how steep the surface is there. A network with a million weights has a gradient with a million entries.
First passWhat is the difference between the objective's value and its gradient?
The value says how bad the parameters are right now: the height of the surface. The gradient says which way is downhill and how steep it is: the slope. For :
- : value , gradient .
- : value , gradient .
- : value , gradient .
and have the same value, 4, but opposite gradients, because they sit on opposite sides of the minimum. The update uses only the gradient; the value is what you watch to see whether training is working.
First passWhat do a positive and a negative gradient tell me?
The sign says which way the objective rises. Positive: goes up as goes up, so decrease . Negative: goes down as goes up, so increase . Subtracting does the right thing automatically.
At : , so , a step to the right, towards 3. The size of the gradient sets the size of the step, which is why steps shrink as you approach the minimum.
Going deeperWhat is the difference between a minimum, a maximum, a saddle point and a plateau?
At all four the gradient is zero, or nearly zero, so the gradient alone can't tell them apart. The shape around the point can:
- Minimum: every direction goes up. at .
- Maximum: every direction goes down. at . Gradient descent moves away from it at the slightest push.
- Saddle point: up in some directions, down in others, like a horse's saddle. at . Common in high-dimensional network losses.
- Plateau: a wide, almost flat region. The gradient is tiny everywhere, so steps are tiny and training looks stuck, for example when sigmoid units saturate.
The curvature (second derivative) separates them: positive in every direction means a minimum, negative in every direction a maximum, mixed signs a saddle.
Going deeperWhy does make the example oscillate?
Each update multiplies the distance to the minimum by : . With the factor is : the distance stays the same and only the side flips.
From : , then , and so on. It bounces between 5 and 1 forever and never converges.
- : approaches 3 steadily from one side.
- : lands on 3 in one step.
- : overshoots each time but closes in (the curve in the chart).
- : overshoots further each time and diverges.
The 2 is the curvature, . In general a step size above diverges.
Going deeperHow do GD, SGD, momentum, RMSprop and Adam differ?
All of them use the same gradient from backpropagation. They differ only in how they turn it into a step.
- Gradient descent (batch): the gradient over the whole training set each step. Exact, but every step is expensive.
- SGD (stochastic or mini-batch): the gradient on one example or a small batch. A noisy estimate, but many cheap steps, and the noise can help escape saddle points.
- Momentum: adds a fraction of the previous step. Speeds up along a consistent direction and damps zig-zag.
- RMSprop: divides each parameter's step by a running average of its recent squared gradients. Parameters with large gradients take smaller steps; flat directions take larger ones.
- Adam: momentum and RMSprop together, with a correction for the first few steps. The usual default, often with .
First passWhat exactly is a point on the objective's graph?
One choice of parameters and how good it is. The horizontal position is the parameter value, ; the height is the objective at that value, . The point on means "with , the objective is 4".
Training starts at one point and moves along the curve, one update at a time, towards the lowest point. With two parameters the graph is a surface and a point is a spot on it; with a million parameters you can't draw it, but the idea is the same.
First passHow does momentum actually help?
Plain gradient descent only looks at the slope where it stands now. In a long narrow valley the slope points mostly across the valley, so it zig-zags from wall to wall and creeps along the floor.
Momentum keeps a running memory of past steps. The side-to-side parts point in opposite directions on alternate steps and cancel out; the part along the valley points the same way every time and builds up. The result: fewer wasted steps, faster progress along gentle slopes, and enough speed to roll through small bumps and flat stretches.
First passHow do I know when gradient descent has converged, and when to stop?
Plot the objective against the iteration number: the convergence curve. A healthy curve falls steeply at first, then flattens. It has converged when it stops falling: the change per step, or the size of the gradient, drops below a small tolerance such as . A curve that zig-zags or climbs means the learning rate is too large.
For neural networks, don't wait for the training loss to flatten. Watch the validation loss and stop when it stops improving: that is early stopping (lesson 9.2). In practice you also cap the number of epochs.
Cheatsheet
Gradient descent
Learning rate
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.