Improving Context-Based Meta-Reinforcement Learning with Self-Supervised Trajectory Contrastive Learning

Bernie Wang, Simon Xu, Kurt Keutzer, Yang Gao, Bichen WuarXiv 2021

Abstract

Meta-reinforcement learning typically requires orders of magnitude more samples than single task reinforcement learning methods. This is because meta-training needs to deal with more diverse distributions and train extra components such as context encoders. To address this, we propose a novel self-supervised learning task, which we named Trajectory Contrastive Learning (TCL), to improve meta-training. TCL adopts contrastive learning and trains a context encoder to predict whether two transition windows are sampled from the same trajectory. TCL leverages the natural hierarchical structure of context-based meta-RL and makes minimal assumptions, allowing it to be generally applicable to context-based meta-RL algorithms. It accelerates the training of context encoders and improves meta-training overall. Experiments show that TCL performs better or comparably than a strong meta-RL baseline in most of the environments on both meta-RL MuJoCo (5 of 6) and Meta-World benchmarks (44 out of 50).

Introduction

Reinforcement Learning (RL) is notoriously sample-inefficient, often requiring millions of interactions to learn a single task. Meta-RL aims to mitigate this by training agents on a variety of tasks, enabling them to "learn how to learn" and adapt to new scenarios rapidly. In context-based Meta-RL, an agent employs a context encoder to process recent experiences and infer a latent variable ($z$) representing the current task (e.g., identifying environmental dynamics or goal locations).

Traditionally, these encoders are trained end-to-end using only the task's reward signal. However, rewards are often sparse, providing "weak supervision" that slows the learning of meaningful task representations. This leads to unstable training and suboptimal performance in complex environments. We propose that trajectories themselves act as "task fingerprints," containing rich information about dynamics and rewards even before success is achieved.

Methods: Trajectory Contrastive Learning (TCL)

We introduce Trajectory Contrastive Learning (TCL) as a self-supervised auxiliary task designed to provide a denser training signal for the context encoder. TCL is a "plug-and-play" module that can be integrated into existing context-based Meta-RL frameworks like PEARL.

The TCL architecture integrates a contrastive loss alongside standard reward supervision. A momentum-updated encoder provides stable targets for the contrastive task.

The core mechanism involves sampling "windows" of transitions from a replay buffer. If two windows originate from the same trajectory, they form a positive pair; otherwise, they are a negative pair. Using a query-key architecture, the encoder is trained to maximize similarity for positive pairs and minimize it for negative pairs via an InfoNCE loss.

Since context encoders typically output a Gaussian distribution, we utilize the negative Wasserstein distance as the similarity metric, allowing the model to compare both the mean and variance of task embeddings. To maintain training stability, a momentum-updated version of the encoder generates the "keys" for the contrastive task.

Experimental Evaluation

We evaluated TCL on two major benchmarks: MuJoCo Continuous Control (6 locomotion tasks) and Meta-World (50 diverse robotic manipulation tasks).

Results on MuJoCo Benchmarks

In the MuJoCo environments, TCL-PEARL outperformed the baseline in 5 out of 6 tasks, achieving a median performance gain of 1.14x. As shown in the learning curves, TCL often reaches higher performance levels earlier in the training process compared to standard PEARL, RL2, or MAML.

Comparative performance of TCL-PEARL against several baselines across six MuJoCo environments. TCL (orange) consistently matches or exceeds the performance of the PEARL baseline (blue) and often approaches the Oracle (purple).

Results on Meta-World

The benefits of TCL were even more pronounced in the challenging Meta-World benchmark. TCL-PEARL performed better or comparably to the PEARL baseline in 44 out of 50 environments. On average, it achieved a 4.3x gain in returns over the baseline, demonstrating its effectiveness in complex manipulation tasks where rapid task identification is critical.

Discussion and Conclusion

TCL provides a dense, self-supervised signal that significantly improves the sample efficiency and stability of context-based Meta-RL. By leveraging the inherent structure of trajectories, it enables the context encoder to learn distinct task representations much faster than reward-based supervision alone. While TCL introduces some computational overhead, its ability to outperform "Oracle" hand-crafted task vectors in some scenarios highlights the power of learned representations.

Summary updated

BibTeX
@misc{wang2021improving,
  author = {Bernie Wang and Simon Xu and Kurt Keutzer and Yang Gao and Bichen Wu},
  title = {{Improving Context-Based Meta-Reinforcement Learning with Self-Supervised Trajectory Contrastive Learning}},
  year = {2021},
  eprint = {2103.06386},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2103.06386},
}
Figure