AI from scratch · Calculus 1 · Foundations
Derivatives: the rules of change
Training a model means asking one question millions of times: if I nudge this number, how much does the error change? That number is a derivative. This lesson builds it from a picture, gives you the handful of rules that differentiate any network, and shows the two places engineers get burned: activations whose slopes vanish, and derivatives computed carelessly by the computer.
1. Zoom in until the curve is a line
Pick a point \(a\) on a curve and a second point a distance \(h\) further along. The red line through both has slope "rise over run". Shrink \(h\): the red line turns into the green tangent, the line the curve looks like if you zoom in far enough. Its slope is the derivative.
\[ f'(a) = \lim_{h \to 0} \frac{f(a+h) - f(a)}{h} \]
Notice the gap between the two slopes shrinks in step with \(h\): halve \(h\), halve the gap. For \(x^2\) you can see why: \(\frac{(a+h)^2 - a^2}{h} = \frac{2ah + h^2}{h} = 2a + h\). Let \(h\) go to zero and what's left is \(2a\).
For \(f(x) = x^2\) at \(a = 3\) with \(h = 0.1\), what is the red line's slope \(\frac{f(a+h) - f(a)}{h}\)?
A derivative is also a sensitivity: near \(a\), with \(\Delta\) (the Greek capital delta, "a small change") for the nudge, \(f(a + \Delta) \approx f(a) + f'(a)\,\Delta\). If the loss has derivative 6 with respect to a weight, nudging that weight up by 0.001 raises the loss by about 0.006. Gradient descent (chapter 2) is just this sentence used backwards.
Zoom in far enough and a smooth curve looks like a straight line; the derivative is that line's slope, and it says how much the output moves per unit you move the input.
Where it breaks: kinks, jumps, and "near"
- Kinks. \(|x|\) at 0 has slope 1 from the right and −1 from the left; no zoom makes it a single line, so there's no derivative there. ReLU has the same kink at 0. Frameworks simply use slope 0 there, and it works because landing exactly on 0.0 almost never happens.
- Jumps. A step that is 0 below a threshold and 1 above has slope 0 everywhere except the jump, where it has none. Its derivative says "nudging changes nothing", which gives learning nothing to follow. That's why networks use the smooth sigmoid instead of a hard on/off threshold.
- "Near" is a real limit. \(f(a + \Delta) \approx f(a) + f'(a)\,\Delta\) is only good for small \(\Delta\): for \(x^2\) at \(a = 3\) a step of 0.1 is off by 0.01, a step of 3 by 9. A learning rate that's too big trusts the slope too far from where it was measured (calculus 3 measures how far is too far).
2. Seven rules differentiate everything
Nobody takes limits by hand. Every function a network computes is built from a few pieces, and each piece has a rule:
| function | derivative | in words |
|---|---|---|
| \(c\) | \(0\) | a constant doesn't change |
| \(x^n\) | \(n x^{n-1}\) | power rule (works for \(n = -1\), \(\tfrac12\), any \(n\)) |
| \(c\,f + g\) | \(c\,f' + g'\) | derivatives add and scale |
| \(f \cdot g\) | \(f' g + f g'\) | product rule: each one changes while the other holds |
| \(f(g(x))\) | \(f'(g(x)) \cdot g'(x)\) | chain rule: multiply the slopes along the way |
| \(e^x\) | \(e^x\) | its own slope (section 3) |
| \(\ln x\) | \(1/x\) | the log flattens as it grows |
Plus \(\sin' = \cos\) and \(\cos' = -\sin\) for waves (chapter 1's sine and cosine: the height and the shadow of a turning arrow). One more way of writing a derivative: \(\frac{d}{dx}\) in front of a formula means "the derivative of this, as \(x\) changes", the same thing as the prime, handy when the function has no name. Two worked examples: \(\frac{d}{dx}\, x^2 \sin x = 2x \sin x + x^2 \cos x\) (product rule), and the bell curve \(\frac{d}{dx}\, e^{-x^2} = e^{-x^2} \cdot (-2x)\) (chain rule: outside slope times inside slope). The chain rule is the one that matters most: a network is a long chain of functions, and backprop (chapter 4) is the chain rule run from the loss backwards.
\(f(x) = 3x^4 - 2x + 7\). What is \(f'(1)\)?
What is the slope of \(e^{3x}\) at \(x = 0\)?
The chain rule multiplies the slopes along the way, and a network is one long chain.
Why the chain rule multiplies
Nudge \(x\) by a small \(\Delta\). The inside, \(g\), moves by about \(g'(x)\,\Delta\) (the sensitivity sentence). That move is the outside's input nudge, so \(f\) moves by about \(f'(g(x))\) times it: \(f'(g(x))\, g'(x)\, \Delta\). Divide by \(\Delta\) and you have the rule. Three gears in a row, turning 2× and then 3×, turn 6×; ten layers that each pass on 0.25 of a nudge pass on \(0.25^{10}\) of it (section 4).
Predict before you compute: what is the slope of the bell curve \(e^{-x^2}\) at \(x = 0\)?
Why the product rule looks like that
Picture \(f \cdot g\) as the area of a rectangle with sides \(f\) and \(g\). Nudge \(x\): the sides grow by \(df\) and \(dg\), and the area gains a strip \(g\,df\), a strip \(f\,dg\), and a corner \(df\,dg\). The corner is a small number times a small number, so it vanishes faster than the strips when you divide by the nudge. What remains is \(f'g + fg'\).
3. ex and ln: the two functions ML is made of
\(e^x\) is the function that grows at a rate equal to its size: its slope everywhere is its own value. That makes it the natural way to turn any score into something positive with a clean derivative, which is exactly what softmax does (chapter 5). Its inverse, \(\ln x\), has slope \(1/x\): steep near zero, flat for large \(x\).
Logs are everywhere in ML for two reasons. First, \(\ln(ab) = \ln a + \ln b\): the probability of a whole sentence is a product of a thousand token probabilities, and the log turns it into a sum, which has a far easier derivative. Second, the product doesn't fit in a computer:
The loss for a prediction is \(-\ln p\), where \(p\) is the probability the model gave the right answer. Its derivative is \(-1/p\): when the model is confidently wrong (tiny \(p\)), the slope is huge and the correction is strong.
The model gave the right answer probability \(p = 0.01\). How many times steeper is \(-\ln p\) there than at \(p = 1\)?
4. Activations and their slopes
Between layers, a network bends its numbers with an activation function. Backprop multiplies by the activation's derivative at every layer, so its shape decides whether learning signals survive the trip. Blue is the function, yellow its derivative.
The sigmoid \(\sigma(x) = \frac{1}{1 + e^{-x}}\) has a neat derivative, \(\sigma'(x) = \sigma(x)\,(1 - \sigma(x))\), and it is never more than 0.25. Stack ten sigmoid layers and the chain rule multiplies ten of those slopes together: the signal reaching the first layer is at most \(0.25^{10}\), about one millionth. That's the vanishing gradient that stalled deep networks for years. ReLU (slope exactly 1 for positive inputs) fixed most of it; its weakness is slope 0 for negative inputs, where a neuron can "die" and never recover. GELU, used in transformers, is a smooth ReLU.
Backprop multiplies one activation slope per layer, so an activation whose slope is usually far below 1 starves the early layers.
What is the largest value the sigmoid's derivative ever takes?
Use the chain rule: what is the slope of \(\ln \sigma(x)\) at \(x = 0\)?
5. Derivatives by computer, and checking them
A computer can estimate a slope straight from the definition with a small \(h\): the forward difference \(\frac{f(x+h) - f(x)}{h}\), or the more accurate central difference \(\frac{f(x+h) - f(x-h)}{2h}\). Smaller \(h\) should be better. Here is the actual error for \(f = \sin\) at \(x = 1\), on a log scale (each grid line is ten times more):
forward difference central difference
Going right to left, both errors fall as \(h\) shrinks (forward like \(h\), central like \(h^2\)), then turn around and climb. Below about \(h = 10^{-8}\), \(f(x+h)\) and \(f(x)\) agree in almost all of their 16 digits, and subtracting them leaves mostly rounding noise, which is then divided by a tiny \(h\). Floating point puts a floor under how precisely you can measure a slope this way.
Roughly which \(h\) gives the forward difference its smallest error?
- As small as possible, 1e-16
- About 1e-8, the square root of float64's precision
- About 0.1
Why not train with numerical derivatives? They cost one or two extra evaluations of the whole network per parameter: a billion-parameter model would need two billion forward passes for one step. Backprop gets every derivative for about the cost of two or three forward passes. So engineers use numerical derivatives for one job: gradient checking, comparing them against the hand-derived or backprop gradient on a few inputs to catch bugs.
6. What to carry forward
- A derivative is a sensitivity: change in output per change in input, near this point. Training is reading these sensitivities and stepping against them.
- The chain rule multiplies slopes along a chain of functions. Deep networks are long chains, so slopes that are always small (sigmoid) or zero (dead ReLU) starve the early layers.
- Work in logs: log-probabilities add instead of multiplying, don't underflow, and give gradients like "1 minus the probability".
- Check gradients numerically with central differences and \(h \approx 10^{-5}\) when you write a derivative by hand; trust backprop for training.
All chapters: AI from scratch
Last updated