Derivatives
How can a curve have a slope at a single point, when a slope needs two points?
The idea
Take a second point a step away, on either side, and measure the slope of the line through both points, the secant. If that slope approaches the same finite number from both sides as shrinks to zero, that number is the derivative: the slope of the tangent line at the point.
Builds on Functions.
Functions has no page here yet. Read OpenStax, Calculus Volume 1, Section 1.1: Review of Functions.
Ready? For f(x) = (x³ − 3x)/2, what is f(2)?
Show the answer
f(2) = (8 − 6)/2 = 1.
The picture
Slope is rise over run, and that takes two points. So take two: the point on the curve, and a second one a step to its right. The line through them is the secant. Its slope, , is the curve's average steepness over that step, and a long step averages over a whole stretch of curve.
Shrink and the secant pivots about the point until it settles on a single line, the tangent, which grazes the curve there. The tangent's slope is the derivative, : the steepness at the point itself, not across a stretch.
The lens magnifies the neighbourhood of the point, more as shrinks. Magnify a smooth curve enough and it looks straight; that straight line is the tangent. The chart underneath draws at every at once: above zero where the curve rises, below zero where it falls, and exactly zero where the tangent lies flat. A corner, like the one in at zero, never straightens however far you zoom.
In machine learning the curve is a model's loss and is one of its parameters. The derivative says which way to nudge that parameter to lower the loss, and how strongly the loss responds.
The mathematics
Definition (derivative). The derivative of at is
The fraction
is a secant slope: the average rate of change over the step. When the limit exists, from both sides and to the same finite number, it is the tangent slope. Steps from the right alone do not establish it.
Once you know , the tangent line at is
For a positive step , the left, right and central quotients at the same point are
The derivative exists when and tend to the same finite limit as shrinks. For a smooth function, the one-sided quotients miss by an amount proportional to , while the central quotient's leading errors cancel and it misses by an amount proportional to . The central quotient can mislead at a corner: for at zero, and at every , yet at every .
Try it
First a worked example, then one to finish, then problems on your own. Check your answer before you look at the solution. Answers typed here are not kept when you leave.
1 · Worked example
The slope at x = 0.5, from the definition
For , find as the limit of the secant's slope.
Rise. Expand , so .
Say the next step to yourself first.2 · Finish it
Average both sides
Instead of one secant to the right, take the line from to , which is the average of the left and right secants. With and , finish the estimate of .
Right point. .
Left point. .
Rise across both sides. .
- Your turn.
3 · On your own
A ten-times-smaller step
At with , the one-sided secant misses by and the two-sided average misses by . Shrink to . Roughly how far off is each now?
4 · On your own
Which way is downhill?
Suppose is a model's loss and its one parameter, currently , where . Training nudges a small amount at a time to lower the loss. Which way should it move, and where do many small nudges end up?
The code
The definition, run with shrinking steps on at , where the true slope is .
import numpy as np
f = lambda x: np.sin(x)
df_true = lambda x: np.cos(x)
x0 = 1.2
for h in [1.0, 0.3, 0.1, 0.03, 0.01]:
df_est = (f(x0 + h) - f(x0)) / h
print("h=", h, "secant=", df_est, "true=", df_true(x0))
The secant's slope climbs from at to at . Each ten-fold shrink of cuts the gap about ten-fold: at , at .
Keep it
Zoom in far enough on a smooth curve at one point. What do you see, and what is its slope?
Answer from memory, then check
A straight line: the tangent. Its slope is the derivative , the steepness of the curve at that point.
You shrink ten times. How does the miss change for a one-sided secant, and for the average of both sides?
Answer from memory, then check
On a smooth curve the one-sided miss shrinks about ten times (in step with ). The average's miss shrinks about a hundred times (like ), because the leading errors of the two sides cancel.
True or false: wherever , the curve has a peak or a valley.
Answer from memory, then check
False. Peaks and valleys of a smooth curve do have , but is flat at and keeps rising through it. A zero slope says where to look, not what you will find.
Why does have no derivative at ?
Answer from memory, then check
The right secant has slope and the left has slope at every . Zooming in never makes them agree, so no single tangent fits. Their average, , is steady but describes neither side.
Why does training a model need derivatives at all?
Answer from memory, then check
The derivative of the loss with respect to a parameter says which way, and how strongly, the loss changes when that parameter is nudged. A small step against it lowers the loss; without it, every nudge would be a blind guess.
Recalling an answer from memory makes it last longer than rereading it (Roediger and Karpicke, 2006). Add these 5 cards and they come back tomorrow, then after gaps of 3, 7, 16, 35 and 75 days while you still remember them. They stay in this browser only.
The edge
Everything here assumed a smooth curve, but the networks trained in practice are full of corners. ReLU, , has exactly the corner of at zero, where no derivative exists. Software simply picks a slope there, usually 0, and training works anyway. For convex functions the choice is justified by subgradients: any slope of a line that touches the function from below without crossing it. For deep networks the theory is still being built. At some points, automatic differentiation of such programs can return a value that is neither the derivative nor any subgradient. Bolte & Pauwels (2020) show that such points form a negligible set and give a framework for reasoning about them, but which slopes training actually needs there is not settled. In practice the choice still matters: in the 32- and 16-bit arithmetic used for training, the slope chosen for ReLU at zero can measurably change the result (Bertoin et al., 2021).