Skip to main content

SVD and Truncated SVD Explained: Theory, Python Examples, and Applications in Machine Learning & Deep Learning

Singular Value Decomposition (SVD) is a matrix factorization method widely used in mathematics, engineering, and economics. Since it's a crucial concept applied in accelerating matrix computations and data compression, it's worth studying at least once.

SVD (Singular Value Decomposition) Theory

Singular Value Decomposition (SVD) is a matrix factorization technique applicable to any real or complex matrix. Any matrix A (m×n) can be decomposed as follows:

$A = U * \Sigma * V^T$

  • U: Orthogonal matrix composed of left singular vectors $(m \times m)$
  • Σ: Diagonal matrix $(m \times n)$ with singular values on the diagonal
  • $V^T$: Transposed matrix of right singular vectors $(n \times n)$

The singular values represent the energy or information content of matrix A, enabling tasks like dimensionality reduction or noise filtering.

Truncated SVD

Truncated SVD approximates the original matrix using only the top k singular values and corresponding singular vectors:

$A \approx U_k * \Sigma_k * V_k^T$

This technique is useful for reducing dimensionality while retaining most of the information, especially in sparse matrices, text data, and image processing.


Implementing SVD and Truncated SVD in Python

import numpy as np

def compute_svd(A):
    AT_A = np.dot(A.T, A)
    eigvals, V = np.linalg.eigh(AT_A)
    idx = np.argsort(eigvals)[::-1]
    eigvals = eigvals[idx]
    V = V[:, idx]
    singular_values = np.sqrt(eigvals)
    Σ = np.diag(singular_values)
    U = np.dot(A, V) / singular_values
    return U, Σ, V.T

A = np.array([[3, 2], [2, 3]])
U, Σ, VT = compute_svd(A)
print("U:\n", U)
print("Σ:\n", Σ)
print("V^T:\n", VT)

Output:

U:
 [[ 0.70710678 -0.70710678]
 [ 0.70710678  0.70710678]]
Σ:
 [[5. 0.]
 [0. 1.]]
V^T:
 [[ 0.70710678  0.70710678]
 [-0.70710678  0.70710678]]

Truncated SVD with Scikit-learn

from sklearn.decomposition import TruncatedSVD
import numpy as np

X = np.array([[3, 2, 2], [2, 3, -2]])
svd = TruncatedSVD(n_components=2)
X_reduced = svd.fit_transform(X)
print("Reduced X:\n", X_reduced)
print("Components:\n", svd.components_)

Output:

Reduced X:
 [[ 3.53553391  2.12132034]
 [ 3.53553391 -2.12132034]]
Components:
 [[ 7.07106781e-01  7.07106781e-01  4.33680869e-18]
 [ 2.35702260e-01 -2.35702260e-01  9.42809042e-01]]

Truncated SVD is particularly suitable for sparse data (e.g., TF-IDF matrices, user-item matrices), and unlike PCA, it does not require mean-centering the data.


Applications of Truncated SVD in Deep Learning / Machine Learning

1. Dimensionality Reduction

Used to compress high-dimensional data (e.g., text, images) into lower dimensions, reducing computation and improving generalization.

2. Natural Language Processing (NLP)

  • LSA (Latent Semantic Analysis): Applies Truncated SVD to a document-word matrix to extract semantic relationships between words.
  • Word Embedding Preprocessing: Used to reduce the dimensionality of vectors like FastText and GloVe for memory optimization.

3. Recommendation Systems

Truncated SVD is applied to sparse user-item matrices to learn latent factor models, used in systems like Netflix and YouTube.

4. Model Compression in Deep Learning

LoRA (Low-Rank Adaptation) is a technique for efficient fine-tuning of large language models (e.g., GPT, BERT). It approximates weight matrices using a low-rank form:

W ≈ W0 + A * B  # A and B are low-rank matrices

A and B are learnable, allowing quick adaptation without retraining the entire model. LoRA is a representative method inspired by the principles of SVD.

5. Vision Applications

Used to approximate convolutional weights in neural networks with low-rank matrices to reduce memory and computation costs.


References

Comments

Popular

Understanding SentencePiece: A Language-Independent Tokenizer for AI Engineers

In the realm of Natural Language Processing (NLP), tokenization plays a pivotal role in preparing text data for machine learning models. Traditional tokenization methods often rely on language-specific rules and pre-tokenized inputs, which can be limiting when dealing with diverse languages and scripts. Enter SentencePiece—a language-independent tokenizer and detokenizer designed to address these challenges and streamline the preprocessing pipeline for neural text processing systems. What is SentencePiece? SentencePiece is an open-source tokenizer and detokenizer developed by Google, tailored for neural-based text processing tasks such as Neural Machine Translation (NMT). Unlike conventional tokenizers that depend on whitespace and language-specific rules, SentencePiece treats the input text as a raw byte sequence, enabling it to process languages without explicit word boundaries, such as Japanese, Chinese, and Korean. This approach allows SentencePiece to train subword models di...

Mastering the Byte Pair Encoding (BPE) Tokenizer for NLP and LLMs

Byte Pair Encoding (BPE) is one of the most important and widely adopted subword tokenization algorithms in modern Natural Language Processing (NLP), especially in training Large Language Models (LLMs) like GPT. This guide provides a deep technical dive into how BPE works, compares it with other tokenizers like WordPiece and SentencePiece, and explains its practical implementation with Python code. This article is optimized for AI engineers building real-world models and systems. 1. What is Byte Pair Encoding? BPE was originally introduced as a data compression algorithm by Gage in 1994. It replaces the most frequent pair of bytes in a sequence with a single, unused byte. In 2015, Sennrich et al. adapted BPE for NLP to address the out-of-vocabulary (OOV) problem in neural machine translation. Instead of working with full words, BPE decomposes them into subword units that can be recombined to represent rare or unseen words. 2. Why Tokenization Matters in LLMs Tokenization is th...

Using Gemini API in LangChain: Step-by-Step Tutorial

What is LangChain and Why Use It? LangChain  is an open-source framework that simplifies the use of  Large Language Models (LLMs)  like OpenAI, Gemini (Google), and others by adding structure, tools, and memory to help build real-world applications such as chatbots, assistants, agents, or AI-enhanced software. Why Use LangChain for LLM Projects? Chainable Components : Easily build pipelines combining prompts, LLMs, tools, and memory. Multi-Model Support : Work with Gemini, OpenAI, Anthropic, Hugging Face, etc. Built-in Templates : Manage prompts more effectively. Supports Multi-Turn Chat : Manage complex interactions with memory and roles. Tool and API Integration : Let the model interact with external APIs or functions. Let's Walk Through the Code: Gemini + LangChain I will break the code into  4 main parts , each showcasing different features of LangChain and Gemini API. Part 1: Basic Gemini API Call Using LangChain import os from dotenv import load_dotenv load_dot...