A step has a direction and a length
What this page answers
The class page says optimisation is one score over a set of choices, and that a gradient points at the steepest way up. It does not say how far to walk. This page separates the two decisions, shows why the gradient alone cannot answer the second, and derives the window a fixed step has to sit in.
It settles the line-search row of the measured gaps: a reader can say why a step length is a separate question, and can compute the window for a quadratic.
The mathematics
Write the score as a quadratic in variables,
with symmetric and positive definite, so the minimum is where . Gradient descent takes
Let be the error, where solves . Subtracting the fixed point from the step gives the error's own rule:
That line is the whole analysis: the error is multiplied by the same matrix every step, so its behaviour is decided by the eigenvalues of . If has eigenvalues , the error along eigenvector is multiplied by each step. It decays only if
which is the window
So the largest curvature sets an upper limit, and the smallest sets the speed: the slowest mode decays by the factor per step, so reaching a relative error needs about
steps. The ratio is the condition number of , the same number the linear-system pages use. A well-conditioned quadratic converges quickly at the best step; a badly conditioned one crawls, or oscillates, because the step that suits the largest curvature is far too short for the smallest.
The step length, chosen rather than fixed. Along the direction , the best distance is the that minimises . For a quadratic that is a one-line calculation: differentiating in and setting it to zero gives
With that , one step lands on the minimum of along . It does not find the minimum of , because the next gradient points somewhere new — but it removes the only question the gradient could not answer.
The check. Take , so and the window is , and start at . At both modes decay and the run reaches . At the first mode is multiplied by every step, so the run explodes. At it converges, but the slow mode decays by per step, which is the slow crawl. The exact step reaches the minimum in one iteration.
The same iteration drawn on its plane. The two unknowns are the two axes, so each step moves along both. The model is and the step is .
One step length cannot be right for both directions. Say which coordinate the step of 0.05 leaves behind, which one the step of 0.19 leaves behind, and why the exact line search removes only half of that problem.
The code
The program runs four fixed steps and one exact line search on that quadratic and prints the final point and score. Sixty iterations is long enough to separate converged from diverged.
Four fixed steps and one exact line search
import sympy as sp
x, y = sp.symbols("x y")
f = 5 * x**2 + y**2
gx, gy = sp.diff(f, x), sp.diff(f, y)
start = (1.0, 1.0)
def fixed(step, n=60):
px, py = start
for _ in range(n):
g = (float(gx.subs({x: px, y: py})),
float(gy.subs({x: px, y: py})))
px, py = px - step * g[0], py - step * g[1]
return px, py
def exact(n=60):
px, py = start
for _ in range(n):
a = float(gx.subs({x: px, y: py}))
b = float(gy.subs({x: px, y: py}))
step = (a * a + b * b) / (10 * a * a + 2 * b * b)
px, py = px - step * a, py - step * b
return px, py
print("step x y f")
for s in (0.05, 0.19, 0.25, 0.45):
px, py = fixed(s)
print(f"{s:>4} {px:>11.3e} {py:>11.3e} {5*px*px + py*py:>11.3e}")
px, py = exact()
print(f"exact {px:>11.3e} {py:>11.3e} {5*px*px + py*py:>11.3e}")
step x y f
0.05 8.674e-19 1.797e-03 3.229e-06
0.19 1.797e-03 3.495e-13 1.615e-05
0.25 3.677e+10 8.674e-19 6.760e+21
0.45 4.406e+32 1.000e-60 9.708e+65
exact 5.458e-30 5.458e-30 1.787e-58
The first two rows are inside the window and the last two are outside it, which is the window's shape rather than a coincidence: . Read the two converged rows against each other and the second decision appears. At the component is still after sixty steps, because its curvature is and it decays by a step; the component, with curvature , is already gone. At the order reverses: has vanished and is the one left over, oscillating as . One step length cannot be right for both directions, and that is the whole reason a line search exists.
The exact row reaches and stops, which is the point at which the arithmetic runs out rather than the method.
The theory
The analysis assumed three things, and each one is where a real problem differs.
- A quadratic. The error rule is exact only for a quadratic. For a general it is the linearisation about the minimum, so the window is a statement about the Hessian near the answer — which is the region a run only reaches if the early steps were safe.
- A known largest curvature. is not known in advance for a real score. A step chosen from a bound on the Hessian is a step chosen from a guess, and too large a guess diverges.
- Exact arithmetic. The divergence at grows by a factor of about eight a step, so it reaches the floating-point ceiling in a few dozen steps and then overflows. Before that it is a real, finite, wrong answer, which is why a diverging run looks plausible for a while.
What the gradient cannot supply is curvature. It gives the direction of steepest increase at the point, and nothing about how that direction changes as you move along it. A method that uses only gradients is therefore choosing a length from information it does not have. There are two ways out. One is to measure the curvature, which is Newton's method and needs a Hessian and a solve. The other is to measure the score along the line, which is a line search. The trade is that Newton needs a Hessian and a linear solve per step, and a line search needs one or more extra score evaluations per step.
The account of line search and of the conditions a step must satisfy is Wright's, in Numerical Optimization; the convergence argument above is the quadratic case of the same theorem.
What it will not do: a line search makes a step safe, and does not make the method fast on a badly conditioned problem. It fixes the length, not the direction, and on an ill-conditioned quadratic the direction is still the slow part.
What this cost
One gradient per iteration, at for a score written as a formula, and the analysis above costs one matrix that a real problem never forms. Memory is . An exact line search costs one extra score evaluation per step for a quadratic, and several for a general , where the minimisation along the line is itself iterative.
The accuracy is limited by the condition number: the iteration count grows with , so a hundredfold worse conditioned problem costs about a hundredfold more steps. The step length is the cheapest thing to fix and the least valuable: it stops the run failing, and it does not stop it crawling.
What it will not do: it will not choose the direction. It will not work without a bound on the curvature. And it will not tell you that a small final gradient means a small error — on a flat valley a small gradient and a large error look the same.
Back to the map
Optimisation is the class page this works under. Convergence, and how to measure it is how the rate is measured when it is not a quadratic. Home is the summary.