Attention-based clustering

Rodrigo Maulen-Soto, Pierre Marion, Claire Boyer

Introduction

While Transformers have fundamentally transformed the landscape of artificial intelligence, our theoretical understanding of their success has largely been confined to supervised learning contexts, such as In-Context Learning (ICL). This paper establishes a rigorous theoretical foundation for how the attention mechanism performs unsupervised learning. Specifically, the authors demonstrate that Transformers are inherently designed to perform clustering, bridging the gap between modern neural architectures and classical statistical methods like Gaussian Mixture Models (GMMs).

Theoretical Framework

To analyze the clustering capabilities of Transformers, the researchers utilize a simplified, mathematically tractable model: a two-headed linear attention layer. This simplification allows for the derivation of closed-form expressions for the model's performance, referred to as "quantization risk."

Key Methodological Components

  • Linear Attention: By replacing the standard softmax nonlinearity with a linear activation, the authors can precisely calculate the error (risk) of the model.
  • Quantization Risk: This theoretical loss function is analogous to the error used in K-means clustering. Minimizing this risk encourages the attention heads to output the "centroid" of the cluster to which a token belongs.
  • Projected Gradient Descent (PGD): The training dynamics are analyzed using PGD on a unit sphere, reflecting the normalization typically found in attention parameters (keys and queries).
  • Disentanglement Regularization: To ensure that different attention heads do not collapse onto the same cluster, a regularization term is introduced to encourage orthogonality and independence between heads.

Attention as a Clustering Mechanism

The core insight of the paper is that attention heads naturally function as centroid learners. During the training process, the parameters of individual heads drift until they align with the true centroids of the data distribution. Once converged, the attention layer acts as a powerful "denoiser."

PCA visualization of the denoising effect where tokens are projected toward cluster centroids.

As shown in the visualization above, the attention mechanism takes noisy input tokens and uses the context of the entire sequence to project them toward their underlying cluster centers. This contextual approach allows the Transformer to outperform standard optimal quantizers. While a standard quantizer evaluates one token at a time, the attention mechanism leverages the entire sequence to reduce variance, achieving a mathematical advantage of $1 - 2/d$ in risk reduction, where $d$ is the dimensionality of the data.

Empirical Results and Validation

The authors provide a formal convergence proof (Theorem 3.4), stating that under specific initialization, PGD iterates will converge to true mixture centroids in a fully unsupervised manner. These theoretical findings are supported by numerical simulations using synthetic datasets, such as Gaussian Mixture Models and Dirac Mixtures.

Convergence of attention parameters to true centroids over training iterations.

Empirical validation confirms that linear convergence to centroids occurs even with random initialization on a sphere, provided the proposed disentanglement regularization is applied. As illustrated in the figure above, the distance to the true centroid decreases rapidly over iterations. Furthermore, the model demonstrates "in-context quantization"—the ability of a fixed attention layer (with identity matrices) to adapt to new data distributions on the fly without any parameter updates.

Conclusion and Future Directions

This research reveals that the Transformer's inductive bias is inherently suited for distributional adaptation and unsupervised structure extraction. However, several open questions remain. The current analysis primarily focuses on linear attention and simplified architectures, omitting features like LayerNorm and MLP blocks. Future work will involve extending these proofs to non-linear softmax attention and exploring how these clustering properties manifest in production-scale Large Language Models (LLMs) dealing with complex, real-world data distributions.

Summary updated

BibTeX
@inproceedings{maulensoto2025attention,
  author = {Rodrigo Maulen-Soto and Pierre Marion and Claire Boyer},
  title = {{Attention-based clustering}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=NRvxzOdSPU},
}
Figure