Search

Search papers and lessons

Hindsight Experience Replay

Level 3 · 9 min read

Hindsight experience replay turns each failed attempt into a success for the goal it did reach, by relabelling stored transitions, so a sparse-reward agent gets learning signal from its own misses.

On this page

  • Colour = topic mix
  • Reinforcement learning
  • Calculus
  • Probability
  • Linear algebra
  • Discrete math
  • Neural network basics
  • Sign in to see your progress

TL;DR

In a goal-reaching task with a sparse reward, almost every attempt fails and the agent learns nothing. Hindsight experience replay stores each attempt with the goal it was given, then adds copies of it relabelled with a goal the attempt actually reached, where the reward is a success. The agent learns from its misses as if it had aimed for them.

Where you'll see it: robot arms learning to push, slide or pick up objects from a binary success signal, and any goal-conditioned agent where the goal can be read off the state it reaches.

Intuition

Aiming at the wrong target and still hitting something

Imagine learning to throw a ball into a basket that is far away. Every throw lands short, and a coach who only says "miss" teaches you nothing about how to aim. Now suppose the coach also says "that throw landed at this spot", and you realise the throw was a perfect throw to that spot. The attempt is no longer a failure. It is a success for a different target, and you can learn from it.

HER does this for a robot. An episode that ends far from its goal still visited a sequence of states. Each of those states is a valid target, so each transition can be stored a second time with that state written in as the goal. Under the new goal the transition is a success, so the reward is no longer zero for every step.

The reason this is useful is that an agent with a sparse reward gets one informative signal per successful episode. Relabelling turns every episode into a set of successful examples for some goal, which is the signal the learner needs.

flowchart LR
  E[Episode with goal g] --> S[Stored transitions with goal g]
  E --> R[Achieved states s1 s2 ... sT]
  R --> H[Relabelled copies with goal g' = s_t]
  S --> B[Replay buffer]
  H --> B
  B --> L[Off-policy update of Q and policy]

The math

Setup

A goal-conditioned task has states ss, actions aa, and a goal gg. The agent's Markov decision process is as in Markov Decision Process, with the goal as an extra input to the policy π(a∣s,g)\pi(a \mid s, g) and to the value function Q(s,a,g)Q(s, a, g), as in Goal-Conditioned Reinforcement Learning.

Each goal comes with an achieved-goal function ff that maps a state to the goal it represents. For a robot reaching a point, f(s)f(s) is the gripper position. The reward is sparse and binary:

r(s,a,g)={0if ∥f(s′)−g∥≤ϵ−1otherwiser(s, a, g) = \begin{cases} 0 & \text{if } \lVert f(s') - g \rVert \le \epsilon \\ -1 & \text{otherwise} \end{cases}

Here s′s' is the next state, ϵ\epsilon is a tolerance, and the reward is 00 for success and −1-1 for every other step. Without relabelling, the only zero rewards come from episodes that reach gg itself, which is rare when gg is hard.

The relabelling step

Take a stored transition (st,at,st+1,g)(s_t, a_t, s_{t+1}, g) from an episode. HER writes a new goal g′g' into it, then recomputes the reward with the same function:

r~t=r(st,at,g′),g′ chosen from the achieved goals f(st′) in the same episode\tilde{r}_t = r(s_t, a_t, g'), \qquad g' \text{ chosen from the achieved goals } f(s_{t'}) \text{ in the same episode}

Because the reward function is known, the new reward is computed exactly, not estimated. The paper defines several ways to choose g′g':

  • final: g′=f(sT)g' = f(s_T), the goal achieved at the end of the episode.
  • future: g′=f(st′)g' = f(s_{t'}) for t′>tt' > t, a state visited later in the same episode. A few such goals are sampled for each transition.
  • episode: g′g' is a state from anywhere in the same episode.
  • random: g′g' is a goal from some other episode in the buffer.

The paper compares these strategies; the future strategy performed best (see History).

The learning update

HER is used with an off-policy actor-critic such as Deep Deterministic Policy Gradient (DDPG). The critic is trained on the Bellman target

y=r~t+γ Q(st+1,π(st+1,g′),g′)y = \tilde{r}_t + \gamma \, Q\big(s_{t+1}, \pi(s_{t+1}, g'), g'\big)

and minimises (Q(st,at,g′)−y)2\big(Q(s_t, a_t, g') - y\big)^2 over sampled transitions. The actor is updated to increase Q(s,π(s,g),g)Q(s, \pi(s, g), g). Relabelling is valid because the critic is off-policy: it learns the value of acting toward g′g' from data gathered while the agent was trying for gg. The Bellman backup itself is the same as in Q-Learning.

Worked examples

Example
Relabelling a three-step corridor

A one-dimensional corridor has positions 0,1,2,3,4,50, 1, 2, 3, 4, 5. The true goal is g=5g = 5. The achieved-goal function is f(s)=sf(s) = s, and the tolerance is ϵ=0\epsilon = 0. The agent's episode is 0→1→2→10 \to 1 \to 2 \to 1, then it stops. The true goal was never reached.

  1. Original rewards with goal g=5g = 5: each step lands at a state other than 55, so the rewards are −1,−1,−1-1, -1, -1.
  2. Final-state relabelling with g′=f(sT)=1g' = f(s_T) = 1.
  3. Step 1 moves 0→10 \to 1. The next state is 1=g′1 = g', so r~=0\tilde{r} = 0.
  4. Step 2 moves 1→21 \to 2. The next state is 2≠12 \neq 1, so r~=−1\tilde{r} = -1.
  5. Step 3 moves 2→12 \to 1. The next state is 1=g′1 = g', so r~=0\tilde{r} = 0.
  6. The relabelled episode has two successes and one failure, where the original had none.

Interpretation: the same three transitions now carry useful signal. The critic learns that moving to position 11 from 00 is a good move when the goal is 11, which is a true statement about the corridor. The example uses illustrative positions and is not a result from the paper.

Example
Future strategy in a bit-flipping task

A task has 3 bits, the goal is g=111g = 111, and the achieved goal is the current bit string, with tolerance ϵ=0\epsilon = 0. The episode starts at 000000 and each action flips one bit. The states are 000→100→110→010000 \to 100 \to 110 \to 010, so there are three transitions.

  1. Original rewards: no state equals 111111, so the rewards are −1,−1,−1-1, -1, -1.
  2. Transition 1 (000→100000 \to 100): sample the later achieved goal g′=110g' = 110 from the state at step 2. The next state is 100≠110100 \neq 110, so r~=−1\tilde{r} = -1.
  3. Transition 2 (100→110100 \to 110): sample g′=010g' = 010 from the state at step 3. The next state is 110≠010110 \neq 010, so r~=−1\tilde{r} = -1.
  4. Transition 3 (110→010110 \to 010): the only later state is the final one, 010010. The next state is 010=g′010 = g', so r~=0\tilde{r} = 0.

Interpretation: one of the three relabelled transitions is a success, where the original data had none. The critic learns that flipping the last bit from 110110 brings the state to 010010, which is a true fact about this task. The bit strings and sampled goals are illustrative.

Tradeoffs and failure modes

SituationProblemBetter choice
Goal is a function of the state, such as a position or an object poseRelabelling is cheap and exactUse HER with the future strategy
Goal cannot be computed from the state, such as "the room looks tidy"There is no achieved-goal function to relabel withLearned goal classifiers, or a dense reward
Off-policy learner is unavailableThe relabelled data is off-policy and cannot be used by an on-policy methodUse an off-policy actor-critic
Goals are almost always reached by chanceRelabelled goals add little new signalKeep the original goals in the mix, or add curriculum
Many relabelled copies per transitionBuffer and compute grow and the critic sees repeated dataTune the number of relabelled goals per transition
Reward tolerance ϵ\epsilon is too tightFew achieved goals count as successesWiden the tolerance

HER does not change which states the agent visits. It only changes which goals the stored data is labelled with, so it cannot help an agent that never reaches interesting states. It also relies on the reward function being known and computable from a stored next state. When it is, the method is cheap; when it is not, the benefits disappear.

History

Andrychowicz and co-authors, working at OpenAI, introduced hindsight experience replay in 2017 (NIPS 2017) as a way to learn from sparse binary rewards without engineering a shaped reward. They first tested the idea on a bit-flipping task with DQN, where standard RL fails for large numbers of bits because it never sees any reward other than −1-1. They then combined relabelling with DDPG on three simulated robot-arm tasks: pushing, sliding and pick-and-place.

In their strategy comparison, all strategies except random solved pushing and pick-and-place almost perfectly, and the future strategy with k=4k = 4 or 88 relabelled goals per transition performed best and was the only one that nearly solved sliding. Raising kk above 8 hurt, because the share of ordinary, unrelabelled data in the buffer became too small. The authors also report that DDPG and DDPG+HER failed on all three tasks with the shaped rewards they tried, and that a pick-and-place policy trained in simulation was deployed on a physical robot, succeeding in 2 of 5 trials at first and 5 of 5 after retraining with noise added to the observations.

Check yourself

QuestionIn one sentence, what does HER change in the stored data, and what does it leave alone?Show answer

It stores extra copies of each transition with a different goal, namely a state the agent actually reached, and recomputes the reward for that goal. It leaves the states, actions and the environment unchanged, so the agent still visits the same states.

QuestionIn the corridor example, the episode is 0→1→2→10 \to 1 \to 2 \to 1 and the goal is 5. With final-state relabelling and ϵ=0\epsilon = 0, what are the rewards of the three relabelled transitions?Show answer

The new goal is g′=1g' = 1. The transitions land on 11, then 22, then 11, so the rewards are 00, −1-1, and 00.

QuestionWhy is HER usually paired with an off-policy learner rather than an on-policy one?Show answer

The relabelled transitions were generated while the agent was pursuing the original goal, so they are off-policy for the new goal. An off-policy critic can learn the value of acting toward the new goal from them. An on-policy method assumes its data came from the current policy for the goal it is optimising, so the relabelled data violates that assumption.

QuestionA task asks a robot to make a room look tidy, judged by a learned classifier that is not a function of the robot's joint state. Can HER be applied directly, and why or why not?Show answer

Not directly. HER needs an achieved-goal function that maps a stored state to a goal and a reward function that can be recomputed for a new goal. The classifier is not a function of the joint state, so the goal of a stored state cannot be read off, and the relabelled rewards cannot be computed exactly. The options are to make the goal observable as a state function or to use a dense reward instead.

Sources