Line Search and Backtracking
Once you have a descent direction , you still have to decide how far to go. A line search chooses the step by looking at the one-dimensional function . Doing it exactly is wasteful; doing it carelessly breaks convergence. Backtracking with the Armijo condition is the cheap, reliable middle ground used almost everywhere.
Sufficient decrease (the Armijo condition)
Accepting any decrease is not enough: steps can get so small that the method stalls. The Armijo condition asks for a decrease proportional to the step and the slope:
The right-hand side is the tangent line with its slope flattened by the factor (typically ). For small enough it always holds along a descent direction.
Backtracking
Start with a trial step (1 for Newton-type methods), and shrink it by a factor until Armijo holds:
import numpy as np
def backtracking(f, g, x, d, t=1.0, c1=1e-4, beta=0.5):
fx, slope = f(x), g @ d
tries = 0
while f(x + t * d) > fx + c1 * t * slope:
t *= beta
tries += 1
return t, tries
f = lambda p: (1 - p[0])**2 + 100 * (p[1] - p[0]**2)**2
grad = lambda p: np.array([-2 * (1 - p[0]) - 400 * p[0] * (p[1] - p[0]**2), 200 * (p[1] - p[0]**2)])
x = np.array([-1.2, 1.0])
evals = 0
for k in range(1, 20001):
g = grad(x)
t, tries = backtracking(f, g, x, -g)
evals += tries + 2
x = x - t * g
if np.linalg.norm(grad(x)) < 1e-6:
break
print(f"stopped after {k} steps, about {evals} function evaluations")
print("x =", np.round(x, 5))stopped after 13756 steps, about 150555 function evaluations
x = [1. 1.]
No Lipschitz constant was needed: the search adapts the step to the local curvature. The run still needs thousands of steps, because the bottleneck on Rosenbrock is the steepest-descent direction, not the step length; better directions come in the next lessons.
Why backtracking terminates and works
If is -Lipschitz, the Armijo condition with holds for every along , so backtracking stops with . Each accepted step then decreases by at least , which is enough to prove the same rates as the fixed step without knowing .
Exact line search on a 1D slice
For comparison, an exact search minimizes . On a quadratic it has a formula; in general it is a 1D optimization in its own right:
import numpy as np
from scipy.optimize import minimize_scalar
f = lambda p: (1 - p[0])**2 + 100 * (p[1] - p[0]**2)**2
grad = lambda p: np.array([-2 * (1 - p[0]) - 400 * p[0] * (p[1] - p[0]**2), 200 * (p[1] - p[0]**2)])
x = np.array([-1.2, 1.0]); d = -grad(x)
phi = lambda t: f(x + t * d)
exact = minimize_scalar(phi, bounds=(0, 0.01), method="bounded")
t_bt = 1.0
while phi(t_bt) > f(x) + 1e-4 * t_bt * (grad(x) @ d):
t_bt *= 0.5
print(f"exact step t = {exact.x:.6f}, f = {exact.fun:.4f}")
print(f"backtracking t = {t_bt:.6f}, f = {phi(t_bt):.4f}")exact step t = 0.000786, f = 4.1282
backtracking t = 0.000977, f = 5.1011
The exact step gives a slightly lower on this iteration, but it needs many evaluations; over a whole run backtracking is usually cheaper.
Practical details
- Initial trial step: 1 for Newton and quasi-Newton (their natural step), or the previous step times 2 for gradient descent.
- Interpolation: instead of halving, fit a quadratic or cubic through , and and jump to its minimum; this is what production codes do.
- Curvature conditions: quasi-Newton methods also need the step not to be too short, which is the second Wolfe condition (The Wolfe Conditions).
In software
- SciPy's
scipy.optimize.line_searchimplements a strong-Wolfe search; BFGS and CG inminimizecall it. - Deep learning rarely uses line search because mini-batch losses are noisy; learning-rate schedules play its role.
Exercises
Exercise 1: Armijo by hand
For at with and , which of satisfy Armijo?
Answer
and .
Solution
The condition is . At : , false. At : , true. At : , true.
Exercise 2: How many halvings?
Backtracking starts at with and is guaranteed to stop once . With , at most how many halvings are needed?
Answer
Solution
, and is not yet below ; so at most 10 halvings.
Exercise 3: Change (code)
Rerun the Rosenbrock backtracking loop with instead of 0.5. Does the number of steps change?
Answer
Yes, and for the worse: about 19,100 steps instead of 13,800, and almost four times as many evaluations.
Solution
import numpy as np
f = lambda p: (1 - p[0])**2 + 100 * (p[1] - p[0]**2)**2
grad = lambda p: np.array([-2 * (1 - p[0]) - 400 * p[0] * (p[1] - p[0]**2), 200 * (p[1] - p[0]**2)])
for beta in (0.5, 0.8):
x, evals = np.array([-1.2, 1.0]), 0
for k in range(1, 20001):
g = grad(x); t = 1.0
while f(x - t * g) > f(x) - 1e-4 * t * (g @ g):
t *= beta; evals += 1
x = x - t * g; evals += 2
if np.linalg.norm(grad(x)) < 1e-6:
break
print(f"beta={beta}: {k} steps, {evals} evaluations")beta=0.5: 13756 steps, 150555 evaluations
beta=0.8: 19079 steps, 575778 evaluations
With each search tries more candidate steps, and the accepted steps are not better enough to pay for it. Typical choices are between 0.3 and 0.5.
Test yourself
The Armijo condition guarantees…
- the exact minimum along the line
- a sufficient decrease proportional to the step and the slope
- that the step is at least 1
- convexity
Backtracking line search needs to know…
- the Lipschitz constant
- the Hessian
- only function values and the slope
- the optimal value
Why is the trial step usually for Newton's method?
- It is the largest allowed
- The Newton step is exact for quadratics, so is natural and gives fast local convergence
- Smaller steps are illegal
- Armijo forces it