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.
| Quantity | Formula | Role |
|---|---|---|
| Step length α | rᵀr / pᵀAp | Exact minimizer along the current direction |
| Iterate x | x + αp | Moves toward the solution |
| Residual r | r − αAp | Negative gradient at the new point |
| Coefficient β | r_newᵀr_new / r_oldᵀr_old | Keeps the next direction conjugate |
| Direction p | r + βp | Next conjugate search direction |
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 descent | Conjugate gradient |
|---|---|---|
| 10 | About 70 | About 20 |
| 1,000 | About 7,000 | About 220 |
| 100,000 | About 700,000 | About 2,200 |
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.
| Variant | Key property | Typical behavior |
|---|---|---|
| Fletcher-Reeves | Global convergence under strong Wolfe search | Can stall with very small steps |
| Polak-Ribière (PR+) | Restarts automatically when β turns negative | Usually the most efficient in practice |
| Hestenes-Stiefel | Matches linear CG on quadratics | Sensitive to line search accuracy |
| Dai-Yuan | Descent direction under weak Wolfe search | Robust but sometimes slower |
| Hager-Zhang | Guaranteed sufficient descent | Strong performance in modern solvers |
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.
Conjugate gradient compared with related optimizers
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.
| Method | Memory cost | Curvature use | Typical use in AI |
|---|---|---|---|
| Gradient descent | One vector | None | Baseline and stochastic training |
| Conjugate gradient | Four or five vectors | Implicit through Ap products | Linear solves, Hessian-free training |
| L-BFGS | Roughly 2m vectors | Low-rank Hessian approximation | Full-batch smooth problems |
| Newton’s method | Full n × n matrix | Exact Hessian | Small or structured problems |
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.