0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Which way should the dials turn?
In our last two episodes, we built a model with dials, and a cost that tells us how wrong it is. Right now the cost is forty point four. Terrible. So we should turn the dials. But which way? Turn this one up and the cost might fall, or it might shoot up. And this model has two dials. A language model has billions. We can't just try every direction. Imagine you're standing on a hillside in thick fog. You can't see the valley. But you can feel the slope under your feet, and that's enough: step in the direction that goes down.
In maths, "feeling the slope" has a name: the derivative. And with many dials, it's called the gradient. Today we'll learn both, and by the end you'll calculate the exact direction that improves Brightcart's model.
1:04Map flight: derivatives and gradients
Season One, Module One, stop three: derivatives, gradients and the chain rule.
1:11Slope and the derivative
Let's start with something you already know: the slope of a straight line. Rise over run. Go two steps across, rise six, and the slope is three. Same slope everywhere on the line. Curves are trickier. Here's a bowl-shaped curve. Think of it as a cost: J of w equals w minus three, squared, plus one. Its lowest point is at w equals three. The slope is different everywhere. Steep on the left, flat at the bottom, steep on the right. So what's the slope at one point? Here's the trick. Pick our point, at w equals one, and a second point nearby.
Draw a line through both and measure its slope. Now slide the second point closer. And closer. The slopes settle down: minus three point five, minus three point nine, minus three point nine nine. They're heading towards minus four. That limiting value is the derivative: the slope of the curve at exactly that point. Here's another way to see it. Zoom in far enough on any smooth curve and it looks like a straight line. The derivative is the slope of that line. We write it as d-J by d-w, read as "the derivative of J with respect to w".
It answers one question: if I nudge w a tiny bit, how fast does J change, and in which direction? Watch the slope meter as we slide along the bowl. On the left, the slope is negative: increasing w makes the cost go down. On the right, it's positive: increasing w makes the cost go up. And at the very bottom, the slope is zero. A flat spot. That's where the minimum lives.
3:04The derivative rules you actually need
We don't want to do that zooming every time, so mathematicians found shortcuts. You need surprisingly few. The power rule: the derivative of w to the power n is n times w to the power n minus one. So w squared becomes two w. W cubed becomes three w squared. W itself has a slope of one, and a constant has a slope of zero, because it never changes. Constants that multiply come along for the ride. And the derivative of a sum is the sum of the derivatives. Let's test them. Expand our bowl and we get w squared, minus six w, plus ten.
Apply the rules term by term: two w, minus six, and the ten disappears. At w equals one, that's two minus six: minus four. Exactly what our zoom found. And where is the slope zero? Two w minus six equals zero, so w equals three. The bottom of the bowl, found with algebra instead of searching. Two more facts to keep in your pocket for later: the derivative of e to the z is itself, and the derivative of the natural log of z is one over z. We'll need both when we meet logistic regression.
4:29Partial derivatives
Real models have more than one dial. Brightcart's sales model has two, w and b, so its cost isn't a curve any more. It's a surface, a bowl floating over a floor of every possible w and b. To get a slope on a surface, we pick one direction at a time. Freeze b, slice the bowl along w, and measure the slope of that curve. That's the partial derivative of J with respect to w, written with this curly d. Then freeze w, slice along b, and measure again. That's the partial derivative with respect to b. Here's where Brightcart's untrained model sits, with both dials at zero, way up the side of the bowl.
The partial derivative with respect to w is minus forty-one point six. With respect to b, it's minus twelve. Both negative. So increasing either dial lowers the cost, and w's slope is much steeper, so it matters more right now.
5:35The gradient: why negative gradient is downhill
Now stack the partial derivatives into one vector. That vector is called the gradient, written with this upside-down triangle, called nabla. The gradient has a beautiful property. It always points in the direction of steepest ascent. Straight uphill. Look at the map of the bowl from above. These rings are contour lines, each one joining points of equal cost. At every point, the gradient arrow cuts straight across the rings, pointing up the hill, and it gets shorter as the ground flattens near the bottom. We want to go down, not up.
So we simply flip it: the negative gradient points straight downhill. That's our foggy hiker's compass. It doesn't matter how many dials the model has, two or two billion. The gradient is one arrow in that many dimensions, pointing the way down. In episode five, we'll turn this into an algorithm: take a step against the gradient, recompute, step again. It's called gradient descent, and you can already see its formula here.
6:43The chain rule
There's one more rule, and it's the most important one in all of deep learning: the chain rule. Models are built as chains. The dial w makes a prediction. The prediction makes an error. The error makes the loss. We want to know how the loss changes when we turn w, but the effect has to travel down the whole chain. Think of gears. Turn the w gear once, and the prediction gear turns three times as much, because here the input x is three. Every bit the prediction rises, the error falls by the same amount, so that rate is minus one.
And the loss changes at two times the error. The error right now is four, so that rate is eight. The chain rule says: to get the overall rate, multiply the local rates along the chain. Eight, times minus one, times three. Minus twenty-four. Let's check by expanding the whole thing directly. The loss is seven minus three w, squared. Its derivative is two times seven minus three w, times minus three. At w equals one, that's minus twenty-four. It matches. Here's why this matters so much. A neural network is a chain of hundreds of these little steps.
The chain rule lets us work out the effect of every single dial by multiplying local rates backwards along the chain. That procedure is called backpropagation, and it's how almost every neural network learns. You've just learned its core.
8:18Deep dive: the MSE gradient
Deep dive time. If maths is not your thing yet, skip ahead to the next chapter. I'll sum up the result in one plain sentence. Here's the cost of Brightcart's sales model: the average, over all m days, of the squared error. To find the gradient, we use the chain rule on each term. The error is y minus the prediction, and the prediction is w x plus b. So the error changes by minus x when w moves, and by minus one when b moves. The square of the error changes at two times the error. Multiply along the chain and average, and we get: the partial of J with respect to w equals minus two over m, times the sum of x times the error.
And with respect to b: minus two over m, times the sum of the errors. Let's put in Brightcart's five days. With both dials at zero, every prediction is zero, so every error is just the true sales. The sum of x times y is one hundred and four. Minus two-fifths of one hundred and four is minus forty-one point six. The sum of the y values is thirty, and minus two-fifths of thirty is minus twelve. Exactly the slopes we measured on the surface. And here's the plain sentence I promised. Each dial's gradient is minus two times the average of the error multiplied by that dial's input.
Big errors on big inputs mean a big push.
9:52Try it yourself: slope, minimum and a gradient
Let's practise. Here's a new bowl: J of w equals w minus five, squared, plus two. What's the slope at w equals two, and where is the minimum? Pause and try. The derivative is two times w minus five. At w equals two, that's two times minus three: minus six. Negative, so increasing w goes downhill. The slope is zero when w equals five, so that's the minimum, where J equals two. Now two dials. f equals w squared plus three b squared, at the point w equals one, b equals two. Which direction is steepest uphill? The partial derivative with respect to w is two w, which is two.
With respect to b, it's six b, which is twelve. So the gradient is two, twelve: uphill is mostly in the b direction, because the bowl is much steeper that way. To go downhill fastest, move by minus two, minus twelve, scaled by a learning rate.
11:07Checking derivatives numerically
How can you be sure your derivative is right? There's a simple check that needs no calculus at all. Nudge w up by a tiny amount, nudge it down by the same amount, and measure how much the function changed, divided by the total nudge. For our bowl, with a nudge of one thousandth, that gives minus six, exactly what the rule said. Engineers use this as a gradient check to test their code, and we'll use it on a real neural network in episode twenty-five. But it needs two extra evaluations for every single parameter. That's fine for testing, and hopelessly slow for training a model with billions of parameters.
That's why we need the rules, and the chain rule, to compute gradients efficiently.
11:56Common mistakes, and a peek at the sigmoid
Three mistakes trip up almost everyone at first. Sign errors: since the error is y minus y-hat, the error goes down when the prediction goes up, a factor of minus one that's easy to drop, and dropping it sends training uphill. Missing chain factors: the derivative of three w plus one, squared, isn't just two times the bracket; you must multiply by the inner derivative, three. And mixing up partial derivatives: when you differentiate with respect to w, every other dial is frozen. And a quick preview. In Module Four we'll meet the sigmoid, an S-shaped curve that turns any number into a probability.
Its slope at its very steepest point is just zero point two five. Remember that number. In Season Two, it explains why early deep networks were so hard to train.
12:50Curvature and the second derivative
One more idea that will pay off in the next episodes: curvature. The derivative tells us the slope. The derivative of the derivative, called the second derivative, tells us how quickly the slope itself changes: how sharply the curve bends. For our bowl, the slope is two w minus six, which changes at a constant rate of two. So the curvature is two, everywhere. Why does that matter? Picture taking a step of the same size in a wide, gentle bowl and in a narrow, sharply curved one. In the wide bowl, the step is safe. In the narrow bowl, the same step flies past the bottom and up the other side.
Curvature decides how big a step is safe. For this particular bowl, as we'll see in episode five, a step size of exactly one makes gradient descent bounce forever, and anything larger blows up. And in many dimensions, a bowl can be sharply curved in one direction and gentle in another, like a long canyon. That shape explains the zig-zagging of gradient descent, and why rescaling features helps so much.
14:02Myth vs Fact: do machines learn by random tweaks?
Myth: machines learn by making random changes and keeping whatever works. For the models in this course, that's not how it happens. Random guessing in a billion dimensions would almost never find a way down. Fact: the gradient tells the model the best direction to move every parameter, all at once, from a single calculation. Calculus turns blind trial and error into a compass.
14:29Recap
Let's recap. A derivative is the slope of a curve at one point, what you'd see if you zoomed in until the curve looked straight. A handful of rules lets us calculate it directly. With several dials, partial derivatives measure the slope one dial at a time, and stacked together they form the gradient, which points straight uphill. Flip it, and it points downhill. And the chain rule lets us find the slope through a long chain of steps by multiplying the local rates, which is the foundation of how neural networks learn.
One sentence to remember: the gradient is a compass that always points uphill, so we walk the other way.
15:10Next: probability and statistics
Mission complete. Which way should Brightcart turn its dials? The gradient is minus forty-one point six and minus twelve, so we turn both dials up, w much more than b. How far to turn them is the job of gradient descent, which is coming up very soon. But first, a dose of reality. Sales are never perfectly predictable. There's always randomness. To build models that handle uncertainty, we need the language of probability and statistics. That's our next stop. See you on the map.