Hindsight experience replay (HER) is a reinforcement learning technique that relabels stored transitions with goals the agent actually achieved, so that an episode which failed to reach its intended goal still provides positive learning signal for some other goal. It was introduced in 2017 for goal-conditioned agents with sparse, binary rewards, where an untrained policy almost never succeeds and ordinary training therefore receives almost no informative reward.
The central idea is a simple change of perspective. If a robot tried to push a block to point A but pushed it to point B instead, the trajectory is a perfect demonstration of how to reach B. HER stores that lesson in the experience replay buffer alongside the original attempt, which lets off-policy algorithms learn from failure without any hand-designed reward shaping.
How hindsight experience replay relabels goals
HER assumes a goal-conditioned policy π(a | s, g) and a reward function r(s, a, g) that returns success only when the achieved state matches the goal within a tolerance, for example 0 for success and -1 otherwise. Each transition is stored with the goal the agent was pursuing and with the achieved goal, such as the final object position.
When transitions are sampled for training, HER replaces the original goal with a substitute goal drawn from states the agent actually visited, and it recomputes the reward for that substitute goal. Because the agent did reach those states, many relabeled transitions carry success rewards, and the value function learns how actions lead toward a wide range of goals.
- Run an episode with goal g and store every transition with its achieved goal.
- For each sampled transition, choose a substitute goal g′ according to a strategy.
- Recompute the reward r(s, a, g′) with the known reward function.
- Train the off-policy learner on a mix of original and relabeled transitions.
Relabeling requires that the reward function can be evaluated for any goal, which is natural in robotics and navigation but not in tasks where reward comes from an opaque external signal.
Formally, HER exploits the fact that a goal-conditioned task is really a family of related Markov decision processes, one per goal, that share the same dynamics. A single trajectory is valid experience for every member of that family, and relabeling simply reuses the same transitions across many goals. This is why HER improves sample efficiency without collecting a single extra environment step, and why a universal value function Q(s, a, g) is the natural learner to pair with it.
Implementations usually store whole episodes rather than isolated transitions, because the future and episode strategies need access to later achieved goals from the same trajectory. Relabeling is typically performed lazily at sampling time, which keeps memory use close to that of ordinary replay and lets the relabeling ratio be tuned without rebuilding the buffer.
Goal selection strategies in HER
The choice of substitute goals strongly influences performance. The original work compared four strategies, summarized in the table below.
| Strategy | Substitute goal source | Typical effect |
|---|---|---|
| Final | Achieved goal at the episode end | Simple, one relabel per transition |
| Future | Achieved goal later in the same episode | Usually the strongest default |
| Episode | Any achieved goal in the same episode | Good, slightly weaker than future |
| Random | Achieved goal anywhere in the buffer | Weakest, goals often unrelated |
The future strategy with about four relabeled goals per original transition, often written as k = 4, is the common default. In terms of sampling probability, this means roughly 80 percent of sampled transitions are relabeled. Larger ratios increase positive signal but bias training toward goals the agent already reaches, so very high ratios can slow progress on the original goal distribution.
HER compared with related replay and reward techniques
HER is often confused with neighboring methods that also address sparse rewards or sample efficiency. The table clarifies how each one changes learning.
| Method | What it changes | Relation to HER |
|---|---|---|
| Experience replay | Reuses stored transitions | HER extends it with goal relabeling |
| Prioritized experience replay | Sampling probability by TD error | Complementary, can be combined |
| Reward shaping | Adds dense hand-designed reward | HER avoids manual shaping |
| Intrinsic motivation | Adds curiosity or novelty bonus | Drives exploration rather than relabeling |
| Goal-conditioned RL | Policy takes the goal as input | Required framework for HER |
A key point is that HER works only with off-policy learning. Relabeled transitions were collected while pursuing a different goal, so they are off-policy with respect to the substitute goal. HER is therefore paired with algorithms such as DDPG, TD3, soft actor-critic or deep Q-networks, not with on-policy methods such as PPO in their standard form.
Where HER works well and where it struggles
HER is most effective in multi-goal manipulation and navigation tasks where the goal space is the same as part of the state space, such as object positions, gripper locations or map coordinates. The table maps typical situations to an expected outcome.
| Situation | HER suitability | Reason |
|---|---|---|
| Robotic pushing and pick-and-place | High | Goals are object positions |
| Maze and grid navigation | High | Goals are reachable coordinates |
| Dense, well-shaped rewards | Low | Little sparse-reward gap to close |
| Goals not expressible as states | Low | Rewards cannot be recomputed |
| Stochastic, noisy dynamics | Moderate | Relabeling bias can mislead values |
- Hindsight bias: in stochastic environments, relabeled successes overstate how reliably an action reaches a goal.
- Goal distribution mismatch: if achieved goals cluster near the start, the agent learns easy goals only.
- Success tolerance: a threshold that is too tight returns almost no relabeled successes.
- Object-free motion: when the agent never moves the object, relabeled goals carry little information.
Later variants address these issues, including curriculum-guided HER, energy-based prioritization of trajectories and corrections for hindsight bias. HER has also inspired hindsight relabeling for language instructions and goal-conditioned imitation learning.
Explained without jargon
Imagine learning to throw a ball into a basket. You miss and the ball lands near a tree. A normal learner says only failure. HER also says: that was a perfect throw to the tree. The next time the goal is the tree, that throw is useful.
This trick matters when success is rare at first. A beginner who never hits the target gets no helpful feedback. By treating every miss as a hit on some other target, the learner gains practice on many goals. Over time, those skills add up, and the real target becomes reachable.
A typical use case
A research team trains a seven-joint robotic arm in simulation to push a block to random target positions on a table. The reward is -1 at every step until the block lies within 5 centimeters of the target, and 0 on success. With DDPG alone, the arm almost never succeeds by chance, and after several million steps the success rate stays near zero.
The team adds hindsight experience replay with the future strategy and four relabeled goals per transition. Every episode where the arm nudges the block anywhere now teaches it how to place the block at that spot. Within a fraction of the earlier training budget, the success rate rises above 90 percent on random targets. The trained policy is then fine-tuned with domain randomization before testing on a physical arm.