Lesson 6 · Calculus
How things change: derivatives
The problem
Back at the pizza shop, you predict each delivery's time with weights : minutes per km and minutes per traffic level. The prediction is .
Here are three real deliveries:
| Delivery | Distance (km) | Traffic level | Real time (min) |
|---|---|---|---|
| A | 2 | 0 | 8 |
| B | 5 | 1 | 26 |
| C | 3 | 2 | 24 |
Say you already know the traffic weight is , but you don't know . You guess . How wrong is that, and should you raise or lower it?
To score a guess, take each delivery's miss (prediction minus real time), square it, and add up the squares. Squaring makes every miss count as positive, and big misses count a lot. Call the total the error .
Worked example
The error for w₁ = 3 (with w₂ = 6)
Predictions: A is . B is . C is .
The error is a function of . Each miss is a multiple of : the misses are , and . Squaring and adding gives
Its graph is a valley with the bottom at , the true weight. This chapter is about one question: standing at , which way is downhill, and how steep is it? The answer is the derivative. The next two chapters build on it to tune both weights at once.
Up close, curves are straight
A straight line has one slope: rise over run. If a line goes up 6 when you move 2 to the right, its slope is . A negative slope means the line goes down as you move right.
A curve has no single slope. It is steep in some places and flat in others. But zoom in on one point and something useful happens. Press play.
Zoom ×1: showing w₁ from 1 to 5
Biggest gap from the dashed tangent line: 44% of the height
Zoomed out, the curve bends a lot: it even reaches the bottom of the valley at w₁ = 4.
Zoomed out, the curve bends a lot. After a few zooms it looks like a straight line, and every further zoom makes it straighter. So near any one point, the curve has a slope after all: the slope of the line you see when you zoom in far enough. That line is the tangent line, drawn dashed in the figure.
This holds for most curves in this book. ReLU, which you'll meet later, has a corner at 0, and there it never straightens out.
Measuring the slope with a nudge
How do you measure the slope of the line you'd see? Pick a second point a small step away. Then take rise over run between the two points:
The straight line through the two points is called a secant line. The smaller is, the closer the two points are, and the closer the secant gets to the tangent. Press play to shrink .
Nudge h = 2: E goes from 38 to 38
Secant slope = (38 − 38) ÷ 2 = 0
Tangent slope (dashed) = −76 · off by 76
| Nudge | Secant slope | |
|---|---|---|
| 2 | 38 | 0 |
| 1 | 0 | −38 |
| 0.1 | 30.78 | −72.2 |
| 0.01 | 37.2438 | −75.62 |
With the secant is flat, which is badly wrong. It jumps right over the valley to the other side, where the error is 38 again. As shrinks, the estimates settle towards . The number they settle on, , is called the limit.
That limit is the derivative. It is the slope of the tangent line at that point:
Read it as: nudge by and divide the change in by . Then see what number that settles on as shrinks to zero. The symbol reads "the limit as goes to 0". Both and mean the same thing. The second one reads as "a tiny change in per tiny change in ."
For our error, . That says: near , the error drops about 76 for each unit you raise . So a small nudge of should lower the error by about . Check: , which is lower than 38.
DeeperLimits without epsilonsWhat 'as h shrinks to zero' really means, without the formal machinery. · 3 min
You can't set in the nudge formula, because then you'd divide 0 by 0. The limit avoids that. It asks what the formula gets close to as gets close to 0, without ever using itself.
For our curve you can see it exactly. Expand . Then
The on the bottom cancels. What's left is plus an error of . That error shrinks in step with : at it is , and at it is . So the estimate settles on , and nothing breaks.
The formal version (with and ) makes "gets close" precise. You won't need it in this book.
Predict first
You stand at the very bottom of the error valley, at w₁ = 4. What is the slope there?
Which way to nudge
The sign of the derivative tells you which way is downhill.
- Negative slope: the curve goes down as you move right. Raise the input to lower the output.
- Positive slope: the curve goes up as you move right. Lower the input.
- Zero slope: you're on flat ground, perhaps at the bottom.
At the slope is , so you should raise . That's right: the true value is 4. The size of the slope says how steep the ground is. Far from the bottom, at , a nudge of 0.01 gives a slope of about : twice as steep.
This one idea, "move against the slope," drives how machine learning models learn. It's called gradient descent, and the ML fundamentals course covers it properly.
The derivative rules
You don't want to run the nudge test every time. For the functions this book uses, a few derivative rules give exact answers:
| Function | Derivative | Example |
|---|---|---|
| a constant | is flat | |
| (power rule) | , | |
| (constant multiple) | ||
| (sum) | ||
| slope equals height | ||
| slope at is |
Here means the natural log, base , as in the log chapter.
Pick a curve below and drag the point. The orange tangent line touches the curve at the point. The readout gives its slope from the rule, and a nudge test with to compare.
At x = 1.5, the curve’s height is 2.25
Tangent slope from the rule 2x: 3
Nudge test with h = 0.001: 3
Things to notice:
- On the slope is . It's negative on the left, zero at the bottom, and positive on the right.
- On the slope always equals the height, which is the defining property of .
- On the slope is . It's steep near 0 and flattens out as grows.
Now use the rules on our error.
Worked example
The derivative of E(w₁) = 38(w₁ − 4)²
Multiply out the square: .
Multiplying out the square worked here, but it gets painful for longer formulas. The chain rule, two chapters on, gets the same answer without it.
Common misconceptions
"The derivative is the value of the function." No. At the error is 38, but its slope is . One says how high you are; the other says how steep the ground is.
"A big slope means you're close to the answer." It's the other way round. In our valley the slope is steepest far from the bottom and zero at the bottom.
"The nudge test gives the exact slope." It gives an estimate. On our error slice the estimate is off by , as the limits aside shows. The rules give the exact answer.
Recap
- Zoom in on a smooth curve and it looks like a straight line, the tangent.
- The derivative is the tangent's slope: rise over run across a nudge , as shrinks to 0.
- Its sign says which way to nudge the input to go downhill. At the bottom of a valley it is 0.
- A short table of rules (power, constant multiple, sum, , ) gives exact derivatives.
Section 2 shows how to compute these in PyTorch.