Linear algebra for LLMsPart 3: Matrices as transformations
Span, basis and rank
8 min
Span: everything you can mix
The span of some vectors is every linear combination of them. and point different ways, so reaches every point of the plane.
Write
Parallel vectors span only a line
, so mixing and never leaves the dashed line. The second vector added nothing new.
Independence and basis
Vectors are linearly independent when none of them is a mix of the others. A basis is an independent set that spans the whole space. Every basis of has exactly vectors; that number is the dimension.
In :
- and : independent, span the plane. A basis.
- and : dependent, span a line.
- , and : dependent (three vectors in 2D), span the plane.
Write
Column space and rank
Every output is a mix of ’s columns, so the outputs form the span of the columns: the column space. Its dimension is the rank: the number of independent columns. (It always equals the number of independent rows too.)
Write
A rank-1 matrix
Row 2 is twice row 1, and every column is a multiple of . Three columns, but only one direction: rank 1. Every rank-1 matrix is an outer product , and every output lands on the line through .
Write
Limits on rank
An matrix can’t have more independent columns than it has columns, or rows. And a product can’t have more rank than either factor: whatever passes through a narrow middle can’t widen again. A product has rank at most .
Write
✏️ Your turn
Find the rank
What is ? Look for a row that’s a mix of the others.
Work it out on paper, then go to the next card to check.
Answer: rank 2
Row 3 = row 1 + row 2, so only two rows are independent. Rows 1 and 2 aren’t multiples of each other, so the rank is exactly 2. The same holds for columns: column 3 = column 1 + column 2.
An attention head is low-rank
In a transformer
and are , so is a matrix of rank at most . With and , each head compares tokens inside a 128-dimensional slice of the 4,096-dimensional space. LoRA (Part 7) uses the same fact on purpose: a rank- update is cheap.
Write
In PyTorch
torch.linalg.matrix_rank(A) # rank, from the singular values
torch.outer(u, v) # a rank-1 matrix u vᵀReal weight matrices are almost never exactly rank-deficient; what matters in practice is how much of them is captured by a few directions. Part 7 measures that with the SVD.
📌 Recap
Span, basis and rank in one card
- Span: all combinations. Independent: none is a combination of the others. Basis: both.
- Column space = all outputs ; rank = its dimension.
- Rank 1 ⇔ outer product .
- : one head’s has rank .
Write