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 terms gives the best possible rank- 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 largest singular values:
Storage drops from numbers to .
Eckart-Young. For every matrix of rank at most ,
The error of the best approximation is read directly off the discarded singular values.
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 we only fit noise.
How much energy is kept?
The fraction of the squared Frobenius norm captured by rank is . 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.
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 by learning only a low-rank update with and , . With and that is trainable numbers instead of million. Details and code: Low-Rank Approximation: Compression and LoRA and LoRA and Low-Rank Adaptation.
In software
sklearn.decomposition.TruncatedSVDandPCA(n_components=k); randomized SVD for very large matrices.- Matrix factorization recommenders learn and directly from the observed entries.
- The
peftlibrary implements LoRA adapters for transformer models.
Exercises
Exercise 1: Approximation error
A matrix has singular values . What are the spectral-norm and Frobenius-norm errors of the best rank-2 approximation?
Answer
Spectral: 1. Frobenius: .
Solution
By Eckart-Young the spectral error is and the Frobenius error is .
Exercise 2: Storage
How many numbers does a rank-10 approximation of a matrix need?
Answer
, about of .
Solution
.
Exercise 3: Energy kept (code)
For singular values , what fraction of the energy does rank 2 keep?
Answer
.
Solution
.
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- approximation of in the spectral norm has error…
- 0
A truncated SVD of rank of an matrix stores about…
- numbers
- numbers
- numbers
- numbers
LoRA keeps the fine-tuning update…
- diagonal
- low rank, as a product
- orthogonal
- equal to the original weights