Lune

NeurIPS2024Top-tier venue

One-Layer Transformer Provably Learns One-Nearest Neighbor In Context

Zihao Li, Yuan Cao, Cheng Gao, Yihan He, Han Liu, Jason M. Klusowski, Jianqing Fan, Mengdi Wang

Abstract

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.

Ask Lune

Know this paper in minutes, not an afternoon.

Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.

12 top-tier papers have cited it since. Know what they found before your reviewers do.

Questions to start from

Lune can draw on

the full paper · the top-tier papers that cite it · related work

Get the answer

Free to start. No credit card required.

Lineage

  • Transformers Efficiently Perform In-Context Logistic Regression via Normalized Gradient Descent cites Towards Understanding Transformers in Learning Random Walks
  • Transformers Efficiently Perform In-Context Logistic Regression via Normalized Gradient Descent cites Offline Reinforcement Learning as One Big Sequence Modeling Problem
  • Transformers Efficiently Perform In-Context Logistic Regression via Normalized Gradient Descent cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
  • Towards Understanding Transformers in Learning Random Walks cites Offline Reinforcement Learning as One Big Sequence Modeling Problem
  • Towards Understanding Transformers in Learning Random Walks cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
  • Towards Understanding Transformers in Learning Random Walks cites Decision Transformer: Reinforcement Learning via Sequence Modeling
  • Offline Reinforcement Learning as One Big Sequence Modeling Problem cites Decision Transformer: Reinforcement Learning via Sequence Modeling
  • Decision Transformer: Reinforcement Learning via Sequence Modeling cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
  • Attention-based clustering cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
Builds onCited byLeft to right by year; the more cited, the higher.

Cited by top-tier papers12

  1. Attention-based clusteringRodrigo Maulen-Soto, Pierre Marion, Claire BoyerNeurIPS 2025 · 3 citations
  2. Decoupling Positional and Symbolic Attention in TransformersFelipe Urrutia, Jorge Salas, Alexander Kozachinskiy, Cristian Buc Calderon et al.ICLR 2026 · 3 citations
  3. Transformers Efficiently Perform In-Context Logistic Regression via Normalized Gradient DescentChenyang Zhang, Yuan CaoICML 2026 · 1 citation
  4. In-context Learning of Linear Dynamical Systems with Transformers: Approximation Bounds and Depth-separationFrank Cole, Yuxuan Zhao, Yulong Lu, Tianhao ZhangNeurIPS 2025 · 1 citation
  5. Towards Understanding Transformers in Learning Random WalksWei Shi, Yuan CaoNeurIPS 2025 · 1 citation

Builds on18

  1. An Image is Worth 16x16 Words: Transformers for Image Recognition at ScaleAlexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn et al.ICLR 2021 · 21,477 citations
  2. Photorealistic Text-to-Image Diffusion Models with Deep Language UnderstandingChitwan Saharia, William Chan, Saurabh Saxena, Lala Li et al.NeurIPS 2022 · 8,965 citations
  3. Decision Transformer: Reinforcement Learning via Sequence ModelingLili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee et al.NeurIPS 2021 · 2,557 citations
  4. An Explanation of In-context Learning as Implicit Bayesian InferenceSang Michael Xie, Aditi Raghunathan, Percy Liang, Tengyu MaICLR 2022 · 1,030 citations
  5. Offline Reinforcement Learning as One Big Sequence Modeling ProblemMichael Janner, Qiyang Li, Sergey LevineNeurIPS 2021 · 950 citations

Related papers

  1. In-Context Learning with Transformers: Softmax Attention Adapts to Function LipschitznessLiam Collins, Advait Parulekar, Aryan Mokhtari, Sujay Sanghavi et al.NeurIPS 2024 · 33 citations
  2. Transformers Implement Functional Gradient Descent to Learn Non-Linear Functions In ContextXiang Cheng, Yuxin Chen, Suvrit SraICML 2024 · 64 citations
  3. In-context Convergence of TransformersYu Huang, Yuan Cheng, Yingbin LiangICML 2024 · 114 citations
  4. In-Context Algorithm Emulation in Fixed-Weight TransformersJerry Yao-Chieh Hu, Hude Liu, Jennifer Yuntong Zhang, Han LiuICLR 2026 · 7 citations
  5. The Closeness of In-Context Learning and Weight Shifting for Softmax RegressionShuai Li, Zhao Song, Yu Xia, Tong Yu et al.NeurIPS 2024 · 53 citations
Dusk over the sea between two cliffs drawn in fine vertical lines