Rates of change
Last updated
Training a model means nudging its numbers so its mistakes shrink. To know which way to nudge, you need to know how fast the mistake changes when a number changes: its rate of change, the derivative. This lesson reads a derivative as the slope of a curve at one point, gives the four rules you'll use most, and shows how to check any derivative by nudging, which the course does every time it works one out by hand.
By the end of this lesson you can
- Read a derivative as the slope of a curve at a point
- Use the power, exponential, log and sum rules
- Estimate a derivative by nudging, with the central difference
- Explain why a step that's too small gives a worse answer, not a better one
Slope of a line
Lesson 1.1 met the slope of a line: how much goes up for each step of 1 in . Between any two points on a curve, the same idea gives the slope of the straight line joining them, the chord:
In words: the change in height divided by the change in , "rise over run". For between and : .
Slope at a point
A curve's slope changes as you move along it. To get the slope at one point, bring the second point closer and closer. The slider below does this: shrink , the gap between the two points, and watch the dashed chord turn into the pink line that just touches the curve, the tangent.
At x = 1, the curve’s slope (pink) is 2: the derivative of x² is 2x. The chord to x + h (dashed) has slope (4 − 1) ÷ 1 = 3. Make h smaller and it gets closer.
The same thing in numbers, for at . As shrinks, the chord's slope closes in on 4:
The derivative
The number those slopes close in on is the derivative of at . It has two common names: , said "f prime of x", and , said "d f by d x".
In words: nudge by a tiny , see how much changes, divide by , and ask what that ratio settles on as shrinks towards 0 ( means "the limit").
What it tells you: if , then nudging up by a small amount makes go up by about 4 times that amount. A positive derivative means "uphill to the right", negative means "downhill to the right", and 0 means the curve is flat there, as at the bottom of a valley. Gradient descent (Stage 3) uses exactly this to decide which way to move a model's weights.
Rules: power, eˣ, ln x and sums
You almost never take the limit by hand. A few rules cover nearly everything in this course:
| Function | Derivative | In words |
|---|---|---|
| bring the power down, lower it by 1 | ||
| its own derivative | ||
| one over | ||
| a constant multiplier stays | ||
| the derivative of a sum is the sum |
So the derivative of is , which is 14 at . Switch the slider above to and see the slope always equal the height. The chain rule, in the next lesson, handles functions inside functions.
Checking by nudging
You can check any derivative without knowing the rules, by nudging. The central difference nudges both ways and is much more accurate than nudging one way:
In words: step a little to the right and a little to the left, and divide the change in by the distance between them. In NumPy: (f(x + h) - f(x - h)) / (2 * h), with around 1e-5. Here it checks each rule:
Each pair agrees to about 10 decimal places. This is gradient checking, and the course does it after every derivative it works out by hand. If your formula and the nudge disagree past the sixth decimal place or so, the formula is wrong.
Wrong way: a step so tiny that rounding wins
If a smaller is better, surely the tiniest is best? Run it:
The answer gets better down to about (lesson 1.1's e notation: 1e-08 is ), then worse: 4.00036 at , 3.55 at , and 0.0 at . A float64 keeps only about 16 significant digits. When is tiny, and agree in nearly all of them, so subtracting leaves mostly rounding error, and dividing by tiny blows that error up. At , rounds to exactly 2 (the last cell), so the difference is 0.
So use around with the central difference. That's the default in every slope function in this course.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 4: Numerical Computation: derivatives as slopes and gradient-based optimisation (4.3), and rounding error (4.1)
- Prince, Understanding Deep Learning: the deep learning textbook the course follows from Stage 6, free online, with maths appendices
- The Python Tutorial: Floating-Point Arithmetic: Issues and Limitations: why a float can’t hold 2 + 0.00000000000000001