Search

Search papers and lessons

Hindsight Experience Replay

Marcin Andrychowicz, Filip Wolski, Alex Ray et al. · 2017

Andrychowicz et al. (2017) show that replaying each failed robot episode with the goals it did reach lets off-policy RL learn pushing, sliding and pick-and-place from a binary success signal alone.

On this page

  • Colour = topic mix
  • Reinforcement learning
  • Probability
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

Robots that learn from a success-or-failure signal get almost nothing to learn from, because nearly every attempt fails. Andrychowicz and co-authors show that an off-policy learner can reuse failed episodes by replaying them with goals the robot did reach, which turns misses into labelled examples. Their agent learned pushing, sliding and pick-and-place in simulation from binary rewards only, and a pick-and-place policy trained in simulation succeeded on a physical arm in 5 of 5 trials once it was retrained with observation noise.

Builds on: Hindsight Experience Replay, Deep Deterministic Policy Gradient (DDPG), Experience Replay · Leads to: Goal-Conditioned Reinforcement Learning

Why it mattered

The paper made goal-conditioned learning from sparse rewards a practical option for robot manipulation. Its core move, relabelling stored transitions with achieved goals, is now a common tool in this area, and it is the reason a sparse success signal is enough for many simulated manipulation tasks that would otherwise need a carefully shaped reward.

It also gave a concrete recipe others could copy: a known reward function, an off-policy actor-critic, and a choice of which goals to relabel with. The paper's ablations on that choice are the part most people reuse. The idea is also an early, clean example of sim-to-real transfer for a manipulation policy, where the policy trained entirely in simulation was run on a physical Fetch arm.

The problem

A standard RL agent learns from a reward signal that says how well it did. In a manipulation task the natural reward is binary: the object is within a tolerance of the goal, or it is not. A random policy almost never succeeds, so almost every stored transition has the same reward, and the learner gets no gradient toward success.

The usual fix is reward shaping, adding a term such as a distance to the goal. The authors argue that this needs domain knowledge and often gives the wrong incentives. In their experiments, shaped rewards did not help either algorithm on any of the three tasks. The paper asks whether an agent can learn from sparse, binary rewards directly.

Two background pieces are needed. Off-policy learners such as DQN and DDPG can learn from data generated by a different behaviour policy, which is what makes replay with new goals valid. Universal policies, as in Goal-Conditioned Reinforcement Learning, take the goal as an input, so one network can pursue many goals.

How it works

The pipeline runs in this order. Each stage is shown in the diagram.

  1. Pick a goal gg and an initial state s0s_0 for the episode.
  2. Run the behaviour policy for TT steps, which is the policy plus exploration noise, and store the episode.
  3. For each transition (st,at,st+1)(s_t, a_t, s_{t+1}), compute the reward for the original goal, r(st,at,g)r(s_t, a_t, g). This is the ordinary replay record.
  4. Choose extra goals g′g' from the same episode, using a strategy SS. The paper's simplest choice is the final achieved goal, m(sT)m(s_T).
  5. Store a second copy of the transition with goal g′g' and reward r(st,at,g′)r(s_t, a_t, g'). This is the hindsight record.
  6. Sample minibatches from the buffer, mixing original and relabelled records, and update the critic and actor with an off-policy algorithm.

A tiny trace of steps 3 to 5 on a one-dimensional line, where the goal is position 5 and the agent ends at position 1 without reaching it:

  • Step 3: the original goal 55 is never reached, so every reward is −1-1.
  • Step 4: with the final strategy, g′=1g' = 1.
  • Step 5: the transition that ends on position 1 gets reward 00 with goal 11, and the others keep −1-1.

Each stored transition now carries one informative label where before it had none. Nothing about the environment changed. Only the goal written into the record changed.

flowchart LR
  A[Sample goal g and start s0] --> B[Run behaviour policy with noise]
  B --> C[Store transitions with original goal g]
  B --> D[Choose extra goals g' from the same episode]
  D --> E[Store copies with goal g' and reward r of s t, a t, g']
  C --> F[Replay buffer]
  E --> F
  F --> G[Off-policy update of critic and actor]
  G --> A

Worked examples

Example
Final strategy on a bit string (tiny)

Two bits, start at 0000, and the goal is g=11g = 11. Each action flips one bit, and a state satisfies a goal only when it equals the goal. The episode is 00→01→00→1000 \to 01 \to 00 \to 10, and the goal 1111 is never reached. The example is illustrative; the paper's bit-flipping setup, with up to 50 bits, is described in its Section 3.1.

  1. Original rewards: no visited state equals 1111, so the three transitions get −1,−1,−1-1, -1, -1.
  2. Final strategy: g′=m(sT)=10g' = m(s_T) = 10, the last state.
  3. Transition 00→0100 \to 01: the next state 0101 is not 1010, so r′=−1r' = -1.
  4. Transition 01→0001 \to 00: the next state 0000 is not 1010, so r′=−1r' = -1.
  5. Transition 00→1000 \to 10: the next state 1010 equals g′g', so r′=0r' = 0.
  6. The relabelled copies contain one success where the original data had none.

Interpretation: the last transition now teaches the critic that moving to 1010 from 0000 is a successful move for the goal 1010. That is a true fact about this environment.

Example
Future strategy with k = 1 (standard)

Use the same episode 00→01→00→1000 \to 01 \to 00 \to 10, but relabel with the future strategy and one goal per transition (k=1k = 1). Label the four states in the episode as s0=00s_0 = 00, s1=01s_1 = 01, s2=00s_2 = 00, s3=10s_3 = 10.

  1. Transition 0 (s0→s1s_0 \to s_1): sample a later state, s3=10s_3 = 10, so g′=10g' = 10. The next state s1=01s_1 = 01 is not 1010, so r′=−1r' = -1.
  2. Transition 1 (s1→s2s_1 \to s_2): the only later state is s3s_3, so g′=10g' = 10. The next state s2=00s_2 = 00 is not 1010, so r′=−1r' = -1.
  3. Transition 2 (s2→s3s_2 \to s_3): the only later state is s3s_3, so g′=10g' = 10. The next state s3=10s_3 = 10 equals g′g', so r′=0r' = 0.
  4. The future strategy gives the same single success as the final strategy here, because the only achieved goal available to each transition was 1010.

Interpretation: with a longer episode, the future strategy would offer several different goals for the same transition, which is what gives it more variety than the final strategy. The paper found this variety valuable (Section 4.5).

Example
Relabelled share and the physical robot (realistic)

The paper reports two numbers from its real-robot test. Before retraining the pick-and-place policy succeeded in 2 of 5 trials. After retraining with Gaussian observation noise (standard deviation 1 cm), it succeeded in 5 of 5 trials.

  1. Success rate before retraining: 2/5=0.42 / 5 = 0.4, which is 40 percent.
  2. Success rate after retraining: 5/5=1.05 / 5 = 1.0, which is 100 percent.
  3. Improvement: 1.0−0.4=0.61.0 - 0.4 = 0.6, which is 60 percentage points.
  4. Separately, the paper's replay accounting. Each transition is stored once with its original goal and kk times with relabelled goals, so the share of relabelled records is k/(k+1)k / (k + 1) in that buffer. For k=4k = 4 this is 4/5=0.84 / 5 = 0.8, and for k=8k = 8 it is 8/98 / 9, about 0.890.89. The paper observed that raising kk above 8 degraded performance because the share of ordinary, unrelabelled data became very low.
  5. The 2-of-5 result is small, so it is a demonstration rather than a statistical study. The paper says the policy was not robust to small errors in the box position estimate, which is why the noise was added.

Interpretation: the real-robot result reflects both the value of training with HER and the gap that observation noise creates. The share calculation is our arithmetic from the paper's description, not a number the paper reports.

Try it: the paper's best setting used k=4k = 4 or k=8k = 8 for the future strategy. Using the share formula above, what is the ratio of relabelled to original records when k=8k = 8, and does it match the share 8/98 / 9? Answer: the ratio is 8/1=88 / 1 = 8, and it matches the share, because a share of 8/98 / 9 means 8 relabelled records for every 1 original record.

The math

Step 2 in symbols: the sparse reward

Let fg(s)f_g(s) be 1 when the state ss satisfies goal gg and 0 otherwise. For the object-position goals used in the robot tasks, fg(s)f_g(s) is 1 when the object lies within a fixed tolerance of gg. The paper's sparse reward is

r(s,a,g)=− [ fg(s′)=0 ]r(s, a, g) = -\,[\, f_g(s') = 0 \,]

where s′s' is the state after action aa is taken in state ss. In words: the reward is 00 on success and −1-1 otherwise. The bracket is 1 when the condition is true and 0 when it is false.

Step 4 in symbols: the goal map

The paper assumes a mapping mm from states to goals, with fm(s)(s)=1f_{m(s)}(s) = 1 for every state ss. In words: every state satisfies some goal, namely the one it represents. For the robot tasks, m(s)m(s) is the object's position in ss. The final-state strategy uses g′=m(sT)g' = m(s_T).

Step 5 in symbols: the relabelled record

A stored transition is written as (st∥g′,at,r′,st+1∥g′)(s_t \| g', a_t, r', s_{t+1} \| g'), where ∥\| means concatenation, and the reward is recomputed for the new goal:

r′=r(st,at,g′)r' = r(s_t, a_t, g')

Because the reward function is known, r′r' is computed exactly. No estimate is needed.

Step 6 in symbols: the critic target

With DQN, the Bellman target is y=r′+γmax⁡a′Q(st+1,a′,g′)y = r' + \gamma \max_{a'} Q(s_{t+1}, a', g'). With DDPG, which the paper uses for all its robot results, it is y=r′+γ Q(st+1,π(st+1,g′),g′)y = r' + \gamma\, Q(s_{t+1}, \pi(s_{t+1}, g'), g'), where π\pi is the actor. The paper uses γ=0.98\gamma = 0.98 and clips targets to [−1/(1−γ),0][-1/(1-\gamma), 0], which for γ=0.98\gamma = 0.98 is [−50,0][-50, 0]. The clip matches the range of possible returns when every reward is −1-1 or 00. The critic is trained to minimise the squared gap between Q(st,at,g′)Q(s_t, a_t, g') and yy over minibatches. Relabelling is valid because the critic learns the value of acting toward g′g' from data gathered while the agent was pursuing gg, and that is an off-policy update.

The strategies in symbols

The paper compares four ways to choose g′g' for a transition at time tt in an episode of length TT:

  • final: g′=m(sT)g' = m(s_T).
  • future: g′=m(st′)g' = m(s_{t'}) for kk random indices t′>tt' > t from the same episode.
  • episode: g′=m(st′)g' = m(s_{t'}) for kk random indices from anywhere in the same episode.
  • random: g′g' is the goal of kk states sampled from anywhere in training.

The parameter kk is the number of relabelled goals per transition. The paper keeps the original goal in the buffer as well, so each transition is stored once with its original goal and kk times with relabelled goals.

Results

Bit-flipping (Section 3.1, Figure 1). The paper says that DQN without HER can only solve the task for n≤13n \le 13 bits, while DQN with HER solves it for nn up to 50. It also says standard RL is bound to fail for n>40n > 40 because it never sees any reward other than −1-1.

Multi-goal robot tasks (Section 4.2, Figure 3). DDPG without HER was unable to solve any of the three tasks, and DDPG with count-based exploration made some progress only on sliding. DDPG with HER solved all three almost perfectly. The paper counts an episode as successful when the object ends within 7 cm of the goal for pushing and pick-and-place, and within 20 cm for sliding. Results are averaged over 5 random seeds, with shaded areas showing one standard deviation.

Single-goal setting (Section 4.3, Figure 4). DDPG with HER performed much better than DDPG alone even when every episode had the same goal. The paper also finds HER learns faster when training episodes contain multiple goals, so it advises training on multiple goals even when only one matters.

Shaped rewards (Section 4.4, Figure 5). Neither DDPG nor DDPG with HER solved any task with any of the shaped rewards they tried. The best shaped reward was −∣g−sobject′∣2-|g - s'_{\text{object}}|^2, and it still failed on all tasks. The authors give two reasons: the shaped reward differs from the success condition, and shaped rewards penalise behaviour that could help exploration.

Choice of goals (Section 4.5, Figure 6). Every strategy except random solved pushing and pick-and-place almost perfectly. The future strategy with k=4k = 4 or k=8k = 8 performed best and was the only one that nearly solved sliding. Raising kk above 8 degraded performance.

Physical robot (Section 4.6, Figure 7). The pick-and-place policy, trained in simulation with the future strategy and k=4k = 4, succeeded in 2 of 5 trials on a Fetch arm without fine-tuning. After retraining with noise (standard deviation 1 cm) on observations, it succeeded in 5 of 5 trials.

Limits. The experiments use one simulated robot arm, three tasks, and a known object-position mapping. The paper's real-robot test is a single task with five trials. The method needs an achieved-goal mapping and a reward function that can be recomputed for a new goal. The paper does not claim HER helps when goals cannot be read off the state.

Reading guide

Check yourself

QuestionIn one sentence, what does HER add to the replay buffer, and what does it leave unchanged?Show answer

It adds extra copies of each transition written with a different goal, usually a state the agent reached later in the episode, and recomputes the reward for that goal. It leaves the states, actions and environment unchanged, so the agent still visits the same states.

QuestionIn the tiny bit-string example, the episode is 00→01→00→1000 \to 01 \to 00 \to 10 with goal 1111. With the final strategy, how many relabelled transitions get reward 0, and why?Show answer

One. The final achieved goal is g′=10g' = 10. Only the last transition ends in 1010, so only its relabelled reward is 0. The other two end in 0101 and 0000, which are not 1010, so they get −1-1.

QuestionWhy does HER need an off-policy learner such as DDPG rather than an on-policy one?Show answer

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

QuestionThe paper reports 2 of 5 successful real-robot trials before retraining and 5 of 5 after. What were the two success rates, and what was the difference in percentage points?Show answer

Before retraining, 2/5=402 / 5 = 40 percent. After retraining, 5/5=1005 / 5 = 100 percent. The difference is 60 percentage points. The paper attributes the early failures partly to the policy being trained on perfect simulated states and not being robust to small errors in the box position.

QuestionA new task rewards a robot only when a judged property of the scene holds, and the property is not a function of the robot's joint state. Can HER be applied directly? Give the reason.Show answer

Not directly. HER needs a map from each stored state to a goal that the state satisfies, and a reward that can be recomputed for any relabelled goal. If the property is not a function of the joint state, the goal a stored state achieves cannot be read off, so the relabelled rewards cannot be computed exactly. The options are to expose the property as a state function, or to use a different reward.

Sources

Read the full paper

Marcin Andrychowicz, Filip Wolski, Alex Ray et al., 2017. arXiv:1707.01495