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

Transformers Can Learn Temporal Difference Methods for In-Context Reinforcement Learning

Jiuqi Wang, Ethan Blaser, Hadi Daneshmand, Shangtong Zhang

Introduction

This paper demystifies the phenomenon of In-Context Reinforcement Learning (ICRL) by proving that transformers can implement Temporal Difference (TD) learning algorithms entirely within their forward pass. Through both theoretical construction and empirical multi-task pretraining, the authors show that transformers naturally discover and execute TD(0) for policy evaluation without requiring any parameter updates at inference time.

The Mystery of In-Context RL

In traditional Reinforcement Learning (RL), an agent learns to solve new tasks by updating its neural network weights through trial and error. However, recent research has revealed a surprising phenomenon: In-Context Reinforcement Learning (ICRL). After certain pretraining, a transformer with frozen weights can be dropped into a new environment, take a sequence of states, actions, and rewards as a prompt (the context), and successfully learn to solve the task purely through its forward pass.

In-Context RL Setup and Performance

The prevailing hypothesis is that the transformer's forward pass is secretly running a standard RL algorithm. While previous work has shown this is possible when the model is explicitly trained to clone the behavior of an existing RL algorithm (supervised pretraining), it has remained a mystery how and why this algorithmic capability emerges from standard reinforcement pretraining—where the model is only told to maximize rewards or minimize value errors, with no instructions on how to do it. This paper aims to fully "white-box" this mechanism, focusing specifically on policy evaluation (estimating the value of a given state).

Methodology

The authors tackle the problem using a mix of theoretical construction, empirical training, and dynamical systems analysis:

  • Constructive Proofs: They mathematically construct the exact weights of a linear transformer to prove that its layer-by-layer forward pass is mathematically equivalent to the iteration-by-iteration updates of several RL algorithms, including batch TD(0), Residual Gradient, TD($\lambda$), and Average-Reward TD.
  • Multi-Task TD Pretraining: To see if these algorithms actually emerge in practice, they pretrain a randomly initialized linear transformer on thousands of randomly generated Markov Reward Processes (MRPs). The model is trained using a standard non-linear TD loss to predict state values.
  • Invariant Set Analysis: To explain why the model learns TD(0), they analyze the expected training dynamics of a 1-layer transformer, proving that the specific network weights that implement TD(0) form an "invariant set" under the pretraining algorithm (meaning once the weights enter this configuration, they stay there).

Because the paper focuses on meta-learning and ICRL, the authors procedurally generate policy evaluation tasks rather than using static offline datasets. They utilize Boyan's Chain, a canonical 10-state RL environment, generating thousands of variations by randomizing transition probabilities, rewards, and state features. To test on environments with more complex dynamics, they also generate tasks based on the classic CartPole environment, using random policies and tile coding to generate continuous state features.

Key Insights

  • Transformers are Secretly RL Algorithms: This is the first time a pretrained agent neural network for ICRL has been fully white-boxed. The attention mechanism in a transformer can be perfectly mapped to the preconditioned semi-gradient updates used in Temporal Difference learning.
  • Natural Emergence of TD(0): Even though the transformer's architecture is expressive enough to implement other algorithms (like Residual Gradient), multi-task TD pretraining naturally drives the weights to implement TD(0). This elegantly mirrors the historical consensus in the RL community, which settled on TD over naive residual gradients after extensive trial and error.
  • RNNs Fail Where Transformers Succeed: The authors investigate whether Recurrent Neural Networks (RNNs) can also perform in-context TD. They find that standard RNNs fundamentally lack the algorithmic capacity to compute the necessary TD errors in their forward pass, highlighting why transformers are uniquely suited for ICRL.
  • Attention as Optimization: The paper reinforces an emerging theme in machine learning: self-attention layers can act as optimization steps (like gradient descent or TD updates) over the context provided in the prompt.

Key Results

  • Perfect Weight Convergence: After pretraining on 4,000 Boyan's Chain MRPs, the learned weights of a 3-layer linear transformer converged almost exactly to the theoretical matrices the authors constructed for batch TD(0).
Weight Convergence after 4000 MRPs
  • Functional Equivalence: When comparing the trained transformer's outputs to an exact batch TD algorithm, the transformer achieved near-perfect Implicit Weight Similarity ($\approx 1.0$) and Sensitivity Similarity ($\approx 1.0$), with the Value Difference dropping to near zero.
  • Generalization to Softmax: While the theoretical proofs rely on linear attention, empirical tests showed that standard softmax-based transformers also learn to behave similarly to linear batch TD, especially when the true value function is perfectly representable by the features.

Limitations and Future Work

The paper strictly analyzes policy evaluation (estimating value functions) and does not address full RL control, where the agent must also select actions and balance exploration versus exploitation. Furthermore, the mathematical proofs rely on linear attention and make specific statistical assumptions about the task distributions that may not hold in all real-world scenarios. The empirical evaluations are also limited to relatively simple environments like Boyan's Chain and CartPole.

For future work, the authors suggest extending this theoretical framework to control tasks, proving strict convergence guarantees for the invariant set, and scaling the empirical validation to high-dimensional environments like Atari or DeepMind Lab.

Summary updated

BibTeX
@inproceedings{wang2025transformers,
  author = {Jiuqi Wang and Ethan Blaser and Hadi Daneshmand and Shangtong Zhang},
  title = {{Transformers Can Learn Temporal Difference Methods for In-Context Reinforcement Learning}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=Pj06mxCXPl},
}
Figure