Skip to content

Deep reinforcement learning and decision models · Method reference · DRL013 · Architecture

Decision Transformer

Treat logged control trajectories as sequences and predict actions conditioned on desired return, states, and prior actions.

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Treat logged control trajectories as sequences and predict actions conditioned on desired return, states, and prior actions. The training loss is supervised sequence prediction, while the application is sequential decision-making from reward-labeled trajectories; it is not a conventional unsupervised language model.

Question / hypotheses

Can a causally masked sequence model reproduce high-return behavior when conditioned on an attainable target return? Conditioning does not guarantee that a requested return is feasible or that the generated policy will achieve it outside dataset support.

Architecture / computational structure

Embed return-to-go, state, and action tokens with timestep information, interleave them, and process a fixed history using a causal Transformer. An action head predicts discrete actions or continuous action vectors. The attention mask must prevent future actions and rewards from leaking into training predictions.

Assumptions

Trajectories have correctly aligned rewards, observations, actions, and episode boundaries. Return conditioning relies on meaningful reward scales and sufficient examples of desired behavior. A finite context may omit information needed in partially observed tasks.

Formula / computational definition

R^t=∑u=tTru\widehat R_t=\sum_{u=t}^{T}r_u and πθ(at∣R^≤t,s≤t,a<t)\pi_\theta(a_t\mid\widehat R_{\le t},s_{\le t},a_{<t}). Minimize action cross-entropy for discrete actions or squared action error for a deterministic continuous head; update desired remaining return after each realized reward during rollout.

Parameters and notation

Learned parameters include modality/timestep embeddings, Transformer blocks, normalization layers, and action head. Hyperparameters include context length, layer count, hidden width, attention heads, dropout, batch/trajectory sampling, learning rate, reward normalization, and evaluation target return.

Fitting, fine-tuning, and adaptation

Transfer compatible embeddings and Transformer blocks, refit the action head if action dimensions change, and recalibrate return scaling for the new task. Begin with frozen-block or partial-block adaptation, then unfreeze with a lower backbone learning rate. Preserve causal masking and trajectory-level validation throughout.

How to use and calibrate

Compare against behavior cloning and offline-RL baselines on held-out episodes. Report return across several feasible target-return settings and seeds, plus sensitivity to dataset quality. Low action-prediction error is insufficient when small action errors compound over long rollouts.

Example application

Generate actions from an offline dataset containing trajectories with several achieved returns. The code computes return-to-go and a masked continuous-action prediction loss; it does not instantiate a Transformer or prove a target return can be attained.

Interpretation / pitfalls

Watch out

Unrealistic target returns can place the sequence model out of distribution. Reward changes invalidate old conditioning semantics. Padding masks and episode boundaries must be correct; using future information produces deceptively good supervised metrics. This original model is distinct from later trajectory-planning variants.

Python and R code examples

Scope: computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop. Dependencies/validation: Python with NumPy executed successfully; base R not executed. Targets and supplied network outputs are ordinary arrays here, so this does not validate automatic-differentiation boundaries.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop. · needs numpy

import numpy as np
rewards=np.array([1.,0.,2.,-1.]); return_to_go=np.cumsum(rewards[::-1])[::-1]
target_action=np.array([[.1,.2],[.3,.4],[.5,.6],[0.,0.]])
prediction=target_action+np.array([[.1,0.],[0.,-.1],[.1,.1],[5.,5.]])
valid=np.array([1.,1.,1.,0.]); per_step=np.mean((prediction-target_action)**2,axis=1)
loss=np.sum(valid*per_step)/valid.sum(); print(return_to_go,loss)
assert np.isclose(loss,(.005+.005+.01)/3)
R

Component demo, not a trained model — computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop.

rewards <- c(1,0,2,-1); return_to_go <- rev(cumsum(rev(rewards)))
target_action <- matrix(c(.1,.2,.3,.4,.5,.6,0,0),4,byrow=TRUE)
prediction <- target_action+matrix(c(.1,0,0,-.1,.1,.1,5,5),4,byrow=TRUE)
valid <- c(1,1,1,0); per_step <- rowMeans((prediction-target_action)^2)
loss <- sum(valid*per_step)/sum(valid); print(return_to_go); print(loss)
stopifnot(abs(loss-(.005+.005+.01)/3)<1e-10)

Sources

From the CodeSplash reference, entry DRL013.