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 by maximum likelihood on the data, with the return of the trajectory each pair came from, and acts by conditioning on a chosen target . Brandfonbrener, Bietti, Buckman, Laroche and Bruna (2022) asked what it converges to with unlimited data and capacity.
Maximum likelihood on triples recovers the conditional distribution of the logged action given the state and the eventual return:
By Bayes' rule, this is the behavior policy reweighted by how likely each action was to lead to return :
Every factor belongs to the behavior policy. is the probability that taking , then continuing as the data did, yields . Conditioning on a high selects actions that were often followed by high returns under the behavior's continuation and the environment's randomness. Whether the learned policy achieves 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,
where is the undiscounted return-to-go (the paper indexes rewards by the step that earns them). A causally masked transformer reads the last steps ( 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.
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
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 , so return 2 appears in 12.5% of trajectories, always after the gamble:
Ask for the best return in the data, . Bayes' rule puts all the probability on the gamble:
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.
In the gamble example, what return does the RCSL policy earn on average when it is asked for ? (Asked for , it plays safe and earns 1.)
Show the answer
0.5
It always gambles and earns : asking for more yields less. In the theorem's terms, and , so the bound is , larger than the whole range of returns. A value-based method compares expected returns, against , and plays safe.
A deterministic problem with three logged trajectories. From , stop ends the episode with reward 0.5, and down leads (reward 0) to a junction . From , left ends with 0 and right with 1. The data: down left (return 0); stop (return 0.5); and a trajectory from another start that reaches and goes right (return 1).
From the best route is down then right, return 1, but no logged trajectory from achieves 1. The request at is uncovered (), so the policy there is unconstrained extrapolation. The covered requests give stop, return 0.5, and down, then (with remaining target 0) left, return 0.
Dynamic programming learns from the trajectory's transition and backs it up through the first transition of the trajectory: . 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 , which the behavior achieves with probability at most : , 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 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.
| Property | Return-conditioned supervised learning | Dynamic programming (value-based offline RL) |
|---|---|---|
| Dynamics | near-deterministic, for guarantees | any |
| Coverage needed | of the requested returns, from each start; can be exponentially rare | of the transitions along a good policy |
| Stitching | no, unless returns are relabeled | yes |
| Training | supervised and stable | bootstrapped; needs conservatism offline |
| Scaling to many tasks | behaves like supervised learning | under active study |
Key takeaways
- Return-conditioned supervised learning fits by maximum likelihood; with unlimited data it equals , 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.
With unlimited data, return-conditioned supervised learning learns . What does conditioning on a high select?
- The action with the highest optimal value
- The behavior policy's most frequent action
- A uniformly random action among those in the data
- Actions that were often followed by return under the behavior's own continuation and the environment's randomness
Show the answer
D. Actions that were often followed by return 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.
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?
- Near determinism: the gamble's outcome is random
- Return coverage: return 2 never appears in the data
- Consistency of the conditioning function
- 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 , and with the bound is vacuous. The data contained return 2, but only as luck.
What did RvS (Emmons et al., 2022) conclude about supervised offline RL?
- Transformers are essential: MLPs cannot learn return-conditioned policies
- A small MLP conditioned on returns or goals is competitive; capacity, regularization and the choice of conditioning variable matter more than the architecture
- Return conditioning is equivalent to Q-learning
- 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.
In the stitching example, why does dynamic programming find the route right (return 1), while return conditioning does not?
- Because dynamic programming uses a larger model
- Because dynamic programming needs every action in every state to be logged
- Because dynamic programming backs up the value of , learned from one trajectory, through a transition of another, so it needs coverage of transitions rather than of whole returns
- Because return conditioning cannot represent deterministic policies
Show the answer
C. Because dynamic programming backs up the value of , learned from one trajectory, through a transition of another, so it needs coverage of transitions rather than of whole returns
comes from the trajectory; the transition comes from a different one. A Bellman backup combines them. RCSL needs a logged trajectory from 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).