Skip to content
BIASED

Iterative methods: power iteration and conjugate gradient

Methods that need only products $v \mapsto Av$: power iteration and its rate $\lvert\lambda_2/\lambda_1\rvert$, a linear system as a minimization, conjugate gradient, Krylov subspaces, convergence in about $\sqrt\kappa$ steps, preconditioning and where ML uses it — and how the whole course fits into training a model.

Only matrix–vector products

The topic Conditioning and numerical stability ended with good solvers: LU, Cholesky and QR are backward stable and lose only the digits the problem itself demands. All of them factor the matrix. For a dense n×nn \times n matrix that costs about 23n3\tfrac23 n^3 operations and n2n^2 stored numbers of 88 bytes each in float64. At n=106n = 10^6 the operations and the bytes are

23n3≈6.7⋅1017,8n2=8⋅1012.\tfrac23 n^3 \approx 6.7 \cdot 10^{17}, \qquad 8 n^2 = 8 \cdot 10^{12}.

That is 88 TB just to store the matrix.

Many large matrices are sparse. Take an image of N×NN \times N pixels with one unknown per pixel. The grid Laplacian couples each unknown to its four neighbours: its row has 44 on the diagonal and −1-1 for each neighbour. It is symmetric positive definite and is behind smoothing and diffusion on images and meshes. At N=1000N = 1000 it has n=106n = 10^6 rows but only five nonzeros per row. One product with a vector costs about 10710^7 operations, and the matrix fits in 6464 MB. A factorization would fill in many of the zeros and lose this advantage.

Some matrices in ML are never stored at all. The Hessian of a network with 10710^7 parameters has 101410^{14} entries. Automatic differentiation still computes a Hessian–vector product ∇2L(θ) v\nabla^2 L(\theta)\, v for the cost of about two gradients. The kernel matrix of a Gaussian process can be multiplied by a vector block by block, without ever being written down.

So this topic looks for methods that touch AA only through the map v↦Avv \mapsto Av. Two classic ones answer the two big questions of linear algebra. Power iteration finds the top eigenvector. The conjugate gradient method (CG) solves Ax=bAx = b when AA is symmetric positive definite.

Power iteration and its rate

Power iteration appeared in Determinants and eigenvalues: multiply a vector by AA again and again, normalizing each time. For a symmetric AA the Rayleigh quotient from Symmetric matrices and quadratic forms reads off the eigenvalue:

xk+1=Axk∥Axk∥,μk=R(xk)=xk⊤Axkxk⊤xk.x_{k+1} = \frac{A x_k}{\lVert A x_k \rVert}, \qquad \mu_k = R(x_k) = \frac{x_k^\top A x_k}{x_k^\top x_k}.

Let AA be symmetric with orthonormal eigenvectors qiq_i and ∣λ1∣>∣λ2∣≥⋯≥∣λn∣\lvert\lambda_1\rvert > \lvert\lambda_2\rvert \ge \dots \ge \lvert\lambda_n\rvert. Expand the start as x0=∑iciqix_0 = \sum_i c_i q_i with c1≠0c_1 \ne 0; a random start has this with probability 11. Then

Akx0=c1λ1k(q1+∑i≥2cic1(λiλ1)kqi).A^k x_0 = c_1 \lambda_1^k \Big( q_1 + \sum_{i \ge 2} \frac{c_i}{c_1} \Big( \frac{\lambda_i}{\lambda_1} \Big)^{k} q_i \Big).

Every other component shrinks relative to q1q_1 at least as fast as ∣λ2/λ1∣k\lvert\lambda_2/\lambda_1\rvert^k. For the angle θk\theta_k between xkx_k and q1q_1:

tan⁡θk≤∣λ2λ1∣ktan⁡θ0.\tan\theta_k \le \Big\lvert \frac{\lambda_2}{\lambda_1} \Big\rvert^{k} \tan\theta_0.

The eigenvalue converges faster than the vector. The Rayleigh quotient is flat at an eigenvector, so its error is at most (λ1−λmin⁡)sin⁡2θk(\lambda_1 - \lambda_{\min})\sin^2\theta_k, proportional to the square of the angle. On the running example A=(5222)A = \begin{pmatrix} 5 & 2 \\ 2 & 2 \end{pmatrix} with eigenvalues 66 and 11, from x0=(1,0)x_0 = (1, 0):

kk 00 11 22 33
tan⁡θk\tan\theta_k 0.50.5 0.0830.083 0.0140.014 0.00230.0023
6−μk6 - \mu_k 11 3.4⋅10−23.4 \cdot 10^{-2} 9.6⋅10−49.6 \cdot 10^{-4} 2.7⋅10−52.7 \cdot 10^{-5}

The angle shrinks sixfold per step, the eigenvalue error about 3636-fold.

ML uses it where one top direction is enough. Spectral normalization in GANs divides each weight matrix by its largest singular value, estimated by power iteration. The safe step 1/L1/L from Unconstrained optimization needs the largest Hessian eigenvalue, and Hessian–vector products give it. PageRank is power iteration on a matrix with billions of rows.

The weak spot is a small gap. For the grid Laplacian with N=100N = 100, the ratio is λ2/λ1=0.99964\lambda_2/\lambda_1 = 0.99964. From a random start, power iteration needs 17,14217{,}142 products to get λ1\lambda_1 to six digits. SciPy's eigsh, which uses the Lanczos method of the next blocks, needs fewer than 300300.

A system as a minimization

Now the second question: solve Ax=bAx = b for a symmetric positive definite (SPD) AA. Such systems are everywhere in ML: the normal equations and ridge regression, a Newton step, a Gaussian process with a kernel matrix plus jitter. The trick is to turn the system into a minimization. Take

f(x)=12x⊤Ax−b⊤x,∇f(x)=Ax−b.f(x) = \tfrac12 x^\top A x - b^\top x, \qquad \nabla f(x) = Ax - b.

By Matrix calculus, the gradient vanishes exactly at the solution x∗x^*. Since A≻0A \succ 0, ff is strictly convex, so x∗x^* is its only minimum. The negative gradient is the residual r=b−Axr = b - Ax. How far xx is from the minimum in value is measured by the energy norm ∥v∥A=v⊤Av\lVert v \rVert_A = \sqrt{v^\top A v}:

f(x)−f(x∗)=12∥x−x∗∥A2.f(x) - f(x^*) = \tfrac12 \lVert x - x^* \rVert_A^2.

The simplest method walks along the residual and picks the step that minimizes ff on that line exactly. This is steepest descent:

αk=rk⊤rkrk⊤Ark,xk+1=xk+αkrk.\alpha_k = \frac{r_k^\top r_k}{r_k^\top A r_k}, \qquad x_{k+1} = x_k + \alpha_k r_k.

After an exact line search the new residual is orthogonal to the old one, so the path zigzags at right angles. The rate is the one from Unconstrained optimization: the energy-norm error shrinks by at least (κ−1)/(κ+1)(\kappa - 1)/(\kappa + 1) per step. Accuracy ε\varepsilon takes about κ2ln⁡(1/ε)\tfrac{\kappa}{2}\ln(1/\varepsilon) steps.

On the running example with b=(1,1)b = (1, 1) the solution is x∗=(0,12)x^* = (0, \tfrac12). From x0=0x_0 = 0, the relative energy-norm error after steps 11 to 66 is 0.520.52, 0.270.27, 0.140.14, 0.0740.074, 0.0390.039, 0.0200.020. It roughly halves each step and never reaches zero, in only two dimensions. Each step undoes part of the work of the previous ones.

Conjugate gradient

Steps that do not undo each other move along conjugate directions, also called AA-orthogonal:

di⊤Adj=0,i≠j.d_i^\top A d_j = 0, \qquad i \ne j.

This is ordinary orthogonality after the change of variables y=A1/2xy = A^{1/2} x, which turns the elongated level sets of ff into spheres. Along nn conjugate directions, nn exact line searches reach x∗x^*: each search finishes its own direction and leaves the others alone.

Conjugate directions could be built by Gram–Schmidt in the AA-inner product, but then each new direction must be corrected against all the previous ones. CG gets away with one correction. Start from x0=0x_0 = 0, r0=br_0 = b, d0=r0d_0 = r_0, and repeat:

αk=rk⊤rkdk⊤Adk,xk+1=xk+αkdk,rk+1=rk−αkAdk,βk=rk+1⊤rk+1rk⊤rk,dk+1=rk+1+βkdk.\begin{aligned} \alpha_k &= \frac{r_k^\top r_k}{d_k^\top A d_k}, & x_{k+1} &= x_k + \alpha_k d_k, & r_{k+1} &= r_k - \alpha_k A d_k, \\ \beta_k &= \frac{r_{k+1}^\top r_{k+1}}{r_k^\top r_k}, & d_{k+1} &= r_{k+1} + \beta_k d_k. & & \end{aligned}

A step costs one product AdkA d_k, two inner products and three vector updates, and the method stores four vectors. An induction shows that the residuals are mutually orthogonal and the directions mutually conjugate. The new residual rk+1r_{k+1} is automatically conjugate to all old directions except the last one, so the single term βkdk\beta_k d_k is all the correction it needs.

By hand on the running example, b=(1,1)b = (1, 1). Step 00: Ad0=(7,4)A d_0 = (7, 4), α0=2/11\alpha_0 = 2/11, x1=(2/11,2/11)x_1 = (2/11, 2/11), r1=(−3/11,3/11)r_1 = (-3/11, 3/11), β0=9/121\beta_0 = 9/121, d1=(−24,42)/121d_1 = (-24, 42)/121. One can check d1⊤Ad0=0d_1^\top A d_0 = 0. Step 11: α1=11/12\alpha_1 = 11/12, x2=(0,12)x_2 = (0, \tfrac12), r2=0r_2 = 0.

Two steps give the exact solution, where steepest descent still had a 2%2\% error after six. The first step is the same in both methods; the difference is the memory in βkdk\beta_k d_k.

Krylov subspaces

Where do CG's iterates live? Each step multiplies by AA once, so xkx_k is a combination of b,Ab,…,Ak−1bb, Ab, \dots, A^{k-1}b. This is the Krylov subspace:

Kk(A,b)=span⁡{b,Ab,…,Ak−1b}.\mathcal K_k(A, b) = \operatorname{span}\{b, Ab, \dots, A^{k-1} b\}.

Power iteration lives in the same space but keeps only its last vector. CG uses all of it, and optimally: xkx_k has the smallest energy-norm error among all vectors of Kk\mathcal K_k.

Polynomials make this concrete. A vector of Kk\mathcal K_k is q(A) bq(A)\,b for a polynomial qq of degree below kk. Since b=Ax∗b = Ax^*, the error is q(A)Ax∗−x∗=−p(A)x∗q(A)Ax^* - x^* = -p(A)x^* with p(λ)=1−λq(λ)p(\lambda) = 1 - \lambda q(\lambda). So pp has degree at most kk and p(0)=1p(0) = 1. In the eigenbasis, p(A)p(A) multiplies the component along qiq_i by p(λi)p(\lambda_i), which gives

∥xk−x∗∥A≤min⁡deg⁡p≤k, p(0)=1 max⁡i∣p(λi)∣⋅∥x∗∥A.\lVert x_k - x^* \rVert_A \le \min_{\deg p \le k,\ p(0) = 1}\ \max_i \lvert p(\lambda_i) \rvert \cdot \lVert x^* \rVert_A.

CG finds the best such polynomial without being told the eigenvalues. Three consequences:

  • With mm distinct eigenvalues, p(λ)=∏j=1m(1−λ/λj)p(\lambda) = \prod_{j=1}^{m} (1 - \lambda/\lambda_j) vanishes on all of them, so CG is exact after mm steps in exact arithmetic.
  • A tight cluster acts like one eigenvalue: a root in its middle makes pp small on the whole cluster.
  • An isolated outlier costs about one extra step, one root of pp.

Two diagonal matrices of size 10001000 have the same κ=1000\kappa = 1000. Take a random bb and stop at a relative residual of 10−810^{-8}. With eigenvalues spread evenly over [1,1000][1, 1000], CG needs 171171 steps. With 995995 eigenvalues in [1,2][1, 2] and five outliers up to 10001000, it needs 2323.

The same subspace finds eigenvalues. For a symmetric AA, the Lanczos method builds an orthonormal basis of Kk\mathcal K_k by a three-term recurrence. In that basis AA becomes a small tridiagonal k×kk \times k matrix, and its eigenvalues approximate the extreme eigenvalues of AA. That is what eigsh ran in the power-iteration example. An orthonormal basis is essential: the raw vectors b,Ab,A2b,…b, Ab, A^2 b, \dots turn almost parallel, which is power iteration at work.

The square root of the condition number

For a worst-case bound, assume only that the eigenvalues lie in [λmin⁡,λmax⁡][\lambda_{\min}, \lambda_{\max}]. The best polynomial with p(0)=1p(0) = 1 on an interval is a rescaled Chebyshev polynomial, and it gives

∥xk−x∗∥A≤2(κ−1κ+1)k∥x0−x∗∥A.\lVert x_k - x^* \rVert_A \le 2 \left( \frac{\sqrt\kappa - 1}{\sqrt\kappa + 1} \right)^{k} \lVert x_0 - x^* \rVert_A.

This is steepest descent's factor with κ\kappa replaced by κ\sqrt\kappa. The step counts for accuracy ε\varepsilon are

kSD≈κ2ln⁡1ε,kCG≈κ2ln⁡2ε.k_{\text{SD}} \approx \frac{\kappa}{2} \ln\frac{1}{\varepsilon}, \qquad k_{\text{CG}} \approx \frac{\sqrt\kappa}{2} \ln\frac{2}{\varepsilon}.

At κ=104\kappa = 10^4 and ε=10−6\varepsilon = 10^{-6} that is about 69,00069{,}000 steps against 725725. On the grid Laplacian with N=100N = 100, κ=4134\kappa = 4134. To shrink the energy-norm error a millionfold from a random right-hand side, steepest descent takes 25,39125{,}391 steps (bound 28,55728{,}557) and CG takes 264264 (bound 466466). CG beats its bound because it adapts to the actual eigenvalues.

Momentum from Unconstrained optimization also reaches the κ\sqrt\kappa rate on a quadratic, but only with η\eta and β\beta tuned from λmin⁡\lambda_{\min} and λmax⁡\lambda_{\max}. CG has nothing to tune: αk\alpha_k and βk\beta_k come from the current vectors.

At N=1000N = 1000 the grid has n=106n = 10^6 unknowns and κ≈4⋅105\kappa \approx 4 \cdot 10^5. CG reaches a relative residual of 10−810^{-8} in 18531853 steps: about 4⋅10104 \cdot 10^{10} operations and under 100100 MB of memory, against 6.7⋅10176.7 \cdot 10^{17} operations and 88 TB for dense LU.

Floating point bends two promises. The residuals slowly lose their orthogonality, so "exact after nn steps" fails; CG still converges, only later. And CG stops on a small residual ∥rk∥/∥b∥\lVert r_k \rVert / \lVert b \rVert, which, as Conditioning and numerical stability showed, allows a relative error up to κ\kappa times larger.

Preconditioning and where ML uses CG

The cure for a large κ\kappa was named in Conditioning and numerical stability: preconditioning. Change variables x=Pyx = P y so that P⊤APP^\top A P is closer to II, and run CG on the new system. With M=(PP⊤)−1M = (P P^\top)^{-1} the method can be written in the original variables. Each step then needs one extra solve Mz=rM z = r, so MM must approximate AA and be cheap to solve with. The extremes: M=IM = I is plain CG, and M=AM = A gives κ=1\kappa = 1 but costs as much as the original problem.

The simplest choice is the Jacobi preconditioner M=diag⁡(A)M = \operatorname{diag}(A): solving with it is one division per entry. It rescales the variables so that AA gets ones on the diagonal, much like standardizing features. Ridge regression with 200200 features of scales from 11 to 10001000 has κ≈1.2⋅106\kappa \approx 1.2 \cdot 10^6. CG needs 23592359 steps on this 200×200200 \times 200 system, more than ten times its size, because rounding breaks the exact finish. With Jacobi, κ≈6.7\kappa \approx 6.7 and 2222 steps suffice. For sparse problems, incomplete Cholesky factors AA but drops the fill-in.

ML uses these methods where factorizations do not fit:

  • Newton-CG (Hessian-free optimization). The Newton step ∇2L(θ) d=−∇L(θ)\nabla^2 L(\theta)\, d = -\nabla L(\theta) from Unconstrained optimization is solved approximately by a few CG steps that use only Hessian–vector products. The Hessian is never formed. SciPy offers it as minimize(method="Newton-CG").
  • Gaussian processes. The system (K+σ2I)α=y(K + \sigma^2 I)\alpha = y with up to 10610^6 points is solved by preconditioned CG with kernel products computed on a GPU, as in GPyTorch. The noise term σ2I\sigma^2 I is the jitter from the previous topic: it bounds κ\kappa.
  • PCA. The top principal components come from Lanczos (PCA(svd_solver="arpack") in scikit-learn) or from randomized subspace iteration, power iteration on a block of vectors at once.

How it all fits into training a model

This is the end of the course. It began with one question: what mathematics does a model need in order to represent data, learn from it and compute reliably? The short answer from the first topic was: data are vectors, models are maps, learning is optimization, and all of it runs in floating point. One training step of a classifier uses every part.

Represent. An example is a vector, a layer is a linear map x↦Wx+bx \mapsto Wx + b, and a network composes such maps with nonlinearities. Rank, projections, eigenvalues and the SVD say what these maps do: which directions they keep, stretch or flatten, and how well a low-rank map can stand in for a large one.

Learn. The loss is a function of millions of parameters. Derivatives, Jacobians and matrix calculus deliver its gradient in one backward pass. Convexity says when a zero gradient is the global answer. Gradient descent and Newton's method go looking for it, at a speed set by the curvature. Lagrange multipliers and duality handle constraints and certify how close we are.

Compute reliably. All of it runs in floating point: about 1616 digits in float64, 77 in float32, two or three in bfloat16. The condition number says how many of those digits a problem can lose, and a handful of rewrites keep the rest: the max shift in softmax, QR instead of the normal equations, formulas without cancellation, float32 master weights. When nn reaches millions, factorizations give way to products v↦Avv \mapsto Av: power iteration and Lanczos for top eigenvectors, CG for SPD systems in about κ\sqrt\kappa steps, preconditioning to shrink κ\kappa itself.

The last topic shows how tightly the parts are woven. CG solves a linear system by minimizing a quadratic, inside a Krylov subspace, at a speed set by the condition number. Power iteration is the eigenvectors of the first module turned into an algorithm. The same eigenvalues decide what a map does, how fast training goes, how many digits survive and how many products a solver needs.

When training misbehaves, with a nan in the loss, two solvers that disagree or a loss that barely moves, these are the tools that say where to look.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Power iteration: dominant eigenvalueoptional

    Run power iteration; match |λ| to np.linalg.eigvalsh/eig.

    ~20 min · easier on a computer · The matching course topic has been read.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

Answers are saved on the server and tied to this browser until you sign in.