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:
This curve is smooth: it has no corners or jumps. Think of 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 :
- the error is
- the slope is , using
- right now the slope is growing at a rate of per unit of (more on this soon)
Zoom in: the tangent line
The tangent line at starts at the point and has slope . If you move a distance along the axis, the line predicts:
This is the linear approximation. In general, near a point :
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 . For our curve, take the derivative of :
At , this gives . 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, , is close.
The figure below plots the slope as a curve of its own. The steepness of that curve is the second derivative.
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.
The second derivative measures the curvature of the curve:
- Positive: the curve bends up, like a bowl. At , .
- Negative: the curve bends down, like a hill top. At , .
- Size: a bigger number means a sharper bend.
Adding a curvature term to the line gives the quadratic approximation:
This parabola matches the curve's height, slope and curvature at . Why the ? The second derivative of is , 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
Collect the three numbers at : height , slope , curvature . So the line is and the parabola is .
Now try it yourself. Drag the big dot to change the point , and move the slider to change . The bars along the bottom show where each approximation stays within 0.25 of the curve.
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
Here is what to notice:
- At , the line stays within 0.25 from about to . The parabola stays within 0.25 from about to .
- Every approximation is good close to and drifts away further out.
- Drag the dot to , 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, , and so on. The full sum is the Taylor series:
Here (read "n factorial") means , so . The linear and quadratic approximations are the first two and three terms of this sum.
Take the exponential . It is its own derivative. Every derivative of at equals . So around :
At the sums go and close in on . Press play below to add terms one at a time.
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
For , the good range spreads outward with every term. With powers up to , the polynomial is within 0.1 of from about to .
Cutting the series after two terms gives short versions that later chapters use. For small :
For example, , close to . Here 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 tab. Its series is Watch the value at as you add terms.
Predict first
What happens at x = 2 as you add more terms?
This series only works for between and . A Taylor series can stop working far from its point, even when the curve itself is smooth. The series for has no such limit: it works for every , given enough terms.
Why small steps downhill work
Gradient descent changes a weight by . The number (alpha) is the step size, also called the learning rate. Put that into the linear approximation:
The square 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 to the prediction. In a bowl, 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:
The bottom is at . Hold and change only , the minutes per km. The error becomes , a parabola with curvature . Hold and change only , the minutes per traffic level. The error becomes , with curvature .
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, . One slider sets the step size for both.
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
Measure the distance to the bottom with a sign: negative left of the bottom, positive right. For a bowl with curvature , the slope is (signed distance). So one step multiplies the signed distance by . A negative factor means you land on the other side of the bottom.
- With in the shallow bowl (), the factor is . Each step closes 20% of the gap.
- With in the sharp valley (), the factor is . Each step jumps over the bottom, but lands closer.
- Above , the factor is below in the sharp valley. Each jump lands further away, and the error grows. From about 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 , with error . It is the lowest point anywhere on the curve, so it is also the global minimum.
- one at , with error
Between them is a hill top at , 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, error 5.77, slope -1.37
Each step: w ← w − 0.08 × slope.
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 to runs from height to height . Halfway along it is at height , but the curve is at . 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 is negative between about and .
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 has its own lowest point. You can find it by setting the parabola's slope to zero:
Newton's method jumps there, then repeats. Compare it with gradient descent's . Newton uses : big steps in shallow bowls, small steps in sharp ones.
w = 2.6000
The parabola opens up, so its lowest point is a good guess.
With numbers on the bumpy curve, start at . The step is , so becomes . One more jump gives , 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 , Newton jumps to , the wrong way for the valley at . Practical versions guard against this.
On the pizza slice in the direction, the curve is an exact parabola with curvature 76. Newton's step from is , which lands exactly on the bottom at .
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 , curvature along , and two "mixed" ones. A mixed one says how the slope in changes as you move in . These go into a 2 × 2 table called the Hessian. For the pizza error it is the same at every point:
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 and , 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.
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.
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 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 series gets worse with more terms at .
"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: .
- The second derivative is the slope of the slope. It measures curvature.
- Adding 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.