Transformer Circuits Can Realize Clustering Algorithms
Kenneth Clarkson, Lior Horesh, Takuya Ito, Charlotte Park, Parikshit Ram
Abstract
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 -means clustering: Lloyd's algorithm. We theoretically prove and empirically demonstrate that this implementation of a transformer architecture, which we term the -means transformer, exactly implements Lloyd's algorithm for -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 -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 -means, spherical -means, trimmed -means. Overall, our results show that transformer circuit mechanisms can instantiate exact algorithmic routines for clustering, while simultaneously providing an effective learnable model.
Ask about this paper
Your agent reads all of it.
Lune indexed this paper to the last equation, along with the top-tier papers that cite it. Ask a question and the answer quotes them.
Your agent calls
Luneget_paper_fulltext
Free to start. No credit card required.
Terminal
Install the CLIlune papers fulltext 3e806843-d852-4d2e-849e-b7331d6cc052Cited by top-tier papers1
Ask how each one uses itBuilds on11
- What Can Transformers Learn In-Context? A Case Study of Simple Function ClassesShivam Garg, Dimitris Tsipras, Percy Liang, Gregory ValiantNeurIPS 2022 · 883 citations
- Transformers Learn In-Context by Gradient DescentJohannes von Oswald, Eyvind Niklasson, Ettore Randazzo, João Sacramento et al.ICML 2023 · 729 citations
- Transformers learn to implement preconditioned gradient descent for in-context learningKwangjun Ahn, Xiang Cheng, Hadi Daneshmand, Suvrit SraNeurIPS 2023 · 324 citations
- The Expressive Power of Transformers with Chain of ThoughtWilliam Merrill, Ashish SabharwalICLR 2024 · 243 citations
- The emergence of clusters in self-attention dynamicsBorjan Geshkovski, Cyril Letrouit, Yury Polyanskiy, Philippe RigolletNeurIPS 2023 · 163 citations
Related papers
- On the Efficiency of K-Means Clustering: Evaluation, Optimization, and Algorithm SelectionSheng Wang, Yuan Sun, Zhifeng BaoVLDB 2021 · 34 citations
- Fast -Means via Data-Aware Grouping and Gap-Optimized Lower BoundXiaogang Huang, Dan Zhuang, Jianbao Chen, Tiefeng Ma et al.ICDE 2026
- Efficient Algorithms for Sum-Of-Minimum OptimizationLisang Ding, Ziang Chen, Xinshang Wang, Wotao YinICML 2024 · 7 citations
- End-to-end Differentiable Clustering with Associative MemoriesBishwajit Saha, Dmitry Krotov, Mohammed J. Zaki, Parikshit RamICML 2023 · 14 citations
- Heterogeneity for the Win: One-Shot Federated ClusteringDon Kurian Dennis, Tian Li, Virginia SmithICML 2021 · 212 citations
