Skip to content
BIASED

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 x↦Wx+bx \mapsto Wx + b. We cannot yet train it. Training changes the parameters θ\theta to lower a loss L(θ)L(\theta), and for that we need to know how the loss reacts to a small change of θ\theta. 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 ff, at xx and at x+hx + h. The line through them is a secant. Its slope is the difference quotient

f(x+h)−f(x)h.\frac{f(x + h) - f(x)}{h}.

For f(x)=x2f(x) = x^2 at x=1x = 1 the quotient equals (1+h)2−1h=2+h\frac{(1 + h)^2 - 1}{h} = 2 + h. With h=0.1h = 0.1 it is 2.12.1, with h=0.01h = 0.01 it is 2.012.01. As h→0h \to 0 the secant turns into the tangent, and its slope settles at one number. That limit is the derivative:

f′(x)=lim⁡h→0f(x+h)−f(x)h.f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}.

Here f′(1)=2f'(1) = 2.

The running example of this topic is a model with one weight that predicts y^=θx\hat y = \theta x. On a single example (x,y)=(2,3)(x, y) = (2, 3) its squared error is

L(θ)=(2θ−3)2.L(\theta) = (2\theta - 3)^2.

At θ=1\theta = 1 the loss is L(1)=1L(1) = 1. The sign of L′(θ)L'(\theta) tells us which way to move θ\theta, 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 xx,

f(x+h)=f(x)+f′(x) h+r(h),r(h)h→0 as h→0.f(x + h) = f(x) + f'(x)\,h + r(h), \qquad \frac{r(h)}{h} \to 0 \text{ as } h \to 0.

On the right is a line in hh plus an error that vanishes faster than hh. This line is the best linear approximation of ff near xx. Any other slope mm leaves the error (f′(x)−m) h+r(h)(f'(x) - m)\,h + r(h), which is of order hh. Only the tangent does better than that.

An example: f(x)=xf(x) = \sqrt x at x=4x = 4. Here f′(x)=12xf'(x) = \frac{1}{2\sqrt x}, so f′(4)=0.25f'(4) = 0.25, and the tangent predicts 4+h≈2+0.25 h\sqrt{4 + h} \approx 2 + 0.25\,h.

hh tangent 4+h\sqrt{4 + h} error
0.10.1 2.0252.025 2.0248462.024846 1.54⋅10−41.54 \cdot 10^{-4}
0.050.05 2.01252.0125 2.0124612.012461 3.88⋅10−53.88 \cdot 10^{-5}

Halving hh divides the error by four. For a smooth function the tangent's error shrinks like h2h^2.

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 f′(x)f'(x); in many variables it becomes a gradient or a Jacobian. For training, it is a forecast: after a small step hh the loss will be about L(θ)+L′(θ) hL(\theta) + L'(\theta)\,h.

The chain rule and backpropagation

We take the derivatives of a few functions as known. The logarithm is natural throughout:

(xn)′=nxn−1,(ex)′=ex,(log⁡x)′=1x.(x^n)' = n x^{n-1}, \qquad (e^x)' = e^x, \qquad (\log x)' = \frac{1}{x}.

Everything else is assembled from them by rules. Derivatives are linear, (af+bg)′=af′+bg′(af + bg)' = af' + bg', and products follow (fg)′=f′g+fg′(fg)' = f'g + fg'.

A model needs one rule above all, the chain rule:

(g∘f)′(x)=g′(f(x)) f′(x).(g \circ f)'(x) = g'\big(f(x)\big)\, f'(x).

It comes from composing two linear approximations. A step hh in xx moves u=f(x)u = f(x) by k≈f′(x) hk \approx f'(x)\,h. A step kk in uu moves gg by about g′(u) kg'(u)\,k. Substituting one into the other, gg moves by g′(f(x)) f′(x) hg'(f(x))\,f'(x)\,h. Along a chain, slopes multiply.

The running loss is such a chain: first u=2θ−3u = 2\theta - 3, then L=u2L = u^2. Hence L′(θ)=2u⋅2=4(2θ−3)L'(\theta) = 2u \cdot 2 = 4(2\theta - 3).

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 zz into a probability:

σ(z)=11+e−z.\sigma(z) = \frac{1}{1 + e^{-z}}.

The chain rule with the inner function 1+e−z1 + e^{-z} gives its derivative:

σ′(z)=e−z(1+e−z)2=σ(z)(1−σ(z)).\sigma'(z) = \frac{e^{-z}}{(1 + e^{-z})^2} = \sigma(z)\big(1 - \sigma(z)\big).

Frameworks love the second form: the backward pass reuses σ(z)\sigma(z), already computed in the forward pass. For example, σ(1)=0.7311\sigma(1) = 0.7311 and σ′(1)=0.7311⋅0.2689=0.1966\sigma'(1) = 0.7311 \cdot 0.2689 = 0.1966. The derivative is largest where σ=12\sigma = \tfrac12 and tiny where σ\sigma is close to 00 or 11. A saturated sigmoid barely passes the gradient back. This is the vanishing gradient problem in its simplest form. The same happens with tanh⁡\tanh, whose derivative is 1−tanh⁡2z1 - \tanh^2 z.

The second is the softplus, a smooth version of ReLU. Its derivative is the sigmoid:

ddzlog⁡(1+ez)=ez1+ez=1e−z+1=σ(z).\frac{d}{dz}\log(1 + e^z) = \frac{e^z}{1 + e^z} = \frac{1}{e^{-z} + 1} = \sigma(z).

The third is the logarithm, which sits in every likelihood-based loss. With the chain rule, (log⁡u)′=u′u(\log u)' = \frac{u'}{u}: the relative rate of change of uu.

Taylor polynomials

The tangent is the polynomial of degree one that matches ff and f′f' at a point aa. Match more derivatives, and the approximation holds further away. The Taylor polynomial of order nn at aa is

Tn(x)=∑k=0nf(k)(a)k! (x−a)k=f(a)+f′(a)(x−a)+f′′(a)2(x−a)2+…T_n(x) = \sum_{k=0}^{n} \frac{f^{(k)}(a)}{k!}\,(x - a)^k = f(a) + f'(a)(x - a) + \frac{f''(a)}{2}(x - a)^2 + \dots

It is the only polynomial of degree at most nn whose value and first nn derivatives at aa coincide with those of ff. The k!k! is there because differentiating (x−a)k(x - a)^k kk times leaves exactly k!k!. T1T_1 is the tangent; T2T_2 adds curvature.

Two expansions at a=0a = 0 are worth remembering. Every derivative of exe^x is exe^x, which equals 11 at zero:

ex=1+x+x22+x36+…e^x = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \dots

For log⁡(1+x)\log(1 + x) the kk-th derivative at zero is (−1)k+1(k−1)!(-1)^{k+1}(k - 1)!, so

log⁡(1+x)=x−x22+x33−…\log(1 + x) = x - \frac{x^2}{2} + \frac{x^3}{3} - \dots

Their first terms are everyday ML approximations: ex≈1+xe^x \approx 1 + x and log⁡(1+x)≈x\log(1 + x) \approx x for small xx. A likelihood that grows by 2%2\% raises the log-likelihood by about 0.020.02. Each extra order multiplies the error by about xx. At x=0.1x = 0.1, where log⁡1.1=0.0953102\log 1.1 = 0.0953102, that is one more digit per order: the errors Tn(0.1)−log⁡1.1T_n(0.1) - \log 1.1 for n=1,…,4n = 1, \dots, 4 are 4.7⋅10−34.7 \cdot 10^{-3}, −3.1⋅10−4-3.1 \cdot 10^{-4}, 2.3⋅10−52.3 \cdot 10^{-5} and −1.8⋅10−6-1.8 \cdot 10^{-6}.

The remainder: how far a polynomial reaches

How large is the error f(x)−Tn(x)f(x) - T_n(x)? Taylor's theorem answers with the remainder in Lagrange form. If ff has n+1n + 1 derivatives, then for some ξ\xi between aa and xx

f(x)=Tn(x)+f(n+1)(ξ)(n+1)! (x−a)n+1.f(x) = T_n(x) + \frac{f^{(n+1)}(\xi)}{(n+1)!}\,(x - a)^{n+1}.

For n=0n = 0 this is the mean value theorem, f(x)−f(a)=f′(ξ)(x−a)f(x) - f(a) = f'(\xi)(x - a): somewhere between aa and xx the tangent is parallel to the chord. Higher orders repeat the argument for ff minus its Taylor polynomial.

We do not know ξ\xi. But a bound on f(n+1)f^{(n+1)} between aa and xx bounds the error. Near aa the error is O((x−a)n+1)O\big((x - a)^{n+1}\big): each extra order adds one power of the distance.

For exe^x at a=0a = 0 and x=1x = 1 the remainder is eξ/(n+1)!e^\xi/(n+1)! with 0<ξ<10 < \xi < 1. So the error lies between 1/(n+1)!1/(n+1)! and e/(n+1)!e/(n+1)!:

nn Tn(1)T_n(1) e−Tn(1)e - T_n(1) bounds
11 22 0.7180.718 0.5000.500 to 1.3591.359
22 2.52.5 0.2180.218 0.1670.167 to 0.4530.453
33 2.66672.6667 0.05160.0516 0.04170.0417 to 0.1130.113

The factorial wins for every xx, so the series of exe^x converges everywhere. Not every series does. For log⁡(1+x)\log(1 + x) at x=1.8x = 1.8 the polynomials T1,…,T4T_1, \dots, T_4 give 1.81.8, 0.180.18, 2.1242.124 and −0.500-0.500, while log⁡2.8=1.030\log 2.8 = 1.030. Beyond x=1x = 1 the terms xk/kx^k/k grow, and adding them only makes things worse. A Taylor polynomial is a local statement.

Two practical payoffs. Numerical libraries compute log⁡(1+x)\log(1 + x) for tiny xx with log1p, which uses the series instead of rounding 1+x1 + x first; expm1 does the same for ex−1e^x - 1. And Taylor explains why gradient checks use the central difference. Expand f(x+h)f(x + h) and f(x−h)f(x - h), subtract, and the even-order terms cancel:

f(x+h)−f(x−h)2h=f′(x)+f′′′(ξ)6 h2.\frac{f(x + h) - f(x - h)}{2h} = f'(x) + \frac{f'''(\xi)}{6}\,h^2.

The forward difference f(x+h)−f(x)h\frac{f(x + h) - f(x)}{h} keeps an error of order hh.

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

f(x+h)≈f(x)+f′(x) h+12f′′(x) h2.f(x + h) \approx f(x) + f'(x)\,h + \tfrac12 f''(x)\,h^2.

First order gives the direction. A gradient step h=−ηf′(x)h = -\eta f'(x) with step size η\eta changes ff by about −ηf′(x)2-\eta f'(x)^2, which is negative. If the curvature is bounded, ∣f′′∣≤M\lvert f'' \rvert \le M, the Lagrange remainder turns this into a guarantee. Write g=f′(x)g = f'(x), take n=1n = 1 and h=−ηgh = -\eta g, and bound the one unknown factor f′′(ξ)≤Mf''(\xi) \le M:

f(x−ηg)=f(x)−ηg2+12f′′(ξ) η2g2≤f(x)−η(1−Mη2)g2.f(x - \eta g) = f(x) - \eta g^2 + \tfrac12 f''(\xi)\,\eta^2 g^2 \le f(x) - \eta\Big(1 - \frac{M\eta}{2}\Big) g^2.

For 0<η<2/M0 < \eta < 2/M the bracket is positive, and every step with f′(x)≠0f'(x) \ne 0 lowers ff. A larger step can overshoot.

Second order gives the size. Set the derivative of the quadratic model, f′(x)+f′′(x) hf'(x) + f''(x)\,h, to zero. The result is Newton's step:

h=−f′(x)f′′(x).h = -\frac{f'(x)}{f''(x)}.

It is a gradient step with η=1/f′′(x)\eta = 1/f''(x): long where the function is flat, short where it curves sharply. It needs f′′(x)>0f''(x) > 0.

Second order also classifies critical points, where f′(x∗)=0f'(x^*) = 0. If f′′(x∗)>0f''(x^*) > 0, it is a strict local minimum; if f′′(x∗)<0f''(x^*) < 0, a strict local maximum. If f′′(x∗)=0f''(x^*) = 0, the test is silent: x3x^3, x4x^4 and −x4-x^4 all have f′′=0f'' = 0 at zero, and they have no extremum, a minimum and a maximum there.

An example: f(x)=ex−2xf(x) = e^x - 2x. Its derivative ex−2e^x - 2 vanishes at x∗=log⁡2≈0.693x^* = \log 2 \approx 0.693, and f′′(x∗)=2>0f''(x^*) = 2 > 0, so this is a minimum. Gradient descent from x0=0x_0 = 0 with η=0.2\eta = 0.2 has errors xk−x∗x_k - x^* of −0.693-0.693, −0.493-0.493, −0.337-0.337, −0.223-0.223. The ratio of successive errors is 0.710.71, 0.680.68, 0.660.66 and tends to 1−ηf′′(x∗)=0.61 - \eta f''(x^*) = 0.6. To see why, subtract x∗x^* from the update and apply the mean value theorem to f′f', with f′(x∗)=0f'(x^*) = 0: xk+1−x∗=(1−ηf′′(ξk))(xk−x∗)x_{k+1} - x^* = \big(1 - \eta f''(\xi_k)\big)(x_k - x^*) for some ξk\xi_k between xkx_k and x∗x^*, and f′′(ξk)→f′′(x∗)f''(\xi_k) \to f''(x^*). 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 f′(x)f'(x) has to become a vector of slopes and f′′(x)f''(x) 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.