Linear algebra for LLMsPart 7: Decompositions and low rank
The SVD
8 min
Rotate, stretch, rotate
Any matrix factors this way. turns the input (an orthogonal matrix: rotation or reflection), stretches each axis by a singular value , and turns the result into place. Unlike eigenvectors, the SVD always exists, for every shape.
Write
Our matrix
and . It’s not a rotation or a pure stretch. The SVD will split it into exactly those pieces.
Singular values come from
is symmetric, so the last deck applies: its eigenvalues are the and its eigenvectors are the columns of . Here those are () and ().
Write
Then from
Each right singular vector lands on times a unit vector . The come out orthogonal too: .
Write
The three factors
rotates by , stretches the axes by 6.708 and 2.236, rotates by about . Multiplied back, they give . Check: .
What the numbers mean
- is the most can stretch any unit vector: , reached at .
- is the least.
- The number of non-zero is the rank.
- A unit circle maps to an ellipse with half-axes and .
Write
✏️ Your turn
Singular values of a diagonal matrix
Compute and the singular values.
Work it out on paper, then go to the next card to check.
Answer
Singular values are never negative, even though the matrix has a : signs and swaps live in and .
For a weight matrix
U, S, Vh = torch.linalg.svd(W, full_matrices=False)
# W ≈ U @ torch.diag(S) @ Vh; S is sorted largest firstIn a transformer
For a weight there are 4,096 singular values. How fast they fall off tells you how much of the matrix a few directions explain: the next deck turns that into compression and LoRA.
📌 Recap
The SVD in one card
- for every matrix: rotate, stretch, rotate.
- = eigenvalues of ; .
- = largest stretch; rank = number of non-zero .
- Example: has .
Write