NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Transformers Implement Functional Gradient Descent to Learn Non-Linear Functions In Context

Xiang Cheng, Yuxin Chen, Suvrit Sra

Overview

In-context learning (ICL)—the ability of a model to learn a new task from a few examples in a prompt without weight updates—is one of the most striking properties of Large Language Models. Recent research has successfully shown that linear Transformers (without softmax) can implement gradient descent to solve linear regression tasks.

However, real-world Transformers are non-linear (using softmax attention), and the data they process is rarely linear. This paper addresses a fundamental gap: How do non-linear Transformers learn non-linear functions in context? The authors demonstrate that Transformers perform functional gradient descent—gradient descent in function space—to achieve in-context learning. By matching their non-linear attention activations (like softmax or ReLU) to the underlying kernel of the data, Transformers effectively implement iterative optimization algorithms during their forward pass.

Bridging Architecture and Learning Theory

The authors bridge the gap between Transformer architecture and classical learning theory using the framework of Reproducing Kernel Hilbert Spaces (RKHS).

  1. Functional Gradient Descent Construction: They prove that a Transformer layer can be configured to implement one step of gradient descent in function space. If the attention activation matches a specific kernel $K$, the forward pass of the Transformer is mathematically equivalent to performing kernel regression via gradient descent.
  2. Stationary Point Analysis: Beyond showing that such a configuration can exist, the authors analyze the "in-context loss" landscape. They prove that these functional gradient descent configurations are actually stationary points, suggesting that standard training (like SGD) will naturally lead a Transformer to learn these algorithms.
  3. Multi-Head Composition: They extend the theory to multi-head attention, showing that different heads can implement different kernels. By summing these in parallel, a multi-head Transformer can learn functions from a much broader class of composite kernels.

Key Insights: The Matching Principle

The study relies on synthetic datasets where the ground truth is known (e.g., $K$-Gaussian Processes), allowing for precise theoretical verification against the Bayes Optimal Estimator.

A core finding is the Matching Principle: the most efficient in-context learning occurs when the Transformer's non-linear activation "matches" the kernel that generated the data.

Performance of different Transformer activations across various data kernels, illustrating the Matching Principle.
  • Activation Superiority: As seen above, a linear Transformer is best at learning functions generated by a linear kernel, while a ReLU-activated Transformer outperforms others on ReLU-generated data.
  • Softmax as a Kernel: The standard softmax activation is closely related to the exponential kernel. While softmax includes a normalization factor that makes it slightly different from pure functional gradient descent, it remains highly effective for learning exponential-type non-linearities.
  • Algorithm Interleaving: In unconstrained settings, the Transformer doesn't just do gradient descent; it interleaves functional gradient descent with covariate transformations, effectively "preconditioning" the data to make the optimization problem easier to solve in subsequent layers.

Convergence to Bayes Optimality

The authors show theoretically and empirically that as the number of layers increases, a Transformer implementing functional gradient descent approaches the Bayes optimal predictor.

Test loss as a function of layer depth, showing convergence toward the Bayes optimal predictor.

Because each layer effectively acts as a step of functional gradient descent, deeper networks can perform more optimization steps during the forward pass. Experiments confirm that Transformers trained on non-linear data actually learn these predicted parameter configurations, with test loss steadily decreasing as layer depth increases.

Limitations and Future Work

While this paper provides a robust theoretical foundation for non-linear ICL, several open questions remain:

  • Global Optimality: The paper identifies these algorithmic configurations as stationary points but does not yet provide a proof of global convergence (a guarantee that training will always find these points from any initialization).
  • Real-World Data: The results are derived using Gaussian Processes with continuous covariates. Extending this to the discrete, categorical nature of natural language remains a significant challenge.
  • Deep Composition: While parallel heads compose kernels additively, the way kernels compose sequentially across many layers is a complex area for future study.
  • Efficiency Trade-offs: The authors suggest softmax might be more "iteration efficient" (requiring fewer layers) but less "statistically efficient" (requiring more data) than other kernels, a trade-off that warrants further investigation.

Summary updated

BibTeX
@inproceedings{cheng2024transformers,
  author = {Xiang Cheng and Yuxin Chen and Suvrit Sra},
  title = {{Transformers Implement Functional Gradient Descent to Learn Non-Linear Functions In Context}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=ah1BlQcLv4},
}
Figure