Existing in-context reinforcement learning methods mainly pretrain Transformers with supervised behavior-prediction objectives. This enables task inference from context, but makes the learned policy strongly depend on the quality of offline actions: when trajectories are weak or suboptimal, imitation itself becomes a biased learning signal. We propose Q-Target Pretrained Transformers (QTPT), which keeps the context-conditioned Transformer architecture but replaces behavior cloning with a Bellman-style Q-target objective. QTPT therefore learns to use rewards and transitions in the context to estimate action values, rather than simply imitating the behavior policy. We theoretically analyze QTPT in stochastic linear bandits and finite-horizon MDPs, showing stronger robustness to data quality than supervised pretraining. Empirically, QTPT improves over supervised behavior prediction on controlled RL benchmarks with random or suboptimal data, and we examine extensions to D4RL Kitchen and AntMaze. Supplementary experiments evaluate backbone robustness, meta-RL comparisons, task-coherent context, and unsupported-action value overestimation. These comparisons distinguish the benefits of Q-target pretraining from the remaining limitations of offline coverage.
Figures & tables
Figure 1: Comparison of Q-target Pretrained Transformers (QTPT) and Supervised Pretrained Transformers (SPT) for in-context RL. While SPT requires expert demonstrations for good performance, QTPT maintains robust performance even when trained on suboptimal data.
Figure 2: (a) Suboptimality gap over time for QTPT and SPT under random and LinUCB pretraining data, averaged over 1000 runs. (b) Supplementary mixture evaluation: mean gap and one standard deviation across five independent training seeds, evaluated on 1,000 task instances. The horizontal axis is the fraction of expert trajectories. These are finite-horizon results at T=200 , not an empirical verification of asymptotic rates or a basis for extrapolating curve crossings.
Variant
Random data
Expert data
QTPT (full)
14.46 ± 3.27
18.29 ± 4.06
QTPT-MC
9.33 ± 3.01 (-35%)
13.77 ± 3.68 (-25%)
QTPT-NoCtx
0.38 ± 0.61 (-97%)
0.34 ± 0.65 (-98%)
Table 1: Controlled ablations on the Darkroom benchmark under matched architectures and offline datasets. Historical submitted evaluation: mean reward with 95% confidence-interval half-widths over 300 evaluation episodes. These intervals measure episode-level variation, not variability across independent training seeds; higher is better.
Figure 3: (a) Average cumulative reward in Darkroom. (b) Average cumulative reward in Dark Key-to-door. QTPT is usually stronger under weak data, while extremely sparse expert-only settings remain difficult for TD learning.
Method
Kitchen-partial-v0
Kitchen-mixed-v0
AntMaze-umaze-v2
QTPT (Ours)
13.23 ± 4.03
14.62 ± 3.36
35.94 ± 13.52
SPT
10.84 ± 4.18
13.25 ± 2.68
26.96 ± 11.39
Q-SFT
11.37 ± 1.14
9.44 ± 4.86
19.05 ± 9.21
QDT
12.58 ± 4.28
7.28 ± 5.23
20.00 ± 8.66
Table 2: Main-text D4RL extension. QTPT is compared against matched Transformer baselines under the same revised experimental protocol. Kitchen entries report D4RL normalized score (%), and AntMaze reports success rate (%). Values report active-stage mean ± standard deviation across 3–4 seeds; active-stage performance averages non-zero periodic evaluation checkpoints. This conditional checkpoint statistic excludes failed checkpoints and is descriptive, not the conventional final-policy D4RL evaluation. Higher is better; boldface is omitted because no seed-level significance claim is made.
Appendix figures & tables15 assets
Supplementary material from the paper’s appendix.
Appendix
Random Data Results
Default
Double DQN
Softmax + Hardmax
ReLU-Attn + Softmax-Attn
Results
30.9
34.01
32.48
197.26
Expert Data Results
Default
Double DQN
Softmax + Hardmax
ReLU-Attn + Softmax-Attn
Results
39.4
45.76
53.73
203.74
Appendix
Table 3: Comparison of results for different configurations of QTPT .
Embedding Dimension
16
32
64
128
256
Results
135.7
54.0
40.5
33.3
30.9
Appendix
Table 4: Results for varying embedding dimensions.
Number of Heads
1
2
4
8
16
Results
34.2
30.2
30.9
29.3
31.4
Appendix
Table 5: Results for varying number of attention heads.
Number of Layers
1
2
4
8
Results
122.3
33.6
31.0
30.9
Appendix
Table 6: Results for varying number of layers.
Figure 4: Comparison of Pretrained Transformer Performance Using Q-learning and Supervised Learning in Stationary and Non-stationary Environments. The shaded regions represent the standard deviation of the suboptimality estimates based on 1000 simulation runs.
Figure 5: Accuracy (%) on the GSM8K test set across different mixtures of suboptimal and expert data.
Figure 6: Performance comparison against strong baselines on the Stochastic Linear Bandit task
Method
GOOD↓
Harmful rate ↓
OOD regret ↓
Rollout regret ↓
QTPT
0.2569±0.1021
0.2064±0.0362
0.1828±0.0754
153.15±58.99
Naive hard-max QTPT
0.3351±0.0387
0.2588±0.0090
0.2856±0.0157
211.67±7.81
QTPT + CQL penalty
0.2582±0.1021
0.2144±0.0390
0.1903±0.0675
139.86±55.74
Naive hard-max + CQL penalty
0.3330±0.0386
0.2514±0.0084
0.2840±0.0176
205.40±10.36
Appendix
Table 7: Support-controlled diagnostic. Mean ± standard deviation across five paired training/data seeds. Lower is better; harmful rate is a fraction.
Expert mix
QTPT reward ↑
QTPT + CQL penalty reward ↑
0.0
8.3053±8.7520
9.3333±8.7971
0.5
15.7753±10.7997
14.0393±7.8813
1.0
6.2247±1.9315
8.9653±4.2767
Appendix
Table 8: Darkroom diagnostic: five-seed held-out reward, mean ± standard deviation, validation-best checkpoints. This protocol is separate from Table 1 .
Method / acquisition
Transitions
Pseudo-regret ↓
Bayes progress ↑
QTPT / fixed random corpus
1M
1.915±0.085
85.4%±5.1%
RL 2 / on-policy trials
1M
2.356±0.112
58.9%±6.7%
QTPT / fixed random corpus
5M
1.784±0.041
93.3%±2.5%
RL 2 / on-policy trials
5M
1.780±0.056
93.6%±3.4%
Appendix
Table 9: Bernoulli bandit, mean ± standard deviation over three independent training seeds.
Method
Pseudo-regret ↓
Mean training time (s)
QTPT
0.969±0.140
842.7
PEARL (fixed-offline adaptation)
2.472±0.074
1062.7
Appendix
Table 10: Fixed-offline continuous meta-bandit. Mean ± standard deviation over seven training seeds; pseudo-regret is ∑t=110(at−M)2 .
Backbone
Expert mix
QTPT gap ↓
SPT gap ↓
GPT-2
0.0
33.52±3.85
222.20±2.35
GPT-2
0.5
34.42±2.10
133.41±13.97
GPT-2
1.0
35.53±3.19
46.92±1.36
Llama
0.0
33.18±1.44
221.93±2.19
Llama
0.5
40.59±2.49
141.29±11.15
Llama
1.0
38.11±2.07
39.88±0.93
Appendix
Table 11: Cumulative gap at T=200 , mean ± standard deviation over five training seeds. Mix is expert-trajectory fraction. Lower is better.
Expert mix
QTPT gap ↓
SPT gap ↓
0.00
31.97±1.45
220.85±1.24
0.10
31.58±0.41
210.40±1.24
0.20
32.57±0.66
196.89±11.14
0.25
33.92±1.25
173.79±9.33
0.50
36.24±1.57
139.98±11.59
0.75
36.43±1.75
78.30±2.56
Appendix
Table 12: Values used for Figure 2(b) ; mean ± standard deviation over five training seeds per mixture.
Input protocol
Regret ↓
Success ↑
Action flip rate
Current-episode history
0.0033±0.0003
0.8808±0.0097
0.2036±0.0120
History + unrelated-task support
0.0037±0.0002
0.8694±0.0096
0.2220±0.0074
History + same-task support
0.0002±0.0000
0.9638±0.0069
0.0475±0.0099
Appendix
Table 13: Diagnostic A: extra-context source, five-seed mean ± standard deviation. Regret is true reward loss against the best grid action; success is the fraction choosing it.
Input protocol
Regret ↓
Success ↑
Action flip rate
No history
0.2386±0.0000
0.1172±0.0000
0.0000±0.0000
Current-episode history
0.0033±0.0003
0.8847±0.0100
0.1978±0.0203
Unrelated history (same length)
0.2386±0.0000
0.1172±0.0000
0.0000±0.0000
History + same-task support
0.0002±0.0001
0.9536±0.0112
0.0502±0.0052
Oracle goal token
0.0001±0.0001
0.9547±0.0110
0.0000±0.0000
Appendix
Table 14: Diagnostic B: task relevance versus token count, five-seed mean ± standard deviation. Displayed 0.0000 values are rounded.
Transformers can implement reinforcement-learning algorithms in context, yet how off-policy control emerges within Transformers remains poorly understood. We study Q-learning, a foundational model-free control algorithm that combines off-policy bootstrapping with greedy policy improvement. We prove that a two-layer attention-only Transformer can implement a finite-temperature form of Q-learning, where the temperature τ>0 controls the approximation to greedy action selection, recovering window Q-learning as τ→0. We train the structured Transformer on randomly generated MDP tasks using output-only supervision. Experiments show that the theoretically predicted parameter structure emerges during training, including the internal greedy action-selection mechanism, and that the trained Transformer achieves performance comparable to Q-learning on unseen tasks. Our theoretical analysis further shows that the equivalent realizations form a solution manifold and that, under suitable task-diversity conditions, the training iterates converge locally to this manifold at an exponential rate. For the random-task model analyzed here, the local worst-case number of gradient steps needed to reduce the distance to the manifold by a factor ε is Θ(τ−5log(1/ε)), quantifying the increasing optimization cost as the learned update approaches window Q-learning.
Yingjun Mo, Yajie Zhao, Fang Kong
Southern University of Science and Technology (SUSTech) · University of Electronic Science and Technology of China (UESTC)
In-context reinforcement learning (ICRL) studies agents that, after pretraining, adapt to new tasks by conditioning on additional context without parameter updates. Existing theoretical analyses of ICRL largely rely on linear attention, which replaces the softmax function in the standard attention with an identity mapping. This paper provides the first theoretical understanding of ICRL without making the unrealistic linear attention simplification. In particular, we consider the standard softmax attention used in practice. We show that, with certain parameters, the layerwise forward pass of a Transformer with such softmax attention is equivalent to iterative updates of a weighted softmax temporal difference (TD) learning algorithm. Here, weighted softmax TD is a new RL algorithm that performs policy evaluation in kernel space and adopts both linear TD and tabular TD as special cases. We also prove that under a certain contraction condition, the policy evaluation error decays as the number of layers grows, with the identified parameters above. Finally, we prove that those parameters are a global minimizer of a pretraining loss, explaining their emergence in our numerical experiments.
Zixuan Xie, Xinyu Liu, Claire Chen +3
University of Virginia · California Institute of Technology · Purdue University
This paper addresses the problem of safe offline reinforcement learning, which involves training a policy to satisfy safety constraints using an offline dataset. This problem is inherently challenging as it requires balancing three highly interconnected and competing objectives: satisfying safety constraints, maximizing rewards, and adhering to the behavior regularization imposed by the offline dataset. To tackle this trilogy challenge, we propose Q-learning Penalized Transformer policy (QPT), a training--inference consistent framework that bridges conditional sequence modeling with constraint-aware value estimation. QPT trains a Transformer policy that generates actions conditioned on trajectory context and target return/cost, retaining strong behavior regularization. To inject explicit safety semantics during learning, we augment sequence-model training with a Q-shaped penalty using learned reward and cost Q-functions to favor high return under low constraint violation. At inference, the same Q-functions enforce the cost threshold and choose the highest-reward feasible action, closing the loop between training and deployment. We provide a principled analysis under stylized near-deterministic CMDPs, characterizing how Q-penalized conditional generation improve safety and performance. Empirically, QPT consistently outperforms strong safe offline RL baselines across 38 tasks on the DSRL benchmark, and exhibits robust zero-shot adaptation to different constraint thresholds.
Shengchao Hu, Peng Wang, Jifeng Hu +5
Shanghai Jiao Tong University, China · Shenzhen Campus of Sun Yat-sen University, China · Jilin University, China +1