Skip to content

Line-search solvers

vertax.cg

Nonlinear conjugate gradient solver (optax GradientTransformation).

A drop-in optax solver implementing the Polak-Ribière nonlinear conjugate gradient (CG) method with automatic restarts and a zoom line search. It plugs into the same optimisation loops as optax.adam / optax.sgd (e.g. vertax.opt_bounded.inner_opt_bounded), provided that loop forwards the line search arguments value, grad and value_fn to solver.update — see the note on the line search below.

Why conjugate gradient

Steepest descent (d = -∇f) zig-zags in elongated valleys: each step partly undoes the previous one. CG removes this by making successive search directions conjugate (non-interfering with respect to the local curvature), so progress along a valley is not destroyed at the next step. On a strictly quadratic objective in n variables it converges in at most n steps; a general energy is not quadratic, hence nonlinear CG with periodic restarts.

The algorithm (one update call)

Given the gradient g_k = ∇f(x_k), the previous gradient g_{k-1} and the previous direction d_{k-1}:

  1. Polak-Ribière coefficient β = ⟨g_k, g_k - g_{k-1}⟩ / ‖g_{k-1}‖². The g_k - g_{k-1} term injects curvature information without ever forming a Hessian.
  2. Restart (set β = 0 → fall back to -g) when either
  3. it has been restart_every steps since the last restart (conjugacy degrades on a non-quadratic objective), or
  4. β ≤ 0 — the PR+ safeguard β⁺ = max(0, β): a negative β means the old direction is no longer useful, so we drop it.
  5. Direction d_k = -g_k + β · d_{k-1} (β = 0 ⇒ steepest descent; β > 0 ⇒ curve the search along the valley).
  6. Descent guard: a line search is only valid if d goes downhill, i.e. ⟨d, g⟩ < 0. Rounding / non-quadratic effects can break this; if so we fall back to the always-valid steepest-descent direction -g.
  7. Zoom line search along d: chooses the step size satisfying the (strong) Wolfe conditions by re-evaluating f along the line. update returns step · d as the optax updates.

Line search requirement

Like optax.lbfgs, this solver wraps optax.scale_by_zoom_linesearch and therefore needs the loss value and a callable to re-evaluate it. The enclosing loop must call::

updates, state = solver.update(grad, state, params,
                               value=current_value, grad=grad,
                               value_fn=loss_fn)

where loss_fn(params) -> scalar. optax.adam / optax.sgd ignore these extra arguments, so a loop that always forwards them stays compatible with both.

NonlinearCGState

Bases: NamedTuple

State carried by the nonlinear CG solver between update calls.

GDLineSearchState

Bases: NamedTuple

State carried by the GD + line-search solver between update calls.

cg_linesearch

cg_linesearch(restart_every: int = 10, max_linesearch_steps: int = 20, linesearch_initial_guess: str = 'keep') -> optax.GradientTransformationExtraArgs

Polak-Ribière nonlinear conjugate gradient with restarts + zoom line search.

Parameters:

Name Type Description Default
restart_every int

force a steepest-descent restart every this many steps. Smaller = more robust on strongly non-quadratic objectives, larger = longer conjugate runs.

10
max_linesearch_steps int

maximum zoom line-search iterations per step.

20
linesearch_initial_guess str

step length from which each line search starts, passed to optax.scale_by_zoom_linesearch as initial_guess_strategy. "keep" (default) starts from the last accepted step. Combined with a per-vertex step cap, one very short accepted step can then persist and a single long call stalls; restarting the call resets it. "one" starts every line search from a unit step and does not stall, at the price of more energy evaluations per step.

'keep'

Returns:

Type Description
GradientTransformationExtraArgs

An optax solver. Its update requires value, grad and value_fn keyword arguments (see module docstring).

gd_linesearch

gd_linesearch(max_linesearch_steps: int = 20, linesearch_initial_guess: str = 'keep') -> optax.GradientTransformationExtraArgs

Steepest descent with a zoom (Wolfe) line search for the step size.

Plain gradient descent picks the search direction d = -g but leaves the step size to a fixed learning rate. This solver keeps d = -g (no conjugate term, unlike cg_linesearch) but chooses the step by the same optax.scale_by_zoom_linesearch used by CG / lbfgs, so each step satisfies the strong Wolfe conditions instead of relying on a hand-tuned learning rate.

Like every line-search solver, its update requires value, grad and value_fn (it re-evaluates the loss along d); the enclosing loop must forward them, exactly as for cg_linesearch.

Note (AD incompatibility): the zoom line search runs an inner lax.while_loop with dynamic bounds, which reverse-mode AD cannot differentiate through. So — like cg_linesearch — this solver is incompatible with the AUTOMATIC_DIFFERENTIATION bilevel method; the factory name contains "linesearch" so the guard in bilevelopt.py rejects the combination automatically.

linesearch_initial_guess has the meaning it has in cg_linesearch.