Hindsight Experience Replay

Marcin Andrychowicz, Dwight Crow, Alex Ray, Jonas Schneider, Rachel Fong, Peter Welinder, Bob McGrew, Josh Tobin, Pieter Abbeel, Wojciech Zaremba

Overview

Hindsight Experience Replay (HER) is a breakthrough technique that allows Reinforcement Learning (RL) agents to learn from failure by treating unsuccessful outcomes as if they were the intended goal. This approach drastically improves sample efficiency and enables robots to learn complex tasks using only simple, binary rewards (success/failure) rather than meticulously engineered reward functions.

The Problem: Sparse Rewards vs. Reward Shaping

In traditional Reinforcement Learning, an agent learns by receiving rewards. For complex tasks like robotics, the "reward" is often a needle in a haystack. If you want a robot to pick up a ball and place it in a box, and you only give it a reward when it succeeds, the robot will likely spend millions of years flailing around before it accidentally completes the task.

To fix this, researchers usually resort to reward shaping—writing complex mathematical functions to give the robot "partial credit" (e.g., "you're 2cm closer to the ball, here is 0.1 points"). However, reward shaping is notoriously difficult: it requires domain expertise, and if done poorly, the robot might find "hacks" to get points without actually solving the task. The authors of this paper sought a way to let agents learn from sparse, binary rewards (0 for failure, 1 for success) without the need for manual engineering.

Methods: How HER Works

The core innovation is Hindsight Experience Replay (HER). It is designed to be used with off-policy RL algorithms (like DQN or DDPG) and Universal Value Function Approximators (UVFAs), which are policies that take both a state and a goal as input.

The algorithm works as follows:

  1. The agent attempts a task with a specific goal (e.g., "put the puck at coordinates X, Y").
  2. The agent fails and ends up at coordinates A, B instead.
  3. Standard RL would look at this and say, "You got 0 reward; learn nothing."
  4. HER looks at this and says, "While you failed at the original goal, you were perfectly successful at reaching coordinates A, B. Let's pretend that was your goal all along."

By re-labeling the transitions in the replay buffer with goals that were actually achieved during the episode, the agent receives a "success" signal (a reward of 1) much more frequently. This allows the agent to learn the mechanics of the environment even when it isn't hitting the target.

Experimental Environments

The authors evaluated HER on several challenging environments:

  • Bit-Flipping: A toy environment where the agent must flip $n$ bits to match a target sequence. This demonstrates how HER handles exponentially large state spaces.
  • MuJoCo Robotics Simulations: Using a simulated 7-DOF Fetch Robotics arm, they tested three tasks: Pushing (moving a box to a target), Sliding (hitting a puck to a target), and Pick-and-Place (lifting a box to a target in the air).
  • Physical Robot: They deployed the policy learned in simulation onto a real-world Fetch robot to test "sim-to-real" transfer.

Key Insights

  • Implicit Curriculum: HER acts as a natural curriculum. Initially, the agent learns to reach goals that are very close to its starting position (accidental successes). As it gets better at controlling the arm, it achieves more distant "accidental" goals, eventually learning enough to hit the actual intended targets.
  • Sparse > Shaped: Surprisingly, the authors found that HER with simple binary rewards actually performed better than standard RL with shaped rewards. Shaped rewards can often distract the agent or lead to sub-optimal "local minima" behaviors.
  • The "Future" Strategy: The most effective way to pick hindsight goals is the "future" strategy—replaying a transition with a goal that was achieved later in that same episode. This ensures the goal was physically reachable from that state.
  • Multi-goal Benefits: Training an agent to reach many different goals actually makes it better at reaching a single specific goal compared to training on that single goal alone.

Key Results

Bit-Flipping

Standard DQN failed completely when the number of bits exceeded 13. DQN with HER easily solved the task with up to 50 bits, demonstrating its ability to handle massive state spaces where accidental success is statistically improbable.

Bit-flipping success rate comparing DQN and DQN+HER

Robotics Tasks

Standard DDPG (a state-of-the-art algorithm at the time) was unable to solve the Pushing, Sliding, or Pick-and-Place tasks with sparse rewards. DDPG augmented with HER solved all three tasks almost perfectly, vastly outperforming baselines that relied on count-based exploration.

Performance of DDPG vs DDPG+HER on MuJoCo robotics tasks

Sim-to-Real Transfer

The Pick-and-Place policy trained in simulation was successfully deployed on a physical robot. After adding a small amount of Gaussian noise to the simulation to account for sensor errors, the robot achieved a 100% success rate (5/5) in real-world trials.

Limitations and Future Work

  • Off-Policy Only: HER requires an off-policy algorithm because the goals used during replay are different from the goals used when the data was collected. It cannot be directly applied to on-policy algorithms like PPO.
  • State-to-Goal Mapping: HER assumes there is a known mapping that can translate a state into a goal. In environments where the goal is abstract (e.g., "make the room look tidy"), defining this mapping remains a challenge.
  • Computational Overhead: Replaying transitions with multiple goals increases the size of the replay buffer and the amount of computation required for training, though the authors argue this is a fair trade-off for the massive gain in sample efficiency.

Summary updated

BibTeX
@inproceedings{andrychowicz2017hindsight,
  author = {Marcin Andrychowicz and Dwight Crow and Alex Ray and Jonas Schneider and Rachel Fong and Peter Welinder and Bob McGrew and Josh Tobin and Pieter Abbeel and Wojciech Zaremba},
  title = {{Hindsight Experience Replay}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2017},
}
Figure