Calculus
Derivatives, chain rule, and gradients — the language of learning. Taught from zero, starting with what a slope is, and built up to the exact quantity every neural network follows downhill.
Start here — what this is really about
Calculus has a fearsome reputation, and almost all of it is undeserved. The part of calculus that machine learning actually runs on is one single question, asked over and over:
If I nudge this number a tiny bit, how much does that number change?
That's it. That's the whole subject, for our purposes. A model has millions of knobs; training needs to know, for each knob, which way and how hard to turn it. "How much does the output change when I nudge this input" is exactly the question calculus answers, and the answer has a name: the derivative.
The page adapts to you. By default it teaches from first principles — no symbols until you already understand the idea they stand for. When you're ready, flip the Depth switch at the top to reveal the formal notation, derivations, and edge cases. Nothing is hidden for good; the deeper material sits behind the "Go deeper" panels so you can open it the moment you're curious.
Classical calculus courses spend half their time on integration — adding up infinitely many tiny pieces to get areas and volumes. It's beautiful, and it matters a lot in probability. But for understanding how models learn, you need the other half: differentiation. This page is about differentiation, and it goes deep on it. You'll meet integrals again in Probability & Statistics.
Step 1 — Slope, the ancestor of everything here
Before derivatives, there's a much older and simpler idea: the slope of a straight line. If you understand "rise over run," you already have the seed of all of calculus.
A answers: for every one step I take to the right, how far up (or down) do I go?
A ramp that climbs 1 metre over 12 metres of floor has a slope of — gentle. A ramp that climbs 1 metre over 2 metres of floor has a slope of — steep enough to be dangerous. Same idea, one number, and you instantly know how hard the climb is.
Take two points on a line: and . (Each pair is x across, y up.)
- Run — how far right did we move? .
- Rise — how far up did we move? .
- Slope — rise divided by run: .
So the line climbs 2 units up for every 1 unit right. Notice we needed two points to get this number: slope is a comparison between two places.
A line passes through (0, 10) and (5, 0). Is its slope positive or negative, and what is it?
Hint: Rise over run — and watch the sign of the rise.
Step 2 — The derivative: slope at a single point
Straight lines are easy: the slope is the same everywhere. Real functions curve. On a curve, the steepness changes as you move along it — a hill is steep near the bottom and flattens at the top.
So here's the problem: slope needs two points, but we want the steepness at one exact spot. How do you get "rise over run" when the run is zero?
Pick a point on a curve and zoom in. Zoom again. Zoom again. Almost every curve you'll meet becomes indistinguishable from a straight line if you zoom in far enough — the same way the Earth's surface looks flat from where you're standing even though the planet is round. The derivative is the slope of that straight line you see when you've zoomed all the way in.
The practical version: take a second point very close by, compute the ordinary rise-over-run between them, and then keep sliding that second point closer and closer. The slope value settles down on a specific number. That number is the .
Drive 120 km in 2 hours and your average speed is 60 km/h — that's rise over run, computed between two points in time. But your speedometer doesn't show the trip average; it shows how fast you're going right now, at this instant. The average speed is the slope between two points; the speedometer reading is the derivative. Calculus is the machinery that turns the first into the second.
Let's find how steep is exactly at . We can't use one point, so we'll use a second point a distance away and shrink .
The slope between and is .
| (gap) | slope | |
|---|---|---|
| 1 | ||
| 0.5 | ||
| 0.1 | ||
| 0.01 | ||
| 0.001 | — |
The numbers are marching toward 2 and never wandering off. So the derivative of at is exactly : right at that spot, the curve climbs 2 units for every 1 unit right.
Here is that table as a picture. Every dot is one row above — the gap on the horizontal axis, the measured slope on the vertical. Notice how the dots flatten onto the dashed line at 2 as shrinks toward the left:
Now do it with your hands. Drag the slider that controls the gap and watch the secant line (the one through two points) swing around until it becomes the tangent line (the one touching at a single point). The tangent's slope is the derivative:
Why can we not just plug a gap of exactly zero into rise-over-run to get the slope at a point?
Hint: Look at what the run would be, and what you would be dividing by.
Let's confirm the worked example numerically — shrink in code and watch the answer arrive:
That last row is the lesson. As shrinks past about , f(x+h) and f(x) become so close that subtracting them throws away most of their significant digits, and then dividing by a tiny magnifies whatever rounding noise is left. Numerical differencing is a debugging tool (a gradient check), never the real thing. Deep-learning frameworks instead apply the rules below symbolically — which is exact, and which we'll see is also far cheaper.
Step 3 — The rules: derivatives without limits
Nobody computes limits by hand every time. Mathematicians worked out the limit once for each basic function shape and wrote down the answers. In practice, differentiating is a lookup-plus-recipe job.
The three you'll use constantly:
| Rule | If your function is… | …its derivative is | Sanity check |
|---|---|---|---|
| Constant | A flat line has no slope | ||
| Power | , matching our table at | ||
| Sum | Slopes just add |
Picture as the area of a square with side . Grow the side by a hair and you glue on two thin strips — one along the top, one along the side — each of area , plus a negligible little corner square of area . So the area grew by about , meaning it grew at a rate of per unit of side length. That's the derivative, and the tiny corner you ignored is exactly what vanishes in the limit.
Find the derivative of .
- — the power rule gives for ; the constant rides along untouched, giving .
- — this is , so the power rule gives , times the : just . (Sensible: a term that's a straight line has a constant slope of 5.)
- — a constant. Flat. Contributes .
- Add them up (the sum rule): .
Check it at a point: at , . Nudge the input from to and goes from to — a rise of about over a run of , which is . The derivative predicted the change.
What is the derivative of f(x) = 4x³ − 2x + 9?
What the derivative tells you about a curve
The sign of the derivative is a compass, and the zeros of the derivative are where the interesting places are:
- → the function is rising here. Step right and the value grows.
- → the function is falling here. Step right and the value shrinks.
- → the function is momentarily flat — a peak, a valley, or a plateau. This is where training stops.
See both curves at once. The orange curve is ; the teal one is its derivative . Look at where the teal curve crosses zero and check what the orange curve is doing at exactly those spots:
During training, the loss stops improving and the gradient is measured at almost exactly zero. Name two different situations that could produce this.
Hint: A derivative of zero means flat — but flat where?
Step 4 — The chain rule: the engine of deep learning
If you remember one thing from this page, make it this one. A neural network is functions stacked on functions stacked on functions: the input goes through layer 1, whose output goes through layer 2, and so on for dozens of layers. To train it, we need to know how a change deep inside affects the final loss at the very end.
The rule that answers that is the , and backpropagation is nothing but the chain rule applied carefully and efficiently.
Suppose a bicycle's pedals turn a middle gear 3 times for every 1 pedal turn, and that middle gear turns the wheel 2 times for every 1 of its own turns. How many wheel turns per pedal turn? You multiply: . Rates chain together by multiplication, not addition. That's the entire chain rule.
Dollars → euros at 0.9, euros → yen at 160. Dollars → yen is . Each conversion is a rate, and you compose them by multiplying. A neural network is a long chain of such conversions, and the chain rule tells you the end-to-end exchange rate between any early parameter and the final loss.
Let (the inner function) and (the outer one). The composed function is . Find at .
- Rate of the inner stage. — nudge by a hair and moves twice as much.
- Rate of the outer stage, at the value the inner one produced. At , . The outer function has , so at that rate is . This is the step everyone forgets: the outer rate must be evaluated at the inner function's output, not at .
- Multiply. .
Check it. . Nudge to : . Rise over run gives . The chain rule was right.
Why must the outer function's derivative be evaluated at g(x), rather than at x itself?
Hint: What number is actually being fed into the outer function?
Now build a two-stage chain in code and propagate a gradient backward through it by hand — this is backpropagation in miniature:
A network computes loss = f(g(k(x))). Which product gives dloss/dx?
Step 5 — Partial derivatives and the gradient
Everything so far had one input knob. A real model has millions. What does "the slope" even mean when there are a million directions you could move in?
Freeze every knob but one. Wiggle that single knob and see how the output responds. That one number is the partial derivative with respect to that knob. Do it for each knob in turn, collect all the answers into a list, and you have the gradient — a full report card of "how sensitive is the output to each individual input."
A is written with a curly instead of , purely as a reminder that other variables exist and are being held still.
You're on a hillside where a road runs exactly east–west and another exactly north–south. Face east and ask how steeply the ground rises — that's the partial derivative in . Turn to face north and ask the same — that's the partial derivative in . Two separate questions, two separate numbers, and neither one is "the" steepness of the hill; together, though, they tell you everything about which way is up.
Let . Find both partials at the point .
- With respect to — treat as a frozen constant. Then is just some fixed number, so its derivative is , and differentiates to . So , which at equals .
- With respect to — now freeze . The term becomes a constant contributing , and differentiates to . So , which at equals .
- Collect them. The gradient at is the list .
Read the answer. The output is six times more sensitive to than to right here. If you could only adjust one knob, is the one worth touching. That relative sensitivity is precisely the information gradient descent acts on.
Drag the point around the surface below and watch the two tangent slices — one cutting in the direction, one in the direction. Each slice is an ordinary single-variable curve, and its slope is one of the partial derivatives:
Here is the surface most relevant to machine learning: a loss bowl, . Rotate it and notice it's steeper along than along — a stretched, elliptical valley rather than a round one. That asymmetry is what makes plain gradient descent zig-zag, and what momentum and Adam exist to fix:
Compute a gradient numerically, then use it to actually walk downhill:
At a point, ∂L/∂w₁ = 0.02 and ∂L/∂w₂ = −4.8. What does this tell you?
Step 6 — Why this is the language of learning
Put the pieces together and the whole training loop is calculus:
- A model produces a prediction; a loss scores how wrong it is — one number.
- The loss depends on every parameter through a long chain of layers.
- The chain rule, applied backward through that chain, gives for every parameter at once. That's backpropagation.
- Those partials, stacked, are the gradient — the direction of steepest increase in wrongness.
- Step the opposite way. Repeat a few million times. That's Optimization.
Watch it run on a real surface — drop a starting point, and see the gradient you just learned to compute being followed downhill step by step:
Brighter = higher loss. Watch how a high learning rate overshoots, and how momentum powers through the small bumps toward a minimum.
Explain to a curious 12-year-old what a derivative is, using the speedometer picture and no symbols. Then explain the chain rule with the bicycle gears. If you stall on either, that is exactly the section to reread.
- A derivative answers one question: nudge the input a hair — how much does the output move? It is the slope of the curve at a single point, found by shrinking the gap between two points until the answer settles.
- Its sign says which way the function is heading; a derivative of zero means flat — a minimum, a maximum, or a saddle.
- The rules (power, sum, product, quotient) let you differentiate without ever computing a limit — exact and cheap, which is why frameworks use them instead of numerical nudging.
- The chain rule multiplies the local rates along a chain of nested functions. Applied backward through a network, it is backpropagation — and the multiplying is why gradients vanish or explode.
- A partial derivative wiggles one input while freezing the rest; the gradient stacks them all into a vector pointing straight uphill, with magnitude equal to the steepness.
- Training is: compute , step against it, repeat. Every idea on this page exists to make that one term computable.
Practice — and how to make it stick
Three research-backed habits, built into this platform:
• Retrieval practice: attempt the problems below before scrolling back up — pulling an answer out of memory builds far more durable knowledge than recognizing it on the page.
• Spaced repetition: mark this topic complete and it joins your Review queue, resurfacing right before you'd otherwise forget it.
• Interleaving: mix these with problems from Linear Algebra and Optimization rather than grinding derivatives in one block — messier practice, sturdier memory.
- By hand: differentiate using the chain rule, then verify by nudging numerically at .
- By hand: find both partials of and write out the gradient. Which variable is the output more sensitive to at ?
- Reason about it: the derivative of is for and for . What does a derivative of exactly mean for a neuron's ability to learn? (This is the "dying ReLU" problem.)
- From scratch: write a
numerical_gradfunction and use it to gradient-check a hand-derived derivative — the standard debugging trick when your backprop math looks suspect.
Try it right here — edit and run the code, and if you get stuck or hit an error, ask Ada on the right: she can see your code and your terminal output.
Ready for the next step? Take these gradients to work in Optimization, then see exactly how a network computes them in Backpropagation.