Goal-Conditioned RL & Hindsight Relabeling
Universal value functions, HER, and learning many tasks at once.
One value function for every goal, and every failure a success for some other goal: these two ideas make goal-conditioned RL one of the most data-efficient corners of deep RL. They also hide a geometry and a trap. Optimal goal-reaching values define a distance that obeys the triangle inequality but not symmetry, and hindsight relabeling, exact in deterministic worlds, can make a 10% gamble look like a sure thing in stochastic ones. This lesson proves both, then follows the field's turn to supervised and contrastive objectives.
What you’ll learn
- Formulate goal-conditioned RL, prove that optimal goal-reaching values define a quasimetric, and explain how quasimetric value learning exploits it.
- Prove that hindsight relabeling gives correct Q-learning targets when the goal does not depend on the outcome, in particular in deterministic environments, and compute its bias in a stochastic example where it reverses the greedy choice.
- State the GCSL lower bound, and explain why goal- and return-conditioned imitation copy lucky outcomes in stochastic environments.
- Show that the discounted future-state distribution is a goal-conditioned Q-function, and derive what a contrastive critic learns.
Goal-reaching values and their geometry
With reward 1 on arrival and the episode ending there, the optimal value of reaching is , where is the arrival time (and ). It is convenient to drop the constant offset and work with for , with , and the induced distance . In a deterministic world, and is the number of steps.
Fix . Run an optimal policy for reaching ; on arrival switch to an optimal policy for reaching . Because the dynamics are Markov, the time from to is independent of how was reached, and arriving at earlier only helps:
Take and divide by , which reverses the inequality. With , this is the triangle inequality:
Symmetry fails in general: rolling down a hill is quicker than climbing it, and a one-way door makes finite and infinite. A function with and the triangle inequality but no symmetry is a quasimetric. Among all quasimetrics with for every one-step transition, the shortest-path distance is the largest, since the triangle inequality bounds any of them by the length of every path:
This geometry is a strong inductive bias. Quasimetric RL (QRL; Wang, Torralba, Isola & Zhang, 2023) parameterizes with an architecture that is a quasimetric by construction, then maximizes over random pairs subject to on observed transitions; by the argument above, the optimum in a deterministic environment is the shortest-path distance. A network that must obey the triangle inequality cannot represent many wrong value functions, and it generalizes across goals accordingly. The same reachability function reappears in side-effect penalties (Goal Misgeneralization & Side Effects).
In a deterministic world with , and . What lower bound does the triangle inequality give for ?
Show the answer
0.59
: is 2 steps from and is 3 steps from , so is at most 5 steps from . A shorter route that bypasses would only raise the value.
Why relabeling is valid, and when it is not
Hindsight relabeling trains on tuples drawn from a replay distribution in which may have been chosen after the fact. Whether that is legitimate depends on one question: does the way was chosen depend on ?
Tabular Q-learning on data from converges, under the usual conditions, to the fixed point of the update averaged over the replay distribution:
The Bellman equation needs . By Bayes' rule, the replay distribution of given reweights by how likely the relabeling procedure was to pick after landing in :
If is chosen without looking at (the commanded goal, or a goal drawn at random), the ratio is 1 and the fixed point is . If is a deterministic function of , only one appears and the ratio is irrelevant: relabeling is unbiased in deterministic environments. Otherwise next states from which was later reached are overweighted, and values of risky actions are inflated.
From , action risky reaches in one step with probability 0.1 and otherwise falls into an absorbing pit; action safe reaches surely in three deterministic steps. Reward 1 on reaching , . The true values favor safe:
Replay each stored transition with its commanded goal with probability , or with the goal achieved at the end of its episode with probability ; commanded goals equal with probability . Tuples then arise from commanded goals (weight , success rate 0.1) and from relabeling, which yields goal only when the step succeeded (weight , always a success):
The safe path is deterministic, so its estimate is unbiased, . With HER's typical four relabeled replays per original one, , and many possible goals, say , the risky action looks almost certain to succeed, and the greedy policy takes the 10% gamble:
Redo the estimate with fewer possible goals: and . What value does relabeled Q-learning assign to the risky action?
Show the answer
0.82
. Still above the safe action's 0.81, and far above the true 0.1. The bias shrinks as grows, because commanded goals, which are chosen independently of the outcome, then make up more of the data.
USHER (Schramm, Deng, Granados & Boularias, 2023) corrects the bias with importance weights built from learned estimates of the goal-selection densities, the ratio in the Bayes formula above, keeping relabeling's sample efficiency. The authors report HER taking a short, risky path in a discrete task and running a red light in a driving task, while USHER did neither, at roughly 2.5 times the network evaluations per update. Deterministic benchmarks, such as the robotic tasks HER was introduced on, never exposed the problem.
Supervised goal reaching, and imitating luck
Goal-conditioned supervised learning (GCSL; Ghosh, Gupta, Reddy, Fu, Devin, Eysenbach & Levine, 2021) drops value functions altogether. Any trajectory demonstrates how to reach the states it visited, so each tuple is a labeled example, and the policy is fit by maximum likelihood. The fitted policy then collects data for commanded goals, the data are relabeled, and the fit repeats. The objective is the probability of being at the goal at the final step, .
Supervised methods that condition on an outcome (reaching , reaching it within steps, achieving return ) fit the behavior policy's actions among trajectories with that outcome. With enough data they learn the conditional distribution given by Bayes' rule:
This chooses actions in proportion to how often they led to , not how reliably. If outcomes are deterministic given , every action in the support achieves for sure, and imitating them is optimal. In the risky shortcut, the outcome "reach in one step" was produced only by the risky action, so the policy conditioned on it gambles every time:
Return-conditioned sequence models are the same construction with = "return " (RL as Sequence Modeling). Paster, McIlraith & Ba (2022) showed that decision transformers and related methods fail in stochastic environments for exactly this reason, since a high return can be luck, and proposed conditioning on the average return of clusters of trajectories, which the environment's randomness affects less.
A return-conditioned policy is trained on logged episodes of the risky-shortcut task in which both actions were taken often. From it is commanded to achieve return 1, which only successful risky steps ever produced (the safe path returns ). What happens?
- It takes the safe path, which is optimal
- It achieves return 1 every time, since it imitates successful episodes
- It takes the risky step and achieves return 1 only 10% of the time, an expected return of 0.1
- It cannot act, since a return of exactly 1 is out of distribution
Show the answer
C. It takes the risky step and achieves return 1 only 10% of the time, an expected return of 0.1
Among logged trajectories with return 1 from , the first action is always risky, so the conditioned policy takes it. Its success still has probability 0.1: imitation copies the choice, not the luck. Commanding return 0.81 would have produced the safe path and a better expected return.
Contrastive critics and offline goal reaching
Define a policy's discounted future-state distribution: the distribution of the state reached after a geometrically distributed number of steps, the successor representation written as a density (Successor Representations & Features):
Take the reward , the scaled probability of landing on next. Its action value sums these probabilities along the trajectory, which is exactly the future-state density:
Contrastive RL (Eysenbach, Zhang, Levine & Salakhutdinov, 2022) estimates it without temporal-difference learning. Positives pair with a future state of the same trajectory, at a geometrically distributed offset; negatives pair it with states from other trajectories, distributed as the marginal . A critic trained to tell them apart (binary NCE with balanced classes) converges to the log density ratio:
For a fixed goal the subtracted term does not depend on the action, so the critic ranks actions exactly as does and supports greedy policy improvement. Here representation learning and goal-conditioned value learning are the same problem:
Offline, goal-conditioned learning meets the distribution shift of Offline RL. OGBench (Park, Frans, Eysenbach & Levine, 2025) collects 85 datasets over 8 types of environments with reference implementations of six methods: goal-conditioned behavior cloning, implicit value and Q-learning variants, quasimetric RL, contrastive RL, and HIQL, a hierarchical method that predicts subgoals as latent states and that the authors found strong across the board. As of the mid-2020s, which family wins depends on the dataset. Stitching datasets, in which no single trajectory connects start and goal, are built to defeat pure imitation, which can only reproduce behavior that some trajectory contains.
Key takeaways
- Optimal goal-reaching values satisfy , so is a quasimetric (path length in deterministic worlds); the shortest-path distance is the largest quasimetric consistent with one-step transitions, which quasimetric RL exploits.
- Relabeled Q-learning is correct when the goal does not depend on the outcome, and always in deterministic environments; otherwise replay reweights next states by , and a 10% gamble can be valued at 0.98.
- GCSL fits relabeled actions by maximum likelihood and maximizes a lower bound ; conditioning on outcomes, whether goals or returns, imitates luck when outcomes are random.
- The discounted future-state density is a goal-conditioned Q-function, and a contrastive critic learns its log-ratio to , which is all greedy improvement needs.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Why is a quasimetric but generally not a metric?
- It is a metric: symmetric and satisfying the triangle inequality
- It satisfies and the triangle inequality, but reaching from can be much easier than the reverse, so it need not be symmetric
- It violates the triangle inequality in stochastic environments
- It is undefined whenever some goal is unreachable
Show the answer
B. It satisfies and the triangle inequality, but reaching from can be much easier than the reverse, so it need not be symmetric
Concatenating an optimal policy to with one from to gives , stochastic environments included, by the Markov property. Asymmetry comes from the dynamics: one-way doors, slopes, irreversible actions.
When is relabeling with goals achieved later in the episode guaranteed to give correct Q-learning targets?
- When relabeled goals are used per transition
- When the goal space equals the state space
- When the behavior policy is greedy
- When transitions are deterministic
Show the answer
D. When transitions are deterministic
The replay distribution of given is times . With deterministic transitions only one is possible, so the reweighting cannot change anything; with stochastic ones it overweights outcomes from which was later reached.
GCSL's guarantee is tightest when what holds?
- The new policy stays close to the one that collected the data, ideally
- The horizon is long
- The environment is stochastic
- Goals are relabeled at random rather than with achieved states
Show the answer
A. The new policy stays close to the one that collected the data, ideally
The penalty vanishes for on-policy data and grows quadratically with both the horizon and the policy change, which is why GCSL iterates: collect with the current policy, relabel, refit.
What does the optimal binary-NCE contrastive critic represent, with positives drawn from the same trajectory's future and negatives from other trajectories?
- The probability of reaching on the next step
- The shortest-path distance from to
- : a goal-conditioned Q-value up to a goal-dependent term that leaves the greedy action unchanged
- The reward function of the task
Show the answer
C. : a goal-conditioned Q-value up to a goal-dependent term that leaves the greedy action unchanged
Telling joint samples from products of marginals has the log density ratio as its optimal logit, and the future-state density is the Q-function of the reward . The goal-only term cancels when actions are compared.
Further reading
- T. Schaul, D. Horgan, K. Gregor & D. Silver, "Universal value function approximators," ICML (2015).
- M. Andrychowicz, F. Wolski, A. Ray, J. Schneider, R. Fong, P. Welinder, B. McGrew, J. Tobin, P. Abbeel & W. Zaremba, "Hindsight experience replay," NeurIPS (2017).
- D. Ghosh, A. Gupta, A. Reddy, J. Fu, C. Devin, B. Eysenbach & S. Levine, "Learning to reach goals via iterated supervised learning," ICLR (2021).
- B. Eysenbach, T. Zhang, S. Levine & R. Salakhutdinov, "Contrastive learning as goal-conditioned reinforcement learning," NeurIPS (2022).
- L. Schramm, Y. Deng, E. Granados & A. Boularias, "USHER: Unbiased sampling for hindsight experience replay," Conference on Robot Learning, PMLR 205 (2023).