AI from scratch · Calculus 3 · Foundations

Taylor series and curvature

The derivative says which way is downhill. It doesn't say how far you can go before downhill turns into uphill. That's the second derivative's job: curvature. This lesson builds functions out of polynomials, uses that to explain exactly when gradient descent works and when a learning rate makes it explode, and shows why stretched valleys make training zigzag and why the obvious fix, Newton's method, is out of reach for neural networks.

1. Any smooth function is a polynomial, up close

Calculus 1 replaced a curve by its tangent line near a point \(c\). Why stop at a line? Match the value, the slope, the curvature, and so on, one derivative per term:

\[ f(x) \approx f(c) + f'(c)(x - c) + \frac{f''(c)}{2!}(x - c)^2 + \frac{f'''(c)}{3!}(x - c)^3 + \dots \]

Two pieces of notation: \(f''\) is the derivative of the derivative (the slope of the slope, the second derivative: how the curve bends), \(f'''\) the third, and \(f^{(k)}\) the \(k\)-th. And \(k!\), "k factorial", is \(1 \times 2 \times \dots \times k\): \(2! = 2\), \(3! = 6\), \(4! = 24\).

f its Taylor polynomial

Order 0 is a flat line, order 1 the tangent, order 2 a parabola that also bends the right way. Each new term hugs the curve over a wider stretch. For \(e^x\) and \(\sin x\) adding terms eventually works everywhere; for \(\ln(1+x)\) around 0 it only helps for \(x\) between −1 and 1, and beyond that more terms make it worse. Try it.

Using the order-2 polynomial of \(e^x\) at \(c = 0\), that is \(1 + x + x^2/2\), estimate \(e^{0.1}\).

\(1 + 0.1 + 0.005 = 1.105\). The true value is 1.10517: the next term, \(0.1^3/6 \approx 0.00017\), is all that's missing. Near the center, each term is much smaller than the last.

Near any point, a smooth function is its value, plus slope times the step, plus half the curvature times the step squared, plus terms that shrink fast. ML lives on the first two.

Why divide by those factorials?
  1. We want a polynomial whose value, slope, curvature and so on match \(f\)'s at \(c\): one coefficient per derivative.
  2. Differentiate \((x - c)^k\) \(k\) times: the power rule brings down \(k\), then \(k - 1\), …, then 1, leaving the constant \(k!\). Every other term vanishes at \(x = c\): lower powers were differentiated away, higher ones still contain a factor \((x - c)\), which is 0 there.
  3. So the \(k\)-th derivative of \(a_k (x - c)^k\) at \(c\) is \(a_k\, k!\). To make it equal \(f^{(k)}(c)\), take \(a_k = f^{(k)}(c) / k!\).

The factorials also explain why the terms shrink so fast near \(c\): \(8! = 40{,}320\), so the order-8 term of \(e^x\) at \(x = 1\) is only 0.000025.

Where it breaks: when more terms stop helping

ML lives almost entirely on the first two orders. The loss near the current weights is a slope plus a curvature: \(L(w + \Delta) \approx L(w) + L'(w)\,\Delta + \tfrac12 L''(w)\,\Delta^2\). The next sections are this one line put to work.

2. Why gradient descent works, and when it blows up

Gradient descent steps \(\Delta = -\eta L'\). Put that into the second-order picture:

\[ L(w - \eta L') \approx L - \eta L'^2 + \tfrac12 L'' \eta^2 L'^2 = L - \eta L'^2 \left(1 - \tfrac12 \eta L''\right) \]

For small \(\eta\) the loss drops: that's the guarantee. But the bracket turns negative once \(\eta > 2/L''\): the step overshoots so far that the far side of the valley is higher than where you started. The cleanest case is the parabola \(L = \tfrac12 k w^2\), with slope \(kw\) and curvature \(k\) everywhere. One step does \(w \leftarrow w - \eta k w = (1 - \eta k)\,w\). Each step multiplies the distance to the bottom by \(1 - \eta k\):

Below \(\eta = 1/k\) the ball creeps down one side. At exactly \(1/k\) it lands on the bottom in one step. Between \(1/k\) and \(2/k\) it overshoots and bounces, but each bounce is smaller. Past \(2/k\) every bounce is bigger: the loss goes to infinity, the "loss is NaN" you'll see in real training. A real network has different curvatures in different directions, and the steepest one sets the limit for all of them.

A step is safe as long as the curvature can't turn downhill into uphill before it lands: the learning rate must stay under 2 divided by the curvature.

The curvature is \(k = 4\). Below what learning rate does gradient descent converge?

\(|1 - 4\eta| < 1\) means \(0 < \eta < 0.5\), that is \(2/k\). Set k = 4 above and slide η across 0.5.

3. Stretched valleys: the Hessian and the condition number

With many weights, the curvature becomes a matrix of second partials, the Hessian \(H_{ij} = \partial^2 L / \partial w_i \partial w_j\) (it's the Jacobian of the gradient, from Calculus 2). Take the simplest valley, \(L = \tfrac12(b x^2 + a y^2)\): the Hessian is \(\begin{pmatrix} b & 0 \\ 0 & a \end{pmatrix}\), gentle along the valley (\(b = 1\), in \(x\)) and steep across it (\(a\), in \(y\)). The ratio \(\kappa = a/b\), the largest curvature over the smallest, is the condition number.

gradient descent Newton

Section 2's rule now applies per direction: η must stay under \(2/a\) for the steep direction, so the gentle direction shrinks by only \(1 - \eta b\) per step, under \(1 - 2/\kappa\). The steps needed grow in proportion to \(\kappa\). Gradient descent zigzags across the valley while barely moving along it. Real networks have condition numbers in the thousands and up, which is why plain gradient descent is slow and why optimizers like momentum and Adam exist (stage 4).

This valley's axes happen to be \(x\) and \(y\). A real one is tilted, but its main axes are still easy to find: the Hessian is symmetric, so its eigenvectors (Linear algebra 2) are at right angles to each other, and they are the valley's main axes. Each eigenvalue is the curvature along its axis, so the condition number is the largest eigenvalue over the smallest. Standardizing features (Probability 2) pushes those eigenvalues toward equal, which is why scaled inputs train faster.

With many weights there are many curvatures. One learning rate must suit the steepest, so the gentlest crawls, and the condition number counts how much slower.

A loss valley's Hessian has eigenvalues 100 and 1. Using the biggest learning rate that's still stable, roughly how many steps does it take to shrink the error along the flat direction by a factor of \(e\)?

The rate must be below \(2/100\); take about \(1/100\). Along the flat direction (curvature 1) each step then shrinks the error by a factor of about \(1 - 1/100\), so it takes about 100 steps per factor of \(e\): the condition number. Fix the scaling and both directions converge in a step or two.

Newton's method fixes it outright: step by \(-H^{-1}\nabla L\) instead of \(-\eta \nabla L\). Dividing by the curvature makes every direction equally easy; on a quadratic it lands on the minimum in one step, whatever \(\kappa\) is. Tick the box.

So why don't we train networks with Newton's method? A model has \(10^9\) parameters. How many entries does its Hessian have?

\(10^9 \times 10^9 = 10^{18}\): eight exabytes in 64-bit floats, before you even try to invert it (about \(n^3 = 10^{27}\) operations). Optimizers settle for cheap hints of curvature instead, like Adam's per-weight running average of squared gradients.

Away from a toy, Newton's method shows its real strength: near a minimum, the number of correct digits roughly doubles each step. Here it is against gradient descent on \(f(x) = e^x - 2x\), whose minimum is at \(x = \ln 2\) (where \(f' = e^x - 2 = 0\)):

Gradient descent's error shrinks by a fixed factor each step. Newton's error is roughly squared each step: 0.04, 0.0009, \(4 \times 10^{-7}\), \(8 \times 10^{-14}\), then exact to the last digit.

4. Minimum, maximum or saddle

Where the gradient is zero, the Hessian's eigenvalues (the curvatures along its main axes, section 3, from Linear algebra 2's eigenvectors) decide what you're standing on:

eigenvaluespointpicture
all positiveminimuma bowl: up in every direction
all negativemaximuma hilltop
mixed signssaddlea mountain pass: up along the ridge, down across it

At a point where the gradient is zero, the Hessian has eigenvalues 3 and −2. What is it?

Curving up one way (3) and down the other (−2) is a saddle, like \(1.5x^2 - y^2\).

In a million dimensions, a minimum needs a million curvatures to all come out positive. If signs were coin flips, that's \(2^{-1{,}000{,}000}\). So almost every flat spot a big network meets is a saddle, not a bad local minimum, and gradient noise is usually enough to slide off it. Random symmetric matrices show the trend:

5. What to carry forward

All chapters: AI from scratch

Last updated