In-context Reinforcement Learning with Algorithm Distillation

Michael Laskin, Luyu Wang, Junhyuk Oh, Emilio Parisotto, Stephen Spencer, Richie Steigerwald, Dj Strouse, Steven Stenberg Hansen, Angelos Filos, Ethan Brooks, maxime gazeau, Himanshu Sahni, Satinder Singh, Volodymyr Mnih

Algorithm Distillation (AD) transforms reinforcement learning (RL) into a large-scale sequence modeling problem. By training a causal transformer on the entire learning histories of source RL algorithms, AD creates an agent capable of "in-context" reinforcement learning. This means the agent improves its policy through trial and error during evaluation without any further weight updates.

The Challenge with Current Distillation

Traditional RL is often data-inefficient and relies on complex, unstable gradient updates to adapt to new tasks. While recent "Policy Distillation" methods (like Decision Transformer) successfully distill expert behaviors into transformers, these models are typically static. They perform well on seen tasks but lack an "improvement operator" to learn or adapt when encountering novel environments.

Current distillation methods train on expert data or small snippets of experience that don't demonstrate progress. The challenge is moving beyond distilling a fixed policy to distilling the RL algorithm itself.

Algorithm Distillation (AD)

The authors propose treating the process of learning as a long-range sequential prediction task. The AD method consists of two primary phases:

  1. Data Generation: A source RL algorithm (e.g., A3C or DQN) is trained from scratch on thousands of different tasks. Crucially, the entire learning history is saved—every observation, action, and reward from the initial random steps to the final expert behavior.
  2. Model Training: A causal transformer is trained via imitation learning to autoregressively predict the actions taken by the source algorithm.
Algorithm Distillation Method

The Secret Sauce: Unlike previous methods that use short context windows, AD uses a context window large enough to span multiple episodes (across-episodic context). Because the training data shows the source agent improving over time, the transformer is forced to learn the underlying "learning rule" to predict actions accurately. It learns that if a previous action led to a reward, it should be more likely to predict that action again in the future.

Key Insights

  • Learning as Sequence Modeling: Accurately predicting the next move of a learning agent effectively internalizes that agent's learning algorithm.
  • The Importance of "Suboptimal" Data: Distilling only expert data (Expert Distillation) fails because the model never learns how to explore or recover from mistakes. AD learns from the entire trajectory of improvement.
  • In-Context RL: During evaluation, the transformer's weights are frozen. "Learning" happens entirely within the activations of the transformer as it attends to its own recent history of successes and failures.
  • Implicit Reward Maximization: AD does not condition on a "target return." It learns to maximize rewards implicitly by imitating a source algorithm that was itself maximizing rewards.

Environments and Key Results

The authors evaluated AD on several environments designed to test exploration and memory, including Adversarial Bandits, Dark Room (2D grid worlds), Dark Key-to-Door (combinatorial tasks), and DMLab Watermaze (complex 3D pixel-based navigation).

Performance across environments
  • Superior Adaptation: AD consistently outperformed "Expert Distillation" (ED) baselines. While ED agents often got stuck in loops or failed to explore, AD agents successfully navigated environments and found goals entirely in-context.
  • Enhanced Data Efficiency: Surprisingly, AD often became a more data-efficient learner than the source algorithm that generated its training data. In the Watermaze task, AD reached high performance in fewer environment steps than the original DQN agent.
  • Scaling with Capacity: Performance improved significantly as the transformer's depth and embedding dimensions increased, suggesting that "learning to learn" benefits from larger model scales.
  • Context Length is Critical: In-context RL only emerges when the context window is long enough to capture multiple episodes, allowing the model to observe the consequences of its previous actions.

Limitations and Future Work

  • Context Window Constraints: Transformers have quadratic complexity relative to context length. For tasks requiring millions of steps to learn, current architectures may struggle to fit the necessary history into memory.
  • Storage Requirements: Saving the full learning histories of thousands of RL runs requires massive amounts of disk space.
  • Domain Specificity: While AD generalizes to new tasks within a domain, cross-domain distillation remains unproven.
  • Future Directions: Exploring more efficient sequence models (like State Space Models or Linear Transformers) could help handle the extremely long histories required for more complex RL tasks.

Summary updated

BibTeX
@inproceedings{laskin2023context,
  author = {Michael Laskin and Luyu Wang and Junhyuk Oh and Emilio Parisotto and Stephen Spencer and Richie Steigerwald and DJ Strouse and Steven Stenberg Hansen and Angelos Filos and Ethan Brooks and maxime gazeau and Himanshu Sahni and Satinder Singh and Volodymyr Mnih},
  title = {{In-context Reinforcement Learning with Algorithm Distillation}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=hy0a5MMPUv},
}
Figure