Sketching Transformed Matrices with Applications to Natural Language Processing

Yingyu Liang, Zhao Song, Mengdi Wang, Lin F. Yang, Xin Yang

Abstract

In many modern data science applications, we encounter matrices so large they cannot be stored in memory. These matrices are often presented as data streams or stored on disk, requiring space-efficient processing. A significant challenge arises when we need to compute a matrix decomposition not of the raw matrix $A$, but of an entry-wise transformed version $f(A) := (f(a_{i,j}))$. Such transformations, like the Pointwise Mutual Information (PMI) used in word embeddings, break the linearity required by standard sketching tools. This paper introduces a space-efficient sketching algorithm for computing the product of a transformed matrix with a smaller matrix. We demonstrate its utility by developing a low-rank approximation method that achieves high accuracy with minimal memory, validated through experiments on synthetic data and a multi-billion token Wikipedia corpus.

Introduction

In Natural Language Processing (NLP), algorithms often build word embeddings by analyzing a "co-occurrence matrix" that tracks the frequency of word pairs across massive datasets. Because these matrices are too large to fit in memory, they are typically processed as a stream of updates. However, standard linear algebraic tools like random projections fail when the objective is to factorize a transformed version of the data. For instance, calculating metrics like PMI requires applying a non-linear transformation—such as $\log(|x| + 1)$—to every entry.

Historically, researchers faced a binary choice: store the entire massive matrix in memory (often impossible) or design application-specific workarounds. This paper proposes a third way: a foundational sketching primitive that allows for complex linear algebra on a transformed matrix without ever storing the matrix itself.

Methodology

The Sketching Primitive

The core contribution is an algorithm that computes the matrix product $f(A)B$, where $A$ is a massive streaming matrix, $f$ is a non-linear transformation, and $B$ is a smaller, known matrix. The authors build this from the ground up:

  1. Vector Sketching: The problem is first addressed at the vector level by estimating the inner product of a known vector and a streaming, transformed vector. For logarithmic transformations, the authors design an algorithm acting as an $\ell_0$-sampler. Using a "K-set" data structure and hash functions with geometrically decreasing probabilities, the algorithm maintains a uniform sample of the non-zero elements of the vector as it is updated.
  2. Matrix Sketching: Since a matrix product is a series of vector inner products, the authors run these vector sketches in parallel to compute $f(A)B$.

Low-Rank Approximation

To find the top singular vectors of the massive matrix, the authors integrate their matrix sketch into a "generalized leverage score sampling" pipeline. The process involves:

  • Using CountSketch to approximate the importance (leverage scores) of the columns.
  • Sampling a subset of columns based on these scores.
  • Applying adaptive sampling to refine the selection.
  • Computing the singular vectors on this significantly reduced representation.

Experimental Evaluation

Datasets

The methodology was tested on both synthetic and real-world data:

  • Synthetic Data (LogData and SqrtData): Matrices up to 50,000 x 50,000 were generated with Gaussian entries and transformed to test the limits of the sketching accuracy.
  • Real Data (Wikipedia Corpus): The authors processed the entire English Wikipedia dump (approximately 3 billion tokens) to build streaming word co-occurrence matrices for the top 10,000 to 50,000 most frequent words, mimicking a production NLP pipeline.

Key Results

The results demonstrate that non-linearities do not have to be a barrier to efficient sketching. By dynamically sampling only from the support (non-zero elements), the algorithm maintains low variance and high accuracy while using minimal memory.

  • Memory Efficiency: On a 50,000 x 50,000 matrix, using memory equivalent to only 5% of the full matrix size yielded an error within 5% of the optimal singular vectors.
  • Performance vs. Baselines: The method significantly outperformed uniform column sampling. The "error ratio" (the algorithm's error divided by the optimal possible error) remained near 1.0, while the uniform sampling baseline hovered around 4.0.
  • Theoretical Soundness: The authors proved that their algorithm requires space proportional to $O(nk/\epsilon^2)$, a massive improvement over the $O(n^2)$ required to store the full matrix.

Conclusion and Future Work

By framing the solution as a way to compute $f(A)B$, the authors created a versatile, "black-box" tool. Any downstream algorithm that interacts with a massive matrix via matrix multiplication can now be made space-efficient.

While the sketching primitive operates in a single pass, the full low-rank approximation algorithm currently requires multiple passes to compute leverage scores and perform adaptive sampling. A primary direction for future research is the development of a strictly one-pass algorithm for the low-rank approximation of transformed matrices.

Summary updated

Abstract

Suppose we are given a large matrix $A=(a_{i,j})$ that cannot be stored in memory but is in a disk or is presented in a data stream. However, we need to compute a matrix decomposition of the entry-wisely transformed matrix, $f(A):=(f(a_{i,j}))$ for some function $f$. Is it possible to do it in a space efficient way? Many machine learning applications indeed need to deal with such large transformed matrices, for example word embedding method in NLP needs to work with the pointwise mutual information (PMI) matrix, while the entrywise transformation makes it difficult to apply known linear algebraic tools. Existing approaches for this problem either need to store the whole matrix and perform the entry-wise transformation afterwards, which is space consuming or infeasible, or need to redesign the learning method, which is application specific and requires substantial remodeling. In this paper, we first propose a space-efficient sketching algorithm for computing the product of a given small matrix with the transformed matrix. It works for a general family of transformations with provable small error bounds and thus can be used as a primitive in downstream learning tasks. We then apply this primitive to a concrete application: low-rank approximation. We show that our approach obtains small error and is efficient in both space and time. We complement our theoretical results with experiments on synthetic and real data.
BibTeX
@inproceedings{liang2020sketching,
  author = {Yingyu Liang and Zhao Song and Mengdi Wang and Lin F. Yang and Xin Yang},
  title = {{Sketching Transformed Matrices with Applications to Natural Language Processing}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2020},
}
Figure