NeurIPS 2026QuestionsPapersAreasAboutMarkdown

One-Layer Transformers Provably Learn In-Context K-Nearest Neighbor Prediction with Chain-of-Thought

Lyumin Wu, Yuan Cao

NeurIPS page · OpenReview · Open on the map

Category: Interpretability & Model Adaptation › Chain-of-Thought Reasoning in Transformers · Type: theory · Keywords: transformers, chain-of-thought reasoning, in-context learning, nearest neighbor prediction, theoretical analysis, gradient descent

Areas: LLM Reasoning › Reasoning Mechanisms › Chain-of-Thought Reasoning; Model Architectures & Sequence Modeling › Language Model Architectures › Transformer Structural Properties

Summary

The paper constructs a one-layer transformer that uses chain-of-thought reasoning to provably learn in-context nearest-neighbor prediction, and shows that gradient descent can recover this capability during training.

Explained to a 5-year-old

A tiny robot brain called a transformer can learn to copy the answers of its friends. It uses a special note-taking trick called chain-of-thought to think step by step. The robot first learns that it cannot remember more than one friend at a time, but then scientists show how to make it remember many friends by writing down extra notes. Through practice, the robot learns to pick the right friends for new puzzles.

Explained to a 5-year-old, longer

Imagine you have a magic notebook that helps you solve puzzles. This notebook is a very simple computer called a one-layer transformer. Normally, the notebook can only look at the single best example to answer a question, like the closest picture. But scientists wanted it to look at several good examples, like the top three pictures. They discovered that the notebook cannot do this on its own because its attention is too greedy. So they gave it a new trick: chain-of-thought, where the notebook writes down little steps of thinking, like "look at this picture, then think about that picture, then combine them." By following these steps, the notebook can now remember and use many examples to answer questions. They also proved that if you train the notebook the right way, it will learn this trick by itself. This makes the notebook smarter and able to solve harder puzzles.

For a technical reader

The work shows that standard softmax attention in a single-layer transformer cannot natively attend to multiple nearest neighbors for odd K>1 on a well-separated binary classification problem. To overcome this, the authors propose an explicit construction that injects chain-of-thought reasoning into the attention mechanism, enabling K-NN prediction. They further prove that gradient descent can recover this construction under a stylized training regime, and that the resulting model generalizes beyond the training assumptions. This provides both a negative theoretical limit and a constructive proof of how CoT expands transformer capabilities.

Problem

How can transformers acquire the ability to perform in-context nearest-neighbor prediction using chain-of-thought reasoning, and why do standard attention mechanisms struggle to handle multiple neighbors beyond the closest one?

Method

The authors provide a negative result showing that standard softmax attention cannot handle multiple nearest neighbors for odd K>1 on a well-separated binary classification class. They then present an explicit construction of a one-layer transformer that leverages chain-of-thought reasoning to solve the task, and prove that gradient descent can recover this construction under a stylized training setup.

Results

The approach demonstrates that a one-layer transformer can solve in-context nearest-neighbor prediction using chain-of-thought reasoning, that gradient descent can recover this capability during training, and that the resulting model generalizes to a broader class of test tasks.

Contributions

The paper provides a negative result establishing the limitation of standard attention for multiple nearest neighbors, presents an explicit construction of a transformer that uses chain-of-thought to achieve nearest-neighbor prediction, and proves that gradient descent can learn this construction, highlighting how CoT expands transformer capabilities.

Limitations and assumptions

The analysis assumes a well-separated binary classification setting, focuses on odd K>1, and relies on a stylized training setup to prove gradient descent recovery.

Research questions this paper is counted under

This page was written by language models from the title and abstract of each paper, and the answers were checked by two other models. It can be wrong: check the paper itself. Suggest a correction