Transformers as Decision Makers: Provable In-Context Reinforcement Learning via Supervised Pretraining

Licong Lin, Yu Bai, Song Mei

Introduction

While Transformers have revolutionized natural language processing, their remarkable success in In-Context Reinforcement Learning (ICRL) has remained largely empirical. In the ICRL setting, a model is "prompted" with a history of states, actions, and rewards from an unseen environment and must decide on the next best action—effectively performing reinforcement learning on the fly without any weight updates.

Existing methods, such as Algorithm Distillation and Decision-Pretrained Transformers (DPT), have demonstrated that pretraining on offline trajectories yields strong in-context policies. However, several fundamental questions have remained unanswered: What specific RL algorithms can a Transformer actually implement within its layers? How does the distribution mismatch in offline training data affect the resulting performance? And crucially, can we provide provable guarantees, such as regret bounds, for these models?

This paper provides the first rigorous theoretical framework proving that Transformers can learn to act as near-optimal RL algorithms through supervised pretraining, efficiently implementing complex strategies like LinUCB and Thompson Sampling entirely within their forward pass.

Theoretical Framework and Methods

The authors establish a comprehensive theoretical foundation to analyze supervised pretraining for ICRL, focusing on two primary training paradigms:

  1. Algorithm Distillation: Training the Transformer to imitate a specific RL algorithm (the "expert").
  2. Decision-Pretrained Transformers (DPT): Training the Transformer to predict optimal actions based on the environment state.

Imitation as Conditional Expectation

A key technical innovation of the paper is proving that a realizable Transformer trained with a log-likelihood loss will converge to the conditional expectation of the expert algorithm, given the observed history. The generalization error of this imitation scales with the model's capacity (its covering number) and a critical factor known as the distribution ratio ($R$). This ratio measures the divergence between the algorithm used to collect the offline data and the expert algorithm. If the training data is vastly different from what the expert would do, the Transformer requires significantly more data to generalize effectively.

Mathematical Expressivity

To prove that Transformers can function as RL algorithms, the authors demonstrate how Transformer layers (specifically utilizing ReLU attention for mathematical tractability) can implement complex mathematical operations:

  • Accelerated Gradient Descent (AGD): Used to solve the ridge regression problems required for the LinUCB algorithm.
  • Padé Decomposition: A method to compute matrix square roots, which is essential for implementing Thompson Sampling.

The paper reveals a concept of "algorithmic depth," showing that RL algorithms are not merely memorized but actively executed. For instance, the number of Transformer layers ($L$) directly relates to the number of optimization steps the model can perform in-context (e.g., $L \approx \sqrt{T}$ for LinUCB).

Key Insights and Results

A major breakthrough in this work is the proof that pretraining on optimal actions (DPT) naturally leads the Transformer to implement Thompson Sampling (posterior sampling). By observing a diverse set of environments during training, the model learns to act according to the posterior distribution of the current environment.

The authors derive provable regret bounds for these pretrained Transformers that match the near-optimal bounds of the algorithms they imitate. For example, they achieve $\tilde{O}(d\sqrt{T})$ for stochastic linear bandits.

While the paper is primarily theoretical, the authors validate their findings using simulations in standard RL environments, including Stochastic Linear Bandits ($d=5, A=10$), Multi-armed Bernoulli Bandits, and Tabular MDPs. Training GPT-2 style architectures on 50K to 100K trajectories, the simulations show that Transformers closely track the regret curves of LinUCB and Thompson Sampling, significantly outperforming simpler baselines like empirical averaging.

Limitations and Future Directions

Despite these strong theoretical guarantees, the authors acknowledge certain limitations. First, Transformers exhibit Out-of-Distribution (OOD) sensitivity; they struggle to generalize to environments with reward distributions or horizons that differ significantly from the pretraining data. Second, the model's performance is currently capped by the "expert ceiling"—it can only be as good as the expert it imitates. The authors suggest that online fine-tuning, which allows the Transformer to interact and learn beyond its pretraining, is a vital next step to surpass algorithmic experts. Finally, while the theoretical proofs rely on ReLU attention for mathematical tractability, most real-world models use softmax attention. Bridging this specific theoretical gap remains an open challenge for future research.

Summary updated

BibTeX
@inproceedings{lin2024transformers,
  author = {Licong Lin and Yu Bai and Song Mei},
  title = {{Transformers as Decision Makers: Provable In-Context Reinforcement Learning via Supervised Pretraining}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=yN4Wv17ss3},
}
Figure