TExcelTAD
Home › Math for Machine Learning › Matrix Approximation

Matrix Approximation

Big matrices are often nearly low rank: a ratings table is driven by a few tastes, an image by a few shapes, a weight update by a few directions. Truncating the SVD after kk terms gives the best possible rank-kk approximation, a result called the Eckart-Young theorem. It is the mathematical basis of PCA, compression, denoising and low-rank adapters.

Truncated SVD

Keep the kk largest singular values:

A^k=∑i=1kσi uiviT=UkΣkVkT\hat{A}_k = \sum_{i=1}^{k} \sigma_i\,\mathbf{u}_i\mathbf{v}_i^T = U_k\Sigma_kV_k^T

Storage drops from mnmn numbers to k(m+n+1)k(m + n + 1).

Eckart-Young. For every matrix BB of rank at most kk,

∥A−A^k∥2=σk+1≤∥A−B∥2,∥A−A^k∥F=σk+12+⋯+σr2≤∥A−B∥F\lVert A - \hat{A}_k \rVert_2 = \sigma_{k+1} \le \lVert A - B \rVert_2, \qquad \lVert A - \hat{A}_k \rVert_F = \sqrt{\sigma_{k+1}^2 + \cdots + \sigma_r^2} \le \lVert A - B \rVert_F

The error of the best approximation is read directly off the discarded singular values.

python · try it yourself
import numpy as np

rng = np.random.default_rng(0)
# 30 users x 20 movies built from 2 hidden tastes, plus noise
users = rng.normal(size=(30, 2)); movies = rng.normal(size=(20, 2))
R = users @ movies.T + 0.1 * rng.normal(size=(30, 20))
U, s, Vt = np.linalg.svd(R, full_matrices=False)
print("singular values:", np.round(s[:5], 3), "...")
for k in (1, 2, 3, 5):
    Rk = U[:, :k] * s[:k] @ Vt[:k]
    err = np.linalg.norm(R - Rk)
    print(f"k={k}: Frobenius error {err:.4f}  predicted by tail of s: {np.sqrt((s[k:]**2).sum()):.4f}")
singular values: [26.159 17.958  0.906  0.815  0.749] ...
k=1: Frobenius error 18.0979  predicted by tail of s: 18.0979
k=2: Frobenius error 2.2446  predicted by tail of s: 2.2446
k=3: Frobenius error 2.0534  predicted by tail of s: 2.0534
k=5: Frobenius error 1.7296  predicted by tail of s: 1.7296

The first two singular values are large and the rest are small and similar: the "elbow" reveals the two hidden tastes. Beyond k=2k = 2 we only fit noise.

How much energy is kept?

The fraction of the squared Frobenius norm captured by rank kk is ∑i≤kσi2/∑iσi2\sum_{i \le k}\sigma_i^2 / \sum_i \sigma_i^2. For centred data this is exactly the explained variance ratio of PCA.

Random matrices are not low rank

Low-rank structure is a property of the data, not a free lunch. A matrix of pure noise has slowly decaying singular values, and truncating it loses a lot.

python · try it yourself
import numpy as np

rng = np.random.default_rng(1)
noise = rng.normal(size=(30, 20))
smooth = np.outer(np.sin(np.linspace(0, 3, 30)), np.cos(np.linspace(0, 2, 20))) \
       + 0.5 * np.outer(np.linspace(-1, 1, 30), np.linspace(0, 1, 20) ** 2)
for name, M in (("noise", noise), ("smooth", smooth)):
    s = np.linalg.svd(M, compute_uv=False)
    kept = (s[:2]**2).sum() / (s**2).sum()
    print(f"{name:6s}: energy kept by rank 2 = {kept:.1%}")
noise : energy kept by rank 2 = 27.2%
smooth: energy kept by rank 2 = 100.0%

Low-rank updates in deep learning

LoRA fine-tunes a big weight matrix W∈Rd×dW \in \mathbb{R}^{d \times d} by learning only a low-rank update ΔW=BA\Delta W = BA with B∈Rd×rB \in \mathbb{R}^{d \times r} and A∈Rr×dA \in \mathbb{R}^{r \times d}, r≪dr \ll d. With d=4096d = 4096 and r=8r = 8 that is 2⋅4096⋅8=65 5362 \cdot 4096 \cdot 8 = 65\,536 trainable numbers instead of 40962≈16.84096^2 \approx 16.8 million. Details and code: Low-Rank Approximation: Compression and LoRA and LoRA and Low-Rank Adaptation.

In software

  • sklearn.decomposition.TruncatedSVD and PCA(n_components=k); randomized SVD for very large matrices.
  • Matrix factorization recommenders learn UkU_k and VkV_k directly from the observed entries.
  • The peft library implements LoRA adapters for transformer models.

Exercises

Exercise 1: Approximation error

A matrix has singular values 5,3,1,0.55, 3, 1, 0.5. What are the spectral-norm and Frobenius-norm errors of the best rank-2 approximation?

Answer

Spectral: 1. Frobenius: 1.25≈1.118\sqrt{1.25} \approx 1.118.

Solution

By Eckart-Young the spectral error is σ3=1\sigma_3 = 1 and the Frobenius error is σ32+σ42=1+0.25\sqrt{\sigma_3^2 + \sigma_4^2} = \sqrt{1 + 0.25}.

Exercise 2: Storage

How many numbers does a rank-10 approximation of a 500×300500 \times 300 matrix need?

Answer

8 0108\,010, about 5.3%5.3\% of 150 000150\,000.

Solution

k(m+n+1)=10⋅(500+300+1)=8 010k(m + n + 1) = 10 \cdot (500 + 300 + 1) = 8\,010.

Exercise 3: Energy kept (code)

For singular values (10,6,2,1,1)(10, 6, 2, 1, 1), what fraction of the energy does rank 2 keep?

Answer

136/142≈95.8%136/142 \approx 95.8\%.

Solution

100+36100+36+4+1+1=136142\frac{100 + 36}{100 + 36 + 4 + 1 + 1} = \frac{136}{142}.

python · try it yourself
import numpy as np
s = np.array([10.0, 6, 2, 1, 1])
print(f"{(s[:2]**2).sum() / (s**2).sum():.4f}")
0.9577

Test yourself

The best rank-kk approximation of AA in the spectral norm has error…

  1. σ1\sigma_1
  2. σk\sigma_k
  3. σk+1\sigma_{k+1}
  4. 0

A truncated SVD of rank kk of an m×nm \times n matrix stores about…

  1. mnmn numbers
  2. k(m+n+1)k(m + n + 1) numbers
  3. k2k^2 numbers
  4. m+nm + n numbers

LoRA keeps the fine-tuning update…

  1. diagonal
  2. low rank, as a product BABA
  3. orthogonal
  4. equal to the original weights