mlsanityData to 14 Oct 2026

Transformer Circuits Can Realize Clustering Algorithms

arXiv 2506.19125 ↗First posted 23 Jun 2025cs.LG cs.AIcited by 0

Not on the map. Why?

Abstract (arXiv)

Although transformers are most commonly optimized as statistical sequence models, it is unclear to what extent they can implement and learn exact algorithmic computations. Here, we specify a transformer implementation from first principles that executes a fundamental and widely used method for k-means clustering: Lloyd's algorithm. We theoretically prove and empirically demonstrate that this implementation of a transformer architecture, which we term the k-means transformer, exactly implements Lloyd's algorithm for k-means clustering using the standard circuit mechanisms of modern transformers: attention block, residual connections, and feed-forward block. In learning experiments, we find that training this base architecture on k-means clustering yields a generalizable clustering algorithm that surpasses Lloyd's algorithm in terms of clustering quality. Finally, we demonstrate that interpretable alterations (e.g., inclusion of layer normalizations) to this architecture yields diverse and novel variants of clustering algorithms, including soft k-means, spherical k-means, trimmed k-means. Overall, our results show that transformer circuit mechanisms can instantiate exact algorithmic routines for clustering, while simultaneously providing an effective learnable model.

Key facts. No citing papers in the map yet (data through 14 Oct 2026). Builds on 20 of its 45 references that are in the map. First posted on arXiv 23 Jun 2025.

Builds on

20 of 45 references.

References · 20

  1. 2017Attention Is All You Needcited by 60,361
  2. 2016Layer Normalizationcited by 5,669
  3. 2016Categorical Reparameterization with Gumbel-Softmaxcited by 2,739
  4. 2019Root Mean Square Layer Normalizationcited by 1,480
  5. 2022Transformers learn in-context by gradient descentcited by 732
  6. 2022What Can Transformers Learn In-Context? A Case Study of Simple Function Classescited by 715
  7. 2022What learning algorithm is in-context learning? Investigations with linear modelscited by 684
  8. 2018Universal Transformerscited by 647
  9. 2016From Softmax to Sparsemax: A Sparse Model of Attention and Multi-Label Classificationcited by 341
  10. 2023Trained Transformers Learn Linear Models In-Contextcited by 337
  11. 2023Transformers learn to implement preconditioned gradient descent for in-context learningcited by 279
  12. 2019Transformer Dissection: A Unified Understanding of Transformer's Attention via the Lens of Kernelcited by 213
8 more
  1. 2023A mathematical perspective on Transformerscited by 164
  2. 2023The emergence of clusters in self-attention dynamicscited by 140
  3. 2023What Formal Languages Can Transformers Express? A Surveycited by 124
  4. 2019Learning with Fenchel-Young Lossescited by 119
  5. 2023In-Context Convergence of Transformerscited by 115
  6. 2021Memory-efficient Transformers via Top-k Attentioncited by 60
  7. 2025Constant Bit-size Transformers Are Turing Completecited by 19
  8. 2024Sparse and Structured Hopfield Networkscited by 13

Built on by

Not cited yet.

Similar, not cited

Close in wording, no citation either way

  1. 2024Log-Time K-Means Clustering for 1D Data: Novel Approaches with Proof and Implementation
  2. 2025Attention-based clustering
  3. 2026The K-SCAN Clustering Algorithm
  4. 2024Clustering in pure-attention hardmax transformers and its role in sentiment analysis
  5. 2026Distributionally Robust K-Means Clustering
  6. 2024Robust Clustering on High-Dimensional Data with Stochastic Quantization
  7. 2025FTCFormer: Fuzzy Token Clustering Transformer for Image Classification
  8. 2026Multi-Domain Clustering via Measure Quantization