Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Identifying Dominant Patterns via SVD

Recommended resources

Welcome back! In our previous lesson, we explored einsum, a powerful tool for expressing complex tensor manipulations in a concise and readable way. This gave us a new level of control over the mechanics of tensor operations. Now, we'll shift our focus from manipulating tensors to interpreting the information they contain.

Today's lesson delves into a cornerstone of linear algebra and data analysis: the Singular Value Decomposition (SVD). Our goal is to understand how to use SVD to dissect a matrix of neural network activations and identify its "dominant directions." These are the directions in the high-dimensional activation space where the most significant variation occurs. Mastering this technique is a crucial first step toward understanding the features a model has learned and is a foundational method in many mechanistic interpretability studies.

1. Decomposing Transformations: The Intuition Behind SVD

At its heart, any matrix represents a linear transformation—it takes vectors from an input space and maps them to an output space. This transformation can involve rotation, scaling, and shearing in complex ways. SVD provides a way to break down any such transformation into three simpler, fundamental operations:

  1. A rotation (and possibly a reflection).
  2. A scaling along the new, rotated axes.
  3. Another rotation.

The decomposition is written as:

Here, and are orthogonal matrices, which represent the rotations. Since you've studied linear algebra, you'll recall that orthogonal matrices preserve the lengths of vectors and the angles between them. is a diagonal matrix containing the singular values (), which represent the scaling factors.

This decomposition is incredibly powerful because it applies to any rectangular matrix, making it a universal tool. The following video provides a fantastic visual explanation of this process.

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

Watch the section from the "Visual Kernel" channel that visualizes how SVD breaks a complex transformation into this sequence of rotation, scaling, and rotation.

Focus on the animated sequence from the visualization. Pay close attention to how a sphere is first rotated by V^T, then scaled into an ellipsoid by \Sigma, and finally rotated again by U. This visual metaphor is key to building a strong intuition for SVD.


2. Identifying Dominant Directions

The real magic of SVD for data analysis comes from the properties of the and matrices.

  • The singular values in are, by convention, sorted in descending order: .
  • The columns of the matrix , called the right singular vectors, form an orthogonal basis for the input space.
  • The columns of the matrix , called the left singular vectors, form an orthogonal basis for the output space.

The sorted singular values act as an importance ranking. The largest singular value, , corresponds to the most significant component of the transformation. The associated right singular vector, (the first column of ), represents the primary input direction. An input aligned with will be scaled by the largest factor, , and mapped to the output direction .

This is the essence of identifying dominant directions. The first few right singular vectors () correspond to the directions along which the matrix has the most "action" or influence.

This idea is central to a technique you may have encountered in statistics called Principal Component Analysis (PCA). PCA aims to find the directions of maximum variance in a dataset. It turns out that the right singular vectors of a (centered) data matrix are precisely the principal components.

This image illustrates the core idea of PCA. The blue dots are data points. The vector **u** represents the first principal component—the direction in which the variance of the projected data (red dots) is maximized. SVD provides a robust way to find this direction.

The following video provides a clear and practical overview of SVD in the context of a data matrix, explaining how the components reveal a hierarchy of importance.

Singular Value Decomposition (SVD): Mathematical Overview

In this video, "Singular Value Decomposition (SVD): Mathematical Overview" by Steve Brunton, you'll see how SVD is applied to a data matrix X.

Watch the segment from the main explanation. Note how the columns of U are described as "eigenfaces" or "eigenflows"—these are the principal patterns in the data. Crucially, pay attention to the explanation of how the singular values \Sigma are hierarchically ordered, and how this ordering signifies the importance of the corresponding singular vectors.


3. SVD in Mechanistic Interpretability

Now, let's connect this back to our goal of understanding neural networks. When we pass a batch of different inputs through a network, we can collect the activation vectors from a specific layer. If we stack these vectors together, we form an activation matrix. This matrix is our data.

By applying SVD to this activation matrix, we can answer the question: "In which directions in the activation space does the network's state vary the most?"

The dominant singular vectors we find may correspond to semantically meaningful features. For example, in a language model, one direction might correspond to "is a plural noun," while another might relate to "is part of a person's name." Identifying these directions is a key activity in interpretability research.

Six (and a half) intuitions for SVD - LessWrong

The article "Six (and a half) intuitions for SVD" provides a fantastic conceptual overview of why SVD is so useful. It directly addresses the connection to interpretability.

First, read the introduction to understand why SVD is central to interpreting transformers. Then, focus on two key intuitions: Intuition #2, which explains how the largest singular values capture the most "important parts" of the matrix, and Intuition #6A, which formally connects SVD to finding directions of highest variance via PCA.


4. Hands-On: Finding Directions in PyTorch

Let's put this into practice. We'll create a synthetic activation matrix with a known dominant direction, then use torch.linalg.svd to recover it. This exercise mirrors the process of analyzing real activations from a model.

First, we create a 2D dataset. Most of the data will be random noise, but we will add a strong signal along a specific direction, [0.5, -0.866].

import torch
import matplotlib.pyplot as plt




# 1. Create a synthetic activation matrix
num_samples = 500
d_activation = 2




# The direction we want to be dominant
signal_direction = torch.tensor([0.5, -0.866])




# Create a strong signal along this direction
signal = torch.randn(num_samples, 1) * signal_direction




# Create random noise
noise = torch.randn(num_samples, d_activation) * 0.3




# Our "activations" are the sum of the signal and noise
activations = signal + noise

print(f"Activation matrix shape: {activations.shape}")

For PCA, the data must be centered (have a mean of zero).




# 2. Center the data
mean = activations.mean(dim=0)
activations_centered = activations - mean

Now, we perform SVD. torch.linalg.svd returns three tensors: U, S, and Vh. S contains the singular values, and Vh is the transpose of the right singular vectors matrix, .




# 3. Perform SVD
U, S, Vh = torch.linalg.svd(activations_centered)

print(f"Singular values (S): {S}")

You should see that the first singular value is much larger than the second, confirming that one direction is dominant. Let's extract this dominant direction. The right singular vectors are the rows of Vh (or columns of V). So, the first principal component is the first row of Vh.




# 4. Extract the dominant direction (first principal component)
pc1 = Vh[0, :]

print(f"First principal component: {pc1}")
print(f"Original signal direction: {signal_direction}")




# 5. Verify the result using cosine similarity
# We can use abs() because the direction could be flipped (v1 or -v1)
cosine_sim = torch.abs(torch.dot(pc1, signal_direction))
print(f"Cosine similarity with original direction: {cosine_sim:.4f}")

The cosine similarity should be very close to 1.0, showing that SVD successfully identified the hidden direction of maximum variance in our data. The principal component pc1 might be [-0.5, 0.866] instead of [0.5, -0.866]. This is perfectly fine, as a direction and its negative represent the same axis of variation.

This process—collecting activations, centering, and applying SVD to find the principal components—is a fundamental workflow you will use to probe the internal representations of neural networks.


Conclusion

In this lesson, we moved from manipulating tensors to interpreting them. We've seen that Singular Value Decomposition is more than just a mathematical curiosity; it's a powerful tool for finding structure in data.

Here are the key takeaways:

  • SVD decomposes any matrix into , representing a sequence of rotation, scaling, and another rotation.
  • The singular values () in are ordered by magnitude and indicate the "importance" of different components of the transformation.
  • The right singular vectors (columns of , or rows of ) represent the principal directions of variation in the data. The first vector, , corresponding to the largest singular value , is the most dominant direction.
  • In interpretability, we apply SVD to activation matrices to discover the most salient feature directions in a model's representation space.

So far, we've discussed analyzing an activation matrix that we assumed we already have. But how do we get these activations from a model as it's running? In our next lesson, we will learn how to use PyTorch hooks, a mechanism that lets us tap into a model's forward pass and record (or even modify) its internal states.