# Transformers learn in-context by gradient descent

**Authors:** Johannes von Oswald, Eyvind Niklasson, Ettore Randazzo, Jo\~ao, Sacramento, Alexander Mordvintsev, Andrey Zhmoginov, Max Vladymyrov

arXiv: 2212.07677 · 2023-06-01

## TL;DR

This paper reveals that Transformers learn in-context by effectively performing gradient descent during their forward pass, providing a mechanistic understanding of in-context learning especially in regression tasks.

## Contribution

It demonstrates the equivalence between self-attention transformations and gradient descent, and shows how trained Transformers act as mesa-optimizers learning models via gradient descent.

## Key findings

- Transformers can be constructed to mimic gradient descent on regression tasks.
- Trained Transformers learn iterative curvature correction to improve regression performance.
- The study links in-context learning mechanisms to gradient descent and induction-head functions.

## Abstract

At present, the mechanisms of in-context learning in Transformers are not well understood and remain mostly an intuition. In this paper, we suggest that training Transformers on auto-regressive objectives is closely related to gradient-based meta-learning formulations. We start by providing a simple weight construction that shows the equivalence of data transformations induced by 1) a single linear self-attention layer and by 2) gradient-descent (GD) on a regression loss. Motivated by that construction, we show empirically that when training self-attention-only Transformers on simple regression tasks either the models learned by GD and Transformers show great similarity or, remarkably, the weights found by optimization match the construction. Thus we show how trained Transformers become mesa-optimizers i.e. learn models by gradient descent in their forward pass. This allows us, at least in the domain of regression problems, to mechanistically understand the inner workings of in-context learning in optimized Transformers. Building on this insight, we furthermore identify how Transformers surpass the performance of plain gradient descent by learning an iterative curvature correction and learn linear models on deep data representations to solve non-linear regression tasks. Finally, we discuss intriguing parallels to a mechanism identified to be crucial for in-context learning termed induction-head (Olsson et al., 2022) and show how it could be understood as a specific case of in-context learning by gradient descent learning within Transformers. Code to reproduce the experiments can be found at https://github.com/google-research/self-organising-systems/tree/master/transformers_learn_icl_by_gd .

## Full text

_Full body text omitted from this summary view._ Fetch the complete paper as Markdown: https://tomesphere.com/paper/2212.07677/full.md

## Figures

116 figures with captions in the complete paper: https://tomesphere.com/paper/2212.07677/full.md

## References

56 references — full list in the complete paper: https://tomesphere.com/paper/2212.07677/full.md

---
Source: https://tomesphere.com/paper/2212.07677