TExcelTAD
Home › Math for Machine Learning › Derivatives and Taylor Series

Derivatives and Taylor Series

Training a model means changing its parameters a little so the loss goes down. "A little" is the language of derivatives: the derivative says how fast a function changes, and the Taylor series says how a function behaves near a point, using its derivatives. Gradient descent is a first-order Taylor argument; Newton's method is a second-order one.

The derivative

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

The rules you need for ML (full treatment in Calculus Essentials):

Rule Formula
sum (f+g)′=f′+g′(f + g)' = f' + g'
product (fg)′=f′g+fg′(fg)' = f'g + fg'
quotient (f/g)′=(f′g−fg′)/g2(f/g)' = (f'g - fg')/g^2
chain (g∘f)′(x)=g′(f(x)) f′(x)(g \circ f)'(x) = g'(f(x))\,f'(x)

And the derivatives that appear in every loss function: (xn)′=nxn−1(x^n)' = nx^{n-1}, (ex)′=ex(e^x)' = e^x, (log⁡x)′=1/x(\log x)' = 1/x, and for the sigmoid σ(x)=1/(1+e−x)\sigma(x) = 1/(1 + e^{-x}),

σ′(x)=σ(x) (1−σ(x))\sigma'(x) = \sigma(x)\,(1 - \sigma(x))
python · try it yourself
import numpy as np

def sigmoid(x): return 1 / (1 + np.exp(-x))
x = np.array([-2.0, 0.0, 1.5])
h = 1e-5
numeric = (sigmoid(x + h) - sigmoid(x - h)) / (2 * h)       # central difference
exact = sigmoid(x) * (1 - sigmoid(x))
print("numeric:", np.round(numeric, 8))
print("exact:  ", np.round(exact, 8))
numeric: [0.10499359 0.25       0.14914645]
exact:   [0.10499359 0.25       0.14914645]

Taylor polynomials

Near a point x0x_0, a smooth function is approximated by the polynomial that matches its first nn derivatives:

Tn(x)=∑k=0nf(k)(x0)k!(x−x0)k=f(x0)+f′(x0)(x−x0)+f′′(x0)2(x−x0)2+⋯T_n(x) = \sum_{k=0}^{n} \frac{f^{(k)}(x_0)}{k!}(x - x_0)^k = f(x_0) + f'(x_0)(x - x_0) + \frac{f''(x_0)}{2}(x - x_0)^2 + \cdots

T1T_1 is the tangent line (the linearization), T2T_2 adds curvature. The error of TnT_n shrinks like (x−x0)n+1(x - x_0)^{n+1} as x→x0x \to x_0.

python · try it yourself
import numpy as np

f = np.exp
x0 = 0.0
for x in (0.1, 0.5, 1.0):
    T1 = 1 + x
    T2 = 1 + x + x**2 / 2
    T4 = T2 + x**3 / 6 + x**4 / 24
    print(f"x={x}: exp={f(x):.6f}  err T1={abs(f(x)-T1):.2e}  T2={abs(f(x)-T2):.2e}  T4={abs(f(x)-T4):.2e}")
x=0.1: exp=1.105171  err T1=5.17e-03  T2=1.71e-04  T4=8.47e-08
x=0.5: exp=1.648721  err T1=1.49e-01  T2=2.37e-02  T4=2.84e-04
x=1.0: exp=2.718282  err T1=7.18e-01  T2=2.18e-01  T4=9.95e-03

Close to x0x_0 even the low-order polynomials are excellent; farther away you need more terms.

Why this matters for optimization

At a parameter value θ\theta, the first-order Taylor expansion of a loss is

L(θ+Δ)≈L(θ)+L′(θ) ΔL(\theta + \Delta) \approx L(\theta) + L'(\theta)\,\Delta

Choosing Δ=−ηL′(θ)\Delta = -\eta L'(\theta) with a small step η>0\eta > 0 gives L(θ+Δ)≈L(θ)−ηL′(θ)2≤L(θ)L(\theta + \Delta) \approx L(\theta) - \eta L'(\theta)^2 \le L(\theta): the loss goes down. That is gradient descent in one dimension. The second-order expansion L(θ)+L′Δ+12L′′Δ2L(\theta) + L'\Delta + \frac{1}{2}L''\Delta^2 is minimized at Δ=−L′/L′′\Delta = -L'/L'', which is Newton's method (Hessians and Multivariate Taylor Series).

In software

  • Libraries implement log⁡(1+x)\log(1 + x) and ex−1e^x - 1 as np.log1p and np.expm1, because near x=0x = 0 the Taylor series shows that the naive formulas lose all precision.
  • The GELU activation used in transformers is often computed with a tanh approximation derived from series expansions.
  • Every optimizer step is justified by a local Taylor model of the loss.

Exercises

Exercise 1: Chain rule

Differentiate f(x)=log⁡(1+e2x)f(x) = \log(1 + e^{2x}).

Answer

f′(x)=2e2x1+e2x=2σ(2x)f'(x) = \frac{2e^{2x}}{1 + e^{2x}} = 2\sigma(2x).

Solution

Outer function log⁡u\log u with u=1+e2xu = 1 + e^{2x}, so f′=1u⋅u′f' = \frac{1}{u} \cdot u' and u′=2e2xu' = 2e^{2x}. Dividing numerator and denominator by e2xe^{2x} gives 21+e−2x=2σ(2x)\frac{2}{1 + e^{-2x}} = 2\sigma(2x).

Exercise 2: A Taylor polynomial

Find the second-order Taylor polynomial of f(x)=log⁡xf(x) = \log x at x0=1x_0 = 1 and use it to estimate log⁡1.1\log 1.1.

Answer

T2(x)=(x−1)−12(x−1)2T_2(x) = (x - 1) - \frac{1}{2}(x - 1)^2; log⁡1.1≈0.095\log 1.1 \approx 0.095.

Solution

f(1)=0f(1) = 0, f′(1)=1f'(1) = 1, f′′(1)=−1f''(1) = -1. So T2(1.1)=0.1−0.005=0.095T_2(1.1) = 0.1 - 0.005 = 0.095, close to the true 0.095310.09531.

Exercise 3: Why log1p (code)

Compare np.log(1 + x) and np.log1p(x) for x=10−17x = 10^{-17}.

Answer

np.log(1 + x) returns 0; np.log1p(x) returns 10−1710^{-17}.

Solution

1+10−171 + 10^{-17} rounds to exactly 1 in double precision, so the log is 0. The Taylor series log⁡(1+x)=x−x2/2+⋯\log(1 + x) = x - x^2/2 + \cdots shows the true value is essentially xx, which log1p computes without forming 1+x1 + x.

python · try it yourself
import numpy as np
x = 1e-17
print(np.log(1 + x), np.log1p(x))
0.0 1e-17

Test yourself

The derivative of the sigmoid σ(x)\sigma(x) is…

  1. σ(x)2\sigma(x)^2
  2. σ(x)(1−σ(x))\sigma(x)(1 - \sigma(x))
  3. 1−σ(x)1 - \sigma(x)
  4. e−xe^{-x}

The first-order Taylor polynomial of ff at x0x_0 is…

  1. f(x0)f(x_0)
  2. the tangent line f(x0)+f′(x0)(x−x0)f(x_0) + f'(x_0)(x - x_0)
  3. f′(x0)f'(x_0)
  4. a parabola

With a small step Δ=−ηL′(θ)\Delta = -\eta L'(\theta), the first-order Taylor model predicts the loss…

  1. increases
  2. decreases by about ηL′(θ)2\eta L'(\theta)^2
  3. stays exactly the same
  4. becomes zero