In one sentence
To minimise a function, take small steps in the direction where it decreases fastest.
The idea
Imagine standing on a hill in fog. You can’t see the valley, but you can feel which way the ground slopes. Step downhill, check again, repeat. That is gradient descent.
The update rule
For parameters , a loss function and a learning rate :
- (the gradient) points uphill, so we subtract it.
- (the learning rate) sets the step size.
Choosing the learning rate
Too small: progress is painfully slow. Too large: you overshoot the valley and bounce around, or diverge.
Example
Minimise . The gradient is . Starting at with :
| Step | ||
|---|---|---|
| 0 | 4.00 | 16.00 |
| 1 | 3.20 | 10.24 |
| 2 | 2.56 | 6.55 |
| 3 | 2.05 | 4.19 |
Each step shrinks by 20%, so it converges to the minimum at .
x, lr = 4.0, 0.1
for step in range(50):
x -= lr * 2 * x # gradient of x^2 is 2x
print(x) # ~0.00006Where it’s used
Every neural network is trained with a variant of this, such as SGD, momentum or Adam. A practical use is in the blog: Why I’m writing in public.