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}\).
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?
- We want a polynomial whose value, slope, curvature and so on match \(f\)'s at \(c\): one coefficient per derivative.
- 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.
- 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
- A wall nearby. \(\ln(1 + x)\) shoots to minus infinity at \(x = -1\), a distance 1 from the center 0, and its Taylor series can't reach further than that distance in either direction: past \(x = 1\) it gets worse with every term. A series around \(c\) only works out to the nearest place where the function misbehaves.
- Kinks. At a point just left of ReLU's kink, every derivative of order 2 and up is 0, so the series predicts a straight line forever and misses the bend.
- "Near the current weights". Sections 2 and 3 trust a second-order picture of the loss. It's only accurate close to where it was measured, which is one reason training often starts with a small learning rate and raises it (warmup): early on, the landscape is least like the local picture.
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?
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\)?
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?
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:
| eigenvalues | point | picture |
|---|---|---|
| all positive | minimum | a bowl: up in every direction |
| all negative | maximum | a hilltop |
| mixed signs | saddle | a 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?
- A minimum
- A maximum
- A saddle
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
- Taylor: near a point, a smooth function is its value + slope × Δ + ½ curvature × Δ² + …; ML mostly uses the first two orders.
- Learning rate limit: gradient descent is stable only for \(\eta < 2/\lambda_{\max}\), where \(\lambda_{\max}\) is the steepest curvature. Past it, the loss explodes.
- Condition number \(\kappa = \lambda_{\max}/\lambda_{\min}\): the bigger it is, the more gradient descent zigzags, and the steps needed grow like \(\kappa\).
- Newton divides by curvature and converges fast, but a Hessian with \(n^2\) entries is out of reach for big models; practical optimizers approximate it.
- Saddles, not local minima, are the typical flat spots in high dimensions.
All chapters: AI from scratch
Last updated