Conjugate gradient method

On this page 8

The conjugate gradient method (CG) is an iterative algorithm for minimizing a convex quadratic function, or equivalently for solving a linear system Ax = b in which A is symmetric positive definite. In optimization for artificial intelligence, it serves two roles: as a memory-efficient linear solver inside second-order and Hessian-free training methods, and, in its nonlinear variants, as a general minimizer of smooth objective functions.

Its defining idea is that each new search direction is conjugate (A-orthogonal) to all previous directions, so progress made along one direction is never undone by later steps. In exact arithmetic, CG reaches the exact solution of an n-dimensional quadratic problem in at most n iterations, while storing only a handful of vectors.

How conjugate directions drive the iteration

The conjugate gradient method minimizes f(x) = ½ xᵀAx − bᵀx by moving along a sequence of mutually conjugate directions. Two directions p and q are conjugate with respect to A when pᵀAq = 0. Because these directions form a basis that decouples the quadratic, an exact line search along each of them settles that component of the solution permanently.

Each iteration needs only one matrix-vector product with A, plus a few vector updates and inner products. The residual r = b − Ax equals the negative gradient, and the new direction combines the current residual with the previous direction through a scalar coefficient β. This short recurrence is the reason CG needs no stored history, unlike methods that orthogonalize against every past vector.

The table below lists the quantities computed in every iteration of linear CG and the role each one plays.

QuantityFormulaRole
Step length αrᵀr / pᵀApExact minimizer along the current direction
Iterate xx + αpMoves toward the solution
Residual rr − αApNegative gradient at the new point
Coefficient βr_newᵀr_new / r_oldᵀr_oldKeeps the next direction conjugate
Direction pr + βpNext conjugate search direction
Quantities updated in each linear CG iteration

Convergence and the role of the condition number

In practice, CG is used as an iterative method stopped early, not as a direct solver that runs all n steps. The error in the A-norm shrinks at a rate bounded by ((√κ − 1)/(√κ + 1)) per iteration, where κ is the condition number of A. This means the iteration count grows roughly with the square root of κ, compared with growth proportional to κ for plain gradient descent.

The bound is often pessimistic. CG adapts to the distribution of eigenvalues: when the spectrum consists of a few tight clusters, the method converges in about as many iterations as there are clusters. This property explains why preconditioning, which reshapes the spectrum, matters more than any other implementation choice.

The following comparison shows how the condition number translates into practical iteration counts for a target reduction of the error.

Condition number κGradient descentConjugate gradient
10About 70About 20
1,000About 7,000About 220
100,000About 700,000About 2,200
Approximate iterations to reduce error by a factor of one million

In floating-point arithmetic, conjugacy is gradually lost because of rounding errors, so the finite-termination guarantee no longer holds exactly. The method still converges, but it may need more than n iterations on ill-conditioned problems, and implementations rely on a residual-based stopping test rather than an iteration count.

Nonlinear conjugate gradient variants

Nonlinear conjugate gradient extends the idea to general smooth objective functions by replacing the residual with the gradient and the exact step with a line search, typically one satisfying the strong Wolfe conditions. The variants differ mainly in the formula for β, which controls how much of the previous direction is retained.

VariantKey propertyTypical behavior
Fletcher-ReevesGlobal convergence under strong Wolfe searchCan stall with very small steps
Polak-Ribière (PR+)Restarts automatically when β turns negativeUsually the most efficient in practice
Hestenes-StiefelMatches linear CG on quadraticsSensitive to line search accuracy
Dai-YuanDescent direction under weak Wolfe searchRobust but sometimes slower
Hager-ZhangGuaranteed sufficient descentStrong performance in modern solvers
Common formulas for the coefficient β

Nonlinear CG is usually combined with periodic restarts, resetting the direction to the steepest-descent direction every n iterations or whenever successive gradients lose orthogonality. Without restarts, accumulated curvature information from distant regions can degrade the search direction.

CG occupies a middle ground between first-order methods and full second-order methods. It uses curvature implicitly through matrix-vector products, which places it close to the quasi-Newton family, yet its memory footprint matches that of gradient descent.

MethodMemory costCurvature useTypical use in AI
Gradient descentOne vectorNoneBaseline and stochastic training
Conjugate gradientFour or five vectorsImplicit through Ap productsLinear solves, Hessian-free training
L-BFGSRoughly 2m vectorsLow-rank Hessian approximationFull-batch smooth problems
Newton’s methodFull n × n matrixExact HessianSmall or structured problems
Position of CG among neighboring methods

A key distinction concerns stochastic gradients. CG assumes consistent gradient and curvature information, so it performs poorly with noisy mini-batch estimates. For this reason, stochastic training of deep networks relies on SGD or Adam, while CG appears in deterministic subproblems where its assumptions hold.

Where CG is used in machine learning practice

The method is most valuable whenever a large linear system must be solved but the matrix is available only through matrix-vector products. Several established techniques in artificial intelligence depend on it.

  • Hessian-free optimization, which solves the Newton system with truncated CG using Hessian-vector or Gauss-Newton products.
  • Trust region policy optimization (TRPO), which computes the natural gradient by solving a Fisher-matrix system with about ten CG iterations.
  • Gaussian process regression, where CG with fast kernel products replaces Cholesky factorization on large datasets.
  • Truncated Newton methods and trust region methods, which use the Steihaug-Toint variant of CG to respect the trust region boundary.
  • Large-scale ridge regression and kernel methods, where the regularized system is symmetric positive definite.

Limitations and common mistakes

The most frequent error is applying linear CG to a matrix that is not symmetric positive definite. With an indefinite matrix, the denominator pᵀAp can become zero or negative, and the iteration breaks down or diverges. Nonsymmetric systems require alternatives such as GMRES or BiCGSTAB.

Other pitfalls include omitting a preconditioner on ill-conditioned problems, using a loose line search in nonlinear variants, and expecting finite termination in floating-point arithmetic. In Hessian-free training, a nonconvex objective produces an indefinite Hessian, so practitioners substitute the positive semidefinite Gauss-Newton matrix or add damping.

The short version

The conjugate gradient method is a smart way to find the lowest point of a smooth, bowl-shaped surface. Plain gradient descent always walks straight downhill. On a long, narrow valley, it zigzags and wastes many steps.

CG chooses each new direction so it does not spoil earlier progress. Think of tuning a sound system with several knobs that interfere with one another. CG finds a special set of combined knob movements that work independently. Once one movement is set, it stays correct. The result is far fewer steps and very little memory, which makes the method useful for very large problems.

Where it makes a difference

A research team training a reinforcement learning agent for robotic grasping uses trust region policy optimization. Each policy update requires solving a linear system involving the Fisher information matrix of a neural network with about two million parameters. Storing that matrix would take roughly 16 terabytes in 32-bit precision, which is impossible on their hardware.

Instead, the team runs ten iterations of conjugate gradient, computing each Fisher-vector product with automatic differentiation in about the cost of two backward passes. The solve adds only a modest fraction to the time of each update, uses memory comparable to a few copies of the parameter vector, and yields a stable natural gradient step that keeps the policy within the chosen trust region.