AI State of the Art
Math/Differential and Integral Calculus/13 min
Section 323

Taylor Series and Approximations

Listen as a podcast β†’

The brilliant idea: replace the complex with the simple

Imagine you need to compute sin⁑(0.1)\sin(0.1) without a calculator. Sine is a complicated function. But near zero, you know that sin⁑(x)β‰ˆx\sin(x) \approx x. So sin⁑(0.1)β‰ˆ0.1\sin(0.1) \approx 0.1. The true value is 0.09983…0.09983\ldots, the approximation is excellent.

This is the fundamental idea of Taylor series: approximate a complicated function with a polynomial, which is much easier to compute and analyze. The more terms you add to the polynomial, the better the approximation.

The Taylor formula

The Taylor series of ff around the point aa is:

f(x)=f(a)+fβ€²(a)(xβˆ’a)+fβ€²β€²(a)2!(xβˆ’a)2+fβ€²β€²β€²(a)3!(xβˆ’a)3+β‹―f(x) = f(a) + f'(a)(x - a) + \frac{f''(a)}{2!}(x - a)^2 + \frac{f'''(a)}{3!}(x - a)^3 + \cdots

Or compactly:

f(x)=βˆ‘k=0∞f(k)(a)k!(xβˆ’a)kf(x) = \sum_{k=0}^{\infty} \frac{f^{(k)}(a)}{k!}(x - a)^k

Each term adds a level of precision:

  • Order 0: f(x)β‰ˆf(a)f(x) \approx f(a), you assume the function is constant. Very rough.
  • Order 1: f(x)β‰ˆf(a)+fβ€²(a)(xβˆ’a)f(x) \approx f(a) + f'(a)(x - a), you follow the slope. It's a tangent line.
  • Order 2: you add curvature. It's a parabola that hugs the curve.
  • Higher orders: you capture finer and finer details of the shape of ff.
Graph of the sine function in red, together with its Taylor polynomials at zero: P1 is a green straight line, P3 a blue curve, P5 an orange curve and P7 a grey curve. All pass through the origin and track the sine over an interval that grows with their degree, then peel away sharply.
Sine (red) and its Taylor approximations around zero. P1 is just the tangent line: it only matches near the origin. Each extra order hugs the curve over a wider interval, but every one of them eventually diverges far from the expansion point, which is exactly why a gradient descent step has to stay small.Source : Wikimedia Commons

Classic examples

The exponential around a=0a = 0:

ex=1+x+x22+x36+β‹―e^x = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \cdots

The cosine around a=0a = 0:

cos⁑(x)=1βˆ’x22+x424βˆ’β‹―\cos(x) = 1 - \frac{x^2}{2} + \frac{x^4}{24} - \cdots

The logarithm around a=1a = 1:

ln⁑(x)=(xβˆ’1)βˆ’(xβˆ’1)22+(xβˆ’1)33βˆ’β‹―\ln(x) = (x - 1) - \frac{(x - 1)^2}{2} + \frac{(x - 1)^3}{3} - \cdots

These expansions aren't just mathematical curiosities. They are at the heart of many ML algorithms.

The linear approximation: it's gradient descent

The first-order Taylor approximation is the most important in ML. If we expand the loss L\mathcal{L} around the current parameters ΞΈ\theta:

L(ΞΈ+Δθ)β‰ˆL(ΞΈ)+βˆ‡L(ΞΈ)T⋅Δθ\mathcal{L}(\theta + \Delta\theta) \approx \mathcal{L}(\theta) + \nabla \mathcal{L}(\theta)^T \cdot \Delta\theta

This formula says: the new loss is approximately equal to the old one, plus the dot product between the gradient and the displacement. To minimize the loss, we want this product to be as negative as possible. The choice Δθ=βˆ’Ξ·βˆ‡L(ΞΈ)\Delta\theta = -\eta \nabla \mathcal{L}(\theta) gives:

L(ΞΈβˆ’Ξ·βˆ‡L)β‰ˆL(ΞΈ)βˆ’Ξ·βˆ₯βˆ‡Lβˆ₯2\mathcal{L}(\theta - \eta \nabla \mathcal{L}) \approx \mathcal{L}(\theta) - \eta \|\nabla \mathcal{L}\|^2

The term βˆ₯βˆ‡Lβˆ₯2\|\nabla \mathcal{L}\|^2 is always positive, so the loss decreases at each step (as long as Ξ·\eta is small enough for the linear approximation to remain valid). This is the theoretical guarantee of gradient descent: locally, you always descend.

Why the learning rate must be small

The linear approximation is only valid locally, when Δθ\Delta\theta is small. If you take a step that's too large, you leave the zone where the tangent line well approximates the curve. That's exactly the role of the learning rate Ξ·\eta: controlling the step size to stay within the validity zone of the first-order Taylor approximation.

The quadratic approximation: Newton's method

If we add the second-order term, we get:

L(ΞΈ+Δθ)β‰ˆL(ΞΈ)+βˆ‡L(ΞΈ)T⋅Δθ+12ΔθTβ‹…H⋅Δθ\mathcal{L}(\theta + \Delta\theta) \approx \mathcal{L}(\theta) + \nabla \mathcal{L}(\theta)^T \cdot \Delta\theta + \frac{1}{2} \Delta\theta^T \cdot H \cdot \Delta\theta

Where HH is the Hessian matrix (second derivatives). This approximation is a parabola (a bowl in nn dimensions). You can analytically find the minimum of this parabola by setting the gradient to zero:

Ξ”ΞΈβˆ—=βˆ’Hβˆ’1βˆ‡L(ΞΈ)\Delta\theta^* = -H^{-1} \nabla \mathcal{L}(\theta)

This is Newton's method. Instead of following the gradient with a fixed step, you jump directly to the minimum of the quadratic approximation. Advantages and disadvantages:

  • Advantage: quadratic convergence, each iteration doubles the number of correct digits. Extremely fast near the minimum.
  • Major disadvantage: you need to compute and invert the Hessian. For nn parameters, that's O(n3)O(n^3). With a billion parameters, it's completely infeasible.
Four plots. Top left, the gradient descent trajectory over the contour lines of a quadratic function: many small zigzagging steps towards the minimum. Top right, the distance to the minimum on a log scale decreases linearly over 100 iterations. Bottom left, the Newton trajectory: three or four large steps reach the minimum directly. Bottom right, the distance to the minimum collapses from 1 to 10 to the power minus 20 in six iterations.
The same quadratic optimised by both methods. On top, gradient descent inches forward and its distance to the minimum shrinks steadily. Below, Newton uses curvature to jump almost straight to the minimum: six iterations buy twenty decimal digits. The price is inverting the Hessian at every step.Source : CS4780 lecture notes, Cornell University

The first-order vs second-order dilemma

PropertyGradient descent (order 1)Newton (order 2)
Information usedGradient (slope)Gradient + Hessian (slope + curvature)
Cost per iterationO(n)O(n)O(n3)O(n^3)
Number of iterationsHighVery low
Feasible in DL?YesNo (too expensive)

That's why the vast majority of deep learning uses first-order methods (SGD, Adam). The computational cost per iteration matters much more than theoretical convergence speed.

Modern optimizers: between first and second order

The best current optimizers use Taylor information cleverly: they approximate certain aspects of second order without paying the full cost.

Adam: the diagonal approximation

Adam maintains two running averages for each parameter:

  • mtm_t: the running average of the gradient (first moment estimate)
  • vtv_t: the running average of the squared gradient (second moment estimate)

The update is:

ΞΈt+1=ΞΈtβˆ’Ξ·β‹…mtvt+Ο΅\theta_{t+1} = \theta_t - \eta \cdot \frac{m_t}{\sqrt{v_t} + \epsilon}

The vt\sqrt{v_t} term plays the role of an approximation of the square root of curvature in each direction. If the gradient oscillates a lot in a direction (high variance, strong curvature), vtv_t is large and the effective step is small. If the gradient is stable (low variance, weak curvature), the step is larger.

Adam doesn't compute the Hessian, but it captures its essence: adapting the learning rate to the local geometry of the loss. It's a remarkable compromise between the simplicity of first order and the intelligence of second order.

AdaGrad and RMSProp

  • AdaGrad: accumulates the square of all past gradients. The effective learning rate decreases over time for each parameter. Problem: it can become too small too fast.
  • RMSProp: uses an exponential moving average instead of a sum, which avoids AdaGrad's problem. It's one of Adam's two ingredients (the other being momentum).

Beyond second order

Higher-order terms (fβ€²β€²β€²(a)6(xβˆ’a)3\frac{f'''(a)}{6}(x-a)^3, etc.) are rarely used in ML. The reason is simple: the cost explodes. If the Hessian has n2n^2 elements, the tensor of third derivatives has n3n^3. For a billion-parameter model, that's 102710^{27} numbers. Absolutely unthinkable.

However, approximations exist:

  • L-BFGS: approximates the inverse Hessian using the last mm gradients (typically m=10m = 10 to 2020). Used for small models or fine-tuning.
  • K-FAC: approximates the Hessian by a Kronecker product of smaller matrices, exploiting the layer structure of networks.
  • Shampoo: an optimizer developed at Google that uses block approximations of the Hessian.

Connection to regularization

The Taylor approximation also sheds light on L2 regularization (weight decay). If we expand the loss around ΞΈβˆ—\theta^* (the minimum without regularization):

L(ΞΈ)β‰ˆL(ΞΈβˆ—)+12(ΞΈβˆ’ΞΈβˆ—)TH(ΞΈβˆ’ΞΈβˆ—)\mathcal{L}(\theta) \approx \mathcal{L}(\theta^*) + \frac{1}{2}(\theta - \theta^*)^T H (\theta - \theta^*)

Adding a regularization term Ξ»2βˆ₯ΞΈβˆ₯2\frac{\lambda}{2}\|\theta\|^2 amounts to shifting the minimum toward the origin. The directions of low curvature (small eigenvalues of HH) are most affected: regularization pushes them toward zero. This eliminates directions where the model is "uncertain," which reduces overfitting.

Key takeaways

  • Taylor series approximate a function with a polynomial of increasing degree
  • The first-order approximation is the theoretical foundation of gradient descent: you minimize a local linear model of the loss
  • The second-order approximation (Newton) converges faster but costs O(n3)O(n^3), impractical in DL
  • Adam is a smart compromise: it approximates the diagonal elements of the Hessian from gradient variance, without the cost of second order
  • The learning rate must be small enough to stay within the validity zone of the linear approximation
  • L2 regularization is interpreted as a shift of the minimum in parameter space, governed by the curvature of the loss

Where this is used. Every modern optimizer is a Taylor expansion truncated at first or second order.