Linear algebra for LLMsPart 7: Decompositions and low rank

Low-rank approximation

6 min

The SVD is a sum of rank-1 pieces

A=σ1u1v1⊤+σ2u2v2⊤+⋯+σrurvr⊤\displaystyle A = \sigma_1 u_1 v_1^\top + \sigma_2 u_2 v_2^\top + \dots + \sigma_r u_r v_r^\top

UΣV⊤U\Sigma V^\top written as a sum of outer products: Part 2’s fourth view of a matrix product. Each piece is rank 1, and they come sorted from the biggest σ\sigma down.

Write

A=∑iσi uivi⊤A = \sum_i \sigma_i\, u_i v_i^\top, largest σi\sigma_i first.

Our two pieces

+=

σ1u1v1⊤=1.5[1133]\sigma_1 u_1 v_1^\top = 1.5\begin{bmatrix} 1 & 1 \\ 3 & 3 \end{bmatrix} carries most of AA; the second piece is the correction.

Keep the top kk

Ak=∑i=1kσi uivi⊤\displaystyle A_k = \sum_{i=1}^{k} \sigma_i\, u_i v_i^\top

Eckart–Young theorem: truncating the SVD gives the best rank-kk approximation there is, measured in the Frobenius or the spectral norm. No other rank-kk matrix is closer.

Write

Best rank-kk approximation: keep the top kk singular triples (Eckart–Young).

How much you lose

∥A−Ak∥F=σk+12+⋯+σr2\displaystyle \|A - A_k\|_F = \sqrt{\sigma_{k+1}^2 + \dots + \sigma_r^2}
∥A−A1∥F=σ2=5≈2.24,∥A∥F=50\displaystyle \|A - A_1\|_F = \sigma_2 = \sqrt5 \approx 2.24, \qquad \|A\|_F = \sqrt{50}

The error is exactly the singular values you dropped. Here the first piece keeps σ12/(σ12+σ22)=45/50=90%\sigma_1^2 / (\sigma_1^2 + \sigma_2^2) = 45/50 = 90\% of the “energy” ∥A∥F2\|A\|_F^2.

Write

Error = the dropped σ\sigma’s; energy kept = ∑i≤kσi2/∑iσi2\sum_{i \le k} \sigma_i^2 / \sum_i \sigma_i^2.

Storage

m×n  ⟶  k(m+n)    (store UkΣk and Vk)\displaystyle m \times n \;\longrightarrow\; k(m + n) \;\; (\text{store } U_k\Sigma_k \text{ and } V_k)
4096×4096, k=64 ⁣:  524,288 vs 16,777,216  (3.1%)\displaystyle 4096 \times 4096,\ k = 64\!: \; 524{,}288 \text{ vs } 16{,}777{,}216 \;(3.1\%)

If the singular values fall off fast, a few directions hold most of the matrix, and storing two thin factors is far cheaper.

✏️ Your turn

Rank-1 approximation

σ1=5, u1=[0.6,0.8], v1=[1,0]\sigma_1 = 5,\ u_1 = [0.6, 0.8],\ v_1 = [1, 0]

Write A1=σ1u1v1⊤A_1 = \sigma_1 u_1 v_1^\top.

Work it out on paper, then go to the next card to check.

Answer

5[0.60.8][10]=[3040]\displaystyle 5\begin{bmatrix} 0.6 \\ 0.8 \end{bmatrix}\begin{bmatrix} 1 & 0 \end{bmatrix} = \begin{bmatrix} 3 & 0 \\ 4 & 0 \end{bmatrix}

Every column is a multiple of u1u_1, every row a multiple of v1⊤v_1^\top: rank 1.

Where low rank shows up

In a transformer

  • LoRA learns a low-rank update to a weight (next deck).
  • Compression: replacing a weight by a truncated SVD shrinks it, at some cost in accuracy.
  • Attention heads are low-rank by construction (Part 3).
  • Measuring the singular values of weights or activations is a standard way to see how many directions really matter.

📌 Recap

Low-rank approximation in one card

  • A=∑iσiuivi⊤A = \sum_i \sigma_i u_i v_i^\top, sorted by σi\sigma_i.
  • Truncating to kk terms is the best rank-kk approximation.
  • Error: ∥A−Ak∥F=∑i>kσi2\|A - A_k\|_F = \sqrt{\sum_{i > k}\sigma_i^2}.
  • Storage: mn→k(m+n)mn \to k(m + n).

Write

The four lines above.