One-Layer Transformers Provably Learn In-Context K-Nearest Neighbor Prediction with Chain-of-Thought
cs.LG, stat.ML
Submitted: 2026-10-12
Updated: 2026-10-12
Comments: 107 pages, 2 figures
License: http://creativecommons.org/licenses/by/4.0/
The gist: Chain-of-Thought (CoT) enables transformers to solve complex reasoning tasks by generating intermediate reasoning steps.
Terminology
Abstract
Chain-of-Thought (CoT) enables transformers to solve complex reasoning tasks by generating intermediate reasoning steps. Despite strong empirical success, the mechanisms by which such reasoning abilities emerge during training remain poorly understood, particularly from a theoretical perspective. In this work, we study this question in a stylized yet fully analyzable setting: in-context K-nearest-neighbor (K-NN) prediction with a one-layer transformer. Earlier work has demonstrated that one-layer transformers can be trained to perform 1-NN without CoT (Li et al., 2024a) by having the softmax attention attend to the nearest neighbor in context. However, this result on 1-NN cannot be extended to K-NN for K>1, as softmax attention is much less naturally suited to attending to the 2nd through K-th nearest neighbors. We first give a rigorous negative result showing that this limitation already appears on a well-separated class of binary classification tasks for odd K>1. We then give an explicit construction of a one-layer transformer, and show that it can solve K-NN via CoT reasoning. We further prove that, under a stylized training setup, gradient descent can recover this construction, thereby showing that transformers can acquire the capability to solve K-NN through training. In addition, we show that the resulting trained model applies to a broader test class than that assumed in the training analysis. Our results shed light on how CoT expands the ability of transformers to learn and solve tasks that are otherwise hard to solve.
Sources
- GPT-4 Technical Report
- Lower Bounds for Chain-of-Thought Reasoning in Hard-Attention Transformers
- Unveiling the Statistical Foundations of Chain-of-Thought Prompting Methods
- Sora: A Review on Background, Technology, Limitations, and Opportunities of Large Vision Models
- The Expressive Power of Transformers with Chain of Thought
- Toward Understanding Why Adam Converges Faster Than SGD for Transformers
- Positional Description Matters for Transformers Arithmetic
- OpenAI GPT-5 System Card
- LLaMA: Open and Efficient Foundation Language Models
- Llama 2: Open Foundation and Fine-Tuned Chat Models
- Looped Transformers with Layer Normalization Provably Learn the Power Method
- In-Context Learning of a Linear Transformer Block: Benefits of the MLP Component and One-Step GD Initialization
- Evaluation of OpenAI o1: Opportunities and Challenges of AGI
- Least-to-Most Prompting Enables Complex Reasoning in Large Language Models
Related papers
- Polynomial-Augmented Neural Networks (PANNs) with Weak Orthogonality Constraints for Enhanced Function and PDE Approximation
- AIRL-S: Unifying Reinforcement Learning and Search-Based Test-Time Scaling via Adversarial Inverse Reinforcement Learning
- Transformers as Bayesian In-Context Experimenters: Smoothness-Adaptive Efficient ATE Estimation
- Convergence issues in Relational Concept Analysis based on AOC-posets
- Beliefs Beyond Posteriors: The Predictive Cost of ELBO Binding
- Understanding Diffusion Models via Ratio-Based Function Approximation with SignReLU Networks