Derivatives and Taylor series
The derivative as the best linear approximation, the chain rule behind backpropagation, derivatives of the sigmoid, softplus and log, Taylor polynomials with their remainder, and what first and second order give an optimizer.
From secant to tangent
The topic The singular value decomposition closed the linear algebra part. We can now describe a model: it is a map such as the linear layer . We cannot yet train it. Training changes the parameters to lower a loss , and for that we need to know how the loss reacts to a small change of . That is what a derivative measures. This topic works with a single parameter; many parameters come in the next one.
Take two points on the graph of , at and at . The line through them is a secant. Its slope is the difference quotient
For at the quotient equals . With it is , with it is . As the secant turns into the tangent, and its slope settles at one number. That limit is the derivative:
Here .
The running example of this topic is a model with one weight that predicts . On a single example its squared error is
At the loss is . The sign of tells us which way to move , and its size tells us how strongly the loss responds.
The best linear approximation
A limit is not the most useful way to think about a derivative. Rewrite the definition as a statement about values. Near ,
On the right is a line in plus an error that vanishes faster than . This line is the best linear approximation of near . Any other slope leaves the error , which is of order . Only the tangent does better than that.
An example: at . Here , so , and the tangent predicts .
| tangent | error | ||
|---|---|---|---|
Halving divides the error by four. For a smooth function the tangent's error shrinks like .
This is the picture to keep. Up close, a differentiable function is a linear map plus a small error. In one variable the linear map is multiplication by ; in many variables it becomes a gradient or a Jacobian. For training, it is a forecast: after a small step the loss will be about .
The chain rule and backpropagation
We take the derivatives of a few functions as known. The logarithm is natural throughout:
Everything else is assembled from them by rules. Derivatives are linear, , and products follow .
A model needs one rule above all, the chain rule:
It comes from composing two linear approximations. A step in moves by . A step in moves by about . Substituting one into the other, moves by . Along a chain, slopes multiply.
The running loss is such a chain: first , then . Hence .
A neural network is a long chain of simple steps. Backpropagation is the chain rule applied in a convenient order. The forward pass computes every intermediate value up to the loss and stores it. The backward pass starts at the loss and walks back, multiplying by one local derivative per node. Each local derivative is evaluated at the values stored during the forward pass.
Sigmoid, softplus and log
Three functions appear in almost every classifier. The first is the sigmoid, which turns a score into a probability:
The chain rule with the inner function gives its derivative:
Frameworks love the second form: the backward pass reuses , already computed in the forward pass. For example, and . The derivative is largest where and tiny where is close to or . A saturated sigmoid barely passes the gradient back. This is the vanishing gradient problem in its simplest form. The same happens with , whose derivative is .
The second is the softplus, a smooth version of ReLU. Its derivative is the sigmoid:
The third is the logarithm, which sits in every likelihood-based loss. With the chain rule, : the relative rate of change of .
Taylor polynomials
The tangent is the polynomial of degree one that matches and at a point . Match more derivatives, and the approximation holds further away. The Taylor polynomial of order at is
It is the only polynomial of degree at most whose value and first derivatives at coincide with those of . The is there because differentiating times leaves exactly . is the tangent; adds curvature.
Two expansions at are worth remembering. Every derivative of is , which equals at zero:
For the -th derivative at zero is , so
Their first terms are everyday ML approximations: and for small . A likelihood that grows by raises the log-likelihood by about . Each extra order multiplies the error by about . At , where , that is one more digit per order: the errors for are , , and .
The remainder: how far a polynomial reaches
How large is the error ? Taylor's theorem answers with the remainder in Lagrange form. If has derivatives, then for some between and
For this is the mean value theorem, : somewhere between and the tangent is parallel to the chord. Higher orders repeat the argument for minus its Taylor polynomial.
We do not know . But a bound on between and bounds the error. Near the error is : each extra order adds one power of the distance.
For at and the remainder is with . So the error lies between and :
| bounds | |||
|---|---|---|---|
| to | |||
| to | |||
| to |
The factorial wins for every , so the series of converges everywhere. Not every series does. For at the polynomials give , , and , while . Beyond the terms grow, and adding them only makes things worse. A Taylor polynomial is a local statement.
Two practical payoffs. Numerical libraries compute for tiny with log1p, which uses the series instead of rounding first; expm1 does the same for . And Taylor explains why gradient checks use the central difference. Expand and , subtract, and the even-order terms cancel:
The forward difference keeps an error of order .
First and second order in optimization
Training repeats one move: expand the loss around the current parameter and step according to the expansion. The second-order expansion is
First order gives the direction. A gradient step with step size changes by about , which is negative. If the curvature is bounded, , the Lagrange remainder turns this into a guarantee. Write , take and , and bound the one unknown factor :
For the bracket is positive, and every step with lowers . A larger step can overshoot.
Second order gives the size. Set the derivative of the quadratic model, , to zero. The result is Newton's step:
It is a gradient step with : long where the function is flat, short where it curves sharply. It needs .
Second order also classifies critical points, where . If , it is a strict local minimum; if , a strict local maximum. If , the test is silent: , and all have at zero, and they have no extremum, a minimum and a maximum there.
An example: . Its derivative vanishes at , and , so this is a minimum. Gradient descent from with has errors of , , , . The ratio of successive errors is , , and tends to . To see why, subtract from the update and apply the mean value theorem to , with : for some between and , and . Newton's method roughly squares the error, so the number of correct digits doubles with each step.
All of this used one variable. A real loss depends on millions of weights. Then has to become a vector of slopes and a matrix of curvatures. That is the topic Gradients, Jacobians and Hessians.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.