Abstract
Transformers are remarkably good at in-context learning (ICL) -- learning from demonstrations without parameter updates -- but how they perform ICL remains a mystery. Recent work suggests that Transformers may learn in-context by internally running Gradient Descent, a first-order optimization method. In this paper, we instead demonstrate that Transformers learn to implement higher-order optimization methods to perform ICL. Focusing on in-context linear regression, we show that Transformers learn to implement an algorithm very similar to Iterative Newton's Method, a higher-order optimization method, rather than Gradient Descent. Empirically, we show that predictions from successive Transformer layers closely match different iterations of Newton's Method linearly, with each middle layer roughly computing 3 iterations. In contrast, exponentially more Gradient Descent steps are needed to match an additional Transformers layer; this suggests that Transformers have an comparable rate of convergence with high-order methods such as Iterative Newton, which are exponentially faster than Gradient Descent. We also show that Transformers can learn in-context on ill-conditioned data, a setting where Gradient Descent struggles but Iterative Newton succeeds. Finally, we show theoretical results which support our empirical findings and have a close correspondence with them: we prove that Transformers can implement $k$ iterations of Newton's method with $\mathcal{O}(k)$ layers.
Introduction
In-Context Learning (ICL) is the mechanism that allows Large Language Models to solve new tasks from a few examples in a prompt without any parameter updates. While the prevailing hypothesis has been that Transformers internally simulate Gradient Descent (GD), this paper investigates whether they actually implement more sophisticated second-order optimization methods. GD is often slow to converge and sensitive to the conditioning of data. In contrast, second-order methods like Newton’s Method use curvature information to find solutions exponentially faster. For linear regression tasks, we demonstrate that Transformers learn an algorithm mimicking Iterative Newton’s Method.
Methods
The authors investigate this by training GPT-2 style Transformers to perform in-context linear regression. The model processes a sequence of input-output pairs to predict the output for a new query.
To identify the underlying algorithm, the Transformer's internal predictions at each layer are compared to several classical optimization algorithms, including first-order methods (GD, Online GD) and second-order methods (Iterative Newton, BFGS, L-BFGS). Similarity is measured using two metrics: Similarity of Errors, which compares residual patterns, and Similarity of Induced Weights, which fits implicit weight vectors to the Transformer’s predictions.
Key Results and Insights
Superlinear Convergence and Algorithmic Similarity
Empirical results show that Transformer error drops at a superlinear rate across layers, closely matching the behavior of Iterative Newton’s Method. The "Similarity of Errors" between the Transformer and Newton’s Method reached a high score of 0.991, significantly outperforming first-order methods.
As shown in the figure above, there is a linear relationship between Transformer layers and Newton iterations, where each middle layer (layers 3-9) computes roughly 3 Newton iterations. In contrast, matching the progress of a single additional Transformer layer requires an exponential increase in Gradient Descent steps. This suggests the Transformer is effectively "skipping" the slow, incremental steps characteristic of first-order optimization.
Robustness to Ill-Conditioning
Gradient Descent typically slows down on ill-conditioned data where features have vastly different scales. Transformers, however, maintain high performance in these settings, mirroring second-order methods that naturally correct for data curvature. This capability appears linked to the Transformer's attention mechanism, which can attend to the entire history of examples to compute the complex matrix inversions required for higher-order optimization.
Theoretical Support
Complementing the empirical findings, the authors provide a theoretical construction proving that a Transformer with $k+O(1)$ layers can mathematically implement $k$ iterations of Newton’s Method. This construction requires a hidden dimension of only $O(d)$, where $d$ is the problem dimension. Ablation studies confirmed this, showing that models below a certain hidden size threshold fail to mimic the optimal solution.
Conclusion
This study provides compelling evidence that Transformers perform ICL by implementing higher-order optimization methods like Iterative Newton's Method. While the current focus is on linear regression, the superlinear convergence observed suggests a fundamental efficiency in Transformer architectures that may extend to more complex, non-linear tasks. Future work will explore these mechanisms in classification and noisier real-world prompt settings.
Summary updated