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 matrix that costs about operations and stored numbers of bytes each in float64. At the operations and the bytes are
That is TB just to store the matrix.
Many large matrices are sparse. Take an image of pixels with one unknown per pixel. The grid Laplacian couples each unknown to its four neighbours: its row has on the diagonal and for each neighbour. It is symmetric positive definite and is behind smoothing and diffusion on images and meshes. At it has rows but only five nonzeros per row. One product with a vector costs about operations, and the matrix fits in 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 parameters has entries. Automatic differentiation still computes a Hessian–vector product 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 only through the map . Two classic ones answer the two big questions of linear algebra. Power iteration finds the top eigenvector. The conjugate gradient method (CG) solves when is symmetric positive definite.
Power iteration and its rate
Power iteration appeared in Determinants and eigenvalues: multiply a vector by again and again, normalizing each time. For a symmetric the Rayleigh quotient from Symmetric matrices and quadratic forms reads off the eigenvalue:
Let be symmetric with orthonormal eigenvectors and . Expand the start as with ; a random start has this with probability . Then
Every other component shrinks relative to at least as fast as . For the angle between and :
The eigenvalue converges faster than the vector. The Rayleigh quotient is flat at an eigenvector, so its error is at most , proportional to the square of the angle. On the running example with eigenvalues and , from :
The angle shrinks sixfold per step, the eigenvalue error about -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 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 , the ratio is . From a random start, power iteration needs products to get to six digits. SciPy's eigsh, which uses the Lanczos method of the next blocks, needs fewer than .
A system as a minimization
Now the second question: solve for a symmetric positive definite (SPD) . 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
By Matrix calculus, the gradient vanishes exactly at the solution . Since , is strictly convex, so is its only minimum. The negative gradient is the residual . How far is from the minimum in value is measured by the energy norm :
The simplest method walks along the residual and picks the step that minimizes on that line exactly. This is steepest descent:
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 per step. Accuracy takes about steps.
On the running example with the solution is . From , the relative energy-norm error after steps to is , , , , , . 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 -orthogonal:
This is ordinary orthogonality after the change of variables , which turns the elongated level sets of into spheres. Along conjugate directions, exact line searches reach : each search finishes its own direction and leaves the others alone.
Conjugate directions could be built by Gram–Schmidt in the -inner product, but then each new direction must be corrected against all the previous ones. CG gets away with one correction. Start from , , , and repeat:
A step costs one product , 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 is automatically conjugate to all old directions except the last one, so the single term is all the correction it needs.
By hand on the running example, . Step : , , , , , . One can check . Step : , , .
Two steps give the exact solution, where steepest descent still had a error after six. The first step is the same in both methods; the difference is the memory in .
Krylov subspaces
Where do CG's iterates live? Each step multiplies by once, so is a combination of . This is the Krylov subspace:
Power iteration lives in the same space but keeps only its last vector. CG uses all of it, and optimally: has the smallest energy-norm error among all vectors of .
Polynomials make this concrete. A vector of is for a polynomial of degree below . Since , the error is with . So has degree at most and . In the eigenbasis, multiplies the component along by , which gives
CG finds the best such polynomial without being told the eigenvalues. Three consequences:
- With distinct eigenvalues, vanishes on all of them, so CG is exact after steps in exact arithmetic.
- A tight cluster acts like one eigenvalue: a root in its middle makes small on the whole cluster.
- An isolated outlier costs about one extra step, one root of .
Two diagonal matrices of size have the same . Take a random and stop at a relative residual of . With eigenvalues spread evenly over , CG needs steps. With eigenvalues in and five outliers up to , it needs .
The same subspace finds eigenvalues. For a symmetric , the Lanczos method builds an orthonormal basis of by a three-term recurrence. In that basis becomes a small tridiagonal matrix, and its eigenvalues approximate the extreme eigenvalues of . That is what eigsh ran in the power-iteration example. An orthonormal basis is essential: the raw vectors 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 . The best polynomial with on an interval is a rescaled Chebyshev polynomial, and it gives
This is steepest descent's factor with replaced by . The step counts for accuracy are
At and that is about steps against . On the grid Laplacian with , . To shrink the energy-norm error a millionfold from a random right-hand side, steepest descent takes steps (bound ) and CG takes (bound ). CG beats its bound because it adapts to the actual eigenvalues.
Momentum from Unconstrained optimization also reaches the rate on a quadratic, but only with and tuned from and . CG has nothing to tune: and come from the current vectors.
At the grid has unknowns and . CG reaches a relative residual of in steps: about operations and under MB of memory, against operations and TB for dense LU.
Floating point bends two promises. The residuals slowly lose their orthogonality, so "exact after steps" fails; CG still converges, only later. And CG stops on a small residual , which, as Conditioning and numerical stability showed, allows a relative error up to times larger.
Preconditioning and where ML uses CG
The cure for a large was named in Conditioning and numerical stability: preconditioning. Change variables so that is closer to , and run CG on the new system. With the method can be written in the original variables. Each step then needs one extra solve , so must approximate and be cheap to solve with. The extremes: is plain CG, and gives but costs as much as the original problem.
The simplest choice is the Jacobi preconditioner : solving with it is one division per entry. It rescales the variables so that gets ones on the diagonal, much like standardizing features. Ridge regression with features of scales from to has . CG needs steps on this system, more than ten times its size, because rounding breaks the exact finish. With Jacobi, and steps suffice. For sparse problems, incomplete Cholesky factors but drops the fill-in.
ML uses these methods where factorizations do not fit:
- Newton-CG (Hessian-free optimization). The Newton step 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 with up to points is solved by preconditioned CG with kernel products computed on a GPU, as in GPyTorch. The noise term is the jitter from the previous topic: it bounds .
- 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 , 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 digits in float64, 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 reaches millions, factorizations give way to products : power iteration and Lanczos for top eigenvectors, CG for SPD systems in about steps, preconditioning to shrink 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.
Answers are saved on the server and tied to this browser until you sign in.