Hindsight Experience Replay (HER)
Hindsight replay files every miss under the goal it actually reached, so a failed run still teaches the agent how to get somewhere on purpose.
Why Does This Exist?
Goal tasks pay sparsely by nature: a robot gets 0 for placing the block right and minus 1 everywhere else. Random flailing almost never lands a distant target, so the buffer fills with pure failure and value updates have no nonzero signal to climb. Everything looks equally futile.
Hand shaping the reward, like paying negative distance, seems to help but bends the goal. Agents learn to hover near obstacles or freeze at local optima instead of finishing the job. Hindsight replay, from Andrychowicz and coauthors in 2017, takes another road. Every failed run at goal g is a perfect run at wherever the agent ended up, so it copies each transition under the achieved goal and recomputes the reward. Failures become successes without touching the true reward function. It slots into goal-conditioned RL, builds on plain experience replay, and replaces much of reward shaping.
The one idea you need first
A goal-conditioned value Q(s, a, g) answers many questions at once: how good is this move for each goal I might want. The sparse reward is 0 when the next state's achieved goal lands within tolerance eps of g, else minus 1. One network then serves every goal, which is exactly what relabeling trains.
Think of It Like This
An archery coach who keeps every miss
A beginner aims at the far target and plants an arrow in a fence post to the left. Strict scoring says zero points, lesson over. A sharp coach instead asks: suppose the post was the target, what did your stance and release just teach us about hitting posts?
Logged that way, a season of misses becomes a manual for hitting posts, rocks, and mounds across the field. When the far target comes back around, it is one more point in a mapped space rather than a first attempt. The physics never changed, only the label on the practice log.
Where it stops: arrows fly the same whatever you wished for, and so do environment transitions, which is why relabeling is legal. But a student who only ever aims left never learns the far right, so the sampling strategy in the mechanism still matters.
How It Actually Works
States pair with goals through a mapping m from state to achieved goal, and the policy plus values read both: pi(a | s, g) and Q(s, a, g). A rollout at goal g stores the usual tuples (s, a, r, s_next, g), mostly with r = -1.
Hindsight copies each transition under replacement goals g' taken from visited states, then recomputes r' = r(s_next, g'). The tested strategies are final (relabel with the last state only), future (sample a few states from later in the same episode, the winner at about four per step), episode (sample anywhere in the episode), and random (sample old history). future wins because its goals stayed reachable within the remaining steps.
This only works off policy. Changing the goal after the fact breaks the on policy promise that actions came from the policy under that goal, while off policy methods like DDPG or SAC just evaluate the Bellman backup under g':
The transition itself stays valid because physics ignores the goal label.
Worked example
A line task starts at 0.0 with target g = 10.0, tolerance 0.5, discount 0.90, and rate 0.20. Three steps of +2.0 visit 2.0, 4.0, 6.0, every distance above 0.5, so the buffer holds three minus 1 transitions and learning stalls.
Relabel with the final strategy at g' = 6.0. The last step now ends exactly on its goal: reward r' = 0.0, terminal value 0.0, target y = 0.0. From Q = -2.50 the error is +2.50 and the new value is -2.50 + 0.20 * 2.50 = -2.00.
The middle step still misses (|4.0 - 6.0| = 2.0, reward minus 1) but bootstraps from the improved downstream value: y = -1.0 + 0.90 * (-2.00) = -2.80. From Q = -4.00 the error is +1.20 and the new value is -4.00 + 0.20 * 1.20 = -3.76. One manufactured success backed signal two steps deep, and many endpoints tile the goal space from there.
Code
This snippet replays the relabeling with plain Python, printing the same reward flip and Q updates derived by hand.
def sparse_reward(next_state: float, goal: float, tol: float = 0.5) -> float: return 0.0 if abs(next_state - goal) <= tol else -1.0
states = [0.0, 2.0, 4.0, 6.0]target, hindsight = 10.0, 6.0orig = [sparse_reward(s_next, target) for s_next in states[1:]]relabelled = [sparse_reward(s_next, hindsight) for s_next in states[1:]]print(orig) # -> [-1.0, -1.0, -1.0]print(relabelled) # -> [-1.0, -1.0, 0.0]
gamma, alpha = 0.90, 0.20q_last = -2.50 + alpha * ((0.0 + gamma * 0.0) - (-2.50))q_mid = -4.00 + alpha * ((-1.0 + gamma * q_last) - (-4.00))print(f"{q_last:.2f} {q_mid:.2f}") # -> -2.00 -3.76Rewards flip from all minus 1 to [-1.0, -1.0, 0.0], and Q values land on -2.00 and -3.76 exactly as the hand pass computed.
Watch Out For
Drowning the buffer in trivial one step wins
Pushing the replay ratio high, like sixteen hindsight copies per real step, floods training with transitions where the goal sat one move away. The agent perfects stopping in place and never learns the long walks the real goals need.
Symptom: relabeled success rates look great while target goal performance flatlines. Fix: hold near four future goals per step, the ratio from the original paper, and mix in prioritized replay so high error transitions get sampled first.
Bolting hindsight onto an on policy learner
PPO and A2C require actions drawn under the goal being evaluated, but relabeled transitions were drawn under another goal. Feeding them in silently trains on a distribution the algorithm never assumed.
Symptom: policy updates swing or collapse once relabeled data dominates the batch. Fix: pair hindsight only with off policy methods, and keep a share of original goal transitions so the true targets stay represented.
The Quick Version
- Sparse goal rewards starve learning because nearly every rollout scores minus 1 on every step.
- Relabeling files each transition under achieved goals and recomputes the reward, manufacturing successes from failures.
- The
futurestrategy with about four later states per step beats final, episode, and random sampling. - Hindsight needs off policy learners, since the goal swap breaks the on policy sampling promise while physics stays valid.
- The worked run turned goal ten failure into goal six success, lifting Q from minus 2.50 to minus 2.00 and then minus 3.76 one step back.
- Read goal-conditioned RL for the framing, experience replay for the buffer, and reward shaping for what this replaces.