Contrastive Retrospection: honing in on critical steps for rapid learning and generalization in RL

Chen Sun, Wannan Yang, Benjamin Alsbury-Nealy, Yoshua Bengio, Blake A. Richards, Bengio, Yoshua, Richards, Blake · DOI

Abstract

In real life, success is often contingent upon multiple critical steps that are distant in time from each other and from the final reward. These critical steps are challenging to identify with traditional reinforcement learning (RL) methods that rely on the Bellman equation for credit assignment. Here, we present a new RL algorithm that uses offline contrastive learning to hone in on these critical steps. This algorithm, which we call Contrastive Retrospection (ConSpec), can be added to any existing RL algorithm. ConSpec learns a set of prototypes for the critical steps in a task by a novel contrastive loss and delivers an intrinsic reward when the current state matches one of the prototypes. The prototypes in ConSpec provide two key benefits for credit assignment: (i) They enable rapid identification of all the critical steps. (ii) They do so in a readily interpretable manner, enabling out-of-distribution generalization when sensory features are altered. Distinct from other contemporary RL approaches to credit assignment, ConSpec takes advantage of the fact that it is easier to retrospectively identify the small set of steps that success is contingent upon than it is to prospectively predict reward at every taken step. ConSpec greatly improves learning in a diverse set of RL tasks.

Introduction

The "Credit Assignment Problem" remains a fundamental challenge in Reinforcement Learning. Traditional methods rely on the Bellman equation to propagate reward information backward one step at a time. In complex tasks with "multiple contingencies"—where success depends on several specific actions separated by long temporal gaps—this incremental propagation is often too slow to be effective. Agents frequently fail to discover the first critical step before the sparse reward signal can reach it.

Contrastive Retrospection (ConSpec) addresses this by adopting a retrospective approach. Instead of attempting to predict future rewards at every frame (prospection), ConSpec looks back at past experiences to identify the specific moments that distinguish success from failure.

Methods: The ConSpec Architecture

ConSpec is designed as a modular "plug-and-play" component that can be integrated with existing RL algorithms like Proximal Policy Optimization (PPO). The system operates through several key stages:

  1. Dual Memory Buffers: The system maintains separate buffers for successful ($S$) and failed ($F$) episodes.
  2. Latent Prototypes: It learns a set of prototypes ($h_1, ..., h_H$) in a latent space that represent the "essence" of critical task steps.
  3. Contrastive Learning: A novel contrastive loss function ($\mathcal{L}_{ConSpec}$) is used to train an encoder. This loss pushes representations of states from successful episodes toward the prototypes while pushing states from failed episodes away.
  4. Intrinsic Reward Generation: Once prototypes are established, the agent receives an internal reward ($\tilde{r}_t$) whenever its current state matches a prototype. This effectively decomposes a long-horizon task into a series of manageable sub-goals.
The ConSpec architecture uses contrastive learning on success/failure buffers to generate prototypes and intrinsic rewards for a backbone RL system.

Evaluation and Key Results

The researchers evaluated ConSpec across diverse environments, including 3D Unity tasks, multi-key gridworlds, delayed-reward Atari games, and continuous control MuJoCo tasks.

The Orange-Tree Task

In the 3D Orange-Tree task, an agent must find a flat object, carry it to a tree, and climb it to reach a reward. This task is particularly difficult because it requires identifying a specific object (a "flat" one) among distractors and performing a sequence of actions with long delays. While standard PPO and curiosity-based methods (like CURL) failed to solve the task, ConSpec achieved near-maximum rewards rapidly.

The Orange-Tree task (a) requires a specific sequence of actions. ConSpec significantly outperforms baselines (b) by learning invariant prototypes for critical steps like finding the flat object (c).

Key Insights

  • Efficiency and Scaling: In multi-key tasks, ConSpec's training time remained nearly constant as complexity increased, whereas baseline performance collapsed. It can begin identifying critical steps from as few as two successful episodes.
  • Invariance and Generalization: Because ConSpec uses contrastive learning, it learns to ignore task-irrelevant details. In the 3D task, it identified the concept of "flatness" regardless of the object's color or specific shape (e.g., box vs. table). This allowed for zero-shot generalization to out-of-distribution environments where textures or colors were changed.
  • Parallel Discovery: Unlike hierarchical RL, which often learns sub-goals sequentially, ConSpec identifies all critical steps in parallel, accelerating the learning process.

Conclusion

ConSpec demonstrates that retrospective identification of critical steps is a powerful alternative to traditional prospective reward prediction. By focusing on the small set of states that success is contingent upon, ConSpec enables agents to solve complex, long-horizon problems that were previously intractable. Future work may involve automating the selection of the number of prototypes, potentially leading to fully self-organizing discovery of task hierarchies.

Summary updated

BibTeX
@inproceedings{sun2023contrastive,
  author = {Chen Sun and Wannan Yang and Benjamin Alsbury-Nealy and Yoshua Bengio and Blake A. Richards and Bengio, Yoshua and Richards, Blake},
  title = {{Contrastive Retrospection: honing in on critical steps for rapid learning and generalization in RL}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=gpJw8f4tIU},
}
Figure