Linear algebra for ML: vectors to SVD, rank, conditioning, low-rank methods

Every matrix as rotate, stretch, rotate, and what its stretches say: maps and products, shapes and einsum, rank and the four subspaces, norms, least squares, eigenvalues, the SVD, conditioning, and what is really low rank in GPT-2's weights and in real LoRA updates. A child page of Topic: math. Data as of October 2026. Reading: 45 min, plus 13 in closed details.

The idea in one screen

A matrix is a map: it rotates, stretches along a few orthogonal directions (squashing some to zero), and rotates again. That is the SVD, and its stretches, the singular values, answer almost every linear-algebra question in ML: which directions survive (rank), how big the map is (norms), how badly it amplifies errors (conditioning), and how much is lost by keeping only the biggest stretches (low-rank approximation).

Fig. 1: the running matrix A = [[3, 0], [4, 5]] is a rotation, a stretch by 6.708 and 2.236, and another rotation

  1. The input. The unit circle and two input directions v1=(1,1)/2v_1 = (1, 1)/\sqrt{2}, v2=(−1,1)/2v_2 = (-1, 1)/\sqrt{2} at right angles: the right singular vectors of AA. Each glass plate behind holds the picture after one more factor.
  2. V⊤V^\top rotates them onto the axes (by −45°-45°). A rotation keeps every length and angle: the circle is still the unit circle, and the dots stream through without spreading.
  3. Σ\Sigma stretches the axes by the singular values. Horizontally by σ1=6.708\sigma_1 = 6.708, vertically by σ2=2.236\sigma_2 = 2.236 (dashed: the unit circle, to scale). The only step that changes shape; area grows by 6.708×2.236=15=det⁡A6.708 \times 2.236 = 15 = \det A.
  4. UU rotates the result onto the output directions (by 71.57°71.57°). The arrows land on σ1u1\sigma_1 u_1 and σ2u2\sigma_2 u_2, u1=(1,3)/10u_1 = (1, 3)/\sqrt{10}, still at right angles.
  5. AA in one step lands on the same ellipse. Read off the last plate: rank 2 (two nonzero stretches), spectral norm 6.708, condition number 6.708/2.236=36.708 / 2.236 = 3.
  6. Drop σ2\sigma_2: the best rank-1 approximation. Keeping only σ1u1v1⊤=(1.51.54.54.5)\sigma_1 u_1 v_1^\top = \begin{pmatrix} 1.5 & 1.5 \\ 4.5 & 4.5 \end{pmatrix} flattens the circle onto a segment of half-length 6.708, and it misses AA by exactly σ2=2.236\sigma_2 = 2.236 (Eckart-Young, section 10).

Section 10 steps through the SVD against the eigendecomposition; the Matrix playground does it for any 2x2 or 3x3 matrix.

The one idea to keep
Ask of any matrix: which directions does it keep, how much does it stretch each, and how unequal are the stretches? The SVD answers all three, which is why it sits in the middle of this page.

You need: the running example of Topic: math (its running example): a tiny three-word model with input x=(2,1)x = (2, 1) and weight rows cat (1,0)(1, 0), dog (0,1)(0, 1), sat (1,−2)(1, -2), so WW is 3×23 \times 2. Worked numbers use it and one running 2x2 matrix AA; real numbers come from GPT-2 small's released weights and two public LoRA adapters; all of them are recomputed by the script in this page's source folder. Matrix calculus (Jacobians, layouts, gradients of matrix expressions, FLOP counts) is on Matrix calculus and backprop; floating point on Numerical computing for ML.

Singular values σi\sigma_i
the stretches of a matrix along its orthogonal input directions, largest first; every matrix has them.
Rank
how many independent directions a map keeps: the number of nonzero singular values.
Column space, null space
the outputs a map can produce; the inputs it sends to zero.
Eigenvector
a direction a square map only scales (by its eigenvalue), never turns.
Condition number κ\kappa
σmax⁡/σmin⁡\sigma_{\max} / \sigma_{\min}: how much a solve can amplify a relative error.
Effective rank
the number of singular values that are large, the useful notion of rank in floating point.
Einsum
a string that names every tensor axis; letters missing from the output are summed.
The whole page in one table (reference)
IdeaIn one lineOne number to rememberWhere ML uses it
Dot product, cosinemultiply pairwise and add; equals lengths times cosine of the anglecos⁡(x,wsat)=0\cos(x, w_{\text{sat}}) = 0: orthogonal, score 0logits, attention scores, retrieval
Matrix as mapcolumns are where the axis arrows landdet⁡A=15\det A = 15: area scales by 15every layer
Four multiplicationsdot products, column combinations, sum of outer products, compositionPQ≠QPPQ \ne QPattention, LoRA's BABA, gradients gx⊤gx^\top
Shapes, broadcasting, einsuminner sizes vanish, outer survive; size-1 axes stretch; letters missing from the output are summedMSE 1.333 for perfect predictionsevery shape bug, reading model code
Rank, four subspacesindependent directions used; what a map can output, ignore, read, never reach−zcat+2zdog+zsat=0-z_{\text{cat}} + 2z_{\text{dog}} + z_{\text{sat}} = 0 alwaysLoRA, collapse, softmax bottleneck
NormsL1 diamond, L2 circle; spectral = largest stretch‖A‖2=6.708\lVert A \rVert_2 = 6.708, ‖A‖F=7.071\lVert A \rVert_F = 7.071weight decay, clipping, Lipschitz
Projection, least squares, QRclosest point leaves an orthogonal remainder; X⊤Xw=X⊤yX^\top X w = X^\top y, better solved by QRP2=PP^2 = P; w=(1.167,0.5)w = (1.167, 0.5)linear probes, ridge, PCA, RoPE
Eigenvaluesdirections only scaled; powers become λk\lambda^kAA: 3 and 5curvature, stability, power iteration
Symmetric, PDreal eigenvalues, orthogonal eigenvectors; x⊤Sx>0x^\top S x \gt 0W⊤WW^\top W: 6 and 1, xx an eigenvectorcovariances, Hessians, Cholesky
SVDrotate, stretch, rotate; truncate for the best rank-kk fitAA: 6.708 and 2.236; error of rank 1 is 2.236compression, PCA, Muon
Conditioningκ=σmax⁡/σmin⁡\kappa = \sigma_{\max}/\sigma_{\min}: error amplificationfloat32 normal equations: 77% error, QR 0.0018%solvers, precision, preconditioning
Low rank in MLupdates and gradients are low rank; pretrained weights are notGPT-2 WQW_Q: 298 of 768 directions for 90%LoRA, MLA, GaLore
Attentiondot products, softmax rows, weighted average of valuesone head's WQWK⊤W_Q W_K^\top: rank 64 of 768every transformer
Einsumletters missing from the output are summedbhqd,bhkd->bhqkreading model code

1. Vectors and the dot product: the score behind every logit

A vector is a list of numbers and, equally, an arrow from the origin. A GPT-2 small token is a list of 768 numbers; nobody can draw 768 axes, but every rule on this page works the same in 768 dimensions as in 2, which is why the 2-D pictures are worth learning. The dot product multiplies two vectors entry by entry and adds, a⋅b=a⊤b=∑iaibia \cdot b = a^\top b = \sum_i a_i b_i, and that is how the tiny model scores each word: x⋅wcat=2⋅1+1⋅0=2x \cdot w_{\text{cat}} = 2 \cdot 1 + 1 \cdot 0 = 2, x⋅wdog=1x \cdot w_{\text{dog}} = 1, x⋅wsat=2⋅1+1⋅(−2)=0x \cdot w_{\text{sat}} = 2 \cdot 1 + 1 \cdot (-2) = 0.

The same number has a geometric reading, a⋅b=‖a‖‖b‖cos⁡θa \cdot b = \lVert a \rVert \, \lVert b \rVert \cos\theta, with the length ‖x‖2=∑ixi2\lVert x \rVert_2 = \sqrt{\sum_i x_i^2} (5≈2.236\sqrt{5} \approx 2.236 for xx). Divide out the lengths and only the direction is left, the cosine similarity cos⁡θ=a⋅b/(‖a‖‖b‖)\cos\theta = a \cdot b / (\lVert a \rVert \lVert b \rVert): 1 for the same direction, 0 at right angles (orthogonal), −1-1 opposite. For the tiny model sat is exactly orthogonal to the input, which is why its score is 0 however long the arrows are.

On real embeddings. GPT-2 small stores one 768-number vector per token in its embedding table. The cosine similarities between a few of them (computed from the released weights, script in this page's source folder):

The projection of aa onto the direction of bb is the shadow aa casts on the line through bb, projb⁡(a)=a⋅bb⋅bb\operatorname{proj}_b(a) = \frac{a \cdot b}{b \cdot b}\, b, and what is left over is orthogonal to bb. Projection is the idea behind least squares (section 7), PCA (section 10) and attention reading values (section 13).

What it is used for in ML
Embeddings are vectors and similar meanings point in similar directions; retrieval ranks documents by cosine similarity (usually after scaling every embedding to length 1, so the dot product is the cosine); attention scores are dot products of queries and keys; a logit is a dot product of the final hidden state with a row of the output matrix.
From zero: lists and arrows, length, the cosines and projection worked, and why the two definitions of the dot product agree

Two pictures of the same thing. As a list, x=(2,1)x = (2, 1) is two numbers in a fixed order, x1=2x_1 = 2 and x2=1x_2 = 1: how code stores it (a tensor of shape (2,)). How many numbers it holds is its dimension, written x∈ℝ2x \in \mathbb{R}^2 ("x is a member of the set of all lists of 2 real numbers"). As an arrow from the origin it is two steps right, one step up: how you reason about it. Adding vectors is placing arrows head to tail, (2,1)+(1,−2)=(3,−1)(2,1) + (1,-2) = (3,-1); multiplying by a number (a scalar) stretches the arrow, 3⋅(2,1)=(6,3)3 \cdot (2,1) = (6,3).

Length. The L2 norm follows from Pythagoras: ‖x‖2=22+12=5≈2.236\lVert x \rVert_2 = \sqrt{2^2 + 1^2} = \sqrt{5} \approx 2.236; the subscript 2 says "squares, then square root" (other norms are in section 6). A unit vector has length 1; dividing any nonzero vector by its length gives the unit vector pointing the same way, x/‖x‖=(0.894,0.447)x / \lVert x \rVert = (0.894, 0.447). The a⊤ba^\top b notation reads the first vector as a row and the second as a column; it is the same number (the transpose is in section 3).

Why the two definitions agree. Draw aa, bb and the third side of their triangle, a−ba - b. The law of cosines (Pythagoras for a triangle without a right angle) says ‖a−b‖2=‖a‖2+‖b‖2−2‖a‖‖b‖cos⁡θ\lVert a - b \rVert^2 = \lVert a \rVert^2 + \lVert b \rVert^2 - 2 \lVert a \rVert \lVert b \rVert \cos\theta. Expanding with entries, ‖a−b‖2=∑i(ai−bi)2=‖a‖2−2a⋅b+‖b‖2\lVert a - b \rVert^2 = \sum_i (a_i - b_i)^2 = \lVert a \rVert^2 - 2\, a \cdot b + \lVert b \rVert^2. The two expressions are equal; cancel ‖a‖2+‖b‖2\lVert a \rVert^2 + \lVert b \rVert^2 and divide by −2-2: a⋅b=‖a‖‖b‖cos⁡θa \cdot b = \lVert a \rVert \lVert b \rVert \cos\theta. Check on xx and wcatw_{\text{cat}}: 2=2.236×1×cos⁡θ2 = 2.236 \times 1 \times \cos\theta, so cos⁡θ=0.894\cos\theta = 0.894, an angle of 26.6 degrees, which is the angle of the arrow (2,1)(2, 1) above the horizontal.

The numbers. cos⁡(x,wcat)=2/5=0.894\cos(x, w_{\text{cat}}) = 2/\sqrt{5} = 0.894, cos⁡(x,wdog)=1/5=0.447\cos(x, w_{\text{dog}}) = 1/\sqrt{5} = 0.447, cos⁡(x,wsat)=0\cos(x, w_{\text{sat}}) = 0. Projecting x=(2,1)x = (2, 1) onto b=(1,1)b = (1, 1): x⋅b=3x \cdot b = 3, b⋅b=2b \cdot b = 2, so the projection is 1.5(1,1)=(1.5,1.5)1.5\,(1, 1) = (1.5, 1.5) and the leftover (0.5,−0.5)(0.5, -0.5) has (0.5,−0.5)⋅(1,1)=0(0.5, -0.5) \cdot (1, 1) = 0.

From the old page, checked: a=(1,2,2)a = (1, 2, 2), b=(2,1,2)b = (2, 1, 2) give a⋅b=8a \cdot b = 8, ‖a‖=‖b‖=3\lVert a \rVert = \lVert b \rVert = 3, cos⁡θ=8/9=0.889\cos\theta = 8/9 = 0.889; and (3,−4)⋅(4,3)=12−12=0(3, -4) \cdot (4, 3) = 12 - 12 = 0, orthogonal.

Go deeper (optional): the root's Gradient lab tab lets you move xx and the word rows; contrastive losses built on cosine similarity are on Contrastive and self-supervised learning.

The right answer, sat, is at right angles to the input, so its score is 0

The tiny model's input x=(2,1)x = (2, 1) and its three word rows: cat (1,0)(1, 0), dog (0,1)(0, 1), sat (1,−2)(1, -2). The dashed line is the projection of xx onto the cat arrow. Every number comes from the root's model.

2. Matrices as maps: the columns are where the axes land

An m×nm \times n matrix times a vector with nn entries gives one with mm entries, so a matrix is a map from ℝn\mathbb{R}^n to ℝm\mathbb{R}^m, and a linear one: A(x+y)=Ax+AyA(x + y) = Ax + Ay and A(cx)=c(Ax)A(c\,x) = c\,(Ax). Straight lines stay straight, the origin stays put, and evenly spaced grid lines stay evenly spaced. Writing x=x1e1+x2e2x = x_1 e_1 + x_2 e_2 with the axis arrows e1=(1,0)e_1 = (1, 0), e2=(0,1)e_2 = (0, 1) (the standard basis), linearity gives Ax=x1(Ae1)+x2(Ae2)Ax = x_1 (Ae_1) + x_2 (Ae_2), and Ae1Ae_1 is just the first column of AA.

The one picture to keep
The columns of a matrix are where the axis arrows land. Know where e1e_1 and e2e_2 go, and you know where every point goes: the whole grid follows them.

The running matrix (from the old page, every number checked) is A=(3045)A = \begin{pmatrix} 3 & 0 \\ 4 & 5 \end{pmatrix}: e1e_1 lands on (3,4)(3, 4), e2e_2 on (0,5)(0, 5), the unit square becomes the parallelogram with those sides and the unit circle becomes an ellipse. Switch the preset to see a rotation, shear, projection and reflection do the same.

The determinant, det⁡(abcd)=ad−bc\det \begin{pmatrix} a & b \\ c & d \end{pmatrix} = ad - bc, is the signed factor by which the map scales area: for AA, 3⋅5−0⋅4=153 \cdot 5 - 0 \cdot 4 = 15. A negative determinant means the map also flips orientation (a mirror image, like the reflection preset). A determinant of 0 means the plane is squashed onto a line or a point (the projection preset): many inputs land on the same output, so the map cannot be undone. In nn dimensions the determinant scales volume the same way.

Non-square matrices change the dimension. The tiny model's WW is 3×23 \times 2: it takes the 2-number input to 3 scores, so it maps the plane into a flat plane sitting inside 3-D space, with e1e_1 and e2e_2 landing on its columns (1,0,1)(1, 0, 1) and (0,1,−2)(0, 1, -2). A 2×32 \times 3 matrix goes the other way and must squash a whole direction of 3-D space to zero (section 5 says exactly which one).

What it is used for in ML
Every dense layer, every attention projection and every embedding lookup is a linear map; a network is linear maps alternating with simple non-linear functions (activations), because a stack of purely linear maps would collapse into one linear map (section 3). Thinking of a weight matrix as "what it does to directions" is what makes rank, conditioning and low-rank methods make sense.

Notation: AijA_{ij} is the entry in row ii, column jj (row first, always); an m×nm \times n matrix has mm rows and nn columns.

Each preset moves the axis arrows; the grid, the square and the circle follow

Four orders, the same 8 multiplications: the readings differ in grouping, not in work

Predict before it plays: which reading builds the answer one whole matrix at a time? The same product in every mode, drawn to the same layout; the step list on the left follows the mode.

3. Matrix multiplication, four ways, and what it costs

An m×nm \times n matrix PP times an n×pn \times p matrix QQ is the m×pm \times p matrix PQPQ: the inner sizes must match and vanish, the outer sizes survive. There are four ways to read the same product, and each is the natural reading in some part of ML. Take

P=(1234),Q=(5678),PQ=(19224350)P = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}, \quad Q = \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix}, \quad PQ = \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}
  1. Row times column (dot products). (PQ)ij=∑kPikQkj(PQ)_{ij} = \sum_k P_{ik} Q_{kj}, so (PQ)11=1⋅5+2⋅7=19(PQ)_{11} = 1 \cdot 5 + 2 \cdot 7 = 19. Attention scores read this way: every query (a row) against every key (a column).
  2. Column picture. Column jj of PQPQ is a combination of the columns of PP with weights from column jj of QQ: 5(1,3)+7(2,4)=(19,43)5\,(1, 3) + 7\,(2, 4) = (19, 43). So every output of a layer lies in the span of the weight matrix's columns (section 5).
  3. Sum of outer products. An outer product uv⊤u v^\top is a whole matrix with entries uivju_i v_j, of rank 1 (every row is a multiple of vv). PQPQ is the sum over kk of (column kk of PP) times (row kk of QQ): (1,3)(5,6)+(2,4)(7,8)(1, 3)(5, 6) + (2, 4)(7, 8), that is (561518)+(14162832)\begin{pmatrix} 5 & 6 \\ 15 & 18 \end{pmatrix} + \begin{pmatrix} 14 & 16 \\ 28 & 32 \end{pmatrix}. This is the SVD's reading (section 10), LoRA's BABA (a sum of rr rank-1 layers) and the gradient of a layer (∇WL=gx⊤\nabla_W L = g\,x^\top, the root's rank-1 gradient).
  4. Composition. (PQ)x=P(Qx)(PQ)x = P(Qx): apply QQ first, then PP; with x=e1x = e_1, Qx=(5,7)Qx = (5, 7), then P(5,7)=(19,43)P(5, 7) = (19, 43). Read right to left. That is why order matters: QP=(23343146)≠PQQP = \begin{pmatrix} 23 & 34 \\ 31 & 46 \end{pmatrix} \ne PQ, because "rotate then stretch" is not "stretch then rotate".

The animation, step by step (pick a reading on the card)

  1. Start. PP and QQ are 2 x 2, so PQPQ is 2 x 2: four numbers to find. Every reading does the same 8 multiplications.
  2. Entry (1, 1). Row 1 of PP dotted with column 1 of QQ: 1×5+2×7=191 \times 5 + 2 \times 7 = 19. One dot product per entry: every query row against every key column, as in attention scores.
  3. Entry (1, 2). 1×6+2×8=221 \times 6 + 2 \times 8 = 22.
  4. Entry (2, 1). 3×5+4×7=433 \times 5 + 4 \times 7 = 43.
  5. Entry (2, 2). 3×6+4×8=503 \times 6 + 4 \times 8 = 50: four dot products of two multiplications each.

Transpose. P⊤P^\top swaps rows and columns, (P⊤)ij=Pji(P^\top)_{ij} = P_{ji}. So a⊤ba^\top b (a row times a column) is a dot product and ab⊤a b^\top (a column times a row) an outer product. Transposing a product reverses the order, (PQ)⊤=Q⊤P⊤(PQ)^\top = Q^\top P^\top (entry (i,j)(i, j) of (PQ)⊤(PQ)^\top is row ii of Q⊤Q^\top dotted with column jj of P⊤P^\top). That reversal is what makes backpropagation multiply by W⊤W^\top on the way back.

Why stacked linear layers collapse. Two linear layers in a row, W2(W1x)W_2 (W_1 x), are one linear layer (W2W1)x(W_2 W_1) x: however many you stack, without a non-linear function between them the network can only ever compute one matrix. The activation function (ReLU, GELU) between layers is what stops the collapse.

What a product costs. An m×nm \times n times n×pn \times p product does m⋅n⋅pm \cdot n \cdot p multiplications and about as many additions, usually counted as 2mnp2mnp floating-point operations (FLOPs). For one GPT-2 small layer of 768 by 768 applied to 1,024 tokens that is 2×1024×768×768≈1.22 \times 1024 \times 768 \times 768 \approx 1.2 billion FLOPs. Counting FLOPs for whole networks and for the backward pass belongs to Matrix calculus and backprop.

4. Shapes, layouts, broadcasting and einsum

Write the shape beside every symbol: WW is 3×23 \times 2, xx has 2 entries, so WxWx has 3. A real model holds activations as a tensor of shape (B,T,d)(B, T, d): BB sequences in the batch, TT tokens each, dd numbers per token; a layer acts on the last axis only, with the same matrix for every token.

Three layouts for the same layer. Papers treat one input as a column and write y=Wx+by = Wx + b with WW of shape (outputs by inputs). Code stacks inputs as rows and computes Y=XW⊤+bY = XW^\top + b: PyTorch's nn.Linear stores weight with shape (out_features, in_features) and computes y=xA⊤+by = xA^\top + b (PyTorch nn.Linear docs). GPT-2's original code uses a third: its Conv1D stores the weight as (in, out) and computes xW+bxW + b; Hugging Face's own docstring says it "works like a linear layer but the weights are transposed" (transformers pytorch_utils.py). Section 12's GPT-2 measurements use this layout. The transpose between conventions is the same map; mixing them inside one derivation is the bug. Reading a model's code against its paper is mostly translating shapes and layouts: which axis is the batch, which is heads, whether the weight is stored (out, in) or (in, out).

Broadcasting. Adding a bias of shape (d,)(d,) to activations of shape (B,T,d)(B, T, d) works because libraries broadcast: they line up shapes from the right, and an axis of size 1 (or a missing axis) is stretched to match. NumPy's rule, word for word: "two dimensions are compatible when they are equal, or one of them is 1" (NumPy broadcasting). So (3,1)+(1,2)(3, 1) + (1, 2) gives (3,2)(3, 2), and (B,T,d)+(d,)(B, T, d) + (d,) gives (B,T,d)(B, T, d). The same rule produces the commonest silent bug:

A (3, 1) column minus a (3,) row is a 3 x 3 table, not three differences

Predictions of shape (3, 1), (1,2,3)(1, 2, 3) as a column, minus targets of shape (3,), also (1,2,3)(1, 2, 3). Instead of three differences of 0 you get every prediction minus every target, (0−1−210−1210)\begin{pmatrix} 0 & -1 & -2 \\ 1 & 0 & -1 \\ 2 & 1 & 0 \end{pmatrix}; its mean square is 12/9=1.33312/9 = 1.333, though the predictions are perfect and the true mean squared error is 0. No error is raised. Fix: make the shapes equal (pred.squeeze(-1)) and assert shapes before every loss.

Einsum: every axis named

Einstein summation labels every axis with a letter. The rule: a letter that appears in the inputs but not in the output is summed over; every other letter is kept, in the order the output names them. torch.einsum, numpy.einsum and jax.numpy.einsum all read the same strings, and writing one is the cure when a shape bug survives ten minutes of staring.

StringReads asMath
i,i->dot product: multiply along i, sum it awaya⋅ba \cdot b
i,j->ijouter product: nothing summedab⊤a b^\top
ij,j->imatrix times vectorWxWx
ij,jk->ikmatrix product: j shared and summedPQPQ
ij->jitranspose: relabel onlyA⊤A^\top
ii->trace: the diagonal, summedtr⁡A\operatorname{tr} A
btd,od->btoa linear layer on every token (PyTorch's (out, in) weight)XW⊤XW^\top
bhqd,bhkd->bhqkattention scores: per batch b and head h, every query q against every key k, summed over dQK⊤QK^\top
bhqk,bhkd->bhqdattention output: weights times values, summed over keys kPVPV

Reading the shape off the string. In bhqd,bhkd->bhqk with b=2b = 2, h=8h = 8, q=k=128q = k = 128, d=64d = 64 (the old page's example), dd is summed away and the output is (2,8,128,128)(2, 8, 128, 128). Counting the multiply-adds from the same string (one per combination of all distinct letters, here 2⋅8⋅128⋅128⋅642 \cdot 8 \cdot 128 \cdot 128 \cdot 64) and seeing why attention is quadratic in length is done on Matrix calculus and backprop. "Splitting heads" in code is a reshape from (B,T,768)(B, T, 768) to (B,T,12,64)(B, T, 12, 64) and a transpose of two axes, no arithmetic; the einops library writes it as a named reshape that documents intent, rearrange(x, 'b t (h d) -> b h t d', h=12).

5. Rank, null space and the four subspaces

A 768×768768 \times 768 weight matrix has 589,824 numbers; the question is how many genuinely different directions it uses. The rank of a matrix is the number of independent columns, which always equals the number of independent rows, and is the dimension of the set of outputs the map can produce. An m×nm \times n matrix has rank at most min⁡(m,n)\min(m, n); a square matrix of full rank is invertible (its map can be undone, A−1A=IA^{-1}A = I, with II the identity matrix). The null space is the set of inputs the map sends to zero.

For (1224)\begin{pmatrix} 1 & 2 \\ 2 & 4 \end{pmatrix} (old page, checked) the second column is twice the first, so the rank is 1 and the map flattens the plane onto the line through (1,2)(1, 2); its determinant is 1⋅4−2⋅2=01 \cdot 4 - 2 \cdot 2 = 0, the same fact seen as area; and the input (2,−1)(2, -1) is sent to (0,0)(0, 0), as is every multiple of it.

The input dimensions are shared out exactly between those that reach the output and those squashed to zero, the rank-nullity theorem:

rank⁡(A)+dim⁡null⁡(A)=n\operatorname{rank}(A) + \dim \operatorname{null}(A) = n

(nn the number of columns; for the rank-1 matrix, 1+1=21 + 1 = 2). The tiny model's WW has independent columns (1,0,1)(1, 0, 1) and (0,1,−2)(0, 1, -2), so its rank is 2 and its null space is just {0}\{0\}: no input direction is lost. The gradient the root computes for it, ∇WL=gx⊤\nabla_W L = g\,x^\top with g=(0.665,0.245,−0.910)g = (0.665, 0.245, -0.910), is an outer product, so it has rank 1: one training step on one example changes WW along a single direction. That small fact is where LoRA starts (section 12).

Rank in floating point. In exact arithmetic rank is an integer you can read off. In floating point, rounding makes almost every matrix full rank, so the useful question is how many singular values (section 10) are large: the effective rank. Section 12 measures it on real GPT-2 weights.

A rank-1 map keeps one input direction and destroys the other

(1224)\begin{pmatrix} 1 & 2 \\ 2 & 4 \end{pmatrix}. Left, the input plane: the null-space line along (2,−1)(2, -1) and, at right angles to it, the row-space line along (1,2)(1, 2). Every input on a dashed line parallel to the null space lands on the same output. Right, the output plane: every output lies on the column-space line through (1,2)(1, 2); x=(2,1)x = (2, 1) and x+(2,−1)=(4,0)x + (2, -1) = (4, 0) both land on (4,8)(4, 8).

The four fundamental subspaces

Which outputs can a layer never produce, whatever its input? Which inputs does it ignore? Every m×nm \times n matrix AA of rank rr comes with four subspaces (lines, planes and their higher-dimensional versions through the origin) that answer these, and Gilbert Strang organises all of linear algebra around them:

SubspaceWhat it holdsLives inDimension
Column space C(A)C(A)every output AxAx the map can produceℝm\mathbb{R}^mrr
Null space N(A)N(A)inputs sent to zeroℝn\mathbb{R}^nn−rn - r
Row space C(A⊤)C(A^\top)the span of the rows: the input directions the map "reads"ℝn\mathbb{R}^nrr
Left null space N(A⊤)N(A^\top)output directions the map can never reachℝm\mathbb{R}^mm−rm - r

The row space and the null space are orthogonal and together fill ℝn\mathbb{R}^n; the column space and the left null space are orthogonal and together fill ℝm\mathbb{R}^m. The reason is one line: xx is in the null space when Ax=0Ax = 0, which says that xx has dot product 0 with every row (Strang, The Fundamental Theorem of Linear Algebra, 1993; MIT 18.06 lectures 9 and 10).

Fig. 4, step by step: the tiny model's WW, from ℝ2\mathbb{R}^2 into ℝ3\mathbb{R}^3

  1. Two numbers in. The input plane ℝ2\mathbb{R}^2 on the floor: the axis arrows e1,e2e_1, e_2 and the input x=(2,1)x = (2, 1). WW has rank 2, so its row space is the whole plane and its null space only the origin: no input direction is lost.
  2. The columns are where the axes land. WW sends e1e_1 to (1,0,1)(1, 0, 1) and e2e_2 to (0,1,−2)(0, 1, -2), its two columns, now arrows in the space of scores (cat, dog, sat).
  3. The whole plane lands on a plane: the column space. The input grid lands as straight, evenly spaced lines on one tilted plane, and xx lands on z=2(1,0,1)+1(0,1,−2)=(2,1,0)z = 2\,(1, 0, 1) + 1\,(0, 1, -2) = (2, 1, 0). Whatever the input, the scores lie on this plane.
  4. The left null space stands at right angles to it. The line along (−1,2,1)(-1, 2, 1) is orthogonal to both columns, so no score vector ever moves along it; dimensions 2+1=32 + 1 = 3 fill the score space.
  5. Going back, W⊤W^\top collapses ℝ3\mathbb{R}^3 onto ℝ2\mathbb{R}^2. Backpropagation multiplies by W⊤W^\top (section 3), a 2×32 \times 3 map: a cloud of score vectors (illustrative points) lands flat on the input plane, and every point of the orange line lands on 0, because W⊤(−1,2,1)=(0,0)W^\top(-1, 2, 1) = (0, 0). That is rank-nullity for W⊤W^\top: rank 2 plus a one-dimensional null space, 3.

Here the left null space is the line along (−1,2,1)(-1, 2, 1), so every score vector this model can ever produce satisfies −zcat+2zdog+zsat=0-z_{\text{cat}} + 2 z_{\text{dog}} + z_{\text{sat}} = 0 (for z=(2,1,0)z = (2, 1, 0), −2+2+0=0-2 + 2 + 0 = 0), whatever the input.

The softmax bottleneck: the same fact at scale. A language model computes its logits as z=Ehz = E h, with hh the final hidden state (dd numbers) and EE the output matrix (V×dV \times d, one row per token), so the logits live in the column space of EE, of dimension at most dd. GPT-2 small has d=768d = 768 and V=50,257V = 50{,}257 and its output layer has no bias (it reuses the embedding table), so all its logit vectors lie in a 768-dimensional subspace of a 50,257-dimensional space. Yang, Dai, Salakhutdinov and Cohen called this the softmax bottleneck: stack the logits for many contexts into one matrix and its rank is at most dd; the log-probabilities differ from the logits by one constant per context, which changes the rank by at most 1; so if the true matrix of log-probabilities of language has rank above d+1d + 1, no setting of the weights can reproduce it (their Corollary 1, Yang et al., ICLR 2018, arXiv 1711.03953). Their fix, a mixture of softmaxes, breaks the linearity.

What it is used for in ML
Collapsed representations, where every embedding lies in a small subspace, show up as low effective rank. The column space says what a layer can output; the null space says what it ignores (an input change along it has no effect, and no gradient flows back along it); the left null space says what it can never produce.
From zero: span, independence, basis, finding a rank by elimination, and a proof of rank-nullity

A linear combination of vectors is a sum of scaled copies, c1v1+c2v2+…c_1 v_1 + c_2 v_2 + \dots; the span of a set is everything you can reach that way. The span of (1,0)(1, 0) and (0,1)(0, 1) is the whole plane; the span of (1,2)(1, 2) alone is a line through the origin; the span of (1,2)(1, 2) and (2,4)(2, 4) is still that same line, because (2,4)=2(1,2)(2, 4) = 2\,(1, 2) adds nothing new. Vectors are linearly independent when none of them is a combination of the others; equivalently, the only way to combine them into the zero vector is with all coefficients 0. (1,2)(1, 2) and (2,4)(2, 4) are dependent: 2(1,2)−1(2,4)=(0,0)2\,(1, 2) - 1\,(2, 4) = (0, 0).

A basis of a space is a set of independent vectors that spans it; every basis of the same space has the same number of vectors, the dimension. Once a basis is fixed, every vector has unique coordinates in it. b1=(1,1)b_1 = (1, 1), b2=(1,−1)b_2 = (1, -1) is a basis of the plane, and in it the tiny model's input is x=1.5b1+0.5b2x = 1.5\, b_1 + 0.5\, b_2 (check: (1.5+0.5,1.5−0.5)=(2,1)(1.5 + 0.5, 1.5 - 0.5) = (2, 1)). Same arrow, different numbers: "the coordinates of a vector" always means "in some basis", and the eigenvector and SVD sections choose bases that make a matrix simple.

Finding the rank by elimination. Subtract multiples of rows from other rows (this does not change the rank) until no more zeros can be made; the rank is the number of nonzero rows left. For M=(123246101)M = \begin{pmatrix} 1 & 2 & 3 \\ 2 & 4 & 6 \\ 1 & 0 & 1 \end{pmatrix}: row 2 minus twice row 1 is all zeros; row 3 minus row 1 is (0,−2,−2)(0, -2, -2), not a multiple of row 1. Two nonzero rows remain, so the rank is 2. The input (−1,−1,1)(-1, -1, 1) satisfies 1⋅(−1)+2⋅(−1)+3⋅1=01 \cdot (-1) + 2 \cdot (-1) + 3 \cdot 1 = 0 and −1+0+1=0-1 + 0 + 1 = 0, so MM sends it to zero, and rank-nullity reads 2+1=32 + 1 = 3.

The tiny model's subspaces, checked. m=3m = 3, n=2n = 2, r=2r = 2: the null space has dimension 2−2=02 - 2 = 0, the left null space 3−2=13 - 2 = 1, spanned by n=(−1,2,1)n = (-1, 2, 1): n⋅(1,0,1)=−1+0+1=0n \cdot (1, 0, 1) = -1 + 0 + 1 = 0 and n⋅(0,1,−2)=0+2−2=0n \cdot (0, 1, -2) = 0 + 2 - 2 = 0. Training changes WW and therefore the plane, but with 2 numbers per input the scores always live on some plane inside the 3-D space of scores.

Why rank-nullity holds. 1. Pick a basis u1,…,uku_1, \dots, u_k of the null space (kk is the nullity). 2. Extend it with v1,…,vn−kv_1, \dots, v_{n-k} to a basis of all of ℝn\mathbb{R}^n. 3. Every output AxAx is a combination of Av1,…,Avn−kAv_1, \dots, Av_{n-k}, because writing xx in the basis, the uu parts are sent to zero. 4. Those n−kn - k outputs are independent: if ∑ciAvi=0\sum c_i A v_i = 0 then ∑civi\sum c_i v_i is in the null space, so it is a combination of the uu's, and since the whole basis is independent all ci=0c_i = 0. So the output space has dimension exactly n−kn - k, which is the rank.

6. Norms: how big is a vector, a matrix, an update

Weight decay, gradient clipping, spectral normalisation and "the update was too large" all measure size, in different senses, and the choice decides what a regulariser pushes towards. For vectors three norms are common:

‖x‖1=∑i|xi|,‖x‖2=∑ixi2,‖x‖∞=maxi⁡|xi|\lVert x \rVert_1 = \sum_i |x_i|, \qquad \lVert x \rVert_2 = \sqrt{\sum_i x_i^2}, \qquad \lVert x \rVert_\infty = \max_i |x_i|

For x=(2,1)x = (2, 1): ‖x‖1=3\lVert x \rVert_1 = 3, ‖x‖2=2.236\lVert x \rVert_2 = 2.236, ‖x‖∞=2\lVert x \rVert_\infty = 2 (old page, checked: (3,−4)(3, -4) gives 7, 5 and 4). Always ‖x‖∞≤‖x‖2≤‖x‖1\lVert x \rVert_\infty \le \lVert x \rVert_2 \le \lVert x \rVert_1.

Why L1 gives sparsity. Minimising a loss subject to ‖w‖1≤c\lVert w \rVert_1 \le c grows the diamond until it first touches a contour of the loss, usually at a corner, where some coordinates are exactly zero; the round L2 ball shrinks every coordinate a little and zeroes none. That is lasso against ridge regression (Regularisation owns the full story).

Matrix norms are functions of the singular values.

  • Frobenius: the matrix as one long vector, ‖A‖F=∑ijAij2=∑σi2\lVert A \rVert_F = \sqrt{\sum_{ij} A_{ij}^2} = \sqrt{\sum \sigma_i^2}. Weight decay penalises ‖W‖F2\lVert W \rVert_F^2.
  • Spectral (operator 2-norm): the largest factor by which the map stretches any vector, ‖A‖2=maxx≠0⁡‖Ax‖2/‖x‖2=σmax⁡(A)\lVert A \rVert_2 = \max_{x \ne 0} \lVert Ax \rVert_2 / \lVert x \rVert_2 = \sigma_{\max}(A).
  • Nuclear: ‖A‖∗=∑iσi\lVert A \rVert_* = \sum_i \sigma_i, the closest convex stand-in for rank, used when a method wants low rank but needs a smooth objective.

For AA (singular values 6.708 and 2.236, section 10): Frobenius 50=7.071\sqrt{50} = 7.071, spectral 6.708, nuclear 8.944.

The norms worked: A, the tiny model's W and its rank-1 gradient

For A=(3045)A = \begin{pmatrix} 3 & 0 \\ 4 & 5 \end{pmatrix} (singular values 45=6.708\sqrt{45} = 6.708 and 5=2.236\sqrt{5} = 2.236, derived in section 10): Frobenius 9+0+16+25=50=7.071=45+5\sqrt{9 + 0 + 16 + 25} = \sqrt{50} = 7.071 = \sqrt{45 + 5}; spectral 6.708; nuclear 6.708+2.236=8.9446.708 + 2.236 = 8.944.

For the tiny model's WW (singular values 6=2.449\sqrt{6} = 2.449 and 1): Frobenius 7=2.646\sqrt{7} = 2.646, spectral 2.449, nuclear 3.449. For its rank-1 gradient gx⊤g x^\top all three coincide: ‖g‖‖x‖=1.153×2.236=2.579\lVert g \rVert \lVert x \rVert = 1.153 \times 2.236 = 2.579, because a rank-1 matrix has a single nonzero singular value.

The spectral norm is a Lipschitz constant. ‖Ax−Ay‖=‖A(x−y)‖≤σmax⁡‖x−y‖\lVert Ax - Ay \rVert = \lVert A(x - y) \rVert \le \sigma_{\max} \lVert x - y \rVert: no two inputs can end up further apart than σmax⁡\sigma_{\max} times their distance. Spectral normalisation divides a layer's weights by σmax⁡\sigma_{\max} to make that factor 1 (Miyato et al. 2018), which is why a layer's largest singular value controls how much it can amplify a perturbation.

What it is used for in ML
Gradient clipping rescales the whole gradient when its L2 norm (over all parameters together) exceeds a threshold; weight decay shrinks the Frobenius norm; adversarial robustness budgets are usually L-infinity balls; spectral norms bound how much a layer amplifies; optimisers such as Muon choose the update with the best decrease per unit of spectral norm (section 11).

What makes a norm a norm: it is zero only for the zero vector, scales with the vector (‖cx‖=|c|‖x‖\lVert c x \rVert = |c| \lVert x \rVert) and obeys the triangle inequality (‖x+y‖≤‖x‖+‖y‖\lVert x + y \rVert \le \lVert x \rVert + \lVert y \rVert).

The L1 ball has corners on the axes, so a loss contour usually touches it where a coordinate is 0

The unit "circle" of each norm: every point at distance 1 from the origin. L1 is a diamond with corners on the axes, L2 the round circle, L-infinity a square. The dot is where a growing ball first touches a straight line of equal loss (the dashed line): for L1 at a corner, where one coordinate is exactly 0.

7. Orthogonality, projections, least squares and QR

Vectors are orthonormal when each has length 1 and every pair is orthogonal. A square matrix with orthonormal columns is an orthogonal matrix, and that fits in one equation, Q⊤Q=IQ^\top Q = I, so its inverse is its transpose, free to compute. Orthogonal matrices are rotations and reflections: they keep every length and angle, ‖Qx‖2=x⊤Q⊤Qx=x⊤x\lVert Qx \rVert^2 = x^\top Q^\top Q x = x^\top x, so multiplying by them never amplifies rounding errors, which is why stable algorithms are built from them. The rotation by θ\theta is (cos⁡θ−sin⁡θsin⁡θcos⁡θ)\begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix}; RoPE, the position encoding of most current LLMs, rotates pairs of query and key coordinates by such matrices.

Least squares: the best fit is a projection

Fit yˆ=w0+w1t\hat y = w_0 + w_1 t to the points (t,y)=(0,1),(1,2),(2,2)(t, y) = (0, 1), (1, 2), (2, 2) (the old page's example, checked). Three equations for two unknowns have no exact solution. Stack them, a column of ones for the intercept and a column of tt values:

X=(101112),y=(122),minimise ‖y−Xw‖22X = \begin{pmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{pmatrix}, \quad y = \begin{pmatrix} 1 \\ 2 \\ 2 \end{pmatrix}, \quad \text{minimise } \lVert y - Xw \rVert_2^2

Three points, no exact line: the best line misses them by (−0.167, 0.333, −0.167)

The three points, the fitted line 1.167+0.5t1.167 + 0.5\,t and the residuals (−0.167,0.333,−0.167)(-0.167, 0.333, -0.167). Fig. 8 draws the same fit in ℝ3\mathbb{R}^3, one axis per data point, where it becomes a projection.

Fig. 8, step by step: the fit as a projection in ℝ3\mathbb{R}^3

  1. One axis per data point. The three targets are one vector, y=(1,2,2)y = (1, 2, 2), in a space with one axis per data point.
  2. The columns of XX span a plane. a1=(1,1,1)a_1 = (1, 1, 1) (the intercept column) and a2=(0,1,2)a_2 = (0, 1, 2) (the tt column). Every prediction Xw=w0a1+w1a2Xw = w_0 a_1 + w_1 a_2 lies on the plane they span, the column space (glass), and yy does not: no ww fits exactly. The camera is turned so the plane looks level.
  3. The best fit is the foot of the perpendicular. yˆ=1.167a1+0.5a2=(1.167,1.667,2.167)\hat y = 1.167\,a_1 + 0.5\,a_2 = (1.167, 1.667, 2.167), the point of the plane closest to yy; w=(1.167,0.5)w = (1.167, 0.5) are its coordinates along the two columns (dashed).
  4. The residual stands at right angles. y−yˆ=(−0.167,0.333,−0.167)y - \hat y = (-0.167, 0.333, -0.167), of length 0.408 against ‖y‖=3\lVert y \rVert = 3, has zero dot product with both columns: X⊤(y−Xw)=0X^\top(y - Xw) = 0, the normal equations below.
  5. QR: an orthonormal basis of the same plane. q1=(1,1,1)/3q_1 = (1, 1, 1)/\sqrt{3} and q2=(−1,0,1)/2q_2 = (-1, 0, 1)/\sqrt{2}, at right angles and of length 1, span the same plane; in them the foot's coordinates are just Q⊤y=(2.887,0.707)Q^\top y = (2.887, 0.707), with no X⊤XX^\top X formed.

The predictions XwXw range over the column space of XX, a plane in ℝ3\mathbb{R}^3, and yy is not on it. The closest point leaves a leftover orthogonal to every column, X⊤(y−Xw)=0X^\top (y - Xw) = 0, which gives the normal equations:

X⊤Xw=X⊤y⇒yˆ=Xw=X(X⊤X)−1X⊤⏟PyX^\top X\, w = X^\top y \;\Rightarrow\; \hat y = Xw = \underbrace{X (X^\top X)^{-1} X^\top}_{P}\, y

No calculus is needed: the derivation is orthogonality. (The same equations come from setting the gradient 2X⊤(Xw−y)2X^\top(Xw - y) to zero; deriving such gradients is on Matrix calculus and backprop.) PP is the projection matrix: projecting twice changes nothing (P2=PP^2 = P) and PP is symmetric.

Here X⊤X=(3335)X^\top X = \begin{pmatrix} 3 & 3 \\ 3 & 5 \end{pmatrix} and X⊤y=(5,6)X^\top y = (5, 6) give w=(1.167,0.5)w = (1.167, 0.5); the residuals (−0.167,0.333,−0.167)(-0.167, 0.333, -0.167) sum to 0 (orthogonal to the column of ones) and have zero dot product with t=(0,1,2)t = (0, 1, 2), exactly as X⊤(y−Xw)=0X^\top(y - Xw) = 0 demands.

QR: the same answer without forming X⊤XX^\top X. Gram-Schmidt makes the columns orthonormal one at a time (subtract from each column its projections onto the ones already done, then scale to length 1); recording the coefficients gives X=QRX = QR, with QQ orthonormal columns and RR upper triangular. The normal equations then become Rw=Q⊤yR w = Q^\top y, solved from the bottom row up. Why bother: forming X⊤XX^\top X squares the condition number (section 11), here κ(X)=2.92\kappa(X) = 2.92 against κ(X⊤X)=8.55\kappa(X^\top X) = 8.55, the square of 2.924. Harmless at this size, ruinous for an ill-conditioned problem in float32, as section 11 shows on a real run. Library routines (LAPACK, behind NumPy and PyTorch) use Householder reflections rather than textbook Gram-Schmidt, because classical Gram-Schmidt loses orthogonality in floating point when columns are nearly parallel (Trefethen and Bau, lectures 8 to 10).

Ridge and the pseudo-inverse. Ridge regression adds λ‖w‖2\lambda \lVert w \rVert^2, which changes the equations to (X⊤X+λI)w=X⊤y(X^\top X + \lambda I) w = X^\top y. Adding λI\lambda I adds λ\lambda to every eigenvalue of X⊤XX^\top X (7.162 and 0.838 here), so the condition number drops from 7.162/0.838=8.557.162 / 0.838 = 8.55 to 8.162/1.838=4.448.162 / 1.838 = 4.44 at λ=1\lambda = 1: regularisation is also a numerical fix. The pseudo-inverse X+X^{+} (section 10) gives the least-squares solution even when XX is rank-deficient, choosing the shortest ww among the many that fit equally well.

What it is used for in ML
Orthogonal initialisation keeps signal norms constant through deep linear stacks; RoPE is a rotation; linear regression, linear probes and the last layer of a network with squared loss are projections (training a linear readout is projecting targets onto the span of the features); the closed-form ridge solution sits behind kernel methods; QR is the stable way to solve least squares and to orthonormalise a basis (randomized SVD routines, such as PyTorch's torch.svd_lowrank, orthonormalise with QR at every pass).
Worked: the normal equations solved by hand, why the orthogonal leftover is the closest point, and the QR of this X

The solve by hand. X⊤X=(3335)X^\top X = \begin{pmatrix} 3 & 3 \\ 3 & 5 \end{pmatrix} (3 points, sum of tt is 3, sum of t2t^2 is 5) and X⊤y=(5,6)X^\top y = (5, 6). The 2x2 inverse is 1det⁡(d−b−ca)\frac{1}{\det}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix} with det⁡=15−9=6\det = 15 - 9 = 6, so w=16(5−3−33)(56)=16(73)=(1.167,0.5)w = \frac{1}{6}\begin{pmatrix} 5 & -3 \\ -3 & 3 \end{pmatrix}\begin{pmatrix} 5 \\ 6 \end{pmatrix} = \frac{1}{6}\begin{pmatrix} 7 \\ 3 \end{pmatrix} = (1.167, 0.5).

Predictions 1.167,1.667,2.1671.167, 1.667, 2.167; residuals y−yˆ=(−0.167,0.333,−0.167)y - \hat y = (-0.167, 0.333, -0.167). They sum to 0 (orthogonal to the column of ones) and 0⋅(−0.167)+1⋅0.333+2⋅(−0.167)=00 \cdot (-0.167) + 1 \cdot 0.333 + 2 \cdot (-0.167) = 0 (orthogonal to the tt column), exactly as X⊤(y−Xw)=0X^\top(y - Xw) = 0 demands.

Closest point. For any other point Xw′Xw' of the subspace, y−Xw′=(y−Xw)+X(w−w′)y - Xw' = (y - Xw) + X(w - w'), the two pieces are orthogonal, so by Pythagoras

‖y−Xw′‖2=‖y−Xw‖2+‖X(w−w′)‖2≥‖y−Xw‖2\lVert y - Xw' \rVert^2 = \lVert y - Xw \rVert^2 + \lVert X(w - w') \rVert^2 \ge \lVert y - Xw \rVert^2

QR by Gram-Schmidt. XX has columns a1=(1,1,1)a_1 = (1, 1, 1) and a2=(0,1,2)a_2 = (0, 1, 2). Step 1: q1=a1/‖a1‖=(1,1,1)/3q_1 = a_1 / \lVert a_1 \rVert = (1, 1, 1)/\sqrt{3}, and r11=3=1.732r_{11} = \sqrt{3} = 1.732. Step 2: the coefficient of a2a_2 along q1q_1 is r12=q1⋅a2=3/3=1.732r_{12} = q_1 \cdot a_2 = 3/\sqrt{3} = 1.732; subtract it: a2−r12q1=(0,1,2)−(1,1,1)=(−1,0,1)a_2 - r_{12} q_1 = (0, 1, 2) - (1, 1, 1) = (-1, 0, 1). Its length is r22=2=1.414r_{22} = \sqrt{2} = 1.414, so q2=(−1,0,1)/2q_2 = (-1, 0, 1)/\sqrt{2}, and

R=(3302),check: 3q1+2q2=(1,1,1)+(−1,0,1)=(0,1,2)=a2R = \begin{pmatrix} \sqrt{3} & \sqrt{3} \\ 0 & \sqrt{2} \end{pmatrix}, \qquad \text{check: } \sqrt{3}\, q_1 + \sqrt{2}\, q_2 = (1,1,1) + (-1,0,1) = (0,1,2) = a_2

Solve. Q⊤y=(5/3,1/2)=(2.887,0.707)Q^\top y = (5/\sqrt{3}, 1/\sqrt{2}) = (2.887, 0.707). Back-substitute from the bottom row: 2w1=1/2\sqrt{2}\, w_1 = 1/\sqrt{2} gives w1=0.5w_1 = 0.5; 3(w0+w1)=5/3\sqrt{3}\,(w_0 + w_1) = 5/\sqrt{3} gives w0=5/3−0.5=1.167w_0 = 5/3 - 0.5 = 1.167.

8. Eigenvalues and eigenvectors: the directions a map does not turn

Most arrows change direction when a matrix acts on them. A few special ones only get longer or shorter (or flip): the eigenvectors, scaled by their eigenvalue, Av=λvA v = \lambda v with v≠0v \ne 0. Along an eigenvector the matrix is just multiplication by a number. Av=λvAv = \lambda v means A−λIA - \lambda I sends a nonzero vv to zero, so it squashes area to zero, so the eigenvalues solve the characteristic equation det⁡(A−λI)=0\det(A - \lambda I) = 0.

For A=(3045)A = \begin{pmatrix} 3 & 0 \\ 4 & 5 \end{pmatrix}: (3−λ)(5−λ)=0(3 - \lambda)(5 - \lambda) = 0, so the eigenvalues are 3 and 5 (a triangular matrix always has its diagonal entries as eigenvalues), with eigenvectors (1,−2)(1, -2) and (0,1)(0, 1). They add up to the trace (sum of the diagonal), 3+5=83 + 5 = 8, and multiply to the determinant, 3×5=153 \times 5 = 15. The two eigenvectors are not orthogonal: their dot product is −2-2. For a non-symmetric matrix that is normal; section 9 shows symmetric matrices are different.

Worked: the eigenvectors of A by hand (old page, checked)

(3−λ)(5−λ)−0⋅4=0(3 - \lambda)(5 - \lambda) - 0 \cdot 4 = 0, so λ=3\lambda = 3 or λ=5\lambda = 5 (a triangular matrix always has its diagonal entries as eigenvalues). For λ=3\lambda = 3: (A−3I)v=(0042)v=0(A - 3I)v = \begin{pmatrix} 0 & 0 \\ 4 & 2 \end{pmatrix} v = 0 needs 4v1+2v2=04v_1 + 2v_2 = 0, so v=(1,−2)v = (1, -2); check A(1,−2)=(3,4−10)=(3,−6)=3(1,−2)A(1, -2) = (3, 4 - 10) = (3, -6) = 3\,(1, -2). For λ=5\lambda = 5: v=(0,1)v = (0, 1); check A(0,1)=(0,5)A(0, 1) = (0, 5). The eigenvalues add up to the trace (sum of the diagonal), 3+5=83 + 5 = 8, and multiply to the determinant, 3×5=153 \times 5 = 15.

The two eigenvectors (1,−2)(1, -2) and (0,1)(0, 1) are not orthogonal: their dot product is −2-2. For a non-symmetric matrix that is normal; section 9 shows symmetric matrices are different.

Why eigenvalues rule repeated application. If the eigenvectors form a basis, write any input as x=c1v1+c2v2x = c_1 v_1 + c_2 v_2; then Akx=c1λ1kv1+c2λ2kv2A^k x = c_1 \lambda_1^k v_1 + c_2 \lambda_2^k v_2: each component is multiplied by its own eigenvalue kk times, the largest |λ||\lambda| wins, and the result grows like |λmax⁡|k|\lambda_{\max}|^k above 1 and shrinks to zero when every |λ|<1|\lambda| \lt 1. For the whole matrix, A=PΛP−1A = P \Lambda P^{-1} (eigenvectors as the columns of PP, eigenvalues on the diagonal of Λ\Lambda) and Ak=PΛkP−1A^k = P \Lambda^k P^{-1}: powers of a matrix are powers of numbers in the eigenvector basis.

Power iteration is how the largest singular value of a layer is estimated cheaply in spectral normalisation (one step per training step).

Exploding and vanishing gradients. A plain recurrent network applies the same matrix at every time step, and the gradient flowing back through TT steps is multiplied by (the transpose of) that matrix TT times: above 1 in the largest eigenvalue magnitude it can explode, below 1 in all it vanishes. (Pascanu, Mikolov and Bengio 2013, quoted below).

When eigenvectors fail. A rotation by 30 degrees turns every arrow, so it has no real eigenvector: its characteristic equation λ2−2cos⁡(30°)λ+1=0\lambda^2 - 2\cos(30°)\lambda + 1 = 0 has complex roots cos⁡30°±isin⁡30°\cos 30° \pm i \sin 30°. Some matrices have too few eigenvectors to form a basis (the shear (1101)\begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix} has only the direction (1,0)(1, 0)). Both are reasons the SVD, which always exists, is usually the better tool for "what does this matrix do".

What it is used for in ML
The Hessian's eigenvalues are the curvatures of the loss along its eigenvectors, and the largest one sets the largest stable learning rate (2/λmax⁡2/\lambda_{\max}, on the root's Reading tab); PCA is the eigendecomposition of a covariance; PageRank and power iteration find a dominant eigenvector; stability of recurrent and state-space models is an eigenvalue condition.
The vanishing-gradient condition in Pascanu et al.'s words

"It is sufficient for λ1<1/γ\lambda_1 \lt 1/\gamma, where λ1\lambda_1 is the absolute value of the largest eigenvalue of the recurrent weight matrix", for the vanishing gradient problem to occur, with γ\gamma a bound on the activation's derivative; for linear units (γ=1\gamma = 1) that is the eigenvalue-below-1 picture above (Pascanu et al. 2013, section 2.1).

Power iteration: the angle to the dominant eigenvector shrinks by about 3/5 each step

Start from (1,1)(1, 1), multiply by AA and rescale to length 1, repeatedly. The angle to the dominant eigenvector (0,1)(0, 1) (eigenvalue 5) goes 18.4, 9.0, 4.8, 2.7, 1.6, 0.9, 0.5, 0.3 degrees: it shrinks by about the ratio 3/5=0.63/5 = 0.6 each step, because the other component is multiplied by 3 while the dominant one is multiplied by 5. Dashed: the two eigenvector directions, not at right angles.

9. Symmetric matrices, the spectral theorem, positive definiteness

Covariance matrices and Hessians are symmetric, S=S⊤S = S^\top. The tiny model gives one for free: S=W⊤W=(2−2−25)S = W^\top W = \begin{pmatrix} 2 & -2 \\ -2 & 5 \end{pmatrix}, always symmetric because (W⊤W)⊤=W⊤W(W^\top W)^\top = W^\top W.

Spectral theorem
A real symmetric matrix has real eigenvalues and a full set of orthonormal eigenvectors, so S=QΛQ⊤S = Q \Lambda Q^\top with QQ orthogonal: rotate into the eigenvector axes, stretch along each axis, rotate back.
Worked: S=W⊤WS = W^\top W
det⁡(S−λI)=(2−λ)(5−λ)−4=λ2−7λ+6=(λ−6)(λ−1)\det(S - \lambda I) = (2 - \lambda)(5 - \lambda) - 4 = \lambda^2 - 7\lambda + 6 = (\lambda - 6)(\lambda - 1)

So the eigenvalues are 6 and 1. For 6: (S−6I)v=0(S - 6I)v = 0 gives v=(1,−2)/5v = (1, -2)/\sqrt{5}; for 1: v=(2,1)/5v = (2, 1)/\sqrt{5}. Their dot product is 2−2=02 - 2 = 0: orthogonal, as promised.

A surprise: (2,1)(2, 1) is the tiny model's input xx. Check Sx=(4−2,−4+5)=(2,1)=1⋅xSx = (4 - 2, -4 + 5) = (2, 1) = 1 \cdot x. So ‖Wx‖2=x⊤W⊤Wx=x⊤Sx=1⋅‖x‖2=5\lVert Wx \rVert^2 = x^\top W^\top W x = x^\top S x = 1 \cdot \lVert x \rVert^2 = 5, and indeed the scores z=(2,1,0)z = (2, 1, 0) have ‖z‖2=4+1+0=5\lVert z \rVert^2 = 4 + 1 + 0 = 5. This input happens to sit on the direction WW stretches least.

A symmetric matrix defines a quadratic form, the number x⊤Sxx^\top S x; here 2a2−4ab+5b22a^2 - 4ab + 5b^2 with x=(a,b)x = (a, b). The matrix is positive definite (PD) when x⊤Sx>0x^\top S x \gt 0 for every nonzero xx, and positive semi-definite (PSD) when x⊤Sx≥0x^\top S x \ge 0. Geometrically the surface x⊤Sxx^\top S x is a bowl, and its level sets are ellipses along the eigenvectors:

Four equivalent tests for a symmetric matrix being PD:

  1. every eigenvalue is positive (6 and 1: yes);
  2. every leading upper-left determinant is positive (2>02 \gt 0, det⁡S=10−4=6>0\det S = 10 - 4 = 6 \gt 0: yes);
  3. it can be written B⊤BB^\top B with BB of full column rank (S=W⊤WS = W^\top W, and WW has rank 2: yes), because x⊤B⊤Bx=‖Bx‖2x^\top B^\top B x = \lVert Bx \rVert^2;
  4. the Cholesky factorisation succeeds (next).

(1111)\begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix} is PSD but not PD: eigenvalues 2 and 0, and x=(1,−1)x = (1, -1) gives x⊤Sx=0x^\top S x = 0.

Cholesky: the square root of a PD matrix. Every PD matrix factors as S=LL⊤S = L L^\top with LL lower triangular and positive diagonal, solved entry by entry from the top-left; if the matrix is not PD a step asks for the square root of a negative number and the factorisation fails, which is the cheapest PD test there is. For S=W⊤WS = W^\top W, L=(20−23)L = \begin{pmatrix} \sqrt{2} & 0 \\ -\sqrt{2} & \sqrt{3} \end{pmatrix}.

Sampling a correlated Gaussian. Draw independent standard normals zz and output μ+Lz\mu + Lz: its covariance is LIL⊤=ΣL\,I\,L^\top = \Sigma. With Σ=(4223)\Sigma = \begin{pmatrix} 4 & 2 \\ 2 & 3 \end{pmatrix}, whose factor is L=(2012)L = \begin{pmatrix} 2 & 0 \\ 1 & \sqrt{2} \end{pmatrix}, and z=(1,−1)z = (1, -1): Lz=(2,1−1.414)=(2,−0.414)Lz = (2, 1 - 1.414) = (2, -0.414). It is the full-covariance form of the reparameterisation trick used to train VAEs (Variational inference and generative-model maths).

What it is used for in ML
Covariances and Gram matrices (X⊤XX^\top X) are PSD; the Hessian at a strict local minimum is PD; Gaussian processes and Kalman filters solve with Cholesky; second-order and natural-gradient methods need PD curvature matrices and add a multiple of II ("damping") to make them so.
S multiplied out, why eigenvectors of a symmetric matrix are orthogonal, and Cholesky by hand
S=W⊤W=(10101−2)(10011−2)=(2−2−25)S = W^\top W = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & -2 \end{pmatrix} \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & -2 \end{pmatrix} = \begin{pmatrix} 2 & -2 \\ -2 & 5 \end{pmatrix}

Take Sv1=λ1v1S v_1 = \lambda_1 v_1 and Sv2=λ2v2S v_2 = \lambda_2 v_2 with λ1≠λ2\lambda_1 \ne \lambda_2. Compute v1⊤Sv2v_1^\top S v_2 two ways: as v1⊤(λ2v2)=λ2v1⋅v2v_1^\top (\lambda_2 v_2) = \lambda_2\, v_1 \cdot v_2, and, because S=S⊤S = S^\top, as (Sv1)⊤v2=λ1v1⋅v2(S v_1)^\top v_2 = \lambda_1\, v_1 \cdot v_2. Subtract: (λ1−λ2)v1⋅v2=0(\lambda_1 - \lambda_2)\, v_1 \cdot v_2 = 0, and since the eigenvalues differ, v1⋅v2=0v_1 \cdot v_2 = 0. (That the eigenvalues are real follows from the same trick with complex conjugates; the full theorem with repeated eigenvalues is in Strang's 18.06 lecture 25.)

Cholesky by hand. For Σ=(4223)\Sigma = \begin{pmatrix} 4 & 2 \\ 2 & 3 \end{pmatrix} (old page, checked): l11=4=2l_{11} = \sqrt{4} = 2, l21=2/l11=1l_{21} = 2 / l_{11} = 1, l22=3−l212=2=1.414l_{22} = \sqrt{3 - l_{21}^2} = \sqrt{2} = 1.414, so L=(2012)L = \begin{pmatrix} 2 & 0 \\ 1 & \sqrt{2} \end{pmatrix}. For S=W⊤WS = W^\top W: l11=2l_{11} = \sqrt{2}, l21=−2/2=−2l_{21} = -2/\sqrt{2} = -\sqrt{2}, l22=5−2=3l_{22} = \sqrt{5 - 2} = \sqrt{3}. Check: LL⊤=(4221+2)LL^\top = \begin{pmatrix} 4 & 2 \\ 2 & 1 + 2 \end{pmatrix} for Σ\Sigma.

The level set xᵀSx = 1 is an ellipse whose axes are S's eigenvectors, with semi-axes 1/√λ

S=W⊤WS = W^\top W: semi-axes 1/6=0.4081/\sqrt{6} = 0.408 along (1,−2)/5(1, -2)/\sqrt{5} and 1/1=11/\sqrt{1} = 1 along (2,1)/5(2, 1)/\sqrt{5}, the direction of the tiny model's input. Steep curvature (large λ\lambda) means a narrow ellipse; a zero eigenvalue (PSD, not PD) would open it into two parallel lines.

The same circle under A in one step, under its SVD, and under its eigendecomposition

Drawn to scale: the circle has radius 1, the ellipse's long axis is 6.708. Only the SVD keeps the two marked arrows at 90° through every step; the eigen route skews the grid on the way.

10. The SVD: every matrix is rotate, stretch, rotate

Eigenvectors need a square matrix and may not exist. The singular value decomposition describes what any matrix does, including a 768×3072768 \times 3072 weight, and always exists: a rotation, a stretch along the axes, and another rotation. Predict first: does applying AA's three SVD factors one at a time end on the same ellipse as applying AA in one step? Then watch it, and compare with the eigendecomposition on the same circle (the buttons on the figure switch the route and the matrix; the steps below follow).

    A=UΣV⊤=∑i=1rσiuivi⊤A = U \Sigma V^\top = \sum_{i=1}^{r} \sigma_i\, u_i v_i^\top
    VV
    an n×nn \times n orthogonal matrix; its columns viv_i (the right singular vectors) are the input directions. V⊤V^\top rotates viv_i onto the ii-th axis.
    Σ\Sigma
    an m×nm \times n matrix, zero except for the singular values σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \dots \ge 0 on its diagonal: the stretch along each axis.
    UU
    an m×mm \times m orthogonal matrix; its columns uiu_i (the left singular vectors) are the output directions.
    rr
    the rank: the number of nonzero singular values.

    There are orthonormal input directions viv_i that the matrix sends to orthogonal output directions, Avi=σiuiA v_i = \sigma_i u_i. The sum form reads the matrix as rr rank-1 layers, largest first. Every matrix, of any shape, has an SVD.

    How to get it. A⊤A=VΣ⊤ΣV⊤A^\top A = V \Sigma^\top \Sigma V^\top, so the right singular vectors are the eigenvectors of the symmetric PSD matrix A⊤AA^\top A, the singular values are the square roots of its eigenvalues, and ui=Avi/σiu_i = A v_i / \sigma_i. For AA, A⊤AA^\top A has eigenvalues 45 and 5, so σ1=45=6.708\sigma_1 = \sqrt{45} = 6.708 and σ2=5=2.236\sigma_2 = \sqrt{5} = 2.236 (worked in the closed block below).

    Eigenvalues are not singular values
    AA's eigenvalues are 3 and 5, its singular values 6.708 and 2.236: a non-symmetric matrix can stretch some direction (by 6.708) more than it scales any eigenvector (at most 5), and its eigenvectors need not be orthogonal. For the symmetric PSD S=W⊤WS = W^\top W the two coincide: eigenvalues and singular values are both 6 and 1, and U=VU = V, so the second rotation exactly undoes the first (switch to S above). Stretch, norms and conditioning are singular-value questions.

    The SVD answers the earlier sections at once. Rank = number of nonzero σi\sigma_i; effective rank = number of σi\sigma_i above a tolerance. Four subspaces: u1..uru_1..u_r span the column space, ur+1..umu_{r+1}..u_m the left null space, v1..vrv_1..v_r the row space, vr+1..vnv_{r+1}..v_n the null space. Norms: spectral σ1\sigma_1, Frobenius ∑σi2\sqrt{\sum \sigma_i^2}, nuclear ∑σi\sum \sigma_i. Pseudo-inverse: A+=VΣ+U⊤A^{+} = V \Sigma^{+} U^\top, with Σ+\Sigma^{+} inverting the nonzero σi\sigma_i and leaving zeros; A+yA^{+} y is the least-squares solution of shortest length. Condition number: κ=σmax⁡/σmin⁡\kappa = \sigma_{\max} / \sigma_{\min} (section 11).

    Eckart-Young: truncation is arithmetic

    Keep only the kk largest layers, Ak=∑i=1kσiuivi⊤A_k = \sum_{i=1}^{k} \sigma_i u_i v_i^\top. The Eckart-Young(-Mirsky) theorem says no rank-kk matrix is closer to AA, in either the spectral or the Frobenius norm, and the errors are exactly

    ‖A−Ak‖2=σk+1,‖A−Ak‖F=σk+12+σk+22+⋯+σr2\lVert A - A_k \rVert_2 = \sigma_{k+1}, \qquad \lVert A - A_k \rVert_F = \sqrt{\sigma_{k+1}^2 + \sigma_{k+2}^2 + \dots + \sigma_r^2}

    Fig. 11, step by step: a real matrix rebuilt from rank-one layers

    1. A real 9 x 9 matrix. The cosine similarities of nine GPT-2 small input embeddings (section 1's table) as bars: 1 on the diagonal, 0.08 to 0.66 off it. It is symmetric and positive definite, so its SVD is its eigendecomposition, C=∑iσiuiui⊤C = \sum_i \sigma_i u_i u_i^\top.
    2. Layer 1, σ1=3.22\sigma_1 = 3.22: the shared direction. u1u_1 is positive for every word (0.23 to 0.38), so σ1u1u1⊤\sigma_1 u_1 u_1^\top is a nearly level table: the common direction that makes two random GPT-2 tokens average a cosine of 0.27 (section 13). Error ‖C−C1‖F=2.216\lVert C - C_1 \rVert_F = 2.216, 57% of ‖C‖F=3.909\lVert C \rVert_F = 3.909.
    3. Layer 2, σ2=1.202\sigma_2 = 1.202: king and queen. u2u_2 is large only on king (−0.58-0.58) and queen (−0.61-0.61), with the same sign, so this layer lifts their block. Error 1.862 (48%).
    4. Layer 3, σ3=1.102\sigma_3 = 1.102: Paris and London against cat and dog. u3u_3 is +0.56,+0.53+0.56, +0.53 on Paris and London and −0.34,−0.48-0.34, -0.48 on cat and dog: the layer lifts both pairs and lowers the cells between them. Error 1.501 (38%); three layers keep 85.2% of ‖C‖F2\lVert C \rVert_F^2.
    5. The error is the singular values left out. 3.22, 1.202, 1.102, 0.873, 0.79, 0.624, 0.442, 0.418, 0.33: after the first the spectrum is flat, so each further layer removes little (rank 4 still misses 31%), the same picture as GPT-2's weights in section 12. ‖C−C3‖F=σ42+⋯+σ92=1.501\lVert C - C_3 \rVert_F = \sqrt{\sigma_4^2 + \dots + \sigma_9^2} = 1.501, exactly as the theorem says.

    The singular values tell you, before you compress anything, exactly what you will lose: a spectrum that falls fast compresses well; a flat one does not. Storing AkA_k for an m×nm \times n matrix costs k(m+n)k(m + n) numbers (fold each σi\sigma_i into uiu_i) instead of mnmn: for 768×768768 \times 768 at k=64k = 64, 98,304 instead of 589,824.

    For AA, the best rank-1 approximation misses by exactly σ2=2.236\sigma_2 = 2.236 in both norms. On a real matrix, the query matrix of GPT-2 small's layer 5 (768 by 768) needs of its 768 singular directions to keep 90% of its squared Frobenius norm; a random matrix of the same shape needs . Real weights are more concentrated than noise, but far from low rank; section 12 shows what truncation does to the model's loss.

    PCA is the SVD of centred data

    Stack nn examples as the rows of XX, subtract each column's mean, and the covariance is C=1nX⊤XC = \frac{1}{n} X^\top X. Its eigenvectors (equivalently the right singular vectors of XX) are the principal directions, and the variance along the ii-th is σi2/n\sigma_i^2/n. For the centred points (2,1),(−2,−1),(1,1),(−1,−1)(2, 1), (-2, -1), (1, 1), (-1, -1): C=(2.51.51.51)C = \begin{pmatrix} 2.5 & 1.5 \\ 1.5 & 1 \end{pmatrix}, eigenvalues 3.427 and 0.073, and the first principal direction (0.851,0.526)(0.851, 0.526) carries 3.427/3.5=97.9%3.427/3.5 = 97.9\% of the variance: these points are almost a line. Centre first: without it the first "principal component" mostly points at the mean.

    What it is used for in ML
    Low-rank compression and LoRA initialisations (PiSSA starts LoRA from the top singular directions); PCA and whitening; the pseudo-inverse; spectral norms and condition numbers; Muon's orthogonalised update, which replaces UΣV⊤U\Sigma V^\top by UV⊤UV^\top (section 11); and analysing what a trained weight or update does (section 12).
    Worked: the SVD of A by hand, its rank-1 truncation, and the PCA numbers

    Signs chosen so both factors are rotations (old page, checked). A⊤A=(3405)(3045)=(25202025)A^\top A = \begin{pmatrix} 3 & 4 \\ 0 & 5 \end{pmatrix}\begin{pmatrix} 3 & 0 \\ 4 & 5 \end{pmatrix} = \begin{pmatrix} 25 & 20 \\ 20 & 25 \end{pmatrix}. Eigenvalues: (25−λ)2=400(25 - \lambda)^2 = 400, so λ=45\lambda = 45 or 5. Eigenvectors: v1=(1,1)/2v_1 = (1, 1)/\sqrt{2}, v2=(−1,1)/2v_2 = (-1, 1)/\sqrt{2}. Then u1=Av1/σ1=(3,9)/(245)=(1,3)/10u_1 = A v_1 / \sigma_1 = (3, 9)/(\sqrt{2}\sqrt{45}) = (1, 3)/\sqrt{10} and u2=Av2/σ2=(−3,1)/(25)=(−3,1)/10u_2 = A v_2/\sigma_2 = (-3, 1)/(\sqrt{2}\sqrt{5}) = (-3, 1)/\sqrt{10}; orthogonal, −3+3=0-3 + 3 = 0. So V⊤V^\top is a rotation by −45°-45° (it takes v1v_1 to the horizontal axis), Σ=diag⁡(6.708,2.236)\Sigma = \operatorname{diag}(6.708, 2.236) stretches, and UU is a rotation by 71.57°71.57° (the angle of (1,3)(1, 3)). Both have determinant +1+1. Checks: |det⁡A|=15=6.708×2.236|\det A| = 15 = 6.708 \times 2.236; ‖A‖F2=50=45+5\lVert A \rVert_F^2 = 50 = 45 + 5.

    A1=σ1u1v1⊤=45⋅(1,3)10⋅(1,1)2=32(1133),A_1 = \sigma_1 u_1 v_1^\top = \sqrt{45} \cdot \frac{(1, 3)}{\sqrt{10}} \cdot \frac{(1, 1)}{\sqrt{2}} = \frac{3}{2}\begin{pmatrix} 1 & 1 \\ 3 & 3 \end{pmatrix},

    The error A−A1=(1.5−1.5−0.50.5)A - A_1 = \begin{pmatrix} 1.5 & -1.5 \\ -0.5 & 0.5 \end{pmatrix} has spectral and Frobenius norm 2.25⋅2+0.25⋅2=5=2.236=σ2\sqrt{2.25 \cdot 2 + 0.25 \cdot 2} = \sqrt{5} = 2.236 = \sigma_2, as the theorem says (the two norms agree because the error has rank 1); A1=(1.51.54.54.5)A_1 = \begin{pmatrix} 1.5 & 1.5 \\ 4.5 & 4.5 \end{pmatrix}.

    PCA, four points. Each column already sums to 0. C=14(10664)C = \frac{1}{4}\begin{pmatrix} 10 & 6 \\ 6 & 4 \end{pmatrix}. Trace 3.5, determinant 2.5−2.25=0.252.5 - 2.25 = 0.25, so λ=(3.5±3.52−1)/2=3.427\lambda = (3.5 \pm \sqrt{3.5^2 - 1})/2 = 3.427 and 0.073.

    11. The condition number: why some problems lose all their digits

    Two linear systems of the same size, solved by the same code: one gives every digit right, the other gives garbage. The difference is one number,

    κ(A)=σmax⁡(A)σmin⁡(A)\kappa(A) = \frac{\sigma_{\max}(A)}{\sigma_{\min}(A)}

    the ratio of the most a matrix stretches any direction to the least: the aspect ratio of the ellipse the unit circle becomes. κ=1\kappa = 1 for an orthogonal matrix (a circle stays a circle); κ=∞\kappa = \infty for a singular one (some direction is squashed flat). For the running examples, κ(A)=6.708/2.236=3\kappa(A) = 6.708/2.236 = 3 and κ(W)=6/1=2.449\kappa(W) = \sqrt{6}/1 = 2.449. Solving Ax=bAx = b when bb carries a small error δb\delta b (measurement noise or rounding):

    ‖δx‖‖x‖≤κ(A)‖δb‖‖b‖\frac{\lVert \delta x \rVert}{\lVert x \rVert} \le \kappa(A)\, \frac{\lVert \delta b \rVert}{\lVert b \rVert}

    A relative error in the data can come out up to κ\kappa times bigger in the answer. Rule of thumb: you lose about log10⁡κ\log_{10}\kappa decimal digits. float32 carries about 7 digits, bfloat16 about 3, so a problem with κ=104\kappa = 10^4 is fine in float64, marginal in float32 and hopeless in bfloat16 (formats themselves are on Numerical computing for ML).

    The same thing happens inside a model fit: the peak of double descent is ill-conditioning. On Regularisation (section 8, whose weak-features double-descent figure draws the singular values inline), the smallest singular value of a 40-row training matrix collapses when the number of features reaches 40 (median 0.086, against 3.55 at p=10p = 10), so least squares divides the noise by almost zero and the test error peaks.

    Two nearly parallel lines: a 0.035% nudge to b moves the crossing from (1, 1) to (0, 2)

    B=(1111.001)B = \begin{pmatrix} 1 & 1 \\ 1 & 1.001 \end{pmatrix}, κ=4,002\kappa = 4{,}002. Each equation is a line; the solution is where they cross. With b=(2,2.001)b = (2, 2.001) they cross at x=(1,1)x = (1, 1); change bb to (2,2.002)(2, 2.002), a relative change of 0.035%, and the crossing jumps to x=(0,2)x = (0, 2), a relative change of 100%: an amplification of 2,829, within the bound of 4,002. The lines are drawn to scale: they differ in slope by 0.03 degrees, which is why the crossing is so poorly pinned down.

    Normal equations square it: a real float32 run

    κ(X⊤X)=κ(X)2\kappa(X^\top X) = \kappa(X)^2, because the singular values of X⊤XX^\top X are the squares of those of XX (section 7 had 2.92 against 8.55). On a harder problem the squaring is decisive. Fit a degree-6 polynomial to 50 evenly spaced points on [0,1][0, 1] (true coefficients 1,−2,3,−4,5,−6,71, -2, 3, -4, 5, -6, 7, no noise) in float32, computed with NumPy by this page's script:

    MethodCondition number it works withRelative error in the coefficients
    Normal equations X⊤Xw=X⊤yX^\top X w = X^\top yκ(X)2≈3.9×108\kappa(X)^2 \approx 3.9 \times 10^80.77 (77%)
    QR, then Rw=Q⊤yRw = Q^\top yκ(X)≈1.98×104\kappa(X) \approx 1.98 \times 10^40.000018

    Same data, same precision, same library; only the algorithm differs. κ(X)2\kappa(X)^2 exceeds 1 over float32's unit roundoff (2−242^{-24}), about 1.7×1071.7 \times 10^7, so the normal equations lose every digit. A rerun on 2026-11-07 with NumPy 2.5.4 on OpenBLAS 0.3.34 gave 0.87 for the normal equations and the same 0.000018 for QR: which digits are lost in float32 depends on the BLAS build, not whether they are.

    What it does to gradient descent

    Near a minimum a loss looks like a bowl 12w⊤Hw\tfrac{1}{2} w^\top H w whose curvature matrix is the Hessian HH (symmetric, PD at a strict minimum: section 9's ellipse). Gradient descent's step must stay below 2/λmax⁡2/\lambda_{\max} to be stable in the steepest direction, but then it crawls along the flattest, curvature λmin⁡\lambda_{\min}. With the best fixed step, η=2/(λmax⁡+λmin⁡)\eta = 2/(\lambda_{\max} + \lambda_{\min}), the error shrinks by (κ−1)/(κ+1)(\kappa - 1)/(\kappa + 1) per step, κ=λmax⁡/λmin⁡\kappa = \lambda_{\max}/\lambda_{\min}. The old page stated this rate without the best-step condition; it holds only at that step size, and is slower at any other. The derivation, with the root's diag(1, 10) bowl, is on Calculus and optimisation for ML; the root's Gradient lab tab animates it.

    Fixing conditioning: preconditioning

    • Rescale the inputs. Standardising features (zero mean, unit variance) makes X⊤XX^\top X closer to a multiple of II; normalisation layers and careful initialisation do the same inside a network (Normalisation and initialisation).
    • Regularise. Adding λI\lambda I lifts the smallest eigenvalue (ridge, section 7; damping in second-order methods).
    • Precondition the update. Multiply the gradient by an approximation of H−1H^{-1}: Newton's method uses H−1H^{-1} itself; Adam divides each coordinate by the square root of an exponential moving average of its squared gradient, a diagonal preconditioner.
    • Orthogonalise the update. Muon takes the momentum update G=UΣV⊤G = U\Sigma V^\top of a 2-D weight and replaces it by UV⊤UV^\top: every singular value set to 1, so the update has κ=1\kappa = 1 and pushes equally in every direction it touches instead of being dominated by a few. A full SVD is too slow, so it runs a few Newton-Schulz iterations, which "can be stably run in bfloat16" (Keller Jordan, Muon, 8 Dec 2024); its tuned coefficients, which matrices it applies to and how it behaves in training are on Optimisers and learning-rate schedulers.
    What it is used for in ML
    Choosing solvers (QR or Cholesky, never an explicit inverse); choosing precision (mixed-precision training keeps some accumulations in float32 for this reason); understanding why normalisation, warmup and adaptive optimisers help; and reading optimiser papers, which are mostly about preconditioning.
    Derivation: why the error bound is κ\kappa

    The computed answer is off by δx=A−1δb\delta x = A^{-1}\delta b. Two inequalities from the SVD: ‖δx‖=‖A−1δb‖≤‖δb‖/σmin⁡\lVert \delta x \rVert = \lVert A^{-1}\delta b \rVert \le \lVert \delta b \rVert / \sigma_{\min} (the inverse stretches by at most 1/σmin⁡1/\sigma_{\min}), and ‖b‖=‖Ax‖≤σmax⁡‖x‖\lVert b \rVert = \lVert Ax \rVert \le \sigma_{\max} \lVert x \rVert. Divide the first by ‖x‖≥‖b‖/σmax⁡\lVert x \rVert \ge \lVert b \rVert / \sigma_{\max} and the bound follows. It is tight: equality when bb lies along u1u_1 and δb\delta b along unu_n.

    Orthogonalising A: the small singular value climbs to 1 in six Newton-Schulz steps

    The simplest Newton-Schulz iteration is X←1.5X−0.5XX⊤XX \leftarrow 1.5X - 0.5\,XX^\top X, which maps each singular value ss to 1.5s−0.5s31.5s - 0.5s^3 and leaves the singular vectors alone. Start from A/‖A‖FA/\lVert A \rVert_F, whose singular values are 0.949 and 0.316. After each step the smaller one reads 0.459, 0.640, 0.829, 0.958, 0.997, 1.000 while the larger reaches 1.000 after two: six steps give UV⊤=(0.894−0.4470.4470.894)UV^\top = \begin{pmatrix} 0.894 & -0.447 \\ 0.447 & 0.894 \end{pmatrix}, the rotation by 26.6° that is the "nearest orthogonal matrix" to AA. Muon's quintic version uses tuned coefficients to push small singular values up faster, in five steps. Try it on any matrix in the Matrix playground.

    12. Low rank in ML: what is really low rank, and what is not

    LoRA, MLA, GaLore and many compression papers all say "low rank". Low rank of what: pretrained weights, the updates fine-tuning makes, the gradients, or the cached keys and values? The answers differ, and real numbers settle them.

    The pattern
    Low rank is a fact about changes and products (fine-tuning updates, gradients, the query-key product of one head, the KV bottleneck a model is trained with), much more than about pretrained weight matrices themselves. Methods that impose low rank during training (LoRA, MLA, GaLore) work; compressing a finished model by truncated SVD costs accuracy fast.

    Pretrained weights: concentrated, but not low rank

    The old page said trained weight matrices "are often effectively low rank". Measured on GPT-2 small (the released weights, 2,048 tokens of WikiText-2 test text, script in this page's source folder), that does not hold in the sense that matters for compression:

    k90: singular directions needed to keep 90% of the squared Frobenius norm, out of 768; "random" is a Gaussian matrix of the same shape and norm. Output error: relative error of the layer's output on the real inputs that reach it, ‖X(W−Wk)‖F/‖XW‖F\lVert X(W - W_k) \rVert_F / \lVert XW \rVert_F. Loss: GPT-2's next-token loss on the same text with only that one matrix replaced by its rank-64 truncation (unchanged model: nats).

    Three readings. First, every real spectrum is more concentrated than random (k90 near 300 against 391 for the attention matrices), so there is structure. Second, it is nowhere near rank 64: keeping 64 of 768 directions leaves a relative output error of 42% for the MLP input matrix and 70% to 86% for the other five. Third, how much a matrix matters is not how compressible it is: cutting the MLP input matrix to rank 64 raises the loss by 0.72 nats, cutting the key matrix by 0.20. Truncating every attention and MLP matrix in all 12 layers to 90% of its rank, which saves no parameters at all, already raises the loss from 3.17 to 3.91; at 50% of the rank it is 8.50, close to the 10.8 of a uniform guess over 50,257 tokens (). Real weights carry information in their small singular values too.

    Truncate one real GPT-2 matrix: the matrix error falls smoothly, the loss does not recover until k is large

    Predict first: at k=64k = 64, how much of the query matrix's output on real text is lost? Then pick a matrix and move kk (the Eckart-Young truncation of section 10).

    Spectrum: real against random

    Singular values, largest first, on a log scale. Grey: a Gaussian random matrix of the same shape scaled to the same Frobenius norm. Shaded: the directions truncation drops.

    Error as k grows

    Relative error of the matrix itself (Frobenius, exact from the spectrum) and of its output on 2,048 real token inputs, ‖X(W−Wk)‖F/‖XW‖F\lVert X(W - W_k)\rVert_F / \lVert XW \rVert_F. The two differ because real inputs do not point equally in every direction.

    The model's loss with this one matrix truncated

    All 48 attention and MLP matrices truncated at once (table)

    Each matrix keeps the stated fraction of its full rank (768); the attention input matrix is truncated as its three 768 by 768 blocks, Q, K and V, separately. "Parameters in low-rank form" counts k(m+n)k(m + n) per matrix, capped at mnmn: below rank 384 for a square matrix, a truncated SVD is not even smaller than the original. A uniform guess over the 50,257-token vocabulary scores ln⁡50257=10.8\ln 50257 = 10.8 nats.

    Fine-tuning updates: genuinely low rank

    LoRA (Hu et al. 2021) does not compress the weights; it constrains the change. The layer computes W0x+αrBAxW_0 x + \frac{\alpha}{r} B A x with the pretrained W0W_0 frozen, BB of shape d×rd \times r, AA of shape r×kr \times k and r≪min⁡(d,k)r \ll \min(d, k). BABA is a sum of rr outer products, so its rank is at most rr. BB starts at zero, so training starts exactly at the pretrained model, and after training BABA can be merged into W0W_0 for free inference. For d=k=4096d = k = 4096, r=8r = 8: 2×4096×8=65,5362 \times 4096 \times 8 = 65{,}536 trainable numbers instead of 16,777,21616{,}777{,}216, 0.39%. The method, its variants (DoRA, rsLoRA, PiSSA) and current practice are on Parameter-Efficient Fine-Tuning (PEFT); the LoRA paper page trains LoRA live on a toy where the true update is known.

    Correction carried from the root
    The old page said "ranks as low as 1 to 8 were competitive on GPT-3". The paper's words: "a rank as small as one suffices for adapting both WqW_q and WvW_v" on GPT-3, with Table 6 testing r=1,2,4,8,64r = 1, 2, 4, 8, 64; and the low "intrinsic rank" of the update is the authors' hypothesis, which they then test (LoRA, arXiv 2106.09685, sections 4 and 7). The idea goes back to Aghajanyan, Zettlemoyer and Gupta: "by optimizing only 200 trainable parameters randomly projected back into the full space, we can tune a RoBERTa model to achieve 90% of the full parameter performance levels on MRPC" (Intrinsic dimensionality, arXiv 2012.13255).

    Two real adapters, measured. Two public GPT-2 LoRA adapters on Hugging Face target the attention input matrix c_attn (768→2304768 \to 2304) in all 12 layers: monsterapi/gpt2_alpaca-lora (r=8r = 8, α=16\alpha = 16, trained on Alpaca) and harigovind511/GPT2-Guanaco-LoRA (r=32r = 32, α=8\alpha = 8, a QLoRA run on a 4-bit base). Rebuilding each ΔW=αrBA\Delta W = \frac{\alpha}{r} BA and taking its SVD:

    • Exactly rr singular values are nonzero (the next is about 10−1410^{-14}, rounding), as the algebra says.
    • Inside its budget the update is far lower rank still: the largest singular value alone carries a median of ‖ΔW‖F2\lVert \Delta W \rVert_F^2 for the r=8r = 8 adapter and for the r=32r = 32 one. Most of the rank budget is unused.
    • The update is small: ‖ΔW‖F/‖W0‖F\lVert \Delta W \rVert_F / \lVert W_0 \rVert_F is 2% to 8% (Alpaca) and 1.5% to 4% (Guanaco), growing with depth.
    The LoRA paper's amplification factor on these adapters, layer by layer (explorer)

    The paper's "amplification factor" (section 7.3 and appendix H.4: ‖ΔWq‖F/‖U⊤WqV⊤‖F\lVert \Delta W_q \rVert_F / \lVert U^\top W_q V^\top \rVert_F, with U,VU, V the top-rr singular directions of ΔWq\Delta W_q) was "21.5≈6.91/0.3221.5 \approx 6.91/0.32 for r=4r = 4" on GPT-3's layer 48, and "only around 2" at r=64r = 64. On these adapters it is 0.6 to 3.4 (Alpaca) and 0.2 to 0.6 (Guanaco). It does not reproduce at this scale; GPT-2 small, different tasks, ranks and scales (α/r\alpha/r = 2 and 0.25) are all different from the paper's setting, so this is a measurement, not a refutation. The paper's other finding does hold here: WqW_q projected on the update's directions is larger than on random directions (median ratio 1.6 and 1.3), so the update is correlated with the pretrained weight.

    Inside its rank budget, a real LoRA update puts most of its energy in one direction

    Gradients: GaLore

    GaLore keeps full-rank weights but notices that the gradient of a weight matrix is approximately low rank (one example's gradient is exactly rank 1, section 5; a batch sums a few such outer products). Every TT steps it takes the SVD of the current gradient GG (m×nm \times n, m≤nm \le n), keeps the top rr left singular vectors as a projector PP (m×rm \times r), runs Adam on the small matrix P⊤GP^\top G (r×nr \times n), and maps the update back with PP. Adam's two moment matrices shrink from 2mn2mn numbers to 2nr2nr, plus mrmr for PP; the paper reports that it "reduces memory usage by up to 65.5% in optimizer states" (GaLore, Zhao et al. 2024, arXiv 2403.03507).

    GaLore's settings and one matrix counted

    The paper uses T=200T = 200 and scale α=0.25\alpha = 0.25 for its LLaMA pretraining runs. For one 4096×40964096 \times 4096 matrix at r=128r = 128: 33,554,432 moment numbers become 4096×128+2×4096×128=1,572,8644096 \times 128 + 2 \times 4096 \times 128 = 1{,}572{,}864.

    Cached keys and values: MLA

    Multi-head latent attention (DeepSeek-V2 and V3) caches one compressed vector per token and layer and rebuilds every head's keys and values from it, so the map from hidden state to keys is a product through a 512-wide bottleneck, exactly the BABA shape: 512+64=576512 + 64 = 576 cached numbers per token per layer against 32,768 for standard multi-head attention with 128 heads of 128. The mechanism, the absorbed form and the animation are on Attention variants.

    13. Embeddings and attention, read as linear algebra

    Attention itself, softmax⁡(QK⊤/dk)V\operatorname{softmax}(QK^\top/\sqrt{d_k})V, with every shape and why the scores are scaled, is taught on The transformer from zero. Three facts from this page's side, measured on GPT-2 small:

    • An embedding lookup is a matrix product. Token ii as a one-hot vector eie_i (all zeros except a 1 in position ii) times the table EE (V×dV \times d) picks out row ii. Libraries index instead, because multiplying by zeros is wasteful, but the gradient is that of a matrix product: only the rows that were used get updated. GPT-2 small's table is 50,257×76850{,}257 \times 768, reused as the output matrix (section 5's softmax bottleneck).
    • Real embeddings are not centred. Two random GPT-2 token embeddings have cosine similarity on average, not 0: the vectors share a common direction (the mean embedding's length is of the average embedding's length). So cosine similarities like cat to dog (0.55) must be read against that baseline, and many retrieval systems subtract the mean or whiten embeddings before comparing them; a PCA of them must centre first.
    • The query-key product is a low-rank bilinear form. The score between hidden states hih_i and hjh_j is qi⋅kj=hi⊤(WQWK⊤)hjq_i \cdot k_j = h_i^\top (W_Q W_K^\top) h_j. The 768×768768 \times 768 matrix WQWK⊤W_Q W_K^\top is a product through a 64-wide middle (dk=64d_k = 64 in GPT-2 small), so its rank is at most 64. Measured on layer 5, head 0: exactly nonzero singular values out of 768 (the 65th is 3×10−143 \times 10^{-14}, rounding). Each head compares tokens through a 64-dimensional "view" of the 768-dimensional residual stream; twelve heads give twelve such views. The same holds for WVWOW_V W_O, the head's output map (Elhage et al., A Mathematical Framework for Transformer Circuits, 2021, call these the QK and OV circuits). The 768×2304768 \times 2304 matrix c_attn holds WQ,WK,WVW_Q, W_K, W_V side by side, each twelve 768×64768 \times 64 blocks, one per head.
    Attention piece by piece in this page's terms, with the root's numbers

    For a sequence of TT token vectors stacked as the rows of HH (T×dT \times d), one head computes Q=HWQQ = H W_Q, K=HWKK = H W_K, V=HWVV = H W_V (projections, section 2); QK⊤QK^\top is T×TT \times T, entry (i,j)(i, j) the dot product of query ii with key jj (row times column, section 3); dividing by dk\sqrt{d_k} keeps the scores' spread near 1 when the entries of qq and kk have variance 1 (the variance of a sum of dkd_k products is dkd_k); softmax along each row turns scores into positive weights summing to 1; multiplying by VV makes each output row a weighted average of value vectors, a point inside their convex hull: the column picture of section 3, with the weights as coefficients.

    Treat the tiny model's input x=(2,1)x = (2, 1) as a query and its three word rows as keys and values (dk=2d_k = 2): scores Wx/2=(1.414,0.707,0)Wx/\sqrt{2} = (1.414, 0.707, 0), weights (0.576,0.284,0.140)(0.576, 0.284, 0.140), output

    0.576(1,0)+0.284(0,1)+0.140(1,−2)=(0.716,0.004)0.576\,(1, 0) + 0.284\,(0, 1) + 0.140\,(1, -2) = (0.716, 0.004)

    The same example is decoded symbol by symbol on the root's Reading tab ("Reading an equation in a paper").

    Go deeper (optional): the full Transformer, trained live, is on the Attention Is All You Need paper page; the cost of attention as einsum counts and the backward pass through softmax are on Matrix calculus and backprop; rotary position encodings, which rotate query and key pairs, on the RoFormer paper page.

    14. Which tool when

    • Solving Ax=bAx = b: factorise and solve (torch.linalg.solve, cholesky_solve); never form A−1A^{-1}, which is slower and less accurate, and fails badly when κ\kappa is large.
    • Least squares: QR by default (lstsq); normal equations only when well conditioned; SVD when rank-deficient.
    • Top few singular directions of a big matrix: power iteration or a randomized SVD (torch.svd_lowrank), not a full SVD.
    • Eigen or SVD: eigen for symmetric matrices; SVD for anything about stretch, size or rank.
    • Rank of a floating-point matrix: never literally; count singular values above a tolerance (NumPy's matrix_rank uses σmax⁡⋅max⁡(m,n)⋅ε\sigma_{\max} \cdot \max(m, n) \cdot \varepsilon) or look at how fast the spectrum falls.
    The decompositions side by side: form, when each exists, cost (reference)
    DecompositionFormExists forCost (n×nn \times n, leading term)Use it for
    LUA=LUA = LU (with row swaps)any square, invertible23n3\tfrac{2}{3}n^3general linear systems
    CholeskyS=LL⊤S = LL^\topsymmetric PD13n3\tfrac{1}{3}n^3PD systems, Gaussian sampling, PD test
    QRA=QRA = QRevery matrix43n3\tfrac{4}{3}n^3 (2mn2−23n32mn^2 - \tfrac{2}{3}n^3 for m×nm \times n)least squares, orthonormal bases
    Eigen (symmetric)S=QΛQ⊤S = Q\Lambda Q^\topsymmetrica small multiple of n3n^3covariances, Hessians, PCA
    Eigen (general)A=PΛP−1A = P\Lambda P^{-1}diagonalisable squarelarger multiple of n3n^3repeated application, stability
    SVDA=UΣV⊤A = U\Sigma V^\topevery matrixthe largest multiple of n3n^3 of theserank, norms, conditioning, compression, pseudo-inverse

    Leading-term operation counts from Trefethen and Bau, Numerical Linear Algebra (1997): Householder QR lecture 10, LU lecture 20, Cholesky lecture 23. Eigen and SVD solvers are iterative; their constants depend on the algorithm and whether vectors are wanted.

    Depth: mistakes and interview rehearsal

    Common mistakes, each with the section that shows it

    Treating eigenvalues as stretches. For a non-symmetric matrix they differ from the singular values (3 and 5 against 6.708 and 2.236 for AA), and the eigenvectors need not be orthogonal (section 10).

    Forgetting to centre before PCA. 1nX⊤X\frac{1}{n}X^\top X is the covariance only if every column of XX has mean zero. GPT-2's embeddings, with a mean cosine near 0.27 between random tokens, are a real case (section 13).

    Mixing layouts and conventions. WxWx with WW as (out, in), XW⊤XW^\top in PyTorch, XWXW with GPT-2's Conv1D (in, out). Each is right; mixing two in one derivation puts transposes in the wrong places. Write the shape beside every symbol. (Derivative layouts are a separate convention, owned by Matrix calculus and backprop.)

    Reading "low rank" as "small", or "small" as "unimportant". A rank-1 matrix can have a huge norm and a full-rank matrix can be tiny: rank counts directions, norms measure size. And small singular values of a pretrained weight still matter: dropping the bottom 10% of every GPT-2 matrix raises the loss from 3.17 to 3.91 (section 12).

    Inverting a matrix to solve a system, or forming X⊤XX^\top X. 77% coefficient error against 0.0018% for QR in the float32 run of section 11.

    Silent broadcasting. (n,1)−(n,)(n, 1) - (n,) gives an n×nn \times n table, not nn differences (section 4). Assert shapes before every reduction.

    Assuming AB=BAAB = BA. Order matters (PQ≠QPPQ \ne QP in section 3), and (AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top, (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1}: both reverse the order.

    Interview questions, with the one-line answer and where it is derived
    QuestionShort answerSection
    What is the rank of an outer product uv⊤uv^\top?1 (if u,v≠0u, v \ne 0): every row is a multiple of v⊤v^\top3, 5
    Why is X⊤XX^\top X PSD? When is it PD?w⊤X⊤Xw=‖Xw‖2≥0w^\top X^\top X w = \lVert Xw \rVert^2 \ge 0; PD when XX has full column rank9
    Eigen decomposition against SVD?eigen: square, directions not turned, may be complex or missing; SVD: every matrix, orthonormal in and out directions, nonnegative stretches8, 10
    How do you get the best rank-kk approximation, and its error?truncated SVD; spectral error σk+1\sigma_{k+1}, Frobenius ∑i>kσi2\sqrt{\sum_{i \gt k} \sigma_i^2} (Eckart-Young)10
    Why does LoRA save memory, and what does it cost?trains r(d+k)r(d + k) instead of dkdk numbers and their optimiser state; caps the update's rank at rr12
    What does the condition number tell you?error amplification in solves (lose about log10⁡κ\log_{10}\kappa digits); gradient descent's zig-zag on a quadratic11
    Why scale attention scores by dk\sqrt{d_k}?a dot product of dkd_k unit-variance terms has variance dkd_k; dividing keeps softmax out of saturation13
    Derive the least-squares solution.residual orthogonal to the columns: X⊤(y−Xw)=0X^\top(y - Xw) = 07
    Why are eigenvectors of a symmetric matrix orthogonal?(λ1−λ2)v1⋅v2=0(\lambda_1 - \lambda_2)\, v_1 \cdot v_2 = 09
    What does the determinant mean?signed area (volume) scale factor; product of eigenvalues; zero iff singular2, 8