Skip to main content
Create your own

Matrix Decomposition: SVD and Eigendecomposition

Hello! Welcome to the third lesson in our module on the mathematical foundations of AI.

In our last session, we discovered eigenvectors and eigenvalues—the special vectors that a linear transformation only scales, without changing their direction. We ended with a fascinating idea: if a matrix has enough eigenvectors to form a basis (an "eigenbasis"), we can change our coordinate system to make the transformation act like a simple diagonal matrix.

Today, we will formalize this concept into a powerful tool called eigendecomposition. We will then go a step further and explore an even more general and widely used technique: Singular Value Decomposition (SVD). SVD can decompose any matrix, not just the special square ones that are diagonalizable. This lesson will show you how to apply these decomposition techniques, which are the mathematical engines behind dimensionality reduction, data compression, and even cutting-edge methods for efficiently training large AI models.

Eigendecomposition: Unpacking a Matrix's DNA

Let's pick up right where we left off. When we have a basis composed entirely of a matrix's eigenvectors (an eigenbasis), the transformation becomes incredibly simple in that new coordinate system.

Eigenvectors and eigenvalues | Chapter 14, Essence of linear algebra

To refresh this concept, let's revisit the final segment of the 3Blue1Brown video from our last lesson. It perfectly illustrates how an eigenbasis simplifies a transformation.

Watch from 12:44 to 16:31. Focus on how changing the basis to the eigenvectors turns the transformation matrix into a diagonal matrix, where the diagonal entries are the eigenvalues.

This process of "diagonalization" is the core of eigendecomposition. For a square matrix that has a full set of linearly independent eigenvectors, we can decompose it into a product of three specific matrices.

Eigendecompositions

The following text from the 'Dive into Deep Learning' book formalizes this process. It will walk you through the construction of the eigendecomposition formula.

Read the section titled 'Decomposing Matrices'. It explains how to construct the eigenvector matrix W and eigenvalue matrix Σ to arrive at the equation A = WΣW⁻¹.

Let's summarize the derivation. If we have a set of eigenvectors with corresponding eigenvalues , we can state this for all vectors simultaneously in matrix form:

  1. Let be the matrix whose columns are the eigenvectors: .
  2. Let (Sigma) be a diagonal matrix with the eigenvalues on the diagonal:
  3. The relationship for each eigenvector can then be written compactly for all eigenvectors at once as:
  4. If the eigenvectors are linearly independent, then is invertible. We can right-multiply by to get the eigendecomposition of :

This equation tells us that the transformation can be thought of as a three-step process:

  1. : Change from the standard basis to the eigenbasis.
  2. : Scale along the eigenbasis axes (a simple operation).
  3. : Change back from the eigenbasis to the standard basis.

Why Eigendecomposition is Useful

This decomposition is far more than a mathematical curiosity. It drastically simplifies complex matrix operations.

Eigendecompositions

Let's look at the practical benefits of this decomposition. The same resource illustrates how it simplifies matrix powers and inversion.

Read the section 'Operations on Eigendecompositions'. Pay attention to how calculating Aⁿ simplifies to WΣⁿW⁻¹.

For instance, computing would normally require 99 matrix multiplications. With eigendecomposition, it becomes . Since is diagonal, is just each diagonal element raised to the power of 100—a computationally trivial task.

The Special Case: Symmetric Matrices

In machine learning, we frequently deal with symmetric matrices (where ), such as covariance matrices. These matrices have a wonderful property: their eigenvectors can always be chosen to be orthonormal (mutually perpendicular and of unit length).

This means the eigenvector matrix becomes an orthogonal matrix, for which . This simplifies the decomposition to:

This is not only cleaner but also more numerically stable to compute, as transposing a matrix is much simpler than inverting it.

Test your understanding!

Suppose you have a symmetric matrix with the eigendecomposition . You need to calculate . How would you do it using the decomposed components, and what do the resulting matrices represent?

Show answer

Using the decomposition, .

Because is an orthogonal matrix, (the identity matrix). The middle terms cancel out:

The resulting matrices are:

  • : The same orthogonal matrix of eigenvectors. It defines the principal axes of the transformation.
  • : A diagonal matrix where each original eigenvalue is now . It represents a much more extreme scaling along the same axes.
  • : The same transpose of the eigenvector matrix.

Essentially, applying the transformation three times is equivalent to performing a much stronger scaling along the same fundamental directions (eigenvectors).


Singular Value Decomposition (SVD): The Master Decomposition

Eigendecomposition is powerful, but it has two major limitations:

  1. It only applies to square matrices.
  2. Not all square matrices are diagonalizable (they might not have enough linearly independent eigenvectors).

In data science, we often work with rectangular matrices (e.g., num_samples x num_features). This is where Singular Value Decomposition (SVD) comes in. SVD is a general-purpose decomposition that works for any matrix.

The core idea of SVD is that any linear transformation can be broken down into three fundamental geometric operations:

  1. A rotation in the input space.
  2. A scaling along the new axes (and possibly a change in dimension).
  3. A rotation in the output space.

SVD Visualized, Singular Value Decomposition explained | SEE Matrix , Chapter 3 #SoME2

This video from Visual Kernel provides an outstanding visual intuition for what SVD is and how it works for rectangular matrices.

First, watch from 01:16 to 05:14 to understand how rectangular matrices can be visualized as transforming vectors between different dimensions. Then, watch the core visualization from 10:00 to 13:04, which shows the sequence of rotation-scaling-rotation.

The Mechanics of SVD

So how do we find these rotation and scaling matrices? The method is incredibly elegant. Even though our matrix might not be square or symmetric, we can construct two related matrices that are: and .

Since these are symmetric, we know they have nice properties: real eigenvalues and a full set of orthogonal eigenvectors. SVD leverages these eigenvectors to decompose the original matrix .

Machine Learning — Singular Value Decomposition (SVD ...

The article 'Machine Learning — Singular Value Decomposition (SVD)' explains how these symmetric matrices are used to define the components of SVD.

Read the sections 'Singular vectors & singular values' and 'SVD'. Focus on how the eigenvectors of AᵀA and AAᵀ become the 'singular vectors' that form the U and V matrices in the final decomposition.

The SVD of an matrix is given by:

Singular Value Decomposition (SVD) Visual Breakdown
Visual representation of SVD. The matrix A is decomposed into U (left singular vectors), Σ (singular values), and Vᵀ (transpose of right singular vectors). The columns of V are eigenvectors of AᵀA, and the columns of U are eigenvectors of AAᵀ.

The components are:

  • : An orthogonal matrix. Its columns are the eigenvectors of , called the right singular vectors of .
  • : An orthogonal matrix. Its columns are the eigenvectors of , called the left singular vectors of .
  • : An rectangular diagonal matrix. Its diagonal entries are the singular values of . They are the square roots of the non-zero eigenvalues of both and .

Applications in AI: Low-Rank Approximation and LoRA

The true power of SVD in machine learning comes from low-rank approximation. The singular values in are ordered by magnitude, indicating how much "energy" or "importance" each dimension holds. By keeping only the top singular values and their corresponding singular vectors, we can construct a matrix that is the best possible rank- approximation of .

This is the principle behind PCA for dimensionality reduction, as well as many data compression and noise reduction techniques.

Perhaps most excitingly, this exact principle is used in a state-of-the-art technique for fine-tuning large AI models called LoRA (Low-Rank Adaptation). When fine-tuning a massive pre-trained model (like a language or image model), we don't want to retrain all billions of parameters. Instead, LoRA assumes that the change in the model's weights () can be represented by a low-rank matrix. It then decomposes this change into two much smaller matrices, and , such that . We only need to train these small matrices, which is vastly more efficient.

Low-Rank Adaptation (LoRA) Overview with Matrix Decomposition
This diagram illustrates Low-Rank Adaptation (LoRA). The large matrix of weight adjustments (\(\Delta W\)) is approximated by the product of two smaller, low-rank matrices (\(W_b W_a\)). This is a direct application of the low-rank approximation concept from SVD, enabling efficient fine-tuning of enormous models.

Your experience in software engineering will tell you that finding ways to do the same job with fewer resources is a huge win. LoRA is a perfect example of applying foundational linear algebra to achieve massive efficiency gains in modern AI.

Conclusion

In this lesson, we've explored two of the most important tools in the linear algebra toolbox for machine learning.

Key Takeaways:

  • Eigendecomposition () breaks down a diagonalizable square matrix into its fundamental directions (eigenvectors) and scaling factors (eigenvalues). For symmetric matrices, this simplifies to .
  • Singular Value Decomposition (SVD) () is a more general technique that works for any matrix. It decomposes a transformation into a sequence of rotation, scaling, and another rotation.
  • The singular values in are sorted by importance, allowing for low-rank approximation, which is the basis for PCA, data compression, and modern AI techniques like LoRA.

Preview of the next lesson:
We have now covered the essential linear algebra concepts that form the static "scaffolding" of neural networks. However, the magic of AI is in learning, which is a dynamic process of optimization. To understand how models learn, we need to shift our focus to calculus. In the next lesson, we will begin our study of multivariate calculus by learning to compute partial derivatives and gradients, the tools that tell us how to adjust our model's parameters to improve its performance.

Can't find a good explanation? Sign up and we'll make it for you

Sign up