0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Watch an AI learn, then explode
Here's Brightcart's sales model, sitting high up on the wall of its cost bowl. Watch it learn. Step, step, step, and in a few hundred moves it settles at the very bottom, at the best possible line. Now watch the exact same algorithm with one number changed. It overshoots, overshoots again, and flies out of the bowl. The cost explodes past seventeen thousand. Same data, same model, same maths, and one tiny setting decides between learning and chaos. By the end of this video, you'll know exactly what that number is, how to choose it, and why this simple algorithm, gradient descent, trains almost every model in modern AI, including the largest neural networks.
0:58One-minute recap: cost and the gradient
Welcome to Module Two: how models learn. A one-minute recap first. If you've seen our briefing on cost functions, this will be familiar. A model makes predictions. For each example, the error is the true value minus the prediction. A cost function turns all those errors into one number. For regression, the most common is mean squared error: square each error, then average. Plotted against the model's parameters, that cost forms a bowl. The best model sits at the bottom. And in episode three, we found our compass: the gradient points uphill, so the negative gradient points down.
Today we'll walk.
1:40The update rule
Gradient descent fits in one line. The new parameters equal the old parameters, minus alpha times the gradient. Let's read it piece by piece. Theta stands for all the parameters, the dials. The gradient tells us which way is uphill. The minus sign turns us around to face downhill. And alpha, the Greek letter in gold, is the learning rate. It controls how big a step we take. For Brightcart's model, that's two updates every step: w becomes w minus alpha times the slope in w, and b becomes b minus alpha times the slope in b.
Then we recompute the gradient at our new position, and repeat. That's exactly the foggy hiker. Feel the slope under your feet. Take a step downhill. Feel the slope again. Step again. You never see the whole valley. You just keep making the best local move, and you end up at the bottom.
2:45By hand: the first two steps
Let's run it by hand on Brightcart's data. Five days: ad spend on one axis, sales on the other. We start with both dials at zero, so the line lies flat along the bottom and every prediction is zero. The cost is forty point four. From episode three, the gradient here is minus forty-one point six for w, and minus twelve for b. Let's choose a learning rate of zero point zero five. New w: zero minus zero point zero five times minus forty-one point six. Two minus signs make a plus, so w becomes two point zero eight. New b: zero minus zero point zero five times minus twelve, which is zero point six.
The line jumps up, and the cost drops from forty point four to about two point one. One step, and the error has shrunk by a factor of nineteen. It's slightly too steep now, overshooting the points on the right, so the next gradient tilts it back. Step two: the cost falls to zero point eight two.
3:53Watch it learn
Now let's let it run, and watch it learn for real. On the left, the line. On the right, the map of the cost bowl seen from above, with every step drawn as a dot. The first few steps are huge, because the slope is steep. By step ten, the cost is zero point seven. Then progress slows down. By step one hundred, the cost is zero point four nine. By step two hundred, zero point four eight, and the line barely moves any more. It's found the bottom: w equals one point four, b equals one point eight. Every hundred dollars of ad spend brings about one thousand four hundred dollars of sales.
In Module Three we'll solve for this line exactly, with algebra, and get the very same answer. Gradient descent found it just by walking downhill. Notice the shape of the cost curve: a cliff, then a long slow slope, then flat. You'll see this shape in almost every training run you ever do. So how do we know when to stop? Usually when the cost stops improving by more than a tiny amount, when the gradient is almost zero, or when we hit a maximum number of steps.
5:08The learning rate
Now, that mysterious number from the start: the learning rate, alpha. Make it too small, and every step is tiny. With alpha at zero point zero zero five, Brightcart's model still gets there, but it takes nearly two thousand four hundred steps. Safe, but slow. At zero point zero five, it takes about two hundred and forty steps. Much better. Now make it too large. At zero point one two, the first step overshoots the bottom and lands on the opposite wall, higher than where it started. The next step overshoots even more.
The cost goes from forty to a hundred and thirty-five, to four hundred and fifty, to over seventeen thousand. That's called divergence: the algorithm runs away instead of learning. Here's a puzzle. This bowl has a tipping point somewhere around zero point zero eight five. Pause and predict: does a learning rate of zero point zero eight converge, and what about zero point one?
Zero point zero eight converges, and fast: just a hundred and forty-seven steps, because it's bold but still under the limit. Zero point one is over the edge, and it diverges. So the fastest learning rates live right next to the cliff. That's why choosing alpha is one of the most important practical decisions in machine learning. In practice, people try a few values that differ by factors of ten, watch the cost curve, and often shrink the learning rate as training goes on. We'll see smarter, adaptive methods like Adam in Season Two.
6:50Why the path zig-zags
Look closely at the path on the map. It doesn't head straight for the bottom. It races sideways first, then crawls along a long, narrow channel. That's because Brightcart's cost bowl isn't round. It's stretched into a canyon: about seventy times more sharply curved in one direction than the other. The gradient points mostly down the steep walls, so w gets close within ten steps, then w and b creep along the flat canyon floor together for hundreds more. And the steep direction is also what limits the learning rate. Go faster than that wall allows and you bounce out, which is exactly the divergence we just saw.
There's a simple fix, which we'll learn in episode three of this module. If we rescale the features so they're on similar scales, the canyon becomes a round bowl, and gradient descent walks almost straight to the bottom.
7:47Batch, stochastic and mini-batch
So far, every step used all five days of data to compute the gradient. That's called batch gradient descent. With five rows, no problem. With ten thousand rows, like one day of Brightcart orders, every step means a pass over all ten thousand, and with millions of rows that becomes very slow. The opposite extreme is stochastic gradient descent, or S-G-D. Each step uses just one random example. Steps are incredibly cheap, but noisy: one example gives only a rough guess of the true slope, so the path jitters. On Brightcart's data, after three passes, the cost bounces around at zero point eight, zero point nine, then one point three, wandering near the bottom without settling.
The noise can even help sometimes, shaking the model out of shallow dips. The everyday compromise is mini-batch gradient descent: each step uses a small batch, say thirty-two or two hundred and fifty-six examples. It's much less noisy than one example, far cheaper than all of them, and it fits perfectly onto a GPU that can process the whole batch in parallel. This is how nearly every neural network is trained today. Two terms you'll hear constantly: one epoch is one full pass through the training data, and the batch size is how many examples go into each step.
9:15Convex and non-convex landscapes
Brightcart's sales model has a perfect bowl. Mathematicians call that convex: any low point is the lowest point. This bowl is strictly convex, so it has exactly one bottom, and with a safe learning rate, gradient descent is guaranteed to find it. Not every problem is so friendly. Neural networks have lumpy landscapes, with many dips, ridges and flat stretches. Start in one place and you settle into one dip; start somewhere else and you might settle into a different one. A dip that isn't the deepest is called a local minimum.
There are also saddle points, where the ground is flat in every direction, but going one way leads down and another way leads up, like a horse's saddle. The gradient there is zero, yet it's not the bottom. This sounds alarming, but in practice it works out remarkably well. In networks with millions of parameters, many of the low points turn out to be about equally good, and the noise of mini-batches helps the model roll off saddles. That's a big part of why gradient descent still trains the giant models of today.
10:26Try it yourself: a simple bowl
Let's run gradient descent by hand on the simple bowl from episode three: J of w equals w minus three, squared, plus one. Start at w equals zero, with a learning rate of zero point one. At w equals zero, the cost is ten, and the slope is two times zero minus three: minus six. Step: w becomes zero minus zero point one times minus six, which is zero point six. Now the cost is six point seven six. The new slope is minus four point eight, so w becomes one point zero eight. Pause and predict the new cost.
Four point six nine. Look at the gap between w and the bottom at three: three, then two point four, then one point nine two. Each step closes twenty percent of the remaining gap, because the steps shrink as the slope flattens. That's why gradient descent slows down as it approaches the minimum.
11:37Bounce and blow-up, by hand
Now watch what the learning rate does on this same bowl. Set it to exactly one. From zero, the step jumps all the way to six, the same height on the opposite wall. The slope there is plus six, so the next step jumps straight back to zero. Then six, then zero, forever. Gradient descent bounces between the walls and never gets any closer. Make it one point one, and it's worse. Zero, then six point six, then minus one point three two, then eight point one eight, then minus three point two two. Each bounce lands higher than the last.
The cost explodes. There's a precise rule behind this. For a bowl whose curvature is c, steps are only stable if the learning rate is less than two divided by c. This bowl has curvature two, so the limit is one. Brightcart's real cost bowl had a sharpest curvature of about twenty-four, which is why its learning rate had to stay below about zero point zero eight five.
12:42From two dials to billions
Everything we did with two dials works for any number of them. A model with a million parameters has a gradient with a million entries, one slope per dial, and one gradient-descent step updates all of them at once. The digit reader we'll train in Season Two has about two thousand four hundred parameters. The tiny language model we'll build in Season Three has about a hundred and ten thousand. Large language models have billions. And every one of them is trained by this same loop, repeated many thousands of times: make predictions, measure the cost, compute the gradient, take a step.
You might wonder how anyone computes a gradient with billions of entries. The answer is an algorithm called backpropagation, which gets all of them for roughly the cost of running the model a couple of times. It's one of the most important ideas in the course, and it's coming in Season Two.
13:41Practical habits
A few habits make gradient descent work in practice. Always plot the cost curve: is it falling smoothly, flat, jumping around, or exploding? Try learning rates a factor of ten apart, like zero point zero zero one, zero point zero one and zero point one, then refine around the best. Scale your features first, so the bowl is rounder and tolerates bigger steps. Shuffle the data every epoch when using mini-batches. Reduce the learning rate as training goes on: big steps early, small steps at the end. And judge progress on held-out data, not just the training cost.
That's next episode's topic. And a quick diagnosis guide. Cost going up? The learning rate is too big. Cost flat from the very first step? It's too small, or there's a bug. Cost jumping wildly? The batches might be too small, or the learning rate too high.
14:41Myth vs Fact
Myth: a bigger learning rate always means faster learning. As we saw, it does, right up to a point, and then everything falls apart. On Brightcart's bowl, zero point zero eight gets there in under a hundred and fifty steps, and zero point one never gets there at all. Fact: there's a sweet spot, and it depends on the shape of the landscape. Finding it is part science, part experiment.
15:08Recap
Let's recap. Gradient descent repeats one update: the new parameters equal the old parameters minus the learning rate times the gradient. On Brightcart's data it took the cost from forty point four to zero point four eight and found the best line. The learning rate controls the step size. Too small is slow, too large diverges. Stretched cost bowls make the path zig-zag, and feature scaling fixes that. And in practice we compute gradients on mini-batches, a compromise between accuracy and speed. One sentence to remember: feel the slope, take a step, repeat.
15:48Next: overfitting
Mission complete. Brightcart's model has trained itself, from a flat line to the best fit, just by walking downhill. But here's the uncomfortable question Dev is asking. A model can be brilliant on the data it trained on and still fail on next month's data. How do we know if a model has truly learned, or just memorised? That's overfitting, and it's next. See you on the map.