Abstract
Large pretrained language models have shown surprising in-context learning (ICL) ability. With a few demonstration input-label pairs, they can predict the label for an unseen input without parameter updates. Despite the great success in performance, its working mechanism still remains an open question. In this paper, we explain language models as meta-optimizers and understand in-context learning as implicit finetuning. Theoretically, we figure out that Transformer attention has a dual form of gradient descent. On top of it, we understand ICL as follows: GPT first produces meta-gradients according to the demonstration examples, and then these meta-gradients are applied to the original GPT to build an ICL model. We comprehensively compare the behaviors of in-context learning and explicit finetuning on real tasks to provide empirical evidence that supports our understanding. Experimental results show that in-context learning behaves similarly to explicit finetuning from multiple perspectives. Inspired by the dual form between Transformer attention and gradient descent, we design a momentum-based attention by analogy with gradient descent with momentum. The improved performance over vanilla attention further supports our understanding from another perspective, and more importantly, shows the potential to utilize our understanding for future model design.
Introduction: The Mystery of In-Context Learning
Large Language Models (LLMs) like GPT-3 possess a "magical" ability called In-Context Learning (ICL). By providing a few examples of a task in the prompt, the model can perform that task for a new input without any changes to its internal weights. While highly effective, the "why" behind ICL has remained an open question. Is the model just performing sophisticated pattern matching, or is it actually "learning" in a way that mirrors traditional training? This paper, presented at ACL 2023, seeks to demystify this mechanism by proving that the Transformer's attention layers are essentially performing a hidden version of the gradient descent algorithm used to train models.
Theoretical Framework: GPT as a Meta-Optimizer
The core thesis of the paper is that GPT-style models aren't just predicting the next token; they are implicitly performing gradient descent. By treating the Transformer's forward pass as a meta-optimization process, the researchers show that ICL is mathematically and behaviorally equivalent to explicit finetuning.
In this "Dual View," finetuning calculates gradients via back-propagation, while ICL produces "meta-gradients" via forward computation. The demonstration examples in a prompt act as training data that the model processes into implicit weight updates ($\Delta W_{ICL}$), which are then applied to the model's internal state on the fly.
Empirical Evidence: Comparing ICL and Finetuning
To validate this theory, the authors compared ICL against a controlled "explicit finetuning" setup across several standard NLP benchmarks, including SST2, SST5, MR, Subj, AGNews, and CB. They observed strong behavioral alignment between the two processes:
- Prediction Overlap: The "Recall to Finetuning Prediction" (Rec2FTP) metric showed that ICL covers over 85% of the correct predictions made by explicit finetuning.
- Representation Similarity: Metrics like Similarity of Attention Output Updates (SimAOU) and Similarity of Attention Matrices (SimAM) demonstrated that the internal state changes during ICL move in the same direction as those during explicit finetuning.
Architectural Innovation: Momentum-Based Attention
Inspired by the analogy to gradient descent, the researchers designed Momentum-Based Attention (MoAttn). Just as optimizers like SGD use "momentum" to speed up training by considering previous gradients, MoAttn uses an Exponential Moving Average (EMA) to weight recent tokens more heavily, treating them as more informative "gradients."
This architectural change consistently outperformed vanilla Transformers, improving ICL accuracy by an average of 2.8 points across six datasets and achieving lower perplexity in general language modeling. This success further supports the meta-optimization understanding of ICL.
Conclusion and Future Work
This research provides a compelling bridge between in-context learning and finetuning, suggesting we should view GPT as a meta-optimizer rather than a static inference engine. While the current proof relies on a simplified "relaxed linear attention," the empirical results on models up to 2.7B parameters are significant. Future work will explore if these meta-optimization behaviors intensify in massive models like GPT-4 and whether they extend to complex, multi-step reasoning tasks.
Summary updated