The derivative: slope, and sensitivity
Shrink the gap between two points on a curve and the line through them becomes the tangent. Its slope is the derivative: how much the output moves when you nudge the input.
1. The idea
The derivative of f at x is the slope of f right there: how fast f(x) changes when x changes a little. Take two points on the curve, x and x + h. The slope of the line through them is (f(x + h) − f(x)) / h. Let h shrink towards 0 and that secant line turns into the tangent; its slope is f′(x).
In machine learning the derivative is a sensitivity: nudge this weight up by a tiny amount, and how much does the loss change, and which way? A positive derivative says the loss goes up, so move the weight down. That one fact is the whole of gradient descent.
Nobody computes limits by hand for long. A handful of rules cover almost everything: the power rule, sums, constants that come out, and the chain rule for functions inside functions (lesson 20). The nudge formula is still useful: it is how you check a gradient numerically.
2. Try it: shrink the secant
Pick a point x, then drag h down towards 0. The red secant line swings onto the dashed tangent, and its slope approaches f′(x).
3. The math
4. The code (python)
def f(x):
return x**3 / 3 - x
def f_prime(x):
return x**2 - 1
x = 1.5
print([round((f(x + h) - f(x)) / h, 5) for h in (1.0, 0.1, 0.001)]) # [3.08333, 1.40333, 1.2515]
print(f_prime(x)) # 1.25
h = 0.001 # a central difference is far more accurate for the same h
print(round((f(x + h) - f(x - h)) / (2 * h), 7)) # 1.25000035. You’ll see this in
6. Check yourself
Finished this chapter?
What changed in ML architectures this week
New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.