Skip to main content
QUICK REVIEW

[Paper Review] Transformers learn in-context by gradient descent

Johannes von Oswald, Eyvind Niklasson|arXiv (Cornell University)|Dec 15, 2022
Domain Adaptation and Few-Shot LearningComputer Science89 citations
TL;DR

The paper shows that in-context learning in Transformers can be mechanistically understood as gradient-descent updates, proving that self-attention layers can implement GD steps on in-context data, and that adding MLPs enables nonlinear regression via gradient-based learning on deep representations.

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 .

Motivation & Objective

  • Motivate understanding of in-context learning mechanisms in Transformers.
  • Show equivalence between linear self-attention updates and one-step gradient descent on linear regression.
  • Demonstrate that stacking attention layers enables iterative GD-like updates and curvature correction (GD++).
  • Explain how MLPs enable nonlinear regression through gradient descent on deep representations.
  • Discuss connections to meta-learning, fast weights, and the induction-head mechanism.

Proposed method

  • Derive a weight construction that makes a single linear self-attention step equivalent to a gradient-descent update on a linear regression loss.
  • Empirically compare trained linear self-attention layers with the GD construction on linear regression tasks to assess alignment.
  • Extend to multi-layer self-attention and show GD-like behavior with iterative data transformations (GD++) and residual curvature corrections.
  • Show that incorporating MLPs in Transformers enables solving nonlinear regression tasks by gradient descent on deep representations (kernel regression perspective).
  • Investigate token constructions and data transformations to demonstrate how Transformers can implement in-context learning via gradient-based updates within forward passes.

Experimental results

Research questions

  • RQ1Can a single linear self-attention layer implement a gradient-descent step on a linear regression task?
  • RQ2Do trained Transformers with self-attention layers converge to GD-like solutions on linear regression data?
  • RQ3How do multiple attention layers and MLPs affect the ability of Transformers to perform gradient-descent-based updates (GD++, nonlinear tasks)?
  • RQ4Can in-context learning in Transformers be understood as learning an algorithm (mesa-optimization) within the forward pass?
  • RQ5What is the role of token construction and data transformations in enabling in-context learning without explicit weight updates external to the forward pass?

Key findings

  • A one-head linear self-attention layer can enact a gradient-descent-like update on training data for linear regression.
  • Trained linear self-attention layers closely align with a constructed GD update, including similar predictions and sensitivities.
  • Stacks of self-attention layers can implement iterative curvature correction (GD++) and outperform plain GD on linear tasks.
  • Incorporating MLPs allows Transformers to solve nonlinear regression by performing gradient descent on deep representations, effectively enabling kernel-like regression.
  • Transformers can yield in-context learning via learned data transformations and task-specific representations, aligning with the concept of mesa-optimization and fast weights.
  • The architecture can replicate or approximate gradient-based learning dynamics across in-distribution and out-of-distribution tasks.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.