# Paper: Transformers learn in-context by gradient descent

The paper argues that auto-regressive transformers implement in-context learning via gradient-based optimization on in-context data. 

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/c40207a7c44db45cdb0995a2c55ebff8cd3f3a6b7e85e7e4.png)The authors start by pointing out that with a single linear self-attention (LSA) layer (that is, no softmax), a Transformer can implement one step of gradient descent on the l2 regression loss (a fancy way of saying w -= LR * (w x-y)x^T), and confirm this result empirically. They extend this result by showing that an N-layer LSA-only transformer is similar to N-steps of gradient descent for small linear regression tasks, both in and out of distribution. They also find that the results pretty much hold with softmax self-attention (which isn’t super surprising given you can make a softmax pretty linear).  
![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/f06805274a889b0996376806bce48b6e50558b92afacd976.png)  
Next, they show empirically that the forward pass of a small transformer with MLPs behaves similarly to an meta-learned MLP + one step of gradient descent on a toy non-linear regression task, again in terms of both in-distribution and OOD performance.   
![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/1fe01319a94c71e554f54fa7365d9ba21be806ef43e114b6.png)  
They then show how you can interpret an induction head as a single step of gradient descent, and provide circumstantial evidence that this explains some of the in-context learning observed in Olsson et al 2022. Specially, they show that 1) a two layer attention-only transformers converge to loss consistent with one step of GD on this task, and 2) the first layer of the network learns to copy tokens one sequence position over in the first layer, prior to the emergence of in-context learning.   
![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/94bb844883bbc8e646c3f4e00e4b5e188ea3ad61027f5eaf.png)

(EDIT:) davidad says below: 

> this is strong **empirical evidence that mesa-optimizers are real in practice**

Personally, while I think you could place this in the same category as papers like [RL^2](https://arxiv.org/abs/1611.02779) or [In-context RL with Algorithmic Distillation](https://arxiv.org/abs/2210.14215), which also show mesa optimization, I think the more interesting results are the mechanistic ones -- i.e., that some forms of mesa optimization in the model seem to be implemented via something like gradient descent.

(EDIT 2) nostalgebraist pushes back on this claim [in this comment](https://www.lesswrong.com/posts/firtXAWGdvzXYAh9B/paper-transformers-learn-in-context-by-gradient-descent?commentId=qxkfbTqAvKdJuF4rB#qxkfbTqAvKdJuF4rB):

> Calling something like this an optimizer strikes me as vacuous: if you don't require the ability to adapt to a change of objective function, you can always take any program and say it's "optimizing" some function.  Just pick a function that's maximal when you do whatever it is that the program does.
> 
> It's *not* vacuous to say that the transformers in the paper "implement gradient descent," as long as one means they  "implement \[gradient descent on $L_2$ loss\]" rather than "implement \[gradient descent\] on \[$L_2$ loss\]."  They don't implement general gradient descent, but happen to coincide with the gradient step for $L_2$ loss.

* * *

(Nitpick: I do want to push back a bit on their claim that they've "mechanistically understand the inner workings of optimized Transformers that learn in-context", since they've only really looked at the mechanism of how single layer attention-only transformers perform in-context learning. )