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

Zihao Li, Yuan Cao, Cheng Gao, Yihan He, Han Liu +3 more
11/16/2024
cs.LGcs.AImath.OC

No confident code implementation found for this paper.

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.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. Any repos shown below are weak matches — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{li2024onelayer,
  title  = {One-Layer Transformer Provably Learns One-Nearest Neighbor In Context},
  author = {Zihao Li and Yuan Cao and Cheng Gao and Yihan He and Han Liu and Jason M. Klusowski and Jianqing Fan and Mengdi Wang},
  year   = {2024},
  eprint = {2411.10830},
  archivePrefix = {arXiv},
  doi    = {10.48550/arXiv.2411.10830},
  url    = {http://arxiv.org/abs/2411.10830v1},
  journal = {NEURIPS 2024 2024}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2411.10830.svg)](https://codeofpaper.com/paper/2411.10830)

Discussion