Decision Transformer: RL as sequence prediction
Treat logged trajectories as sequences of return-to-go, state and action, train a transformer to predict the actions, and ask for a high return at test time. See what that recombines well, and what it can't do.
What runs in CodeSplash
- DRL013Decision Transformer Explained here
You will be able to
- Compute the return-to-go for each step of an episode
- Describe the Decision Transformer's input sequence and training loss
- Explain why asking for a return far above the logs doesn't produce it
- Explain why stochastic environments can mislead return conditioning
No Bellman equation at all
ReadA Decision TransformerDecision TransformerA sequence model trained on logged trajectories to predict actions given past states and the return still wanted. drops value functions and policy gradients. It treats each logged episode as a sequence of tokens, one triple per step:
where is the return-to-goReturn-to-goThe sum of the rewards from a step to the end of the episode; Decision Transformers condition on it., the sum of rewards from step to the end. A causal transformerTransformerA stack of blocks, each self-attention then a feed-forward network, with residual connections and layer normalization. learns to predict each action from everything before it. Training is supervised: cross-entropy or squared error on the logged actions.
Return-to-go and masking
ReadReturn-to-go is a reversed cumulative sum. In the reference's example, rewards give returns-to-go of 2, 1, 1 and −1.
Episodes have different lengths, so batches are padded and a mask removes padded steps from the loss. In the example the padded step has a huge prediction error, yet the masked loss is 0.0067: only the three real steps count.
Ask for what you want
ReadAt test time you set the first return-to-go to the return you want, act on the predicted action, subtract each reward received, and continue. Conditioning on a high return asks the model: "act like the trajectories that went well from here."
That makes it a form of filtered behavior cloningBehavior cloningLearns to copy the logged actions from the states (here a multinomial logistic regression): a baseline every learned policy should beat.: it imitates the logged behavior that led to high returns, and it can stitch together pieces of different good trajectories when the states line up.
What it can't do
ReadIt can't exceed the data. Ask for twice the best logged return and the model has no examples to imitate; it doesn't plan its way there. Its outputs then become unpredictable.
It can mistake luck for skill. In a stochastic environment, some trajectories got high returns by chance. Conditioning on a high return conditions on that luck, and the actions that preceded it may not cause high returns at all.
The Decision Transformer is explained here; it isn't an option of CodeSplash's offline learner. For logged episodes, decide.offline_rl offers fitted Q iteration, CQL, IQL and behavior cloning.
Predict: an ambitious target
PredictPredict first
The best logged episode returned 50. You condition a Decision Transformer on a return-to-go of 200. What do you get?
Reveal
Possibly erratic behavior. The model imitates trajectories with similar returns-to-go; none exist near 200, so its predictions are extrapolations with nothing to anchor them.
What runs here
ReadCheck: Decision Transformer
CheckpointCheck yourself
Rewards over an episode are 3, 0, −1, 2. What are the returns-to-go?
Show the answer
Answer: a
Sum from each step to the end: 3 + 0 − 1 + 2 = 4, then 0 − 1 + 2 = 1, then −1 + 2 = 1, then 2.
Check yourself
How is a Decision Transformer trained?
Show the answer
Answer: a
No values, no policy gradients: sequence prediction on logged trajectories, with padded steps masked out.
Check yourself
Why can return conditioning mislead in a stochastic environment?
Show the answer
Answer: a
Conditioning on the outcome selects lucky episodes; their actions aren't necessarily the cause of their success.
Recap
Recap- Decision Transformers predict actions from sequences of return-to-go, state and action, trained like a language model.
- Return-to-go is the remaining sum of rewards; padded steps are masked from the loss.
- Conditioning on a high return imitates the logged behavior that achieved it: filtered behavior cloning.
- It can't exceed the logs, and in noisy environments it can imitate luck.
Concepts in this lesson