Open Interpretability Models Contributors Research Posts About

Transformers Learn In-Context by Gradient Descent

Source

Citation. Johannes von Oswald (ETH Zürich and Google Research), Eyvind Niklasson, Ettore Randazzo, João Sacramento (ETH Zürich), Alexander Mordvintsev, Andrey Zhmoginov and Max Vladymyrov (Google Research). "Transformers Learn In-Context by Gradient Descent". Proceedings of the 40th International Conference on Machine Learning (ICML 2023), PMLR 202. arXiv 2212.07677 [cs.LG]: v1 15 December 2022, v2 31 May 2023. We read v2. Paper. Notebooks for the main experiments are at google-research/self-organising-systems. The tasks are synthetic, so there is no dataset to link. A Swiss National Science Foundation Ambizione grant (PZ00P3 186027) and an ETH Research Grant (ETH-23 21-1) supported Sacramento. The paper gives no statement about tools or help from an LLM. The ICML year agrees with the v2 date.

Status. Planned. #4782 would use it in its synthesis on weight-space programming and fast weights. The paper is the clearest case where a reading of weights alone names the algorithm that a trained layer runs. It also shows the conditions that this reading needs, and the registry's models meet none of them. The page proposes one small follow-up issue.

Read. We read the arXiv v2 PDF on 2026-11-19, as text that pdftotext took from it. We have the source's own text for the abstract, §1 to §5, the acknowledgements and the references. We also have Appendices A.1 to A.12, which hold the constructions, the softmax and LayerNorm results, phase transitions and the hyperparameters. We also read the arXiv abstract page for the version history. We did not see the 16 figures as images. Most results are only in the figures, so this page gives the text's own words for them and no number read off a plot. Some equations came out of the extraction with their layout broken. We read them against the text around them. A model (Claude Opus 5.5) did the reading itself, with no subagent.

Summary

Claim. One linear self-attention layer can do the same update as one step of gradient descent on a squared-error regression loss over the in-context data. Self-attention-only transformers trained on such regression tasks find this construction, or a close variant. So the trained model learns by gradient descent in its forward pass, a "mesa-optimizer". The authors hypothesise that in-context learning in language models comes from similar mechanisms.

Method.

Findings, as the source reports them.

Setting Result
One trained LSA layer against one GD step (§3, Figure 2) "excellent agreement between the two models over a wide range of hyperparameters". Figure 2 shows the mean and std, or single runs, of 5 seeds
The same, in weight space (§3, Appendix A.4, Figure 9) After one scalar adjustment, the trained weights show the construction. The interpolated weights give "identical loss"
The trained layer, applied again and again (Appendix A.6) Both GD and the layer diverge quickly. With a damping λ = 0.75, both lower the loss steadily over 50 steps
Recurrent two-layer LSA (§3, Figure 3a) It beats plain GD, and "realigns perfectly with GD++"
Five-layer LSA, no weight tying (§3, Figure 3b) "almost perfect alignment" with GD++, which has "10 additional parameters". Interpolation fails with the simple scale adjustment, and works "only for some seeds" (Appendix A.3)
Sine-wave regression, MLP plus LSA (§3, Figure 4, Appendix A.7) Amplitude from U(0.1, 5), phase from U(0, π), N = 10, a 40-dimensional embedding and a 160-unit GELU MLP. The text reports "strong alignment" with a meta-learned MLP plus one GD step
Two layers on alternating x and y tokens (§4, Figure 5) The loss matches one GD step, "not two", and training takes "an order of magnitude longer". The first layer becomes sensitive to token j + 1 just before the loss drops. Training worked only with softmax in the first layer
One softmax head (Appendix A.9, Figure 12) It cannot match GD. Two heads "significantly" reduce the gap, and their off-diagonal QK terms cancel in the sum (Figure 11)
LayerNorm before attention (Appendix A.9, Figure 14) Worse loss than GD and lower alignment. "Including more heads does not help substantially"
Training seeds (Appendix A.11, Figure 15) With one head, some of 10 seeds show long phases of almost no progress before a sudden drop. Two heads remove this
A fixed set of 8,192 tasks, with an MLP (Appendix A.11) Grokking-like curves, with a final test loss near the GD loss of 0.2
A fixed set of 128 tasks (Appendix A.12, Figure 16) The trained layer still aligns "remarkable well" with the construction

Where the claims go beyond the evidence

The authors state several limits themselves. These are the restriction to small models and regression, the missing noisy and logistic cases, and the failed interpolation for deep models. They also say that their mechanism may be "one of many possible methods giving rise to in-context learning".

Use in this repo