Lesson 1 · Unit 01
SGD and momentum
Noisy gradients, and a velocity that remembers them
What you practise
- Mini-batch gradients are noisy estimates, batch size against noise, momentum as a running average computed by hand, what the learning rate does to each
Try a question from this lesson
Pick one
Each number is one example's gradient for the same weight. The true gradient is the mean of all four. What do two mini-batches of 2 report instead?
g = torch.tensor([4., -2., 6., 0.])
full = g.mean().item()
a = g[:2].mean().item()
b = g[2:].mean().item()
print(full, a, b)Hint
Average all four numbers, then average each half on its own.
Correct answer
2.0 1.0 3.0
Why
The true gradient is 2.0, but one batch says 1.0 and the other 3.0. A mini-batch gradient is an estimate: right on average, off every single time. Training runs on these estimates because the exact gradient costs a pass over all the data.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Noise shrinks with the batch
A mini-batch gradient is the true gradient plus noise. Averaging more examples cancels more of the noise, but slowly: the noise falls with the square root of the batch size.
So a batch four times as large gives a gradient half as noisy, at four times the compute per step. Every choice of batch size is that trade.
Concept
Momentum
Plain SGD forgets each gradient as soon as it has used it. Momentum keeps a velocity: on every step it keeps 0.9 of the old velocity, adds the new gradient, and moves the weight along the result.
Gradients that keep pointing the same way pile up. Gradients that flip sign cancel out.
v = 0.9 * v + g # velocity
w = w - lr * v # step10 more exercises in the app
- 3× type the answer
- 3× multiple choice
- 2× true or false
- 1× fill the blanks
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.