Search

Search papers and lessons

Conjugate Gradient

Level 3 · 12 min read

Conjugate gradient solves a large symmetric positive definite linear system by walking along directions that do not undo each other, reaching the answer in at most n steps in exact arithmetic.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Discrete math
  • Probability
  • Losses & regularization
  • Optimizers
  • ReadReady to readNot yet

TL;DR

Many scientific and machine learning problems reduce to solving Ax=bA\mathbf{x} = \mathbf{b}, where AA is a huge matrix and x\mathbf{x} is the unknown vector. When AA 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 nn 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 w\mathbf{w} by solving (X⊤X+λI)w=X⊤y(X^\top X + \lambda I)\mathbf{w} = X^\top \mathbf{y}. The matrix on the left is symmetric and positive definite when λ>0\lambda \gt 0. 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:

2x+y=3x+2y=0soA=(2112),b=(30).\begin{aligned} 2x + y &= 3 \\ x + 2y &= 0 \end{aligned} \qquad\text{so}\qquad A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix},\quad \mathbf{b} = \begin{pmatrix} 3 \\ 0 \end{pmatrix}.

The answer is x=(2,−1)\mathbf{x} = (2, -1). Check: 2⋅2+(−1)=32 \cdot 2 + (-1) = 3 and 2+2⋅(−1)=02 + 2 \cdot (-1) = 0. We will find it without knowing it in advance.

  1. Start with a guess. Let x0=(0,0)\mathbf{x}_0 = (0, 0). The residual r0=b−Ax0=(3,0)\mathbf{r}_0 = \mathbf{b} - A\mathbf{x}_0 = (3, 0) says how far off the guess is, and it points in the direction of steepest descent of the bowl. Set the first search direction p0=r0=(3,0)\mathbf{p}_0 = \mathbf{r}_0 = (3, 0).
  2. Choose how far to go along p0\mathbf{p}_0. The step length is α0=r0⊤r0p0⊤Ap0\alpha_0 = \dfrac{\mathbf{r}_0^\top \mathbf{r}_0}{\mathbf{p}_0^\top A \mathbf{p}_0}. Here r0⊤r0=9\mathbf{r}_0^\top\mathbf{r}_0 = 9, Ap0=(6,3)A\mathbf{p}_0 = (6, 3), and p0⊤Ap0=18\mathbf{p}_0^\top A \mathbf{p}_0 = 18, so α0=12\alpha_0 = \tfrac{1}{2}. Check: this is the step that makes the bowl as low as possible along that line.
  3. Take the step. x1=x0+12p0=(1.5, 0)\mathbf{x}_1 = \mathbf{x}_0 + \tfrac{1}{2}\mathbf{p}_0 = (1.5,\ 0). The new residual is r1=(0,−1.5)\mathbf{r}_1 = (0, -1.5). Check: Ax1=(3,1.5)A\mathbf{x}_1 = (3, 1.5), so b−Ax1=(0,−1.5)\mathbf{b} - A\mathbf{x}_1 = (0, -1.5).
  4. Choose the next direction so it does not undo the last one. Plain steepest descent would now take r1\mathbf{r}_1 again. Conjugate gradient instead mixes in the old direction: p1=r1+β0p0\mathbf{p}_1 = \mathbf{r}_1 + \beta_0 \mathbf{p}_0, where β0=r1⊤r1r0⊤r0=2.259=14\beta_0 = \dfrac{\mathbf{r}_1^\top\mathbf{r}_1}{\mathbf{r}_0^\top\mathbf{r}_0} = \tfrac{2.25}{9} = \tfrac{1}{4}. So p1=(0,−1.5)+14(3,0)=(0.75,−1.5)\mathbf{p}_1 = (0, -1.5) + \tfrac{1}{4}(3, 0) = (0.75, -1.5). Check the key property: p0⊤Ap1=(3,0)⋅(A(0.75,−1.5))\mathbf{p}_0^\top A\mathbf{p}_1 = (3,0)\cdot(A(0.75,-1.5)). Since A(0.75,−1.5)=(0, −2.25)A(0.75, -1.5) = (0,\ -2.25), the product is 3⋅0+0⋅(−2.25)=03 \cdot 0 + 0 \cdot (-2.25) = 0. The two directions are conjugate, meaning they do not interfere when measured through AA.
  5. Take the second step. α1=r1⊤r1p1⊤Ap1=2.253.375=23\alpha_1 = \dfrac{\mathbf{r}_1^\top\mathbf{r}_1}{\mathbf{p}_1^\top A\mathbf{p}_1} = \dfrac{2.25}{3.375} = \dfrac{2}{3}. Then x2=(1.5,0)+23(0.75,−1.5)=(2,−1)\mathbf{x}_2 = (1.5, 0) + \tfrac{2}{3}(0.75, -1.5) = (2, -1). Check: this is the answer, and r2=b−Ax2=(0,0)\mathbf{r}_2 = \mathbf{b} - A\mathbf{x}_2 = (0, 0).

The system has two unknowns and took two steps. That is no accident, as the math section explains.

−1−0.500.511.522.53−2−1012xy
  • 2x + y = 3
  • x + 2y = 0
The two lines cross at (2, -1), the solution. The iterate (1.5, 0) after one step lies on the first line but not on the second, so it is not yet the answer.

The math

Step 1 in symbols: the bowl

For symmetric positive definite AA, the solution of Ax=bA\mathbf{x} = \mathbf{b} is the unique minimiser of

f(x)=12x⊤Ax−b⊤x.f(\mathbf{x}) = \tfrac{1}{2}\mathbf{x}^\top A\mathbf{x} - \mathbf{b}^\top\mathbf{x}.

This is a quadratic form plus a linear term, a bowl. Its Gradient is ∇f(x)=Ax−b\nabla f(\mathbf{x}) = A\mathbf{x} - \mathbf{b}, which is zero exactly at the solution. The negative gradient is the residual, r=b−Ax\mathbf{r} = \mathbf{b} - A\mathbf{x}, so the first direction is steepest descent, as in Gradient Descent.

Step 2 in symbols: the step length

Move from xk\mathbf{x}_k along a direction pk\mathbf{p}_k by αkpk\alpha_k \mathbf{p}_k. The value of ff along that line is a parabola in α\alpha, and its minimum is at

αk=rk⊤rkpk⊤Apk.\alpha_k = \frac{\mathbf{r}_k^\top\mathbf{r}_k}{\mathbf{p}_k^\top A\mathbf{p}_k}.

Check with step 2 of the walkthrough: 9/18=129 / 18 = \tfrac12.

Step 3 in symbols: the residual update

After the step, xk+1=xk+αkpk\mathbf{x}_{k+1} = \mathbf{x}_k + \alpha_k \mathbf{p}_k, and the new residual is

rk+1=rk−αkApk.\mathbf{r}_{k+1} = \mathbf{r}_k - \alpha_k A\mathbf{p}_k.

This avoids a second multiplication by AA per step. Check: (3,0)−12(6,3)=(0,−1.5)(3,0) - \tfrac12 (6,3) = (0,-1.5), which matches the walkthrough.

Step 4 in symbols: conjugate directions

Two directions pi\mathbf{p}_i and pj\mathbf{p}_j are conjugate (or AA-orthogonal, a version of the dot product with AA inserted) if pi⊤Apj=0\mathbf{p}_i^\top A\mathbf{p}_j = 0 for i≠ji \ne j. Conjugate gradient builds each new direction as

pk+1=rk+1+βkpk,βk=rk+1⊤rk+1rk⊤rk.\mathbf{p}_{k+1} = \mathbf{r}_{k+1} + \beta_k \mathbf{p}_k, \qquad \beta_k = \frac{\mathbf{r}_{k+1}^\top\mathbf{r}_{k+1}}{\mathbf{r}_k^\top\mathbf{r}_k}.

The choice of βk\beta_k is what makes pk+1\mathbf{p}_{k+1} conjugate to all earlier directions. Check with step 4: β0=2.25/9=14\beta_0 = 2.25/9 = \tfrac14, and the product p0⊤Ap1=0\mathbf{p}_0^\top A\mathbf{p}_1 = 0.

Why it finishes in nn steps

The residuals r0,…,rn−1\mathbf{r}_0, \dots, \mathbf{r}_{n-1} are mutually orthogonal in the ordinary sense, at right angles. The iterate xk\mathbf{x}_k minimises ff over x0\mathbf{x}_0 plus the Krylov subspace: all combinations of r0,Ar0,…,Ak−1r0\mathbf{r}_0, A\mathbf{r}_0, \dots, A^{k-1}\mathbf{r}_0. Each step adds one new dimension to this subspace until it fills the whole space. An nn-dimensional space has at most nn independent directions, so after at most nn 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 κ=λmax⁡/λmin⁡\kappa = \lambda_{\max}/\lambda_{\min} is the ratio of the largest to the smallest eigenvalue of AA (its condition number), the error after kk steps, measured in the AA-norm (a norm that weighs each direction by AA), satisfies

∥ek∥A≤2(κ−1κ+1)k∥e0∥A.\|\mathbf{e}_k\|_A \le 2\left(\frac{\sqrt{\kappa}-1}{\sqrt{\kappa}+1}\right)^{k} \|\mathbf{e}_0\|_A.

Small κ\kappa means fast convergence. Large κ\kappa means many steps, which is why preconditioning (rescaling AA before solving) is used in practice.

Worked examples

Example
1. A tiny case: a symmetric 2 by 2 system

Solve Ax=bA\mathbf{x} = \mathbf{b} with A=(2112)A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} and b=(3,3)\mathbf{b} = (3, 3). Start at x0=(0,0)\mathbf{x}_0 = (0, 0).

  1. r0=b=(3,3)\mathbf{r}_0 = \mathbf{b} = (3, 3), and p0=r0\mathbf{p}_0 = \mathbf{r}_0.
  2. r0⊤r0=9+9=18\mathbf{r}_0^\top\mathbf{r}_0 = 9 + 9 = 18.
  3. Ap0=(2⋅3+3, 3+2⋅3)=(9,9)A\mathbf{p}_0 = (2\cdot 3 + 3,\ 3 + 2\cdot 3) = (9, 9).
  4. p0⊤Ap0=3⋅9+3⋅9=54\mathbf{p}_0^\top A\mathbf{p}_0 = 3 \cdot 9 + 3 \cdot 9 = 54.
  5. α0=18/54=13\alpha_0 = 18/54 = \tfrac13.
  6. x1=(0,0)+13(3,3)=(1,1)\mathbf{x}_1 = (0,0) + \tfrac13 (3,3) = (1, 1).
  7. r1=b−Ax1=(3,3)−(3,3)=(0,0)\mathbf{r}_1 = \mathbf{b} - A\mathbf{x}_1 = (3,3) - (3,3) = (0,0). The residual is zero, so x1\mathbf{x}_1 is the answer. Check: A(1,1)=(3,3)A(1,1) = (3,3).
Example
2. The standard case: a system with fractions

Solve Ax=bA\mathbf{x} = \mathbf{b} with A=(4113)A = \begin{pmatrix} 4 & 1 \\ 1 & 3 \end{pmatrix} and b=(1,2)\mathbf{b} = (1, 2). The exact answer is x=(111,711)\mathbf{x} = (\tfrac{1}{11}, \tfrac{7}{11}), since 4⋅111+711=14 \cdot \tfrac{1}{11} + \tfrac{7}{11} = 1 and 111+3⋅711=2\tfrac{1}{11} + 3 \cdot \tfrac{7}{11} = 2.

  1. Start at x0=(0,0)\mathbf{x}_0 = (0, 0), so r0=(1,2)\mathbf{r}_0 = (1, 2) and p0=(1,2)\mathbf{p}_0 = (1, 2). Then r0⊤r0=5\mathbf{r}_0^\top\mathbf{r}_0 = 5.
  2. Ap0=(4+2, 1+6)=(6,7)A\mathbf{p}_0 = (4 + 2,\ 1 + 6) = (6, 7), and p0⊤Ap0=6+14=20\mathbf{p}_0^\top A\mathbf{p}_0 = 6 + 14 = 20. So α0=520=14\alpha_0 = \tfrac{5}{20} = \tfrac14.
  3. x1=(14,12)\mathbf{x}_1 = (\tfrac14, \tfrac12). Then r1=(1,2)−(1.5, 1.75)=(−0.5, 0.25)\mathbf{r}_1 = (1,2) - (1.5,\ 1.75) = (-0.5,\ 0.25). Check: Ax1=(4⋅14+12, 14+3⋅12)=(1.5,1.75)A\mathbf{x}_1 = (4\cdot\tfrac14 + \tfrac12,\ \tfrac14 + 3\cdot\tfrac12) = (1.5, 1.75).
  4. r1⊤r1=0.25+0.0625=0.3125=516\mathbf{r}_1^\top\mathbf{r}_1 = 0.25 + 0.0625 = 0.3125 = \tfrac{5}{16}, so β0=5/165=116\beta_0 = \dfrac{5/16}{5} = \tfrac{1}{16}.
  5. p1=r1+116p0=(−716, 38)\mathbf{p}_1 = \mathbf{r}_1 + \tfrac{1}{16}\mathbf{p}_0 = (-\tfrac{7}{16},\ \tfrac38).
  6. Ap1=(−118, 1116)A\mathbf{p}_1 = (-\tfrac{11}{8},\ \tfrac{11}{16}), and p1⊤Ap1=77128+33128=5564\mathbf{p}_1^\top A\mathbf{p}_1 = \tfrac{77}{128} + \tfrac{33}{128} = \tfrac{55}{64}.
  7. α1=5/1655/64=411\alpha_1 = \dfrac{5/16}{55/64} = \tfrac{4}{11}.
  8. x2=(14,12)+411(−716,38)=(111,711)\mathbf{x}_2 = (\tfrac14, \tfrac12) + \tfrac{4}{11}(-\tfrac{7}{16}, \tfrac38) = (\tfrac{1}{11}, \tfrac{7}{11}). This is the exact answer after two steps.
Example
3. A realistic case: an error bound for a large system

A structural model gives a symmetric positive definite matrix with condition number κ=100\kappa = 100. The engineer wants the error in the energy norm to drop by a factor of 10610^{6} (a millionfold).

  1. The bound is 2(κ−1κ+1)k2\left(\dfrac{\sqrt{\kappa}-1}{\sqrt{\kappa}+1}\right)^k. With κ=100\kappa = 100, κ=10\sqrt{\kappa} = 10, so the ratio is 911≈0.818\dfrac{9}{11} \approx 0.818.
  2. After 10 steps the bound is 2⋅0.81810≈2⋅0.134=0.272 \cdot 0.818^{10} \approx 2 \cdot 0.134 = 0.27. The error is cut to about a quarter, which is not yet enough.
  3. We need 2⋅0.818k≤10−62 \cdot 0.818^{k} \le 10^{-6}. Taking logarithms, k≥ln⁡(2⋅106)ln⁡(11/9)=14.510.2007≈72.3k \ge \dfrac{\ln(2 \cdot 10^6)}{\ln(11/9)} = \dfrac{14.51}{0.2007} \approx 72.3.
  4. 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 κ=4\kappa = 4. Find the bound on the error after 5 steps, as a fraction of the starting error. Answer: 4=2\sqrt{4} = 2, so the ratio is 2−12+1=13\tfrac{2-1}{2+1} = \tfrac13, and the bound is 2⋅(13)5=2243≈0.00822 \cdot (\tfrac13)^5 = \tfrac{2}{243} \approx 0.0082.

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 AA has been applied: pi⊤Apj=0\mathbf{p}_i^\top A\mathbf{p}_j = 0. Plain orthogonal directions would not give the finite-step guarantee.

It is tempting to think that the nn-step guarantee means the method is always fast. In fact for large nn and a large κ\kappa, it can need close to nn 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 Ax=bA\mathbf{x} = \mathbf{b} for symmetric positive definite AA, which is the same as finding the bottom of a bowl 12x⊤Ax−b⊤x\tfrac12\mathbf{x}^\top A\mathbf{x} - \mathbf{b}^\top\mathbf{x}.
  • Each step moves along a direction pk\mathbf{p}_k by the length αk\alpha_k that minimises the bowl along that line.
  • The new direction mixes the new residual with the old direction, so that all directions are conjugate: pi⊤Apj=0\mathbf{p}_i^\top A\mathbf{p}_j = 0.
  • In exact arithmetic it finishes in at most nn steps. The error bound depends on κ\sqrt{\kappa}, 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 AA have for conjugate gradient to apply, and what is the bowl that the method minimises?Show answer

AA must be symmetric and positive definite. The method minimises f(x)=12x⊤Ax−b⊤xf(\mathbf{x}) = \tfrac12\mathbf{x}^\top A\mathbf{x} - \mathbf{b}^\top\mathbf{x}, whose lowest point is the solution of Ax=bA\mathbf{x} = \mathbf{b}.

QuestionFor A=(2001)A = \begin{pmatrix} 2 & 0 \\ 0 & 1 \end{pmatrix} and b=(4,3)\mathbf{b} = (4, 3), starting at x0=(0,0)\mathbf{x}_0 = (0,0), find r0\mathbf{r}_0, p0\mathbf{p}_0 and α0\alpha_0. Give the numbers.Show answer

r0=p0=(4,3)\mathbf{r}_0 = \mathbf{p}_0 = (4, 3). Then r0⊤r0=16+9=25\mathbf{r}_0^\top\mathbf{r}_0 = 16 + 9 = 25, Ap0=(8,3)A\mathbf{p}_0 = (8, 3), and p0⊤Ap0=32+9=41\mathbf{p}_0^\top A\mathbf{p}_0 = 32 + 9 = 41. So α0=25/41≈0.610\alpha_0 = 25/41 \approx 0.610.

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 AA. 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 κ=9\kappa = 9 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 10−310^{-3}.Show answer

The ratio is 9−19+1=24=12\dfrac{\sqrt{9}-1}{\sqrt{9}+1} = \dfrac{2}{4} = \dfrac12. The bound is 2⋅(1/2)k≤10−32 \cdot (1/2)^k \le 10^{-3}, so (1/2)k≤5⋅10−4(1/2)^k \le 5 \cdot 10^{-4}, which means k≥log⁡2(2000)≈10.97k \ge \log_2(2000) \approx 10.97. Answer: 11 steps.

Sources