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
The rules you need for ML (full treatment in Calculus Essentials):
| Rule | Formula |
|---|---|
| sum | |
| product | |
| quotient | |
| chain |
And the derivatives that appear in every loss function: , , , and for the sigmoid ,
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 , a smooth function is approximated by the polynomial that matches its first derivatives:
is the tangent line (the linearization), adds curvature. The error of shrinks like as .
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 even the low-order polynomials are excellent; farther away you need more terms.
Why this matters for optimization
At a parameter value , the first-order Taylor expansion of a loss is
Choosing with a small step gives : the loss goes down. That is gradient descent in one dimension. The second-order expansion is minimized at , which is Newton's method (Hessians and Multivariate Taylor Series).
In software
- Libraries implement and as
np.log1pandnp.expm1, because near 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 .
Answer
.
Solution
Outer function with , so and . Dividing numerator and denominator by gives .
Exercise 2: A Taylor polynomial
Find the second-order Taylor polynomial of at and use it to estimate .
Answer
; .
Solution
, , . So , close to the true .
Exercise 3: Why log1p (code)
Compare np.log(1 + x) and np.log1p(x) for .
Answer
np.log(1 + x) returns 0; np.log1p(x) returns .
Solution
rounds to exactly 1 in double precision, so the log is 0. The Taylor series shows the true value is essentially , which log1p computes without forming .
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 is…
The first-order Taylor polynomial of at is…
- the tangent line
- a parabola
With a small step , the first-order Taylor model predicts the loss…
- increases
- decreases by about
- stays exactly the same
- becomes zero