Simple Algorithm
Reinforcement Learning · Fixed Point · 26 min

RL as Sequence Modeling

Decision Transformer, return conditioning, and when it works.

What if you skipped value functions entirely? Train a sequence model on logged trajectories, conditioned on the return each one achieved, then ask it for a high return. The Decision Transformer did this in 2021 and matched value-based methods on several benchmarks. Theory and careful experiments then showed when it works (near-deterministic dynamics, data that contain the returns you ask for) and when it cannot: when good outcomes were luck, and when the best route must be stitched from pieces of different trajectories.

What you’ll learn

  • Formulate return-conditioned supervised learning, derive its infinite-data policy by Bayes' rule, and describe the Decision Transformer and the Trajectory Transformer.
  • Compute, in small examples, how return conditioning fails in stochastic environments and why it cannot stitch trajectories.
  • State Brandfonbrener et al.'s guarantee for return-conditioned learning with its three assumptions, and explain why its coverage requirement can be exponential in the horizon.
  • Summarize the empirical record (the Decision Transformer, filtered behavior cloning, RvS) and the hybrids that put dynamic programming back.

Upside-down RL and return conditioning

Schmidhuber (2019) proposed turning RL upside down: instead of predicting returns from actions, map desired returns (and time limits) to actions, and learn that map by supervised learning from the agent's own experience; Srivastava et al. (2019) trained agents this way. The trick is hindsight relabeling. Every logged trajectory is a perfect demonstration of how to obtain the return it actually obtained, just as every trajectory reaches the goal it reached in goal-conditioned relabeling (Goal-Conditioned RL & Hindsight Relabeling).

Formally, return-conditioned supervised learning (RCSL) fits a policy π(a∣s,g)\pi(a\mid s, g) by maximum likelihood on the data, with gg the return of the trajectory each pair came from, and acts by conditioning on a chosen target f(s)f(s). Brandfonbrener, Bietti, Buckman, Laroche and Bruna (2022) asked what it converges to with unlimited data and capacity.

DerivationWhat return conditioning learns
  1. πRCSL(a∣s,g)=Pr⁡β(A=a∣S=s, G=g)\pi^{\text{RCSL}}(a\mid s, g) = \Pr_\beta(A = a\mid S = s,\ G = g)

    Maximum likelihood on (s,a,g)(s, a, g) triples recovers the conditional distribution of the logged action given the state and the eventual return:

  2. πRCSL(a∣s,g)=πβ(a∣s) Pr⁡β(G=g∣s,a)Pr⁡β(G=g∣s)\pi^{\text{RCSL}}(a\mid s, g) = \pi_\beta(a\mid s)\,\frac{\Pr_\beta(G = g\mid s, a)}{\Pr_\beta(G = g\mid s)}

    By Bayes' rule, this is the behavior policy reweighted by how likely each action was to lead to return gg:

  3. Every factor belongs to the behavior policy. Pr⁡β(G=g∣s,a)\Pr_\beta(G = g\mid s,a) is the probability that taking aa, then continuing as the data did, yields gg. Conditioning on a high gg selects actions that were often followed by high returns under the behavior's continuation and the environment's randomness. Whether the learned policy achieves gg when it acts is a separate question, and the rest of this lesson is about it.

A cruder relative is filtered behavior cloning: clone only the trajectories whose return lies in the top X% ("%BC"). IQL's advantage-weighted policy extraction is a per-action relative with a learned critic deciding what counts as good (Conservative Offline RL: BCQ, CQL & IQL).

The Decision Transformer and the Trajectory Transformer

The Decision Transformer (Chen, Lu, Rajeswaran, Lee, Grover, Laskin, Abbeel, Srinivas & Mordatch, 2021) writes each trajectory as tokens, three per step,

(R^1,s1,a1, R^2,s2,a2, …),R^t=∑t′≥trt′,\big(\hat R_1, s_1, a_1,\ \hat R_2, s_2, a_2,\ \dots\big),\qquad \hat R_t = \sum_{t'\ge t}r_{t'},

where R^t\hat R_t is the undiscounted return-to-go (the paper indexes rewards rtr_t by the step that earns them). A causally masked transformer reads the last KK steps (K=20K = 20 in the locomotion experiments, 30 or 50 on Atari), each modality with its own embedding plus a learned timestep embedding, and is trained to predict each action from the tokens before it: cross-entropy for discrete actions, squared error for continuous ones. Predicting states or returns as well did not help. At test time you set the first token to a desired return and subtract each reward as it arrives.

text
Decision Transformer at test time
R̂ ← target return (for example, near the best return in the data)
s ← env.reset();  context ← []
while the episode is not over:
    context.append(R̂, s)                 # keep the last K steps
    a ← model.predict_action(context)
    s′, r ← env.step(a)
    context.append(a)
    R̂ ← R̂ − r                             # what is left to earn
    s ← s′

The model never computes a value or a maximum. Its only lever on quality is the return it is asked for, and the data decide whether that request means anything.

The Trajectory Transformer (Janner, Li & Levine, 2021) models whole trajectories instead. It discretizes every dimension of states, actions and rewards into tokens, and plans by beam search: among sequences the model finds likely, it keeps those with high predicted reward plus a learned reward-to-go estimate. It is a learned model and a planner in one, closer to model-based RL than to cloning.

The empirical record. In Chen et al.'s experiments the Decision Transformer was competitive with CQL: ahead on average over the D4RL locomotion datasets, mixed across four Atari games learned from 1% of a DQN agent's replay data. A baseline in the same paper complicates the picture: on the plentiful D4RL data, cloning only the top 10% of trajectories matched the Decision Transformer on average, though it did much worse on the small Atari datasets. RvS (Emmons, Eysenbach, Kostrikov & Levine, 2022) then showed that a two-layer MLP conditioned on a target return or a goal and trained by maximum likelihood is competitive with both the Decision Transformer and value-based methods across the suites they tested. Capacity, regularization and the choice of conditioning variable mattered, not the transformer. Sequence models do scale well: a single Decision Transformer trained offline played up to 46 Atari games at close to human level, improving with model size (Lee et al., 2022).

When it works, and two ways it fails

DerivationFailure 1: conditioning on luck
  1. Pr⁡β(G=2∣gamble)=0.25,Pr⁡β(G=2∣safe)=0\Pr_\beta(G = 2\mid\text{gamble}) = 0.25,\qquad \Pr_\beta(G = 2\mid\text{safe}) = 0

    One decision. Safe returns exactly 1. Gamble returns 2 with probability 0.25 and 0 otherwise, an expected 0.5. The behavior picks each with probability 12\tfrac12, so return 2 appears in 12.5% of trajectories, always after the gamble:

  2. πRCSL(gamble∣g=2)=12⋅0.2512⋅0.25+12⋅0=1\pi^{\text{RCSL}}(\text{gamble}\mid g = 2) = \frac{\tfrac12\cdot0.25}{\tfrac12\cdot0.25 + \tfrac12\cdot0} = 1

    Ask for the best return in the data, g=2g = 2. Bayes' rule puts all the probability on the gamble:

  3. High returns in the data were partly luck, and conditioning on them imitates the circumstances of the luck, not a way to reproduce it. Paster, McIlraith and Ba (2022) showed that this breaks the Decision Transformer and RvS in stochastic environments such as the game 2048, and proposed ESPER, which conditions instead on the average return of a learned cluster of trajectories, a quantity the environment's randomness cannot fake.

Predict before you look

In the gamble example, what return does the RCSL policy earn on average when it is asked for g=2g = 2? (Asked for g=1g = 1, it plays safe and earns 1.)

Show the answer

0.5

It always gambles and earns 0.25×2=0.50.25\times2 = 0.5: asking for more yields less. In the theorem's terms, ϵ=0.25\epsilon = 0.25 and αf=0.125\alpha_f = 0.125, so the bound is 0.25×(8+2)=2.50.25\times(8 + 2) = 2.5, larger than the whole range of returns. A value-based method compares expected returns, 11 against 0.50.5, and plays safe.

DerivationFailure 2: return conditioning cannot stitch
  1. sA→ down x→ left 0,sA→ stop 0.5,sB→x→ right 1s_A\xrightarrow{\ \text{down}\ }x\xrightarrow{\ \text{left}\ }0,\qquad s_A\xrightarrow{\ \text{stop}\ }0.5,\qquad s_B\to x\xrightarrow{\ \text{right}\ }1

    A deterministic problem with three logged trajectories. From sAs_A, stop ends the episode with reward 0.5, and down leads (reward 0) to a junction xx. From xx, left ends with 0 and right with 1. The data: sA→s_A\to down →x→\to x\to left (return 0); sA→s_A\to stop (return 0.5); and a trajectory from another start sBs_B that reaches xx and goes right (return 1).

  2. Pr⁡β(G=1∣sA)=0\Pr_\beta(G = 1\mid s_A) = 0

    From sAs_A the best route is down then right, return 1, but no logged trajectory from sAs_A achieves 1. The request g=1g = 1 at sAs_A is uncovered (αf=0\alpha_f = 0), so the policy there is unconstrained extrapolation. The covered requests give g=0.5⇒g = 0.5\Rightarrow stop, return 0.5, and g=0⇒g = 0\Rightarrow down, then (with remaining target 0) left, return 0.

  3. Dynamic programming learns V(x)=1V(x) = 1 from the sBs_B trajectory's transition and backs it up through the first transition of the sAs_A trajectory: Q(sA,down)=0+V(x)=1>Q(sA,stop)=0.5Q(s_A,\text{down}) = 0 + V(x) = 1 > Q(s_A,\text{stop}) = 0.5. DP needs coverage of transitions; RCSL needs coverage of whole returns. A network asked for an uncovered return may still extrapolate sensibly, as some of the paths in Chen et al.'s graph experiment did, but nothing in the method makes it do so.

Return coverage can also be exponentially expensive. Brandfonbrener et al. give a deterministic problem in which the only way to prefer the good action is to condition on returns above H/2H/2, which the behavior achieves with probability at most 10−H/210^{-H/2}: αf\alpha_f, and with it the data needed, scales exponentially in the horizon, where dynamic programming would need only coverage of the good policy's transitions. On the four D4RL datasets they tested, they found the value-based methods TD3+BC and IQL generally ahead of RCSL and behavior cloning, most clearly on AntMaze, where stitching and return coverage matter; RCSL did well on a dataset of high-quality human demonstrations with dense rewards.

Putting dynamic programming back

The successful repairs keep the sequence model and fix what it conditions on, or what data it sees:

  • Q-learning Decision Transformer (Yamagata, Khalil & Santos-Rodriguez, 2023) relabels the returns-to-go in the data with values learned by Q-learning, so that a trajectory passing through a state with a better continuation is told so, and then trains a Decision Transformer on the relabeled data. In the stitching example, the step at sAs_A would carry return 1.
  • ESPER (Paster et al., 2022) conditions on a trajectory statistic that luck cannot fake.
  • Online Decision Transformer (Zheng, Zhang & Grover, 2022) pretrains offline and then fine-tunes online with sequence-level entropy regularization: acting supplies the corrective data that offline training lacks.
PropertyReturn-conditioned supervised learningDynamic programming (value-based offline RL)
Dynamicsnear-deterministic, for guaranteesany
Coverage neededof the requested returns, from each start; can be exponentially rareof the transitions along a good policy
Stitchingno, unless returns are relabeledyes
Trainingsupervised and stablebootstrapped; needs conservatism offline
Scaling to many tasksbehaves like supervised learningunder active study
After Brandfonbrener et al. (2022). The two approaches fail in different places, which is why hybrids are promising.

Key takeaways

  • Return-conditioned supervised learning fits π(a∣s,g)\pi(a\mid s,g) by maximum likelihood; with unlimited data it equals πβ(a∣s)Pr⁡β(G=g∣s,a)/Pr⁡β(G=g∣s)\pi_\beta(a\mid s)\Pr_\beta(G = g\mid s,a)/\Pr_\beta(G = g\mid s), a reweighting of the behavior policy by the behavior's own outcomes.
  • The Decision Transformer conditions a causal transformer on returns-to-go and decrements the target as rewards arrive. Filtered cloning and small MLPs often match it, and the approach scales well across many tasks.
  • With near-deterministic dynamics, coverage of the requested return and consistent conditioning, RCSL is near-optimal (Brandfonbrener et al., 2022). Randomness turns high returns into luck that the policy imitates, stitching is out of reach, and return coverage can be exponentially rare in the horizon.
  • Hybrids put dynamic programming back: relabeling returns with learned Q-values, conditioning on luck-independent statistics, or fine-tuning online.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

With unlimited data, return-conditioned supervised learning learns π(a∣s,g)=πβ(a∣s)Pr⁡β(G=g∣s,a)/Pr⁡β(G=g∣s)\pi(a\mid s,g) = \pi_\beta(a\mid s)\Pr_\beta(G = g\mid s,a)/\Pr_\beta(G = g\mid s). What does conditioning on a high gg select?

  1. The action with the highest optimal value Q⋆(s,a)Q^\star(s,a)
  2. The behavior policy's most frequent action
  3. A uniformly random action among those in the data
  4. Actions that were often followed by return gg under the behavior's own continuation and the environment's randomness
Show the answer

D. Actions that were often followed by return gg under the behavior's own continuation and the environment's randomness

Every factor is a property of the behavior policy and the environment. RCSL selects actions associated with high returns in the data, which coincides with optimal action selection only under conditions such as near-deterministic dynamics and coverage of the requested return.

Question 2

A single decision: safe returns 1; gamble returns 2 with probability 0.25 and 0 otherwise. Asked for return 2, the RCSL policy gambles and earns 0.5. Which assumption of Brandfonbrener et al.'s guarantee fails?

  1. Near determinism: the gamble's outcome is random
  2. Return coverage: return 2 never appears in the data
  3. Consistency of the conditioning function
  4. A finite horizon
Show the answer

A. Near determinism: the gamble's outcome is random

The gamble's outcome deviates from any fixed deterministic outcome with probability 0.25, so ϵ=0.25\epsilon = 0.25, and with αf=0.125\alpha_f = 0.125 the bound ϵ(1/αf+2)H2=2.5\epsilon(1/\alpha_f + 2)H^2 = 2.5 is vacuous. The data contained return 2, but only as luck.

Question 3

What did RvS (Emmons et al., 2022) conclude about supervised offline RL?

  1. Transformers are essential: MLPs cannot learn return-conditioned policies
  2. A small MLP conditioned on returns or goals is competitive; capacity, regularization and the choice of conditioning variable matter more than the architecture
  3. Return conditioning is equivalent to Q-learning
  4. Supervised methods work best on random data
Show the answer

B. A small MLP conditioned on returns or goals is competitive; capacity, regularization and the choice of conditioning variable matter more than the architecture

A two-layer feedforward network trained by maximum likelihood matched the Decision Transformer and value-based methods on the suites tested. The authors also found these methods comparatively weak on random data.

Question 4

In the stitching example, why does dynamic programming find the route sA→x→s_A\to x\to right (return 1), while return conditioning does not?

  1. Because dynamic programming uses a larger model
  2. Because dynamic programming needs every action in every state to be logged
  3. Because dynamic programming backs up the value of xx, learned from one trajectory, through a transition of another, so it needs coverage of transitions rather than of whole returns
  4. Because return conditioning cannot represent deterministic policies
Show the answer

C. Because dynamic programming backs up the value of xx, learned from one trajectory, through a transition of another, so it needs coverage of transitions rather than of whole returns

V(x)=1V(x) = 1 comes from the sBs_B trajectory; the transition sA→xs_A\to x comes from a different one. A Bellman backup combines them. RCSL needs a logged trajectory from sAs_A with return 1, and there is none.

Further reading

  • L. Chen, K. Lu, A. Rajeswaran, K. Lee, A. Grover, M. Laskin, P. Abbeel, A. Srinivas & I. Mordatch, "Decision Transformer: Reinforcement learning via sequence modeling," Advances in Neural Information Processing Systems 34 (2021).
  • M. Janner, Q. Li & S. Levine, "Offline reinforcement learning as one big sequence modeling problem," Advances in Neural Information Processing Systems 34 (2021).
  • D. Brandfonbrener, A. Bietti, J. Buckman, R. Laroche & J. Bruna, "When does return-conditioned supervised learning work for offline reinforcement learning?" Advances in Neural Information Processing Systems 35 (2022).
  • K. Paster, S. McIlraith & J. Ba, "You can't count on luck: Why decision transformers and RvS fail in stochastic environments," Advances in Neural Information Processing Systems 35 (2022).
  • S. Emmons, B. Eysenbach, I. Kostrikov & S. Levine, "RvS: What is essential for offline RL via supervised learning?" International Conference on Learning Representations (ICLR) (2022).