TExcelTAD
Home › Optimization › Line Search and Backtracking

Line Search and Backtracking

Once you have a descent direction dk\mathbf{d}_k, you still have to decide how far to go. A line search chooses the step tt by looking at the one-dimensional function ϕ(t)=f(xk+tdk)\phi(t) = f(\mathbf{x}_k + t\mathbf{d}_k). 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:

f(xk+tdk)≤f(xk)+c1t ∇f(xk)Tdk,0<c1<1f(\mathbf{x}_k + t\mathbf{d}_k) \le f(\mathbf{x}_k) + c_1 t\,\nabla f(\mathbf{x}_k)^T\mathbf{d}_k, \qquad 0 < c_1 < 1

The right-hand side is the tangent line with its slope flattened by the factor c1c_1 (typically 10−410^{-4}). For small enough tt it always holds along a descent direction.

Backtracking

Start with a trial step (1 for Newton-type methods), and shrink it by a factor β∈(0,1)\beta \in (0, 1) until Armijo holds:

python · try it yourself
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 ∇f\nabla f is LL-Lipschitz, the Armijo condition with c1≤12c_1 \le \frac{1}{2} holds for every t≤1/Lt \le 1/L along d=−∇f\mathbf{d} = -\nabla f, so backtracking stops with t≥β/Lt \ge \beta/L. Each accepted step then decreases ff by at least c1βL∥∇f∥2c_1\frac{\beta}{L}\lVert \nabla f \rVert^2, which is enough to prove the same rates as the fixed step 1/L1/L without knowing LL.

Exact line search on a 1D slice

For comparison, an exact search minimizes ϕ(t)\phi(t). On a quadratic it has a formula; in general it is a 1D optimization in its own right:

python · try it yourself
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 ff 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 ϕ(0)\phi(0), ϕ′(0)\phi'(0) and ϕ(t)\phi(t) 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_search implements a strong-Wolfe search; BFGS and CG in minimize call 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 f(x)=x2f(x) = x^2 at x=1x = 1 with d=−f′(1)=−2d = -f'(1) = -2 and c1=0.5c_1 = 0.5, which of t=1,0.5,0.25t = 1, 0.5, 0.25 satisfy Armijo?

Answer

t=0.5t = 0.5 and t=0.25t = 0.25.

Solution

The condition is (1−2t)2≤1+0.5t⋅(−4)=1−2t(1 - 2t)^2 \le 1 + 0.5t \cdot (-4) = 1 - 2t. At t=1t = 1: 1≤−11 \le -1, false. At t=0.5t = 0.5: 0≤00 \le 0, true. At t=0.25t = 0.25: 0.25≤0.50.25 \le 0.5, true.

Exercise 2: How many halvings?

Backtracking starts at t=1t = 1 with β=0.5\beta = 0.5 and is guaranteed to stop once t≤1/Lt \le 1/L. With L=1000L = 1000, at most how many halvings are needed?

Answer
Solution

2−10=1/1024≤1/10002^{-10} = 1/1024 \le 1/1000, and 2−9=1/5122^{-9} = 1/512 is not yet below 1/L1/L; so at most 10 halvings.

Exercise 3: Change β\beta (code)

Rerun the Rosenbrock backtracking loop with β=0.8\beta = 0.8 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
python · try it yourself
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 β=0.8\beta = 0.8 each search tries more candidate steps, and the accepted steps are not better enough to pay for it. Typical choices are β\beta between 0.3 and 0.5.

Test yourself

The Armijo condition guarantees…

  1. the exact minimum along the line
  2. a sufficient decrease proportional to the step and the slope
  3. that the step is at least 1
  4. convexity

Backtracking line search needs to know…

  1. the Lipschitz constant LL
  2. the Hessian
  3. only function values and the slope ∇fTd\nabla f^T\mathbf{d}
  4. the optimal value f∗f^*

Why is the trial step usually t=1t = 1 for Newton's method?

  1. It is the largest allowed
  2. The Newton step is exact for quadratics, so t=1t = 1 is natural and gives fast local convergence
  3. Smaller steps are illegal
  4. Armijo forces it