Learning Spectral Methods by Transformers

Yihan He, Yuan Cao, Hong-Yu Chen, Dennis Wu, Jianqing Fan, Han LiuarXiv 2025 · DOI

Introduction

While Transformers have revolutionized natural language processing, their internal mechanics often remain opaque. Recent research has highlighted their capacity for In-Context Learning (ICL), where models perform supervised tasks—such as linear regression—simply by processing examples within a prompt. However, a significant question remained: can these architectures internalize the logic of unsupervised learning?

Unsupervised tasks, such as Principal Component Analysis (PCA) and spectral clustering, are fundamental to data science but rely on complex iterative logic rather than simple label mapping. This paper investigates whether the Transformer architecture is mathematically and empirically capable of approximating "spectral methods," which identify the underlying structure of data through eigenvalues and eigenvectors.

Theoretical Framework: The Transformer as an Iterative Solver

The authors propose a two-pronged approach, beginning with a constructive theoretical proof. They demonstrate that a multi-layered Transformer can be designed to approximate the Power Method, a classic iterative algorithm used to find eigenvectors.

In this theoretical construction, the Transformer's layers are mapped to specific algorithmic steps:

  • Symmetrization: Initial sub-networks compute the covariance structure ($XX^\top$) of the input data.
  • Power Iterations: Subsequent layers perform iterative matrix-vector multiplications, effectively simulating the steps of the Power Method.
  • Deflation: Specific layers are dedicated to removing previously identified eigenvectors from the data, allowing the model to find the next components in the sequence.

To facilitate this proof, the researchers utilized a modified Transformer architecture employing ReLU activations instead of the standard Softmax and averaged multi-head attention. This suggests that the core power of the Transformer for numerical tasks may lie in its ability to act as a programmable mathematical engine.

Methodology and Learning Paradigm

The study treats the Transformer as an "algorithm approximator." Rather than memorizing specific datasets, the model is pre-trained on a vast array of problem instances (e.g., various matrices and their corresponding principal components). Through this process, the model learns the procedure for solving the problem.

This approach moves beyond standard ICL. While supervised ICL requires (input, label) pairs, this work demonstrates that Transformers can internalize the inherent structure of data itself, performing statistical estimation without explicit labels in the prompt.

Empirical Results

The authors validated their theoretical findings using a mix of synthetic and real-world benchmarks:

  1. Synthetic Data: Using multivariate Gaussian distributions and Gaussian Mixture Models (GMMs), the authors confirmed that the Transformer could accurately perform PCA and clustering under controlled conditions.
  2. Image Data (MNIST & Fashion-MNIST): The pre-trained Transformers successfully extracted the top-10 eigenvalues and eigenvectors from high-dimensional image data. Notably, the Transformer's predictions often surpassed the accuracy of a standard Power Method baseline limited to a small number of iterations.
  3. Real-World Clustering (Covertype): On the Covertype dataset, the model's ability to perform 2-class clustering (measured by Adjusted Rand Index and Normalized Mutual Information) improved significantly as the volume of pre-training data increased, showing a clear "learning curve" for algorithmic logic.

Key Insights

  • Forward Pass as Iteration: The most significant insight is the mathematical mapping between a Transformer's forward propagation and the iterations of classical algorithms. Each layer effectively functions as a single step in an iterative solver.
  • Atomic Decomposition: The research shows that complex algorithms can be broken down into "atomic" sub-tasks, each of which can be mapped to specific sub-networks within the Transformer architecture.
  • Robust Generalization: Despite the strict mathematical assumptions required for the theoretical proofs, the models demonstrated robust performance on real-world datasets that violated those assumptions, such as MNIST.

Limitations and Future Directions

While the results are promising, several challenges remain. The current theoretical proofs assume the model reaches a global minimum (Empirical Risk Minimization), whereas practical training via Stochastic Gradient Descent (SGD) only guarantees local minima. Additionally, the constructive proof relies on a "context-augmented" input matrix to store intermediate values—a requirement that does not seem strictly necessary in empirical practice.

Future work aims to expand this framework to multi-class clustering and more diverse unsupervised tasks, such as manifold learning or independent component analysis. Furthermore, extending these theoretical guarantees to the more common Softmax-based Transformers remains a high priority for the research community.

Summary updated

BibTeX
@misc{he2025learning,
  author = {Yihan He and Yuan Cao and Hong-Yu Chen and Dennis Wu and Jianqing Fan and Han Liu},
  title = {{Learning Spectral Methods by Transformers}},
  year = {2025},
  eprint = {2501.01312},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2501.01312},
}
Figure