Nonlinear Conjugate Gradient
The conjugate gradient method was built for quadratics, but its recipe, "new direction = negative gradient + times old direction", makes sense for any smooth function. Nonlinear CG keeps CG's tiny memory footprint (a few vectors) and usually beats gradient descent by a wide margin, though it is less robust than L-BFGS.
The iteration
The step comes from a line search (strong Wolfe with a small , around 0.1, because CG needs fairly accurate searches). Different formulas for agree on quadratics but behave differently elsewhere:
| Name | Notes | |
|---|---|---|
| Fletcher-Reeves (FR) | global convergence theory, can stall | |
| Polak-Ribière (PR) | resets itself when progress stalls; usually faster | |
| PR+ | PR with a safeguard; the common default | |
| Hestenes-Stiefel (HS) | close to PR in practice |
import numpy as np
import warnings
from scipy.optimize import line_search
warnings.filterwarnings("ignore") # line_search warns when it gives up on a step
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)])
def ncg(beta_rule, x, tol=1e-6, max_iter=20000):
g = grad(x); p = -g
for k in range(max_iter):
if np.linalg.norm(g) < tol:
return k
t = line_search(f, grad, x, p, gfk=g, c2=0.1)[0]
if t is None: # line search failed: restart along -g
p = -g; t = line_search(f, grad, x, p, gfk=g, c2=0.1)[0] or 1e-4
x = x + t * p; g_new = grad(x)
if beta_rule == "FR":
beta = (g_new @ g_new) / (g @ g)
else:
beta = max((g_new @ (g_new - g)) / (g @ g), 0.0)
p = -g_new + beta * p; g = g_new
return max_iter
for rule in ("FR", "PR+"):
print(rule, "iterations:", ncg(rule, np.array([-1.2, 1.0])))FR iterations: 48
PR+ iterations: 20
Restarts
Nonlinear CG loses conjugacy as the function departs from a quadratic, so implementations restart with every iterations or whenever consecutive gradients are far from orthogonal (). PR+ restarts automatically: when the max sets it to 0.
Comparison on a larger problem
import numpy as np
from scipy.optimize import minimize
n = 200
rng = np.random.default_rng(0)
A = rng.normal(size=(n, n)) / np.sqrt(n); Q = A.T @ A + 0.01 * np.eye(n); b = rng.normal(size=n)
f = lambda x: 0.5 * x @ Q @ x - b @ x + 0.1 * np.sum(np.log(np.cosh(x)))
grad = lambda x: Q @ x - b + 0.1 * np.tanh(x)
for method in ("CG", "L-BFGS-B", "BFGS"):
r = minimize(f, np.zeros(n), jac=grad, method=method, options={"gtol": 1e-6, "maxiter": 10000})
print(f"{method:9s} iterations {r.nit:4d}, gradient evaluations {r.njev:4d}")CG iterations 152, gradient evaluations 211
L-BFGS-B iterations 87, gradient evaluations 91
BFGS iterations 107, gradient evaluations 109
When to choose nonlinear CG
- Memory is extremely tight (it stores 3–4 vectors versus L-BFGS's ).
- The function is close to quadratic, where CG's conjugacy pays off.
- Otherwise L-BFGS is usually the safer default: it tolerates inexact line searches better.
In software
scipy.optimize.minimize(method="CG")is Polak-Ribière with a strong-Wolfe line search;scipy.optimize.fmin_cgis the older interface.- Large-scale scientific codes (geophysical inversion, molecular mechanics energy minimization) traditionally use nonlinear CG because of its tiny memory.
Exercises
Exercise 1: Reduce to linear CG
Show that on a quadratic with exact line searches, the Fletcher-Reeves and Polak-Ribière formulas give the same .
Answer
Consecutive gradients are orthogonal, so and the PR numerator reduces to the FR numerator.
Solution
In linear CG, residuals (negative gradients) are mutually orthogonal. Then .
Exercise 2: Descent property
Why must the line search be accurate for to be a descent direction?
Answer
, and the second term is only small if the search made nearly zero.
Solution
An exact search makes , leaving . With strong Wolfe and small , , which keeps the sum negative for FR when .
Exercise 3: Restart frequency (code)
On a convex quadratic in 50 variables, compare linear CG with a "CG" that restarts () every 5 iterations. Which needs more iterations to reach a residual of ?
Answer
The restarted version, because each restart throws away conjugacy information.
Solution
import numpy as np
rng = np.random.default_rng(3)
M = rng.normal(size=(50, 50)); A = M @ M.T / 50 + 0.05 * np.eye(50); b = rng.normal(size=50)
def run(restart):
x = np.zeros(50); r = b.copy(); p = r.copy()
for k in range(1, 100000):
Ap = A @ p; a = (r @ r) / (p @ Ap); x += a * p; r_new = r - a * Ap
if np.linalg.norm(r_new) < 1e-8:
return k
beta = 0.0 if (restart and k % restart == 0) else (r_new @ r_new) / (r @ r)
p = r_new + beta * p; r = r_new
print("no restart:", run(None), " restart every 5:", run(5))no restart: 58 restart every 5: 171
Test yourself
Nonlinear CG's new direction is…
- only
- a random direction
What does the "+" in PR+ do?
- Adds momentum
- Sets , restarting when is negative
- Doubles the step
- Adds a Hessian term
Compared with L-BFGS, nonlinear CG…
- uses more memory
- uses less memory but is usually less robust
- needs Hessians
- cannot handle more than 100 variables