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 , actions , and a goal . The agent's Markov decision process is as in Markov Decision Process, with the goal as an extra input to the policy and to the value function , as in Goal-Conditioned Reinforcement Learning.
Each goal comes with an achieved-goal function that maps a state to the goal it represents. For a robot reaching a point, is the gripper position. The reward is sparse and binary:
Here is the next state, is a tolerance, and the reward is for success and for every other step. Without relabelling, the only zero rewards come from episodes that reach itself, which is rare when is hard.
The relabelling step
Take a stored transition from an episode. HER writes a new goal into it, then recomputes the reward with the same function:
Because the reward function is known, the new reward is computed exactly, not estimated. The paper defines several ways to choose :
- final: , the goal achieved at the end of the episode.
- future: for , a state visited later in the same episode. A few such goals are sampled for each transition.
- episode: is a state from anywhere in the same episode.
- random: 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
and minimises over sampled transitions. The actor is updated to increase . Relabelling is valid because the critic is off-policy: it learns the value of acting toward from data gathered while the agent was trying for . The Bellman backup itself is the same as in Q-Learning.
Worked examples
A one-dimensional corridor has positions . The true goal is . The achieved-goal function is , and the tolerance is . The agent's episode is , then it stops. The true goal was never reached.
- Original rewards with goal : each step lands at a state other than , so the rewards are .
- Final-state relabelling with .
- Step 1 moves . The next state is , so .
- Step 2 moves . The next state is , so .
- Step 3 moves . The next state is , so .
- 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 from is a good move when the goal is , which is a true statement about the corridor. The example uses illustrative positions and is not a result from the paper.
A task has 3 bits, the goal is , and the achieved goal is the current bit string, with tolerance . The episode starts at and each action flips one bit. The states are , so there are three transitions.
- Original rewards: no state equals , so the rewards are .
- Transition 1 (): sample the later achieved goal from the state at step 2. The next state is , so .
- Transition 2 (): sample from the state at step 3. The next state is , so .
- Transition 3 (): the only later state is the final one, . The next state is , so .
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 brings the state to , which is a true fact about this task. The bit strings and sampled goals are illustrative.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Goal is a function of the state, such as a position or an object pose | Relabelling is cheap and exact | Use 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 with | Learned goal classifiers, or a dense reward |
| Off-policy learner is unavailable | The relabelled data is off-policy and cannot be used by an on-policy method | Use an off-policy actor-critic |
| Goals are almost always reached by chance | Relabelled goals add little new signal | Keep the original goals in the mix, or add curriculum |
| Many relabelled copies per transition | Buffer and compute grow and the critic sees repeated data | Tune the number of relabelled goals per transition |
| Reward tolerance is too tight | Few achieved goals count as successes | Widen 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 . 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 or relabelled goals per transition performed best and was the only one that nearly solved sliding. Raising 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 and the goal is 5. With final-state relabelling and , what are the rewards of the three relabelled transitions?Show answer
The new goal is . The transitions land on , then , then , so the rewards are , , and .
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.