TL;DR
Many scientific and machine learning problems reduce to solving , where is a huge matrix and is the unknown vector. When is symmetric and positive definite, this is the same as finding the bottom of a bowl-shaped function. Conjugate gradient finds that bottom by stepping along a sequence of directions that do not undo each other, so each step is kept for good. In exact arithmetic it reaches the answer in at most steps, and in practice it often stops much sooner.
Builds on: Gradient Descent, Positive Definite Matrix, Quadratic Form · Leads to: trust-region methods and natural-gradient methods used to train large models
Where it's used
Structural and physical simulation. Engineers modelling a bridge, a car body or the heat in a metal part divide the object into a mesh. The mesh gives a linear system with one unknown per point, often a million or more, and the matrix is sparse and symmetric positive definite. Conjugate gradient is a standard way to solve these systems without ever computing a matrix inverse.
Reinforcement learning. Trust Region Policy Optimization (TRPO), a 2015 method for training policies, needs a step direction that solves a large linear system involving the curvature of the policy. The paper uses conjugate gradient to solve it approximately, using only matrix-vector products. See TRPO.
Large least-squares problems. Ridge regression chooses weights by solving . The matrix on the left is symmetric and positive definite when . With millions of features, forming a direct solution is too costly, and conjugate gradient solves the system iteratively.
How it works
Take a tiny 2 by 2 system from start to finish:
The answer is . Check: and . We will find it without knowing it in advance.
- Start with a guess. Let . The residual says how far off the guess is, and it points in the direction of steepest descent of the bowl. Set the first search direction .
- Choose how far to go along . The step length is . Here , , and , so . Check: this is the step that makes the bowl as low as possible along that line.
- Take the step. . The new residual is . Check: , so .
- Choose the next direction so it does not undo the last one. Plain steepest descent would now take again. Conjugate gradient instead mixes in the old direction: , where . So . Check the key property: . Since , the product is . The two directions are conjugate, meaning they do not interfere when measured through .
- Take the second step. . Then . Check: this is the answer, and .
The system has two unknowns and took two steps. That is no accident, as the math section explains.
- 2x + y = 3
- x + 2y = 0
The math
Step 1 in symbols: the bowl
For symmetric positive definite , the solution of is the unique minimiser of
This is a quadratic form plus a linear term, a bowl. Its Gradient is , which is zero exactly at the solution. The negative gradient is the residual, , so the first direction is steepest descent, as in Gradient Descent.
Step 2 in symbols: the step length
Move from along a direction by . The value of along that line is a parabola in , and its minimum is at
Check with step 2 of the walkthrough: .
Step 3 in symbols: the residual update
After the step, , and the new residual is
This avoids a second multiplication by per step. Check: , which matches the walkthrough.
Step 4 in symbols: conjugate directions
Two directions and are conjugate (or -orthogonal, a version of the dot product with inserted) if for . Conjugate gradient builds each new direction as
The choice of is what makes conjugate to all earlier directions. Check with step 4: , and the product .
Why it finishes in steps
The residuals are mutually orthogonal in the ordinary sense, at right angles. The iterate minimises over plus the Krylov subspace: all combinations of . Each step adds one new dimension to this subspace until it fills the whole space. An -dimensional space has at most independent directions, so after at most steps the subspace is everything and the answer is found. This holds in exact arithmetic; with floating point numbers the orthogonality slowly erodes.
Convergence
If is the ratio of the largest to the smallest eigenvalue of (its condition number), the error after steps, measured in the -norm (a norm that weighs each direction by ), satisfies
Small means fast convergence. Large means many steps, which is why preconditioning (rescaling before solving) is used in practice.
Worked examples
Solve with and . Start at .
- , and .
- .
- .
- .
- .
- .
- . The residual is zero, so is the answer. Check: .
Solve with and . The exact answer is , since and .
- Start at , so and . Then .
- , and . So .
- . Then . Check: .
- , so .
- .
- , and .
- .
- . This is the exact answer after two steps.
A structural model gives a symmetric positive definite matrix with condition number . The engineer wants the error in the energy norm to drop by a factor of (a millionfold).
- The bound is . With , , so the ratio is .
- After 10 steps the bound is . The error is cut to about a quarter, which is not yet enough.
- We need . Taking logarithms, .
- Read the result: the guaranteed bound needs about 73 steps. Each step costs one multiplication by the sparse matrix, which is far cheaper than a direct inversion of a million-by-million matrix. The bound is pessimistic, and the actual run often stops sooner.
Try it: a different system has . Find the bound on the error after 5 steps, as a fraction of the starting error. Answer: , so the ratio is , and the bound is .
Common mistakes and tradeoffs
It is tempting to think that conjugate gradient works on any matrix. In fact it needs the matrix to be symmetric and positive definite. Without symmetry, the conjugacy argument and the short recurrences break down, and other methods are used.
It is tempting to think that "conjugate" means "orthogonal." In fact conjugate directions are orthogonal after the matrix has been applied: . Plain orthogonal directions would not give the finite-step guarantee.
It is tempting to think that the -step guarantee means the method is always fast. In fact for large and a large , it can need close to steps. The payoff is that each step is cheap, and the method stops early once the residual is small enough.
When to use it. Use conjugate gradient for large, sparse, symmetric positive definite systems, where forming a factorisation would be too costly or would fill in the sparsity.
When not to use it. Do not use it for small dense systems, where a direct factorisation is simpler and faster. Do not use it for a matrix that is not symmetric positive definite without checking the method's assumptions first.
Alternatives. Direct methods such as Cholesky factorisation are best for small or dense problems. For badly conditioned systems, preconditioned conjugate gradient is standard.
History
Magnus Hestenes and Eduard Stiefel described the conjugate gradient method in 1952, as a way to solve linear systems exactly in a finite number of steps. In exact arithmetic it is a clean method, but rounding errors in computers of the time spoiled the finite-step property, so it was seen as a curiosity.
In the 1970s, researchers realised that the method was valuable as an iterative method. Stopping early gave a good approximate solution, and each step needed only a multiplication by the matrix, so it suited large sparse systems that appeared in engineering simulation. Preconditioning then made it much faster.
In 1964, Fletcher and Reeves adapted the method to minimise nonlinear functions, which is how it entered optimisation. Its ideas return in modern machine learning, in methods that need curvature information (the Hessian or its action on a vector) without forming it fully.
Recap
- Conjugate gradient solves for symmetric positive definite , which is the same as finding the bottom of a bowl .
- Each step moves along a direction by the length that minimises the bowl along that line.
- The new direction mixes the new residual with the old direction, so that all directions are conjugate: .
- In exact arithmetic it finishes in at most steps. The error bound depends on , so good conditioning means fast convergence.
- Read next: Gradient Descent for the simpler method it improves on, and Eigenvalues and Eigenvectors for the role of the eigenvalue ratio.
Check yourself
QuestionWhat two properties must the matrix have for conjugate gradient to apply, and what is the bowl that the method minimises?Show answer
must be symmetric and positive definite. The method minimises , whose lowest point is the solution of .
QuestionFor and , starting at , find , and . Give the numbers.Show answer
. Then , , and . So .
QuestionWhy is it not enough for the search directions to be orthogonal in the ordinary sense?Show answer
The finite-step guarantee comes from the fact that each new direction is conjugate to all previous ones, with respect to . Ordinary orthogonality ignores the matrix, and the step taken along one direction would then partly undo the earlier steps once the matrix stretches the space. Conjugacy keeps each step's progress intact.
QuestionA system has and a starting error of 1 in the energy norm. Use the bound to estimate how many steps are needed to reach an error of at most .Show answer
The ratio is . The bound is , so , which means . Answer: 11 steps.