What Can Transformers Learn In-Context? A Case Study of Simple Function Classes
Shivam Garg, Dimitris Tsipras, Percy Liang, Gregory Valiant
Abstract
In-context learning refers to the ability of a model to condition on a prompt sequence consisting of in-context examples (input-output pairs corresponding to some task) along with a new query input, and generate the corresponding output. Crucially, in-context learning happens only at inference time without any parameter updates to the model. While large language models such as GPT-3 exhibit some ability to perform in-context learning, it is unclear what the relationship is between tasks on which this succeeds and what is present in the training data. To make progress towards understanding in-context learning, we consider the well-defined problem of training a model to in-context learn a function class (e.g., linear functions): that is, given data derived from some functions in the class, can we train a model to in-context learn "most" functions from this class? We show empirically that standard Transformers can be trained from scratch to perform in-context learning of linear functions-that is, the trained model is able to learn unseen linear functions from in-context examples with performance comparable to the optimal least squares estimator. In fact, in-context learning is possible even under two forms of distribution shift: (i) between the training data of the model and inference-time prompts, and (ii) between the in-context examples and the query input during inference. We also show that we can train Transformers to in-context learn more complex function classes-namely sparse linear functions, two-layer neural networks, and decision trees-with performance that matches or exceeds task-specific learning algorithms. 1 1 Our code and models are available at https://github.com/dtsip/in-context-learning .
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.
327 top-tier papers have cited it since. Know what they found before your reviewers do.
Lune can draw on
the full paper · the top-tier papers that cite it · related work
Free to start. No credit card required.
Lineage
- Hyena Hierarchy: Towards Larger Convolutional Language Models cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
- Hyena Hierarchy: Towards Larger Convolutional Language Models cites Language Models are Few-Shot Learners
- Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection cites Fantastically Ordered Prompts and Where to Find Them: Overcoming Few-Shot Prompt Order Sensitivity
- Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
- Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection cites An Explanation of In-context Learning as Implicit Bayesian Inference
- Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection cites Language Models are Few-Shot Learners
- Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection cites Transformers Learn In-Context by Gradient Descent
- Fantastically Ordered Prompts and Where to Find Them: Overcoming Few-Shot Prompt Order Sensitivity cites Language Models are Few-Shot Learners
- Fantastically Ordered Prompts and Where to Find Them: Overcoming Few-Shot Prompt Order Sensitivity cites Calibrate Before Use: Improving Few-shot Performance of Language Models
- A Survey on In-context Learning cites Fantastically Ordered Prompts and Where to Find Them: Overcoming Few-Shot Prompt Order Sensitivity
- A Survey on In-context Learning cites An Explanation of In-context Learning as Implicit Bayesian Inference
- A Survey on In-context Learning cites Language Models are Few-Shot Learners
- A Survey on In-context Learning cites Transformers Learn In-Context by Gradient Descent
- A Survey on In-context Learning cites Calibrate Before Use: Improving Few-shot Performance of Language Models
- An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale cites Language Models are Few-Shot Learners
- An Explanation of In-context Learning as Implicit Bayesian Inference cites Language Models are Few-Shot Learners
- An Explanation of In-context Learning as Implicit Bayesian Inference cites Calibrate Before Use: Improving Few-shot Performance of Language Models
- Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective cites Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm Selection
- Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective cites An Explanation of In-context Learning as Implicit Bayesian Inference
- Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective cites Language Models are Few-Shot Learners
- Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective cites Transformers Learn In-Context by Gradient Descent
- Transformers Learn In-Context by Gradient Descent cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
- Transformers Learn In-Context by Gradient Descent cites Language Models are Few-Shot Learners
- Calibrate Before Use: Improving Few-shot Performance of Language Models cites Language Models are Few-Shot Learners
Cited by top-tier papers327
- Transformers Learn In-Context by Gradient DescentJohannes von Oswald, Eyvind Niklasson, Ettore Randazzo, João Sacramento et al.ICML 2023 · 729 citations
- Hyena Hierarchy: Towards Larger Convolutional Language ModelsMichael Poli, Stefano Massaroli, Eric Nguyen, Daniel Y. Fu et al.ICML 2023 · 481 citations
- A Survey on In-context LearningQingxiu Dong, Lei Li, Damai Dai, Ce Zheng et al.EMNLP 2024 · 479 citations
- Towards Revealing the Mystery behind Chain of Thought: A Theoretical PerspectiveGuhao Feng, Bohang Zhang, Yuntian Gu, Haotian Ye et al.NeurIPS 2023 · 470 citations
- Transformers as Statisticians: Provable In-Context Learning with In-Context Algorithm SelectionYu Bai, Fan Chen, Huan Wang, Caiming Xiong et al.NeurIPS 2023 · 356 citations
Builds on18
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah et al.NeurIPS 2020 · 64,255 citations
- 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
- Calibrate Before Use: Improving Few-shot Performance of Language ModelsZihao Zhao, Eric Wallace, Shi Feng, Dan Klein et al.ICML 2021 · 1,843 citations
- Fantastically Ordered Prompts and Where to Find Them: Overcoming Few-Shot Prompt Order SensitivityYao Lu, Max Bartolo, Alastair Moore, Sebastian Riedel et al.ACL 2022 · 1,494 citations
- An Explanation of In-context Learning as Implicit Bayesian InferenceSang Michael Xie, Aditi Raghunathan, Percy Liang, Tengyu MaICLR 2022 · 1,030 citations
Related papers
- In-context Learning on Function Classes Unveiled for TransformersZhijie Wang, Bo Jiang, Shuai LiICML 2024 · 8 citations
- In-Context Learning through the Bayesian PrismMadhur Panwar, Kabir Ahuja, Navin GoyalICLR 2024 · 79 citations
- In-Context Learning with Representations: Contextual Generalization of Trained TransformersTong Yang, Yu Huang, Yingbin Liang, Yuejie ChiNeurIPS 2024 · 45 citations
- How Do Transformers Learn In-Context Beyond Simple Functions? A Case Study on Learning with RepresentationsTianyu Guo, Wei Hu, Song Mei, Huan Wang et al.ICLR 2024 · 80 citations
- Can In-context Learning Really Generalize to Out-of-distribution Tasks?Qixun Wang, Yifei Wang, Xianghua Ying, Yisen WangICLR 2025
