Maths toolkit9 / 17

Lesson 9 · Calculus

Zooming in: linear and quadratic approximation (Taylor)

The problem

A model learns by changing its weights to lower its error. The usual method is gradient descent. At each step it looks at the slope where it stands, and takes a small step downhill.

The slope only describes the curve at one point. So why should a step based on it work? And how small is "small"?

This chapter answers both questions by zooming in. Near a point, the curve looks like its tangent line, and gradient descent trusts that line. A bit further out, the line drifts away from the curve, and a parabola fits better. The parabola tells us how far the line can be trusted.

Our example: a curve with two valleys

We need a curve to zoom in on. The pizza error from the last few chapters has one valley and no bumps (we'll come back to it). Here we'll use a made-up error curve with two valleys:

E(w)=(w2−4)24+w2+2E(w) = \frac{(w^2 - 4)^2}{4} + \frac{w}{2} + 2

This curve is smooth: it has no corners or jumps. Think of ww as one weight of a bigger model. Models like neural networks often have bumpy error curves like this one. You don't need to remember the formula. You need three facts about the point w=2w = 2:

  • the error is E(2)=3E(2) = 3
  • the slope is E′(2)=0.5E'(2) = 0.5, using E′(w)=w3−4w+0.5E'(w) = w^3 - 4w + 0.5
  • right now the slope is growing at a rate of 88 per unit of ww (more on this soon)

Zoom in: the tangent line

The tangent line at w=2w = 2 starts at the point (2,3)(2, 3) and has slope 0.50.5. If you move a distance hh along the ww axis, the line predicts:

E(2+h)≈3+0.5 hE(2 + h) \approx 3 + 0.5\,h

This is the linear approximation. In general, near a point aa:

f(a+h)≈f(a)+f′(a) hf(a + h) \approx f(a) + f'(a)\,h

Read it as: start at the curve's height, then add slope × distance moved.

Predict first

At w = 2 the curve bends upwards, like the inside of a bowl. Move h = 0.2. The line predicts 3.1. Is the true error above or below 3.1?

A bit further out: the tangent parabola

The line knows the height and the slope. It doesn't know that the slope is changing. We can add that.

The slope of the slope is the second derivative, written f′′f''. For our curve, take the derivative of E′(w)=w3−4w+0.5E'(w) = w^3 - 4w + 0.5:

E′′(w)=3w2−4E''(w) = 3w^2 - 4

At w=2w = 2, this gives E′′(2)=12−4=8E''(2) = 12 - 4 = 8. Right now the slope is growing at 8 per unit. Move 0.1 right and it grows by about 0.8: from 0.5 to about 1.3. The true slope there, E′(2.1)=1.361E'(2.1) = 1.361, is close.

The figure below plots the slope as a curve of its own. The steepness of that curve is the second derivative.

−3−2−101230246810weight wE(w)−3−2−10123−15−10−5051015weight wslope E′(w)

Slope E′ = 0.5 · slope of the slope E″ = 8

Move 0.1 right and the slope changes by about 0.8. The curve bends up here.

Top: the error curve and its tangent. Bottom: the slope E′ as its own curve. The steepness of the bottom tangent is E″, the slope of the slope.

The second derivative measures the curvature of the curve:

  • Positive: the curve bends up, like a bowl. At w=2w = 2, E′′=8E'' = 8.
  • Negative: the curve bends down, like a hill top. At w=0w = 0, E′′=−4E'' = -4.
  • Size: a bigger number means a sharper bend.

Adding a curvature term to the line gives the quadratic approximation:

f(a+h)≈f(a)+f′(a) h+12f′′(a) h2f(a + h) \approx f(a) + f'(a)\,h + \tfrac12 f''(a)\,h^2

This parabola matches the curve's height, slope and curvature at aa. Why the 12\tfrac12? The second derivative of 12f′′(a)h2\tfrac12 f''(a) h^2 is f′′(a)f''(a), so the half makes the parabola's curvature come out right.

Worked example

Predict E(2 + h) at w = 2, for h = 0.1 and h = 0.2

  1. Collect the three numbers at w=2w = 2: height 33, slope 0.50.5, curvature 88. So the line is 3+0.5h3 + 0.5h and the parabola is 3+0.5h+4h23 + 0.5h + 4h^2.

Now try it yourself. Drag the big dot to change the point aa, and move the slider to change hh. The bars along the bottom show where each approximation stays within 0.25 of the curve.

−3−2−101230246810weight werror E(w)

Drag the big dot to move a. At a = 2: E = 3, slope 0.5, curvature 8.

Curve at w = 2.5: 4.52

Line (dashed): 3.25, off by 1.27

Parabola: 4.25, off by 0.27

Bars along the bottom: where each stays within 0.25 of the curve · line from w = 1.74 to 2.24 · parabola from w = 1.49 to 2.49

The dashed line and the parabola both touch the curve at the big dot. The parabola stays close for about twice as far. Over a hill top they bend the other way.

Here is what to notice:

  • At a=2a = 2, the line stays within 0.25 from about w=1.73w = 1.73 to 2.242.24. The parabola stays within 0.25 from about 1.491.49 to 2.492.49.
  • Every approximation is good close to aa and drifts away further out.
  • Drag the dot to a=0a = 0, the hill top. The curvature is negative, so the parabola opens downwards.

Taylor series: keep adding terms

Why stop at the parabola? Each extra term matches one more derivative at the point. The next term uses the third derivative, f′′′(a) h3/6f'''(a)\,h^3 / 6, and so on. The full sum is the Taylor series:

f(a+h)=f(a)+f′(a) h+f′′(a)2!h2+f′′′(a)3!h3+…f(a + h) = f(a) + f'(a)\,h + \frac{f''(a)}{2!}h^2 + \frac{f'''(a)}{3!}h^3 + \dots

Here n!n! (read "n factorial") means 1×2×⋯×n1 \times 2 \times \dots \times n, so 3!=63! = 6. The linear and quadratic approximations are the first two and three terms of this sum.

Take the exponential exe^x. It is its own derivative. Every derivative of exe^x at 00 equals e0=1e^0 = 1. So around a=0a = 0:

ex=1+x+x22+x36+x424+…e^x = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \frac{x^4}{24} + \dots

At x=1x = 1 the sums go 1,2,2.5,2.667,2.708,2.717,…1, 2, 2.5, 2.667, 2.708, 2.717, \dots and close in on e=2.71828e = 2.71828. Press play below to add terms one at a time.

−3−2−10123−20246810x
Step 0 of 7

eˣ ≈ 1

At x = 1: polynomial 1, true value 2.718

At x = 2: polynomial 1, true value 7.389

Thick line: eˣ · thin line: the polynomial · bar: within 0.1 from x = -0.1 to 0.09

Each new term makes the polynomial match the curve over a wider range. The second tab, log(1 + x), is for the Deeper aside below.

For exe^x, the good range spreads outward with every term. With powers up to x7x^7, the polynomial is within 0.1 of exe^x from about x=−2.9x = -2.9 to 2.72.7.

Cutting the series after two terms gives short versions that later chapters use. For small xx:

ex≈1+x,log⁡(1+x)≈xe^x \approx 1 + x, \qquad \log(1 + x) \approx x

For example, log⁡(1.1)=0.0953\log(1.1) = 0.0953, close to 0.10.1. Here log⁡\log is the natural log.

DeeperWhen a Taylor series stops workingThe series for log(1 + x) goes wild beyond x = 1, even though the curve is smooth. · 2 min

Switch the figure above to the log⁡(1+x)\log(1 + x) tab. Its series is x−x2/2+x3/3−…x - x^2/2 + x^3/3 - \dots Watch the value at x=2x = 2 as you add terms.

Predict first

What happens at x = 2 as you add more terms?

This series only works for xx between −1-1 and 11. A Taylor series can stop working far from its point, even when the curve itself is smooth. The series for exe^x has no such limit: it works for every xx, given enough terms.

Why small steps downhill work

Gradient descent changes a weight by h=−α E′(w)h = -\alpha\, E'(w). The number α\alpha (alpha) is the step size, also called the learning rate. Put that hh into the linear approximation:

E(w+h)≈E(w)+E′(w) h=E(w)−α E′(w)2E(w + h) \approx E(w) + E'(w)\,h = E(w) - \alpha\, E'(w)^2

The square E′(w)2E'(w)^2 is never negative. So the line promises that the error goes down, whatever the sign of the slope. This promise holds only while the line can be trusted, which means small steps.

The parabola shows what a bigger step costs. It adds +12E′′(w) h2+\tfrac12 E''(w)\,h^2 to the prediction. In a bowl, E′′E'' is positive, so this term pushes the error back up. The sharper the bowl, the smaller the step at which this term takes over. The step-size figure below shows both terms.

Sharp valleys and shallow bowls

Now back to the pizza error. Its formula is the sum of squared errors over the three deliveries:

E(w1,w2)=∑deliveries(w1⋅distance+w2⋅traffic−minutes)2E(w_1, w_2) = \sum_{\text{deliveries}} (w_1 \cdot \text{distance} + w_2 \cdot \text{traffic} - \text{minutes})^2

The bottom is at (4,6)(4, 6). Hold w2=6w_2 = 6 and change only w1w_1, the minutes per km. The error becomes 38(w1−4)238(w_1 - 4)^2, a parabola with curvature 7676. Hold w1=4w_1 = 4 and change only w2w_2, the minutes per traffic level. The error becomes 5(w2−6)25(w_2 - 6)^2, with curvature 1010.

So the same error surface is sharply curved in one direction and gently curved in another. Both slices are exact parabolas, so the quadratic approximation is exact here.

Predict first

You stand in a sharply curved valley and take a big step downhill. Where do you land?

Both starting points below have the same slope, −38-38. One slider sets the step size for both.

05010015020001020304050number of deliveries so farminutesfaster drivers start
━ α = 1/n (plain average)━ α = 0.10 (constant)╍ true mean

After delivery 100 (true mean 32): 1/n gives 32.6, α gives 31.9

After delivery 200 (true mean 26): 1/n gives 28.6, α gives 23.2

The same slope means different things in different bowls. A step size that is slow in the shallow bowl overshoots in the sharp valley.

Measure the distance to the bottom with a sign: negative left of the bottom, positive right. For a bowl with curvature kk, the slope is k×k \times (signed distance). So one step multiplies the signed distance by 1−αk1 - \alpha k. A negative factor means you land on the other side of the bottom.

  • With α=0.02\alpha = 0.02 in the shallow bowl (k=10k = 10), the factor is 0.80.8. Each step closes 20% of the gap.
  • With α=0.02\alpha = 0.02 in the sharp valley (k=76k = 76), the factor is −0.52-0.52. Each step jumps over the bottom, but lands closer.
  • Above α=2/76≈0.026\alpha = 2/76 \approx 0.026, the factor is below −1-1 in the sharp valley. Each jump lands further away, and the error grows. From about α=0.06\alpha = 0.06 the path leaves the plot.

So the curvature decides how big a step is safe. The ML fundamentals course turns this into a rule for the learning rate.

Local minima and convexity

A point where the curve is lower than everywhere nearby is a local minimum. At the bottom of a valley the slope is zero, and the curvature is positive.

Our bumpy curve has two:

  • one at w≈−2.06w \approx -2.06, with error 0.980.98. It is the lowest point anywhere on the curve, so it is also the global minimum.
  • one at w≈1.93w \approx 1.93, with error 2.982.98

Between them is a hill top at w≈0.13w \approx 0.13, where the slope is also zero but the curvature is negative.

Predict first

Gradient descent starts at w = 0.5, just right of the hill top. Where does it stop?

Press play, then try other starting points. Then switch to the second tab, a new example: a simple bowl, (w−0.5)2+1(w - 0.5)^2 + 1.

−3−2−101230246810weight werror E(w)
Step 0 of 30

w = 0.5, error 5.77, slope -1.37

Each step: w ← w − 0.08 × slope.

On the bumpy curve, the start decides which valley you end in. On the simple bowl, every start ends at the same bottom.

The simple bowl has a property the bumpy curve lacks. Pick any two points on it and join them with a straight line, called a chord. The curve never rises above the chord. A curve with this property is convex.

Tick "Show the chord" to test it. On the bumpy curve, the chord from w=−2w = -2 to w=2w = 2 runs from height 11 to height 33. Halfway along it is at height 22, but the curve is at E(0)=6E(0) = 6. The curve pokes above the chord, so it is not convex.

There is a second test: a curve is convex when its curvature is never negative. The bumpy curve fails it, since E′′=3w2−4E'' = 3w^2 - 4 is negative between about −1.15-1.15 and 1.151.15.

A convex curve has only one valley. So gradient descent, with a small enough step, reaches the bottom from any start. The squared error of a linear model, like the pizza error, is always convex. The error of a neural network is not, so where you start matters there.

DeeperNewton's method: jump to the parabola's bottomUse the curvature to choose the step, and land at the bottom in one or two jumps. · 3 min

The tangent parabola at ww has its own lowest point. You can find it by setting the parabola's slope to zero:

E′(w)+E′′(w) h=0⇒h=−E′(w)E′′(w)E'(w) + E''(w)\,h = 0 \quad\Rightarrow\quad h = -\frac{E'(w)}{E''(w)}

Newton's method jumps there, then repeats. Compare it with gradient descent's h=−αE′(w)h = -\alpha E'(w). Newton uses α=1/E′′\alpha = 1/E'': big steps in shallow bowls, small steps in sharp ones.

−3−2−101230246810weight werror E(w)
Jump 0 of 4

w = 2.6000

The parabola opens up, so its lowest point is a good guess.

Each jump goes to the bottom of the tangent parabola. Near a valley it lands there in two or three jumps. Start on the hill, where the parabola opens down, and it jumps the wrong way.

With numbers on the bumpy curve, start at w=2w = 2. The step is −0.5/8=−0.0625-0.5 / 8 = -0.0625, so ww becomes 1.93751.9375. One more jump gives 1.93431.9343, the bottom of the valley to four decimal places.

Newton has a catch. Where the curvature is negative, the parabola opens down, and its "bottom" is its top. From w=1w = 1, Newton jumps to w=−1.5w = -1.5, the wrong way for the valley at 1.931.93. Practical versions guard against this.

On the pizza slice in the w1w_1 direction, the curve is an exact parabola with curvature 76. Newton's step from 3.53.5 is 38/76=0.538/76 = 0.5, which lands exactly on the bottom at 44.

Why don't neural networks use it? With millions of weights, the curvature becomes a table with millions × millions of entries. That is far too big to compute or store, so practical methods use cheap estimates of it instead.

DeeperCurvature in many directions: the HessianWith two weights, curvature becomes a 2 × 2 table that describes the valley's shape. · 3 min

With two weights, there are four second derivatives: curvature along w1w_1, curvature along w2w_2, and two "mixed" ones. A mixed one says how the slope in w1w_1 changes as you move in w2w_2. These go into a 2 × 2 table called the Hessian. For the pizza error it is the same at every point:

H=(76222210)H = \begin{pmatrix} 76 & 22 \\ 22 & 10 \end{pmatrix}

The 76 and 10 are the curvatures of the two slices in the step-size figure. The 22 says the valley runs at an angle, not along the axes.

The Hessian is a symmetric matrix: the two mixed entries are equal. Its eigenvectors point along the valley's steepest and flattest directions. Its eigenvalues, about 82.782.7 and 3.33.3, are the curvatures in those directions. Both are positive, so the surface is convex: a bowl in every direction.

Their ratio is about 25, so the bowl is a long, narrow valley. The contour plot shows it: each oval joins weights with the same error.

1234567024681012w₁ (min per km)w₂ (min per traffic level)best

After 30 steps: (3.91, 6.31), error 0.17

Drag the start. Each oval is one error level (2, 10, 40, 120). Steps bounce across the narrow direction and creep along the long one.

Contours of the pizza error. A step size safe for the steep direction bounces across the valley and creeps along it.

A step size small enough for the steep direction crawls along the flat one. This is a common reason why gradient descent is slow.

Common misconceptions

"A slope of zero means I'm at the bottom." Not always. The slope is also zero on a hill top, like w≈0.13w \approx 0.13 on the bumpy curve. Check the curvature: positive means a valley, negative means a hill top.

"Curvature and slope are the same thing." No. Slope says which way the curve goes. Curvature says how fast the slope changes. A steep straight line has a big slope and zero curvature.

"More Taylor terms always help." Near the point, yes. Far from it, not always: the log⁡(1+x)\log(1 + x) series gets worse with more terms at x=2x = 2.

"A local minimum is the best answer." It is the best nearby. Another valley can be lower, as on the bumpy curve.

Recap

  • Near a point, a curve is close to its tangent line: f(a+h)≈f(a)+f′(a)hf(a + h) \approx f(a) + f'(a)h.
  • The second derivative f′′f'' is the slope of the slope. It measures curvature.
  • Adding 12f′′(a)h2\tfrac12 f''(a)h^2 gives a parabola that stays close for longer.
  • Gradient descent trusts the tangent line, so it needs small steps. Sharper curvature means a smaller safe step.
  • Gradient descent stops in whichever valley it rolls into. A convex curve has only one valley.

Section 2 shows how to compute these in PyTorch.