What is a derivative? Slope, limit and gradient descent, interactively
The route follows the first lecture of Andrej Karpathy's Neural Networks: Zero to Hero, where building a neural network from scratch starts with the derivative. This is the first of two parts: what the derivative is and why it is a limit. Part 2 computes it on a computer — finite differences, the chain rule and backpropagation. Every panel below is live: change something and the numbers are recomputed, never typed in.
The lecture works with the parabola . This page uses a deliberately uneven curve instead, so its slope changes size and sign from place to place and there is more than one valley to fall into:
What the derivative is for: walking downhill
Start with what the derivative is for. The point below walks down the curve by one rule: step against the slope under it, and repeat.
is the slope under your feet — the derivative, which the rest of this page builds from scratch. Its sign says which way goes up, so subtracting it steps down. The step size is the learning rate. The step is times the slope, not a fixed length: where the slope is steep the step is long; near a minimum, where , it shrinks, and the walk stops by itself.
Gradient descent on the wavy curve: a point repeatedly steps against the slope of its tangent until the slope is zero at a local minimum; a large learning rate makes it bounce.
From with , the walk reaches the deepest valley, , in 20 steps. Drag the start (or move its slider) to instead — just past the peak at — and the dashed preview shows the same rule taking it the other way, into the shallower valley at . The slope is local information: it knows which way is down here, not where the best valley is, so where the walk ends depends on where it starts.
Now raise . At the bottom of the deepest valley the walk settles only while ; above that, every step overshoots by more than it corrects, and the point bounces between the walls of the valley instead of settling. Lower far enough and the walk still arrives, only slowly. The threshold is , two over the curvature at the bottom: here.
Why a minimum, and why the slope
Almost every machine-learning problem comes down to one thing: there is an error function, and it has to be made as small as possible. For a face-recognition model the error measures how often it confuses two people; for a music recommender, how often the listener skips a song. The tasks differ, the mechanism is the same: find where the error is lowest.
The walk above is how that minimum is found. Training a neural network is exactly that loop: becomes millions of weights, becomes the loss on the training data, and each step moves every weight a little against its slope.
We look at the graph and see the valley at once. A computer does not see the graph. It can evaluate the function at any point it chooses, one point at a time — like standing on a mountainside in thick fog, knowing only the height where you stand. What it can still find out is which way the ground slopes under its feet and how steeply: the sign and the size of the derivative. That is all the walk above uses.
Everything in the rule is easy except one number, the slope under your feet. The rest of this part is about what that number is.
From secant to tangent: the limit
The derivative of a function at a point is the number
A single point gives no slope, so the definition takes a second one. The points and fix a line, the secant, and its slope is the ratio of the legs of the orange triangle:
It is the tangent of the secant's angle to the horizontal, — rise over run.
Now drag the second point towards the first. The slider does the same job: its right end is always the edge of the plot and its left end a single pixel, whatever the zoom. As shrinks, the secant turns onto the tangent.
The curve with a point x and a second point x + h joined by a secant; as h shrinks the secant rotates onto the dashed tangent, and zooming in shows the curve becoming indistinguishable from a straight line.
At the true slope is . The secant gets there step by step:
| step | secant slope | error |
|---|---|---|
| 1 | 6.6 | |
| 0.1 | 0.86 | |
| 0.01 | 0.084 | |
| 0.001 | 0.0084 |
At the secant even has the wrong sign: it jumps over the crest just past . Every further tenfold decrease of makes the error ten times smaller.
So how small is small enough? In practice has gone far enough once shrinking it further stops changing the digits you care about: each row of the table settles about one more digit of .
Now zoom in — with the zoom slider, or a pinch on a trackpad or a touchscreen. The second point keeps its place on screen, so shrinks with the view, and the curve straightens under it: the secant turns onto the tangent without being touched. At a thousandth of the original width the curve is indistinguishable from its tangent. This is why the limit exists at all: a smooth function is, locally, linear. The derivative is the slope of that local line.
What a computer actually gets
The limit is a statement about going to zero; a program has to stop at some . So a secant computed from function values is an estimate of the derivative, for two reasons.
- The step is finite. The secant at a small is very close to the tangent but is not it: in the table above the error shrinks tenfold with and never reaches zero.
- The numbers have gaps. A floating-point number is a ruler with marks: whatever falls between two marks is rounded to the nearest one, and the marks are dense near zero and sparse for large numbers. has no mark of its own, so the nearest one is stored instead, and in Python
0.1 + 0.2 == 0.3isFalse: the sum is0.30000000000000004. That is why floats are compared with a relative tolerance,math.isclose(a, b), rather than with==: a fixedabs(a - b) < 1e-9stops working once the numbers are large enough that neighbouring marks lie further apart than that.
The two reasons pull against each other: a smaller shrinks the first error and amplifies the second. Part 2 measures that trade-off, and then shows how neural networks avoid it altogether — by computing the derivative from the formula itself, with the chain rule, instead of from nearby points.
What the number means
The limit above, in words: how fast the output changes per unit change of the input, measured so close to that nothing but the local behaviour of matters.
The same number has a second reading that is more useful than the first. For small ,
Near , the function behaves like a straight line with slope — the tangent. Read left to right, the formula is a prediction: move the input by and the output moves by about , and the sign of says which way. One question is left: why exactly one line deserves that name.
One point has no slope
Which line through one point is the tangent? Through a single point of a curve pass infinitely many straight lines. Which of them "has the slope of the curve"?
Drag the point along the curve and turn the line by its round handle. From a distance, plenty of lines look about right. The inset magnifies a small window around the point, and there the difference shows: a wrong line keeps crossing the curve at an angle however far you zoom, while one line merges with the curve. That line is the tangent, and its slope is .
A wavy curve with one marked point and a line through it whose slope the reader sets; an inset magnifies the point, where a wrong line crosses the curve at an angle and only the tangent merges with it.
The legend measures what the lens shows: the gap between curve and line divided by the distance from the point, at , and . At a flat line gives — the ratio closes in on and stays there, and every other wrong slope settles the same way on its own error. Turn the line to slope and the same three numbers become : ten times smaller for every tenfold zoom. Move the point, and the slope that worked a moment ago no longer does. A derivative is not one number for the whole curve but a new number at every point.
Why exactly one line? Expand the curve around . The gap between it and a line of slope , per unit of distance , is
As shrinks, everything but the first term disappears. Any error in the slope survives the zoom; only lets the ratio go to zero. This, and not "the line that touches the curve at one point", is what a tangent is: a line can touch a curve once and still cross it, and a tangent can cross the curve (at an inflection point it does).
That line is what the walk at the top of the page follows: the sign of its slope says which way is down, and its size how long a step to take. Part 2 computes it on a computer — how small can be in floating point, and how neural networks get the slope from the formula itself, with the chain rule.
Short answers
Why does machine learning need derivatives?
Training a model means making an error function as small as possible. The derivative says which way the error goes down and how steeply, and gradient descent steps that way over and over.
What is a derivative in one sentence?
The derivative f′(x) is the rate at which f changes per unit change of its input near x — the slope of the straight line that best approximates the graph at that point.
Why is the tangent defined by a limit?
A single point does not determine a line's slope: infinitely many lines pass through it. A second point at distance h does determine one, the secant, and the tangent is what the secant turns into as h goes to zero.
Can a computer find a derivative exactly?
From function values alone it can only estimate one: a secant over a small but finite step h, computed in floating-point numbers that round every result. When the function is a formula the program executes — a neural network is one — automatic differentiation applies the chain rule to that formula and gets the derivative exactly, up to floating-point rounding. Backpropagation is that.
Further reading
- A. Karpathy, "The spelled-out intro to neural networks and backpropagation: building micrograd", Neural Networks: Zero to Hero, lecture 1, 2022. The lecture this page follows.
- 3Blue1Brown, Essence of Calculus, video series, 2017. The geometric intuition behind derivatives and the chain rule.
- M. Spivak, Calculus, 4th ed., Publish or Perish, 2008. Chapters 9–10: the definition of the derivative and the rules, proven properly.