0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09The optimiser race (and the x100 twist)
Here's the cost bowl from our gradient-descent episode: long, narrow and canyon-shaped. Four optimisers race from the same starting point, each with a learning rate we picked from a small grid. Plain gradient descent finishes in a hundred and forty-seven steps. Momentum in a hundred and fifteen. RMSProp in a hundred and twenty-five. And Adam in eighty-five. Now multiply the whole cost by a hundred. Same shape, just bigger numbers. Gradient descent, with the same learning rate, explodes. Adam finishes in exactly eighty-five steps again, as if nothing happened.
Today we'll open up these optimisers and see the clever, surprisingly simple ideas inside, including the one that makes Adam the default choice for training most modern networks, from digit readers to language models.
1:08Where we are on the map
Season Two, Module Seven, stop five: optimisers.
1:14What's wrong with plain gradient descent?
Plain gradient descent has four weaknesses. In narrow canyons, it zig-zags across the steep walls and crawls along the flat floor. With mini-batches, its gradients are noisy. It uses one learning rate for every parameter, even though some parameters get huge gradients and others get tiny ones. And it's sensitive to scale: multiply the loss by a hundred and the safe learning rate shrinks by a hundred. Each optimiser we'll meet fixes some of these, using one simple idea: keep a running memory of past gradients.
1:52Momentum: the heavy ball
The first idea is momentum. Picture a heavy ball rolling down the canyon instead of a light one. Instead of stepping in the direction of the current gradient, we keep a velocity: a running total of recent gradients, where older ones count less and less. Each step, the velocity is ninety percent of the old velocity plus the new gradient, and we move along the velocity. Watch what that does. Across the canyon, the gradients keep flipping direction, left wall, right wall, so they cancel out in the velocity and the zig-zag dies down.
Along the canyon floor, the gradients keep pointing the same way, so they add up and the ball speeds up. On Brightcart's bowl, momentum reaches the bottom in a hundred and fifteen steps, against a hundred and forty-seven for the best plain gradient descent. The idea is old, from the nineteen-sixties, and still part of nearly every modern optimiser.
2:54RMSProp: a step size for every parameter
The second idea gives every parameter its own step size. It's called RMSProp. For each parameter, we keep a running average of its squared gradients, a measure of how big its gradients usually are. Then we divide each step by the square root of that average. Parameters whose gradients are usually large take smaller steps. Parameters whose gradients are usually small take larger ones. In the canyon, that's exactly right. The steep direction gets calmed down, and the flat direction gets a boost. The path becomes much more direct: a hundred and twenty-five steps.
RMSProp has an unusual history. It was introduced by Geoffrey Hinton in an online lecture course, not a paper, and became widely used anyway.
3:48Adam: both ideas together
Adam, short for adaptive moment estimation, combines both ideas. It keeps a momentum-style running average of the gradients, and an RMSProp-style running average of the squared gradients. Each step moves along the first, divided by the square root of the second. There's one subtle fix. Both averages start at zero, so for the first few steps they're too small, biased towards zero. Adam divides them by a correction factor that's large at the start and fades to one over time. Because of that correction, Adam's very first step has a size of about alpha in every parameter's direction.
That gives Adam's learning rate a very concrete meaning: roughly the largest distance any parameter moves in one step. On Brightcart's bowl, with a learning rate of zero point three, Adam reaches the bottom in eighty-five steps. With carefully tuned learning rates, momentum or RMSProp can be just as fast on a tiny two-parameter bowl like this one. Adam's real advantage is the next part: it barely needs re-tuning when the scale changes.
5:01Why Adam ignores gradient scale
Remember the twist from the start? Here's why it happened. If every gradient becomes a hundred times bigger, plain gradient descent takes steps a hundred times bigger, and flies out of the bowl. To recover, you'd need a learning rate a hundred times smaller. But in Adam, the numerator, the average gradient, grows by a hundred, and so does the denominator, the square root of the average squared gradient. The hundreds cancel. Adam takes exactly the same steps, and finishes in eighty-five again. This is why one learning rate often works reasonably well for very different layers and models.
In a deep network, some layers have gradients thousands of times bigger than others, and Adam adapts automatically. That robustness, more than raw speed, is why it became the default.
5:52AdamW: weight decay done right
One refinement matters a lot in practice. In episode seven, we saw L2 regularisation: a penalty on large weights. With plain gradient descent, that's the same as shrinking every weight a little at each step, which is why it's called weight decay. With Adam, that equivalence breaks. The penalty's gradient gets divided by Adam's adaptive denominator, so some weights are barely regularised at all. The fix, called AdamW, applies the weight decay separately, shrinking every weight by the same small fraction each step. It's a standard choice for training transformers and language models.
6:35Learning-rate schedules: warm-up and cosine
Even with Adam, the best learning rate changes during training. Early on, big steps help the model move quickly. Later, small steps help it settle precisely into a good spot instead of bouncing around it. So we use a schedule. Step decay cuts the learning rate by a factor of ten at set points. Cosine decay lowers it smoothly, like half a wave: from zero point one at the start, to zero point zero five halfway, to nearly zero at the end. And many large models also use warm-up: start with a tiny learning rate and ramp it up over the first few hundred or thousand steps, because at the very beginning the weights are random and the optimiser's averages haven't settled yet.
Then decay.
7:26Which optimiser should you use?
So which should you use? For most deep learning today, Adam or AdamW is the safe default, with a starting learning rate around one thousandth, tuned from there. Stochastic gradient descent with momentum is still popular too, especially for some image models, where with a carefully tuned schedule it can generalise as well or better. And a myth to avoid: Adam doesn't always find a better model. Its big advantage is that it works well with less tuning. Whichever you choose, the learning rate is still the most important setting to get right.
8:04What a running average remembers
Momentum and Adam rely on running averages, so let's see what they remember. With a factor of zero point nine, the newest gradient counts fully, the previous one ninety percent, then eighty-one, seventy-three, sixty-six percent, fading into the past. Those weights add up to at most ten, so the average effectively remembers roughly the last ten steps. Adam's second average, with zero point nine nine nine, has a memory of roughly a thousand steps: a long, smooth sense of how big each parameter's gradients usually are.
A single noisy spike barely disturbs either average, which is exactly why they stabilise training.
8:48Adam's first step, by hand
Let's check Adam's bias correction by hand, on its very first step. The momentum average becomes a tenth of the gradient, and the squared average becomes a thousandth of the squared gradient. Both are far too small, simply because they started at zero. The correction divides them by one minus zero point nine, and one minus zero point nine nine nine: the momentum estimate becomes exactly the gradient, and the squared estimate exactly the squared gradient. So the first step is alpha, times the gradient divided by its own size: alpha times plus or minus one.
Every parameter moves by exactly alpha, in the downhill direction. That's why Adam's learning rate means "how far to move", not "how steep the slope is".
9:37Picking a learning rate quickly
How do you find a good learning rate quickly? One popular trick is a range test: train for a short while, increasing the learning rate steadily from tiny to huge, and plot the loss against it. The loss falls, then flattens, then shoots up. A good starting point is a little below where the loss was falling fastest. Or simply try a few values spaced about three times apart, like one ten-thousandth, three ten-thousandths, one thousandth, three thousandths, and compare the validation curves. Re-tune whenever you change the model size, batch size or optimiser.
10:17Gradient clipping and optimisers
Optimisers are usually paired with gradient clipping. Every so often, an unusual batch produces an enormous gradient, which would cause a wild step. Clipping caps the gradient's overall size before the optimiser sees it. Adam's normalisation already dampens big gradients somewhat, but a sudden spike can still disturb its running averages, so clipping remains standard practice for large models. The full update pipeline: backpropagate, clip, take the optimiser step, then adjust the learning rate by the schedule.
10:53Optimiser memory costs
Smarter optimisers aren't free. Momentum stores one extra number for every parameter; Adam stores two. For our digit reader, that's trivial. For a seven-billion-parameter language model in thirty-two-bit precision, Adam's two extra numbers per parameter take fifty-six gigabytes, on top of the weights and gradients themselves. We'll do the full memory maths in Season Five, where this cost is a big reason for memory-saving techniques like LoRA.
11:27Optimisers and the loss landscape
A final, more subtle point. Optimisers don't just find a low point in the loss landscape; they influence which one. Researchers have long discussed the idea that wide, flat valleys tend to generalise better than sharp, narrow ones, and that the noise of small batches helps find them. It's an active research topic, not a settled law. The practical takeaway: the choice of optimiser matters less than tuning its learning rate and schedule well, regularising sensibly, and always judging the result on validation data.
12:03A family tree of optimisers
To finish, here's the family tree, so the names you'll meet make sense. Plain stochastic gradient descent gained momentum, the heavy ball, and a refinement called Nesterov momentum, which peeks ahead before stepping. Separately, AdaGrad gave each parameter its own step size using the sum of all its past squared gradients, but because that sum only grows, its steps could shrink to almost nothing. RMSProp fixed that by using a running average instead. Adam combined RMSProp with momentum and bias correction, and AdamW fixed how weight decay works with it.
New variants keep appearing, some saving memory, some adapting differently. But look closely, and almost all of them combine the two ideas from this episode: remember the direction, and normalise the size. A quick note: names, prices and rankings in AI change fast. Everything on screen was accurate when this video was made, but some of it may have changed by the time you're watching.
13:08Recap
Let's recap. Momentum averages recent gradients, cancelling zig-zags and speeding up along consistent directions. RMSProp divides by the typical size of each parameter's gradient, giving every parameter its own step size. Adam combines both, with a bias correction, and its steps barely care how the loss is scaled. AdamW handles weight decay properly. And learning-rate schedules, often with warm-up, move fast early and settle gently at the end. One sentence to remember: remember the direction, and normalise the size.
13:49Next: making deep networks train
Mission complete. Brightcart's models now train faster and far more reliably, with optimisers that remember where they've been and adapt each step to each parameter. But a stranger problem appears when we make networks deeper. Add more layers, and the network often trains worse. Gradients vanish, or explode. Next episode, the four inventions that made truly deep networks possible: careful initialisation, normalisation, dropout and residual connections. See you on the map.