Taylor Approximations and Why Optimizers Rely on Them
See how linear and quadratic approximations of a loss lead to gradient descent, step size limits, Newton steps and tree splits.
Key takeaways
- A first-order approximation uses the value and gradient, and a second-order one adds curvature through the Hessian.
- Both are accurate only near the expansion point, and the error shrinks quickly as the step gets smaller.
- Gradient descent minimizes a linear model of the loss plus a penalty on step length.
- Newton's method jumps to the stationary point of the quadratic model, which helps only when the curvature there is positive.
- A bound on curvature turns the approximation into a guarantee that a small enough step lowers the loss.
A Taylor approximation replaces a complicated function, near one point, with a simple polynomial built from its derivatives there. Optimizers need this because they cannot see the whole loss surface. At the current weights they can measure the slope, sometimes the curvature, and must guess what lies a short step away.
Gradient descent is what you get from trusting a straight-line approximation only nearby. Newton's method is what you get from trusting a quadratic one. Most other optimizers are variations on those two ideas.
The one-variable version
For a smooth function f and a small step h:
first order: f(x + h) ≈ f(x) + f′(x) h
second order: f(x + h) ≈ f(x) + f′(x) h + ½ f″(x) h² The first line is the tangent line. The second bends the line into a parabola that matches the slope and the curvature at x. Under suitable smoothness, the error of the first shrinks like h², and the error of the second like h³. Halving a small step therefore cuts the errors to about a quarter and an eighth.
Testing the models on real numbers
Let L(w) = eʷ − 2w and expand at w = 0. The value is 1, the slope is eʷ − 2 = −1 and the curvature is eʷ = 1.
| Step Δ | True L(Δ) | Linear model 1 − Δ | Quadratic model 1 − Δ + Δ²/2 |
|---|---|---|---|
| 0.1 | 0.9052 | 0.9000 | 0.9050 |
| 0.5 | 0.6487 | 0.5000 | 0.6250 |
| 2.0 | 3.3891 | −1.0000 | 1.0000 |
At a step of 0.1 both are close. At 0.5 the linear model is noticeably optimistic. At 2.0 it predicts a large improvement while the loss has in fact risen from 1 to more than 3. That is divergence from too large a learning rate, in miniature.
Many parameters
With a weight vector w, a gradient g and a Hessian H, all evaluated at w, the same formulas read:
L(w + Δ) ≈ L(w) + gᵀ Δ first order
L(w + Δ) ≈ L(w) + gᵀ Δ + ½ Δᵀ H Δ second order Among all steps of a given small length, the linear term gᵀΔ is most negative when Δ points opposite to g. That is why the negative gradient is called the direction of steepest descent, measured with ordinary Euclidean length. The Hessian and curvature explains what the quadratic term adds.
Gradient descent from a model
A linear model has no minimum: follow it far enough and it predicts a loss of negative infinity. You must say how far you trust it. Add a cost for distance and minimize:
minimize over Δ: L(w) + gᵀ Δ + ‖Δ‖² / (2η)
set derivative to zero: g + Δ / η = 0
result: Δ = −η g The learning rate η is therefore a statement of trust. A large η says the straight-line model holds over a long distance. Changing how distance is measured changes the optimizer: weighting coordinates differently gives preconditioned methods, and measuring distance by how much the model's predictive distribution changes leads to natural gradient methods.
When does a step actually lower the loss?
Suppose the gradient cannot change too fast: ‖∇L(a) − ∇L(b)‖ ≤ β‖a − b‖ for all a and b. Such a loss is called β-smooth, and it satisfies an upper bound everywhere.
L(w + Δ) ≤ L(w) + gᵀ Δ + (β/2) ‖Δ‖²
put Δ = −η g:
L(w − η g) ≤ L(w) − η (1 − βη/2) ‖g‖² The loss is guaranteed to fall whenever g is not zero and 0 < η < 2/β. Choosing η = 1/β gives a drop of at least ‖g‖²/(2β). Real networks do not come with a known global β, and some losses have none, but this argument is the template for many convergence proofs. Convex and nonconvex losses discusses what such proofs deliver.
Newton's method
Minimizing the quadratic model gives a different step.
g + H Δ = 0 → Δ = −H⁻¹ g Apply it to L(w) = eʷ − 2w, whose true minimum is at w = ln 2 ≈ 0.6931.
| Iteration | w | Gradient eʷ − 2 | Newton step |
|---|---|---|---|
| 0 | 0.0000 | −1.0000 | +1.0000 |
| 1 | 1.0000 | 0.7183 | −0.2642 |
| 2 | 0.7358 | 0.0871 | −0.0417 |
| 3 | 0.6940 | about 0.0018 | small correction |
After the first step, the distance from the minimum falls from about 0.31 to 0.043 to under 0.001. Close to a well-behaved minimum, the number of correct digits roughly doubles per step.
Far away there is no such promise. If the curvature is negative, the stationary point of the model is a maximum along that direction. A common fix is damping: solve (H + μI)Δ = −g, which behaves like Newton when μ is small and like a short gradient step when μ is large. Trust region methods adjust the allowed step according to how well the model predicted the last improvement.
Second-order ideas in practice
Quasi-Newton methods such as L-BFGS infer curvature from how gradients changed over recent steps, avoiding the full Hessian.
Gradient boosting libraries such as XGBoost expand the loss to second order around the current prediction of each example. For a leaf containing examples with gradient sum G and Hessian sum H, and an L2 penalty λ on leaf values, the best value of the quadratic model is −G/(H + λ). The approach is described in second-order splits in XGBoost.
Adaptive methods such as Adam rescale each coordinate using running averages of squared gradients. This is not a Hessian estimate, although it serves a related purpose of giving different coordinates different step sizes.
Predicted against actual improvement
The model of the loss can be checked while you train, and that check is worth more than intuition. For a proposed step Δ, compare two numbers.
predicted change: gᵀ Δ + ½ Δᵀ H Δ
actual change: L(w + Δ) − L(w)
ratio: actual / predicted When the model predicts a decrease and the ratio is near 1, the step stayed inside the region where the approximation holds, and you could afford a longer one. A ratio near zero or below means the loss barely moved or rose, so the step was too long.
From the table above, take the step of 0.5. The quadratic model predicted a change of 0.625 − 1 = −0.375. The actual change was about −0.351, for a ratio near 0.94. At a step of 2.0 the model predicted no change at all, while the loss rose by about 2.39.
Trust region methods automate this rule: grow the allowed radius after good ratios and shrink it after bad ones. A backtracking line search is a simpler relative. Try a step, and if the actual decrease is less than a chosen fraction of what the gradient promised, halve the step and try again.
Both need reliable loss values. With a different mini-batch for each evaluation, noise can swamp the comparison, so evaluate both points on the same examples with the same random seed for layers such as dropout.
Reading expansions in other places
The same tool shows up well beyond optimizers. Analyses of weight initialization linearize an activation around zero to track how variance passes from layer to layer. Influence estimates ask how the fitted weights would move if one training example were weighted slightly more, using a quadratic model of the total loss. Quantization and pruning heuristics estimate the cost of a small change to a weight from its gradient and an approximate curvature.
In every case, ask the same two questions: around which point was the function expanded, and how large is the change being predicted? A claim derived for tiny perturbations may say little about large ones.
A test that uses the error rate
You can verify a gradient by checking how fast the first-order error shrinks.
import numpy as np
def loss(w):
return np.sum(np.log1p(np.exp(w))) - 0.3*np.sum(w)
def grad(w):
return 1/(1 + np.exp(-w)) - 0.3
rng = np.random.default_rng(0)
w, v = rng.normal(size=5), rng.normal(size=5) # shapes (5,)
for h in [0.1, 0.05, 0.025, 0.0125]:
err = abs(loss(w + h*v) - loss(w) - h*grad(w) @ v)
print(h, err, err / h**2) # last column levels off If the gradient is right, the error falls about fourfold each time h halves, so the last column settles. If it is wrong, the error only halves and the last column keeps growing. Very small h eventually runs into rounding noise.
Where the approximation misleads
- Kinks. A ReLU network is only piecewise smooth. A step that crosses many kinks leaves the region where the local derivatives apply.
- Cliffs. In some recurrent models the loss changes abruptly, and one large gradient can throw the weights far away. Gradient clipping limits the damage.
- Noise. A mini-batch gradient approximates the gradient of the batch loss, so the local model itself is uncertain.
- Large edits. Estimating the effect of deleting a whole group of weights from a Taylor term can be badly off, since the change is not small.
None of these makes the method useless. They tell you what to monitor: the size of each update relative to the weights, the loss before and after a step on a fixed batch, and sudden jumps in the gradient norm.
When training diverges, a useful first question is whether the step was far longer than the distance over which your local model could be believed.