TL;DR
Recent work showed that transformers can perform in-context supervised learning by implicitly executing gradient descent inside their forward pass. This paper extends that principle to reinforcement learning (RL), proving mathematically and showing experimentally that transformers can implement temporal difference (TD) learning—a fundamentally non-gradient, semi-gradient algorithm—to evaluate policies in-context purely through forward inference.
What Problem Does This Paper Address?
In-context learning allows models to solve new tasks at inference time without modifying their weights. While researchers have shown that transformers can simulate steps of gradient descent on supervised regression prompts, intelligence also requires sequential decision-making.
In RL, estimating the value of states is traditionally tackled with Temporal Difference (TD) learning. Unlike supervised regression, TD is not gradient descent; it is a semi-gradient method that bootstraps from future value estimates. Can transformers execute TD learning during a forward pass? If so, does this capability emerge naturally when models are trained across multiple tasks?
Methods
The authors study linear and standard softmax self-attention architectures on Markov Reward Processes (MRPs):
-
Constructive Proofs (Expressivity): The authors construct explicit weight matrices showing that an $L$-layer linear transformer can simulate $L$ steps of preconditioned batch TD(0). By adapting attention masks and prompt structures, they extend this construction to:
-
Residual Gradient (RG) methods.
- TD($\lambda$) with eligibility traces, achieved via a decay-weighted attention mask ($M^{\text{TD}(\lambda)}$).
- Average-Reward TD, using two-head linear attention and an over-parameterized "memory" token in the prompt.
-
Multi-Task TD Pre-Training: Instead of supervised pre-training via maximum likelihood, the authors propose training transformer parameters across diverse random MRP tasks using a multi-task TD objective.
-
Theoretical Invariance Analysis: For a single-layer linear transformer, the authors prove that the constructed TD parameters lie within an invariant set of the multi-task TD training dynamics under mild symmetry assumptions.
-
Evaluation Metrics: To verify algorithm emergence, the authors examine element-wise weight matrix convergence as well as functional alignment metrics: Value Difference (VD), Implicit Weight Similarity (IWS), and Sensitivity Similarity (SS).
Datasets
The study evaluates models on synthetic task distributions based on Boyan's chain (a standard diagnostic benchmark in RL), adapted into an infinite-horizon MRP:
- Default setup: Chains of $m = 10$ states with $d = 4$ feature dimensions, context length $n = 30$, and discount factor $\gamma = 0.9$.
- Larger-scale setup: Chains with $m = 20$ states, feature dimension $d = 8$, and context length $n = 60$.
- Task variants:
- Non-representable value functions (random rewards and features).
- Representable value functions (features guaranteed to linearly span the true value function).
No external real-world datasets were used.
Key Insights and Ideas
- Beyond Gradient Descent: In-context algorithm execution is not limited to gradient descent on error objectives. Transformers are flexible enough to implement bootstrapped semi-gradient fixed-point iterations.
- Weights Naturally Converge to the Construction: When trained with multi-task TD across randomly generated environments, the weights of autoregressive linear transformers closely mirror the exact hand-constructed matrices from the mathematical proof (e.g., specific block traces converging to $\pm d$).
- Architecture Dictates Dynamics:
- For single-layer transformers ($L = 1$), the model settles into a specialized degenerate solution where historical weights are zero.
- For multi-layer transformers ($L \ge 2$), it reliably settles into the general iterative TD formulation.
- TD Beats Residual Gradients In Practice: Even though transformers can express naive Residual Gradient updates, multi-task training naturally learns TD(0), mirroring the empirical superiority of TD observed in classic RL literature.
Key Results
- Parameter Convergence: Across 30 random seeds, trained weight matrices $P_0$ and $Q_0$ matched theoretical predictions: the trace of the upper-left $d \times d$ block of $Q_0$ approached $-d$ ($-4$), and the upper-right block approached $+d$ ($+4$), while non-critical elements stayed near zero.
- Functional Alignment:
- Implicit Weight Similarity (cosine similarity between optimal linear fit and batch TD weights) and Sensitivity Similarity both rapidly approached $\approx 1.0$.
- Value Difference approached 0 within a few hundred training MRPs.
- Sequential and Softmax Transformers: Non-autoregressive sequential layers also successfully converged functionally to batch TD(0). Standard softmax-based attention likewise achieved high implicit weight similarity and low value error, displaying enhanced sensitivity similarity when the ground-truth value function was linear/representable.
Limitations and Future Work
- Policy Evaluation Only: The framework addresses value prediction (MRPs/policy evaluation) but does not yet extend to active control or policy improvement (MDPs with action selection).
- Theoretical Scope: Invariant set convergence proofs are currently limited to single-layer linear transformers; theoretical guarantees for multi-layer models and nonlinear softmax attention remain open.
- Synthetic Scale: Experiments rely on small synthetic Boyan chain environments running on CPUs; verifying scalability to complex, high-dimensional RL domains is left for future research.
Summary updated