Runoob Tutorial ArchiveEnglish

Singular Value Decomposition (SVD) – The decomposition of arbitrary matrices

Singular Value Decomposition (SVD) -- The decomposition of arbitrary matrices

Eigen decomposition is only applicable to square matrices, but SVD has no such limitation—any matrix can be decomposed by SVD.

It is the mathematical engine behind technologies such as recommendation systems, image compression, and LoRA fine-tuning.

SVD: Decomposing a matrix into three parts

A

(m×n)

=

U

(m×m)

Rotation

×

Σ

(m×n)

Scaling

×

V

T

(n×n)

Rotation

Geometric intuition: any linear transformation = rotation + scaling along coordinate axes + another rotation.

The diagonal elements of $\Sigma$ are called singular values, arranged from largest to smallest, representing the "energy" of the matrix in various orthogonal directions.

Truncated SVD: approximate using the first k singular values

[ A \approx U_k \Sigma_k V_k^T ]

The smaller k is, the higher the compression ratio, and the coarser the approximation. This is the core idea of dimensionality reduction and compression.


Real-life examples

**Compressing an image

For a 1000×1000 table (1 million numbers), after SVD decomposition, only the first 10 singular values are used for reconstruction.

Storing only about 10×1000×2 = 20000 numbers is enough to get a good approximation.

This is the power of SVD compression—discarding unimportant "energy" and retaining the core structure.


Mathematical definition

[ A = U \Sigma V^T ]

The columns of U are the left singular vectors (orthogonal), the rows of V are the right singular vectors (orthogonal), and the diagonal elements of \Sigma, $\sigma_1 \geq \sigma_2 \geq ... \geq \sigma_r > 0$, are the singular values.


Python Hands-on Practice

Example

import

numpy

as

np

A

=

np.

array

(

[

[

1

,

0

,

0

,

0

,

2

]

,

[

0

,

0

,

3

,

0

,

0

]

,

[

0

,

0

,

0

,

0

,

0

]

,

[

0

,

4

,

0

,

0

,

0

]

]

)

U

,

S

,

VT

=

np.

linalg

.

svd

(

A

)

print

(

"Singular values:"

,

np.

round

(

S

,

2

)

)

 

[4. 3. 2.24 0.]

print

(

"Number of non-zero singular values = rank ="

,

np.

sum

(

S

1e-10

)

)

Image compression demo: rank-1 image

img

=

np.

outer

(

np.

array

(

[

1

,

2

,

3

,

2

,

1

]

)

,

np.

array

(

[

2

,

4

,

6

,

4

,

2

]

)

)

U_i

,

S_i

,

VT_i

=

np.

linalg

.

svd

(

img

)

print

(

"Singular values of the image matrix:"

,

np.

round

(

S_i

,

2

)

)

Only 1 singular value is needed for perfect reconstruction (because rank=1)

k

=

1

approx

=

U_i

[

:

,

:k

]

@

np.

diag

(

S_i

[

:k

]

)

@

VT_i

[

:k

,

:

]

print

(

"k=1 reconstruction error:"

,

np.

linalg

.

norm

(

img - approx

)

)

Original prompt (Chinese, left untranslated)



奇异值: [4. 3. 2.24 0.]

非零奇异值个数 = 秩 = 3

图像矩阵的奇异值: [78.26 0. 0. 0. 0.]

k=1重建误差: 7.11e-14 (几乎完美)


Application scenarios in AI

Mathematical foundation of LoRA fine-tuning

LoRA assumption: (\Delta W) is low-rank—this is equivalent to assuming that only the first r singular values in the SVD of (\Delta W) are significant, and the rest can be ignored. The smaller r is, the fewer trainable parameters there are, but the lower the approximation accuracy. In practice, r=8 or 16 is often the best balance between performance and efficiency, which stems from empirical observations of the decay rate of singular values of the weight matrix.

Recommender systems: Matrix factorization collaborative filtering

Perform truncated SVD on the user-item rating matrix R (m×n): ( R \approx U_k \Sigma_k V_k^T ). Each row of U_k is the k-dimensional latent vector of a user, and each row of V_k is the k-dimensional latent vector of an item. The predicted rating of user u for item i = the dot product of the latent vector of u and the latent vector of i.

Model compression and distillation

After performing SVD on the weight matrix W of a fully connected layer, use truncated SVD approximation: ( W \approx U_k \Sigma_k V_k^T ). The number of parameters in the original m×n matrix is mn, while after decomposition it is k(m+n+1). When k << min(m,n), the number of parameters is significantly reduced. This is very useful when deploying large models on mobile devices.

SVD implementation of PCA

sklearn's PCA uses SVD instead of eigendecomposition—it directly performs SVD on the centered data matrix, and the columns of the right singular vector V are the principal component directions. SVD is more numerically stable and does not require explicitly calculating the covariance matrix.