Unveiling the Hidden Structure of Self-Attention via Kernel Principal Component Analysis

Rachel S.y. Teo, Tan Minh Nguyen · DOI · OpenReview

TL;DR

This paper reveals that the self-attention mechanism in transformers can be mathematically derived from Kernel Principal Component Analysis (Kernel PCA). This insight leads to a novel robust attention mechanism, RPC-Attention, which significantly improves performance and robustness, especially in the presence of noisy or corrupted data.

What Problem Does This Paper Address?

Transformers have achieved remarkable success across various domains, largely due to their self-attention mechanism. However, the design of these attention mechanisms has historically relied on heuristics and intuition rather than a principled theoretical foundation. This lack of theoretical understanding makes it challenging to analyze their behavior, improve their efficiency, and enhance their robustness against real-world data imperfections like noise and adversarial attacks. This paper aims to bridge this gap by providing a theoretical framework for self-attention and leveraging it to develop a more robust variant.

Methods

The core of this paper's contribution lies in its theoretical derivation of self-attention from Kernel PCA. The authors show that:

  • Self-attention as Kernel PCA Projections: The query vectors in self-attention are projected onto the principal component axes of the key matrix in a feature space induced by a kernel function.
  • Value Matrix as Eigenvectors: The value matrix in self-attention is shown to capture the eigenvectors of the Gram matrix of the key vectors.
  • RPC-Attention: Based on this Kernel PCA understanding, the authors propose "Attention with Robust Principal Components" (RPC-Attention). This method leverages Principal Component Pursuit (PCP), a robust technique for recovering low-rank matrices from corrupted data, to make the attention mechanism resilient to data contamination. RPC-Attention essentially applies PCP to the key matrix before it's used in the attention calculation.

Datasets

The paper uses standard datasets for evaluation across various tasks:

  • ImageNet-1K: For object classification and robustness benchmarks.
  • ImageNet-C, ImageNet-A, ImageNet-O, ImageNet-R: For evaluating robustness against common corruptions and data distribution shifts.
  • ADE20K: For image segmentation.
  • WikiText-103: For language modeling.

Key Insights and Ideas

  • Self-Attention is Kernel PCA in Disguise: The most significant insight is the direct mathematical link between self-attention and Kernel PCA. This provides a principled way to understand why self-attention works and how it learns representations.
  • Value Matrix Holds Key Information: The finding that the value matrix directly corresponds to eigenvectors of the key matrix's Gram matrix is crucial. It suggests that the learned value representations are not arbitrary but are tied to the principal components of the key space.
  • Robustness through Robust PCA: The paper demonstrates that by replacing standard PCA (implicit in self-attention) with a more robust version (PCP), significant gains in robustness against corrupted data can be achieved without sacrificing performance on clean data.
  • Theoretical Foundation for Heuristics: This work moves beyond heuristic design by providing a theoretical basis for the self-attention mechanism, opening doors for more principled analysis and development.

Key Results

  • Improved Robustness: RPC-Attention consistently outperforms the baseline softmax attention on various robustness benchmarks, including ImageNet-C (common corruptions), adversarial attacks (PGD, FGSM, etc.), and word swap attacks on WikiText-103. For instance, on ImageNet-1K, RPC-SymViT with 6 iterations in the first layer achieved over 1% improvement in accuracy on clean data and nearly 3 AUPR on ImageNet-O.
  • Competitive Performance on Clean Data: RPC-Attention does not degrade performance on clean data and often shows slight improvements. For example, RPC-SymViT (6iter/layer1) achieved 71.49% top-1 accuracy on clean ImageNet-1K, slightly better than the baseline's 70.44%.
  • Effective on Various Tasks: The benefits of RPC-Attention were demonstrated across image classification, image segmentation (ADE20K), and language modeling (WikiText-103).
  • Efficiency: While iterative, RPC-Attention is shown to be computationally comparable to standard attention at test time, with only a slight increase during training.

Limitations and Future Work

  • Iterative Nature: The proposed RPC-Attention, derived from PCP, involves an iterative algorithm. While the paper mitigates this by applying it only to the first layer, it still introduces some computational overhead compared to a single-pass softmax attention.
  • Extending to Multi-Layer: The current derivation and application of RPC-Attention are primarily focused on understanding and improving the first layer. Extending the kernel PCA framework to fully explain multi-layer transformers is suggested as future work.
  • Theoretical Proof for Convergence: While empirical results strongly suggest that self-attention layers converge to kernel PCA solutions, a formal theoretical proof for this convergence in complex multi-layer, multi-head transformer models is left for future work.
  • Generalizing RPC-Attention: The paper notes that the ADMM algorithm for PCP is not easily extended to multiple corrupted matrices, which limits the direct application of RPC-Attention to asymmetric attention mechanisms. Investigating ways to overcome this limitation is a potential future direction.

Summary updated

BibTeX
@inproceedings{teo2024unveiling,
  author = {Rachel S.Y. Teo and Tan Minh Nguyen},
  title = {{Unveiling the Hidden Structure of Self-Attention via Kernel Principal Component Analysis}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=VUWvVvNi6r},
}
Figure