AI State of the Art
Math/Linear Algebra/13 min
Section 317

Singular Value Decomposition (SVD)

Listen as a podcast β†’

Introduction: the universal factorization

The eigendecomposition is powerful, but it only works on square matrices. In ML, matrices are rarely square: a dataset of 1000Γ—501000 \times 50, a weight matrix of 4096Γ—7684096 \times 768, an embedding matrix of 50000Γ—30050000 \times 300.

The SVD (Singular Value Decomposition) is the generalization that works for any matrix, regardless of its shape. It's probably the most important factorization in all of applied linear algebra.

The intuition: rotation, stretching, rotation

Any linear transformation (matrix) can be decomposed into three simple steps:

  1. Rotation (or reflection) in the input space
  2. Stretching (or compression) along the axes
  3. Rotation (or reflection) in the output space

Imagine you take a circle and apply a matrix to it. The circle becomes an ellipse. The SVD tells you exactly: which rotations, and which stretches. It's a complete X-ray of the transformation.

Four-panel diagram showing the singular value decomposition of a matrix A: the unit circle is first rotated by V transposed, then stretched into an ellipse by Sigma, then rotated again by U; the direct path through A leads to the same result.
The circle becoming an ellipse, step by step. The top path applies the matrix A directly. The bottom path does the same thing in three separate moves: V transposed rotates the circle, Sigma stretches it along the axes into an ellipse, U rotates that ellipse. Both paths end on the same drawing, that is the whole of the SVD. The singular values are the half-axis lengths of the final ellipse: they say how much the matrix stretches along each preferred direction.Source : Hadrien Jean, Essential Math for Data Science

The formula

For any matrix A∈RmΓ—n\mathbf{A} \in \mathbb{R}^{m \times n}, the SVD is:

A=UΞ£VT\mathbf{A} = \mathbf{U} \mathbf{\Sigma} \mathbf{V}^T

Where:

  • U∈RmΓ—m\mathbf{U} \in \mathbb{R}^{m \times m} is an orthogonal matrix (its columns are the left singular vectors)
  • Σ∈RmΓ—n\mathbf{\Sigma} \in \mathbb{R}^{m \times n} is a diagonal matrix containing the singular values Οƒ1β‰₯Οƒ2β‰₯…β‰₯Οƒr>0\sigma_1 \geq \sigma_2 \geq \ldots \geq \sigma_r > 0 (ordered by decreasing value)
  • VT∈RnΓ—n\mathbf{V}^T \in \mathbb{R}^{n \times n} is an orthogonal matrix (its rows are the right singular vectors)

The number rr is the rank of the matrix, the number of nonzero singular values.

What each component represents

  • VT\mathbf{V}^T: the first rotation. It identifies the important directions in the input space.
  • Ξ£\mathbf{\Sigma}: the stretching. Each singular value Οƒi\sigma_i tells how much the transformation stretches in the ii-th direction.
  • U\mathbf{U}: the second rotation. It places the result in the output space.

Concrete example

Take a simple matrix:

A=(3001)\mathbf{A} = \begin{pmatrix} 3 & 0 \\ 0 & 1 \end{pmatrix}

This matrix stretches the horizontal axis by 3 and leaves the vertical axis unchanged. Its SVD is trivial:

  • U=I\mathbf{U} = \mathbf{I}, V=I\mathbf{V} = \mathbf{I} (no rotation needed)
  • Οƒ1=3\sigma_1 = 3, Οƒ2=1\sigma_2 = 1

For a more complex matrix, the rotations U\mathbf{U} and V\mathbf{V} are nontrivial, but the idea remains the same: we decompose the transformation into its elementary components.

Connection with eigenvalues

SVD and eigendecomposition are closely related:

  • The singular values of A\mathbf{A} are the square roots of the eigenvalues of ATA\mathbf{A}^T\mathbf{A}: Οƒi=Ξ»i(ATA)\sigma_i = \sqrt{\lambda_i(\mathbf{A}^T\mathbf{A})}

  • The right singular vectors (columns of V\mathbf{V}) are the eigenvectors of ATA\mathbf{A}^T\mathbf{A}

  • The left singular vectors (columns of U\mathbf{U}) are the eigenvectors of AAT\mathbf{A}\mathbf{A}^T

For a symmetric positive semi-definite matrix, SVD coincides with the eigendecomposition.

The advantage of SVD: singular values are always real and non-negative, even when the eigenvalues of A\mathbf{A} are complex or negative.

Low-rank approximation

This is where SVD becomes truly powerful for ML.

The Eckart-Young theorem

The rank-kk approximation of A\mathbf{A} is obtained by keeping only the kk largest singular values:

Ak=βˆ‘i=1kΟƒiuiviT\mathbf{A}_k = \sum_{i=1}^{k} \sigma_i \mathbf{u}_i \mathbf{v}_i^T

This theorem says: among all rank-kk matrices, Ak\mathbf{A}_k is the best approximation of A\mathbf{A} (in terms of the Frobenius norm or spectral norm).

The approximation error is:

βˆ₯Aβˆ’Akβˆ₯F=Οƒk+12+Οƒk+22+…+Οƒr2\|\mathbf{A} - \mathbf{A}_k\|_F = \sqrt{\sigma_{k+1}^2 + \sigma_{k+2}^2 + \ldots + \sigma_r^2}

If the singular values decay quickly, you can approximate the matrix with very few components. A 1000Γ—10001000 \times 1000 matrix of rank 5 can be reconstructed exactly with only 5Γ—(1000+1000+5)5 \times (1000 + 1000 + 5) numbers instead of 1 000 0001\,000\,000.

Applications in machine learning

LoRA: efficient adaptation of LLMs

LoRA (Low-Rank Adaptation) is directly inspired by SVD. The idea: when you fine-tune a large language model, the weight changes Ξ”W\Delta\mathbf{W} are often low-rank, they only affect a few directions in parameter space.

Instead of modifying the full matrix W∈RdΓ—d\mathbf{W} \in \mathbb{R}^{d \times d} (millions of parameters), you learn:

Ξ”W=BA\Delta\mathbf{W} = \mathbf{B}\mathbf{A}

Where B∈RdΓ—r\mathbf{B} \in \mathbb{R}^{d \times r} and A∈RrΓ—d\mathbf{A} \in \mathbb{R}^{r \times d} with rβ‰ͺdr \ll d (typically r=8r = 8 or r=16r = 16).

It's a low-rank approximation. Instead of storing d2d^2 parameters, you only store 2dr2dr. For d=4096d = 4096 and r=16r = 16, you go from 16 million to 131,000 parameters. A compression factor of 120.

Two diagrams side by side. On the left, regular finetuning: inputs feed a grey square of pretrained weights W and a blue square of the same size for the update delta W, with both outputs added together. On the right, LoRA: the grey square W is kept as is, but the blue square is replaced by two stacked trapezoids A and B narrowing down to an inner dimension labelled r.
The difference between regular finetuning and LoRA. On the left, the update delta W is a block as large as the original weight matrix. On the right, that block is replaced by two trapezoids that pinch in the middle: information has to squeeze through a bottleneck of width r, which is tiny. That bottleneck is what enforces the low rank, and it is where all the parameter savings come from.Source : Sebastian Raschka, Practical Tips for Finetuning LLMs Using LoRA

Dimensionality reduction

Like PCA (which is a special case of SVD applied to centered data), SVD allows reducing the dimensionality of data by keeping only the most important components.

Truncated SVD is often preferred over classical PCA because it's more computationally efficient for large sparse matrices.

Image compression

A grayscale image is an mΓ—nm \times n matrix. Its SVD gives rr components. By keeping only the first kk (kβ‰ͺrk \ll r), you get a compressed version that preserves the main structures (shapes, contrasts) while eliminating noise and fine details.

The compression ratio is approximately mnk(m+n+1)\frac{mn}{k(m + n + 1)}.

Grid of greyscale images of a photographer with his tripod. Top left is the original image. Below it, five successive reconstructions keeping 4, 8, 16, 32 and then 64 singular values, each sharper than the last. To the right of each reconstruction, a blue and red error map that fades progressively, with the compression rate and the percentage of information retained.
The same photo reconstructed while keeping more and more singular values. With only 4 you can barely make out a silhouette; with 16 the scene is recognisable; with 32 it is almost perfect even though half the data has been thrown away. The right-hand column shows what is missing at each step: the error sits on edges and fine detail, and fades as components are added. This is the Eckart-Young theorem in pictures, the first singular values carry the large structures, the last ones carry the grain.Source : Dustin Stansbury, The Clever Machine, SVD and Data Compression Using Low-rank Matrix Approximation

Recommendation systems

Netflix, Spotify, and recommendation engines use matrix factorization, a cousin of SVD. The user-movie matrix (with many empty entries) is approximated by a product of two low-rank matrices:

Rβ‰ˆUVT\mathbf{R} \approx \mathbf{U}\mathbf{V}^T

The rows of U\mathbf{U} are the "latent profiles" of users, the columns of VT\mathbf{V}^T are the "latent profiles" of movies. The product predicts missing ratings.

Embeddings and Latent Semantic Analysis

LSA (Latent Semantic Analysis) applies SVD to the term-document matrix to discover "latent concepts." Words that appear in similar contexts end up close in the reduced space. It's an ancestor of modern embeddings like Word2Vec.

SVD in practice

In Python with NumPy:

import numpy as np

# Full SVD
U, S, Vt = np.linalg.svd(A)

# Rank-k approximation
A_k = U[:, :k] @ np.diag(S[:k]) @ Vt[:k, :]

For large sparse matrices, you use the truncated SVD from scipy.sparse.linalg.svds or sklearn.decomposition.TruncatedSVD, which only compute the first kk components, much faster.

Why this matters for AI

SVD is a cross-cutting tool in ML:

  • LoRA makes fine-tuning LLMs accessible to everyone through low-rank approximations
  • Model compression reduces size and inference cost
  • Matrix factorization powers recommendation systems
  • Dimensionality reduction allows visualizing and denoising data

When you see a concept of "low-rank" or "factorization" in ML, SVD is never far away. It's the tool that says: this gigantic matrix can be well approximated by something much smaller. And in ML, smaller often means faster, cheaper, and sometimes even better.

Where this is used. The low-rank approximation described here is the entire idea behind LoRA.