One-Layer Transformer Provably Learns One-Nearest Neighbor In Context
Not on the map. Why?
Abstract (arXiv)
Transformers have achieved great success in recent years. Interestingly, transformers have shown particularly strong in-context learning capability -- even without fine-tuning, they are still able to solve unseen tasks well purely based on task-specific prompts. In this paper, we study the capability of one-layer transformers in learning one of the most classical nonparametric estimators, the one-nearest neighbor prediction rule. Under a theoretical framework where the prompt contains a sequence of labeled training data and unlabeled test data, we show that, although the loss function is nonconvex when trained with gradient descent, a single softmax attention layer can successfully learn to behave like a one-nearest neighbor classifier. Our result gives a concrete example of how transformers can be trained to implement nonparametric machine learning algorithms, and sheds light on the role of softmax attention in transformer models.
Key facts. Cited by 16 papers in the map (data through 14 Oct 2026). Builds on 33 of its 43 references that are in the map. First posted on arXiv 16 Nov 2024.
Builds on
33 of 43 references.
References · 33
- 2017Attention Is All You Needcited by 60,361
- 2018BERT: Pre-training of Deep Bidirectional Transformers for Language Understandingcited by 40,399
- 2020An Image is Worth 16x16 Words: Transformers for Image Recognition at Scalecited by 30,186
- 2023GPT-4 Technical Reportcited by 18,396
- 2021Masked Autoencoders Are Scalable Vision Learnerscited by 7,403
- 2021Decision Transformer: Reinforcement Learning via Sequence Modelingcited by 1,546
- 2022Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?cited by 1,434
- 2022MaskGIT: Masked Generative Image Transformercited by 1,099
- 2021An Explanation of In-context Learning as Implicit Bayesian Inferencecited by 938
- 2022Transformers learn in-context by gradient descentcited by 732
- 2022What Can Transformers Learn In-Context? A Case Study of Simple Function Classescited by 715
- 2017Spectrally-normalized margin bounds for neural networkscited by 686
21 more
- 2022What learning algorithm is in-context learning? Investigations with linear modelscited by 684
- 2021Offline Reinforcement Learning as One Big Sequence Modeling Problemcited by 644
- 2016Linear Convergence of Gradient and Proximal-Gradient Methods Under the Polyak-Lojasiewicz Conditioncited by 539
- 2018A Mean Field View of the Landscape of Two-Layers Neural Networkscited by 466
- 2023Larger language models do in-context learning differentlycited by 394
- 2023Trained Transformers Learn Linear Models In-Contextcited by 337
- 2023Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selectioncited by 298
- 2023Transformers learn to implement preconditioned gradient descent for in-context learningcited by 279
- 2019Stabilizing Transformers for Reinforcement Learningcited by 270
- 2023Transformers as Algorithms: Generalization and Stability in In-context Learningcited by 222
- 2023In-Context Convergence of Transformerscited by 115
- 2023Scan and Snap: Understanding Training Dynamics and Token Composition in 1-layer Transformercited by 102
- 2022Vision Transformers provably learn spatial structurecited by 93
- 2023How Do Transformers Learn Topic Structure: Towards a Mechanistic Understandingcited by 86
- 2022MAE-AST: Masked Autoencoding Audio Spectrogram Transformercited by 82
- 2024Training Dynamics of Multi-Head Softmax Attention for In-Context Learning: Emergence, Convergence, and Optimalitycited by 74
- 2020A Mean-field Analysis of Deep ResNet and Beyond: Towards Provable Optimization Via Overparameterization From Depthcited by 56
- 2014Rates of Convergence for Nearest Neighbor Classificationcited by 46
- 2020Modeling from Features: a Mean-field Framework for Over-parameterized Deep Neural Networkscited by 44
- 2024How Well Can Transformers Emulate In-context Newton's Method?cited by 25
- 2023A Mechanism for Sample-Efficient In-Context Learning for Sparse Retrieval Taskscited by 17
Built on by
Citing papers per year: 2025: 8; 2026: 8.
16 papers.
Sort:
By area:
- 2025Lost in Cultural Translation: Do LLMs Struggle with Math Across Cultural Contexts?cited by 9
- 2025Decoupling Positional and Symbolic Attention Behavior in Transformerscited by 6
- 2025Transformers versus the EM Algorithm in Multi-class Clusteringcited by 5
- 2025In-Context Learning of Linear Dynamical Systems with Transformers: Approximation Bounds and Depth-Separationcited by 4
- 2025Softmax ≥ Linear: Transformers may learn to classify in-context by kernel gradient descentcited by 4
- 2025Attention-based clusteringcited by 3
- 2025Bilevel Data Curation for LLM Fine-tuning: Offline Selection and Online Self-Refining Generationcited by 2
- 2025Towards Understanding Transformers in Learning Random Walkscited by 2
- 2026Attention-Based Sampler for Diffusion Language Modelscited by 1
- 2026Transformers Can Implement Preconditioned Richardson Iteration for In-Context Gaussian Kernel Regressioncited by 1
- 2026A Theoretical Analysis of Why Masked Diffusion Models Mitigate the Reversal Cursecited by 0
- 2026Attention-based PCAcited by 0
4 more
- 2026One-Layer Transformer Provably Learns Multiclass One-Nearest Neighbor in Contextcited by 0
- 2026Theory on Attention Dynamics for Out-of-Distribution In-Context Learningcited by 0
- 2026Retrieval-Based In-Context Learning: A Domain Adaptation Frameworkcited by 0
- 2026Adaptive Mean Estimation by In-Context Learning: A Gradient-Flow Analysiscited by 0
- 2026Adaptive Mean Estimation by In-Context Learning: A Gradient-Flow Analysis6 Oct 2026
- 2026Retrieval-Based In-Context Learning: A Domain Adaptation Framework5 Oct 2026
- 2026Theory on Attention Dynamics for Out-of-Distribution In-Context Learning29 Sep 2026
- 2026One-Layer Transformer Provably Learns Multiclass One-Nearest Neighbor in Context1 Sep 2026
- 2026Attention-based PCA18 May 2026
- 2026Transformers Can Implement Preconditioned Richardson Iteration for In-Context Gaussian Kernel Regression8 May 2026
- 2026Attention-Based Sampler for Diffusion Language Models18 Mar 2026
- 2026A Theoretical Analysis of Why Masked Diffusion Models Mitigate the Reversal Curse2 Feb 2026
- 2025Towards Understanding Transformers in Learning Random Walks28 Nov 2025
- 2025Bilevel Data Curation for LLM Fine-tuning: Offline Selection and Online Self-Refining Generation26 Nov 2025
- 2025Softmax ≥ Linear: Transformers may learn to classify in-context by kernel gradient descent12 Oct 2025
- 2025Decoupling Positional and Symbolic Attention Behavior in Transformers3 Oct 2025
4 more
- 2025Attention-based clustering19 May 2025
- 2025Lost in Cultural Translation: Do LLMs Struggle with Math Across Cultural Contexts?23 Mar 2025
- 2025In-Context Learning of Linear Dynamical Systems with Transformers: Approximation Bounds and Depth-Separation12 Feb 2025
- 2025Transformers versus the EM Algorithm in Multi-class Clustering9 Feb 2025
- 2025Decoupling Positional and Symbolic Attention Behavior in Transformerscited by 6
- 2025Transformers versus the EM Algorithm in Multi-class Clusteringcited by 5
- 2025In-Context Learning of Linear Dynamical Systems with Transformers: Approximation Bounds and Depth-Separationcited by 4
- 2025Softmax ≥ Linear: Transformers may learn to classify in-context by kernel gradient descentcited by 4
- 2025Attention-based clusteringcited by 3
- 2025Bilevel Data Curation for LLM Fine-tuning: Offline Selection and Online Self-Refining Generationcited by 2
- 2025Towards Understanding Transformers in Learning Random Walkscited by 2
- 2026Transformers Can Implement Preconditioned Richardson Iteration for In-Context Gaussian Kernel Regressioncited by 1
- 2026Attention-based PCAcited by 0
- 2026One-Layer Transformer Provably Learns Multiclass One-Nearest Neighbor in Contextcited by 0
- 2026Theory on Attention Dynamics for Out-of-Distribution In-Context Learningcited by 0
- 2026Retrieval-Based In-Context Learning: A Domain Adaptation Frameworkcited by 0
12 most cited of 13 All 13 (JSON)
- 2026Adaptive Mean Estimation by In-Context Learning: A Gradient-Flow Analysis6 Oct 2026
- 2026Retrieval-Based In-Context Learning: A Domain Adaptation Framework5 Oct 2026
- 2026Theory on Attention Dynamics for Out-of-Distribution In-Context Learning29 Sep 2026
- 2026One-Layer Transformer Provably Learns Multiclass One-Nearest Neighbor in Context1 Sep 2026
- 2026Attention-based PCA18 May 2026
- 2026Transformers Can Implement Preconditioned Richardson Iteration for In-Context Gaussian Kernel Regression8 May 2026
- 2025Towards Understanding Transformers in Learning Random Walks28 Nov 2025
- 2025Bilevel Data Curation for LLM Fine-tuning: Offline Selection and Online Self-Refining Generation26 Nov 2025
- 2025Softmax ≥ Linear: Transformers may learn to classify in-context by kernel gradient descent12 Oct 2025
- 2025Decoupling Positional and Symbolic Attention Behavior in Transformers3 Oct 2025
- 2025Attention-based clustering19 May 2025
- 2025In-Context Learning of Linear Dynamical Systems with Transformers: Approximation Bounds and Depth-Separation12 Feb 2025
12 newest of 13 All 13 (JSON)