Inferring Behavior-Specific Context Improves Zero-Shot Generalization in Reinforcement Learning

Tidiane Camaret Ndir, André Biedenkapp, Noor H. Awad

TL;DR

This paper introduces Joint Context and Policy Learning (JCPL), a method that significantly improves zero-shot generalization in reinforcement learning. By training context encoders and policies together rather than in separate stages, the agent learns "behavior-specific" representations that capture exactly what the policy needs to know to adapt to unseen environmental dynamics, like changes in gravity or mass.

What Problem Does This Paper Address?

Reinforcement Learning (RL) agents are notoriously brittle. An agent trained to walk on flat ground often fails completely if the gravity changes slightly or if it has to carry a heavier load. The "holy grail" is Zero-Shot Generalization (ZSG): the ability for an agent to be dropped into a brand-new environment and perform correctly the very first time, without any additional training or fine-tuning.

To achieve this, agents often try to infer "context"—hidden variables like friction or mass—from a short history of observations. However, most existing methods decouple this: they first train an encoder to predict general environment dynamics and then train a policy on top of it. The authors argue this is inefficient. A policy doesn't need to understand every nuance of the physics engine; it only needs to understand the specific nuances that affect its current behavior.

Methods

The authors propose JCPL, which moves away from the standard two-phase training approach.

  • Standard Approach (Predictive Identification): As shown in the figure below, the context encoder is typically trained as a separate module to predict the next state ($s'$). Once trained, it is frozen, and the policy is trained using its outputs. Two-phase training vs. joint training
Figure 1: Two-phase training of predictive context encoding methods. Typically, no gradient updates go through the frozen context encoder while learning the policy as depicted in (b).
  • The JCPL Approach: Instead of freezing the encoder, JCPL allows the policy's loss gradients to flow back into the context encoder. This forces the encoder to prioritize information that helps the policy succeed. This creates a "chicken-and-egg" problem (you need a good policy to get good transitions, and good transitions to learn context), which the authors solve by leveraging the iterative nature of Generalized Policy Iteration (GPI).

At test time, the agent maintains a short history of recent transitions. It passes these through the learned encoder to generate a latent context vector, which is then concatenated with the current state to inform the policy's actions immediately.

Datasets

The researchers evaluated their method using the CARL (Contextual Adaptive RL) library, which provides wrappers for classic benchmarks with modifiable physical properties:

  • Cartpole: Generalizing to changes in the time interval between states.
  • Pendulum: Adapting to different pendulum lengths.
  • MountainCar: Handling variations in engine power.
  • Ant: Controlling a 4-legged robot with varying torso masses.

They specifically tested both interpolation (values within the training range) and extrapolation (values outside the training range) to measure true zero-shot capability.

Key Insights and Ideas

  • Behavior-Specific vs. Task-Agnostic: The core "aha moment" is that context shouldn't just be a compressed version of the physics engine. By conditioning the context on the policy's needs, the agent ignores "noise" in the dynamics that doesn't impact the task at hand.
  • Latent Space Clarity: The joint learning process actually results in cleaner latent representations. When visualized, the JCPL embeddings show much better separation between different environment settings compared to predictive models, especially in complex tasks like the Ant environment. Learned embeddings showing better separation in JCPL
  • Direct Access isn't always best: Surprisingly, giving an agent direct, explicit access to the ground-truth context (like the exact mass value) sometimes performs worse than letting the agent infer its own behavior-specific representation.
Learned embeddings showing better separation in JCPL

Key Results

JCPL consistently outperformed the predictive baseline across most environments:

  • Training Efficiency: In the Pendulum and Ant tasks, JCPL achieved the highest episodic returns, even rivaling agents that had direct access to ground-truth context. Training curves showing JCPL performance
  • Generalization Scores: Using the Interquartile Mean (IQM) of normalized scores, JCPL showed superior performance in both interpolation and extrapolation. For example, in the complex Ant environment, JCPL achieved an IQM of 1.05 for all values, significantly higher than the 0.94 achieved by predictive identification.
  • Context Recovery: In the Ant environment, JCPL reduced the Mean Squared Error (MSE) of predicting the true mass from the latent code from 1221.84 (predictive) down to 1107.84.
Figure 3: Episodic returns during training when learning with explicit context, hidden context and learned context embeddings. Our joint learning method (jcpl) is capable of reaching the same performance as if learning with the ground-truth context, outperforming the predictive identification baseline.

Limitations and Future Work

The authors noted that JCPL struggled with the MountainCar environment, where the predictive baseline remained superior. They suggest this might be because MountainCar dynamics are simple enough that a general predictive model is sufficient, or that the joint optimization becomes unstable in that specific reward landscape.

Future directions include: 1. Incorporating Reward Signals: Currently, the encoder only looks at state transitions. Including rewards could help the agent adapt to tasks where the goal changes, not just the physics. 2. Temporal Architectures: The current encoder averages transitions. Using RNNs or Transformers could allow the agent to capture how the environment evolves over time more effectively. 3. Uncertainty Estimation: Adding a way for the agent to express how "sure" it is about its context inference could lead to more robust and safe exploration in novel settings.

Summary updated

Abstract

In this work, we address the challenge of zero-shot generalization (ZSG) in Reinforcement Learning (RL), where agents must adapt to entirely novel environments without additional training. We argue that understanding and utilizing contextual cues, such as the gravity level of the environment, is critical for robust generalization, and we propose to integrate the learning of context representations directly with policy learning. Our algorithm demonstrates improved generalization on various simulated domains, outperforming prior context-learning techniques in zero-shot settings. By jointly learning policy and context, our method acquires behavior-specific context representations, enabling adaptation to unseen environments and marks progress towards reinforcement learning systems that generalize across diverse real-world tasks. Our code and experiments are available at https://github.com/tidiane-camaret/contextual_rl_zero_shot.
BibTeX
@misc{ndir2024inferring,
  author = {Tidiane Camaret Ndir and André Biedenkapp and Noor H. Awad},
  title = {{Inferring Behavior-Specific Context Improves Zero-Shot Generalization in Reinforcement Learning}},
  year = {2024},
  eprint = {2404.09521},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2404.09521},
}
Figure