Calculus for Learning
Derivatives and the chain rule
Slope, sensitivity, and composing change
A function tells you where you are. Its derivative tells you which way and how fast things are changing right now: the slope of the curve at a single point. That one number is what gradient descent reads to decide which way is downhill.
Zoom far enough into any smooth curve and it looks straight. The is the slope of that straight zoom-in, the tilt of the tangent line that kisses the curve at . Positive slope means rising, negative means falling, and exactly zero means you are standing on a peak or in a valley. Everything else in this lesson is a consequence of that single picture.
The slope that never stops shrinking
Average speed is easy: distance over time. But what is your speed at one instant, when no time has passed at all? The trick is to measure the slope of a line through two nearby points, a secant, and slide the second point closer and closer until the gap vanishes. The secant tips over into the tangent, and its slope stops changing. That limiting slope is the derivative.
The payoff: local linear approximation
Scrub the tangent
Drag the point along the curve and watch the amber tangent line pivot. Read its slope live, and see the sign flip from negative to positive as you cross the bottom of a valley. Where the tangent lies perfectly flat, the slope is zero.
Where the slope hits zero
Two rules you will use forever
You almost never take a limit by hand. Instead you memorize a handful of rules; two carry most of the weight in machine learning. The power rule handles any raised to a power: bring the exponent down front, then subtract one.
The handles a function inside a function, . Its slope is the outer function's slope (measured at the inner value) times the inner function's slope.
The gears make this physical. Gear turns times for each turn of the input; gear turns times for each turn of . So per turn of the input, gear turns times, and the middle gear's size literally cancels out of the ratio, just as cancels in the algebra.
g'(x) = 12/6 = 2.00 f'(g) = 6/4 = 1.50
f'(g)·g'(x) = 6/4 × 12/6 = 12/4 = 3.00 turns of f per input turn
Why this matters next
The definition and the chain rule, in a few lines of Python.
# numerical derivative: slope of a tiny secant (the limit, approximated)
def deriv(f, x, h=1e-6):
return (f(x + h) - f(x)) / h
# chain rule by hand for y = (3x + 1) ** 2
g = lambda x: 3 * x + 1 # inner: u = g(x)
f = lambda u: u ** 2 # outer: y = f(u)
dg = 3 # g'(x)
df = lambda u: 2 * u # f'(u)
x = 2
chain = df(g(x)) * dg # f'(g(x)) * g'(x) = 2*(7)*3 = 42Check yourself
At the exact bottom of a smooth valley, the derivative f'(x) is:
For y = f(g(x)), the chain rule says the overall rate is:
State the chain rule for f(g(x)) and say why the rates multiply.
Think of the fraction form. Try to state it, then check.
Lock it in
- The derivative is the tangent's slope: the local rate of change.
- Near a point, f(x + dx) is about f(x) + f'(x)·dx. That is one descent step.
- A flat tangent (f'(x) = 0) marks a peak or valley: how we find best points.
- The chain rule multiplies rates along a chain, the engine of backprop.
Primary source