Generalization to New Sequential Decision Making Tasks with In-Context Learning

Sharath Chandra Raparthy, Eric Hambro, Robert Kirk, Mikael Henaff, Roberta Raileanu

Introduction

In the realm of Large Language Models (LLMs), "In-Context Learning" (ICL) is a well-known superpower: by providing a model with a few examples of a new task in its prompt, it can adapt and solve the task without requiring any gradient updates. However, translating this capability to Reinforcement Learning (RL) and sequential decision-making has proven notoriously difficult.

Standard architectures, such as the Decision Transformer, typically train on single, isolated trajectories. While these models can generalize to new levels of a previously seen game, they usually fail when dropped into a fundamentally new game with different rules, rewards, and dynamics. The challenge is twofold: sequential tasks have a very low tolerance for error (one wrong move can lead to an unrecoverable state), and naive training regimens do not force the model to actively "look" at its context window to deduce the current task's underlying logic.

Recent research demonstrates that Transformers can achieve ICL for sequential decision-making, allowing agents to solve entirely new, unseen tasks zero-shot or few-shot. By training on "bursty" datasets—where the model observes multiple trajectories of the same task within a single context window—the agent learns to use provided demonstrations as a guide to navigate complex environments.

Methods: Multi-Trajectory Sequence Training

To overcome the limitations of single-trajectory training, the authors propose a shift toward multi-trajectory sequence training.

  1. Sequence Construction: Instead of feeding the Transformer one episode at a time, multiple episodes (trajectories) are stacked into a single, long sequence.
  2. Trajectory Burstiness ($p_b$): This is the critical "secret sauce" of the approach. The researchers ensure that the training data is "bursty," meaning a single sequence has a high probability ($p_b$) of containing at least two trajectories from the exact same task level. This structural choice forces the model to realize that the information needed to solve the current trajectory is embedded in the previous trajectories within its context window.
Sequence construction illustrating trajectory burstiness ($p_b$), where multiple trajectories from the same level are grouped to encourage in-context learning.
  1. Architecture: The system employs a standard Causal Transformer (GPT-style). The model is trained via imitation learning to predict the next action given the current state and the history of previous states and actions.
  2. No Explicit Rewards: Interestingly, the authors found that including explicit reward tokens was not strictly necessary for generalization. The model successfully learned the "how-to" of a task purely from the state-action transitions present in expert demonstrations.

Datasets and Environments

The study evaluates this approach using two diverse and challenging RL benchmarks:

  • MiniHack: A grid-world sandbox based on the game NetHack. The model was trained on 12 tasks (such as MultiRoom and LavaOpen) and tested on 4 to 8 completely unseen tasks (like Labyrinth and Memento).
  • Procgen: A suite of 16 procedurally generated arcade games. The model trained on 12 games (e.g., Bigfish, Chaser) and was evaluated on 4 entirely different games (e.g., Ninja, Climber).
  • Expert Data: To create a high-quality offline dataset, training data was generated using expert RL agents (PPO and E3B).
Procgen environments split into training tasks and entirely unseen test tasks to evaluate cross-game generalization.

Key Insights and Results

Multi-Trajectory vs. Single-Trajectory A motivating experiment revealed that a multi-trajectory transformer achieved a 0.780 return on an unseen task, whereas a single-trajectory version (standard Behavioral Cloning) scored a dismal -0.323. Without the multi-episode context, the model lacks a "map" for the new task and fails to generalize.

Stochasticity is a Feature, Not a Bug Training in environments with "sticky actions" (where an action might randomly repeat, introducing stochasticity) actually improved ICL. It prevents the model from simply memorizing exact paths and forces it to learn a robust, reactive policy that can recover from unexpected states.

The "In-Weights" vs. "In-Context" Divide The authors identified that task success stems from two distinct mechanisms. Some tasks are solved because the model learned similar patterns during training and internalized them ("In-Weights"). Others are solved specifically by "copying" the logic provided in the few-shot demonstrations ("In-Context").

Scaling Laws and Few-Shot Performance Just as in Natural Language Processing, scaling laws apply here: larger models (up to 210M parameters) and larger datasets (up to 30k levels) led to better few-shot generalization. On unseen MiniHack tasks, the model's performance improved consistently as more expert demonstrations (from 1 to 7) were added to the context.

Cross-Game Generalization The model significantly outperformed standard Behavioral Cloning baselines. Most impressively, it successfully learned to play Ninja (a platformer) after only seeing games like Bigfish (an eating game) during training. This proves that the Transformer learned the meta-strategy of following a demonstration rather than just memorizing specific game mechanics.

Limitations and Future Work

Despite these successes, sequential decision-making remains an unforgiving domain. The authors noted specific "failure modes" in environments where a single mistake leads to an unrecoverable state (e.g., falling into lava). Even with a strong context window, the model can suffer from "covariate shift," where a minor error places it in an unfamiliar state it doesn't know how to handle. Furthermore, if the test task suffers from extreme distributional drift—being too far removed from anything seen in training (e.g., totally different observation spaces)—ICL still struggles.

Because this work focuses on offline imitation learning, a natural next step for future research is "In-Context Reinforcement Learning." In such a paradigm, the agent would learn from its own trial-and-error interactions within the context window, rather than relying strictly on expert demonstrations.

Summary updated

BibTeX
@inproceedings{raparthy2024generalization,
  author = {Sharath Chandra Raparthy and Eric Hambro and Robert Kirk and Mikael Henaff and Roberta Raileanu},
  title = {{Generalization to New Sequential Decision Making Tasks with In-Context Learning}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=lVQ4FUZ6dp},
}
Figure