---
title: "Train your first Decision Transformer"
description: "Decision Transformers recast reinforcement learning as conditional sequence modeling: a GPT-2 model takes the last K timesteps of return-to-go, state and action tokens and autoregressively predicts actions that achieve a desired return."
url: "https://swe.quest/articles/train-your-first-decision-transformer-d9877713a380ce1a"
canonical: "https://huggingface.co/blog/train-decision-transformers"
source: "Hugging Face"
published: "2022-09-08"
difficulty: "hard"
kind: "How-to"
reading_minutes: 8
tags: ["model-training","machine-learning","python"]
---

# Train your first Decision Transformer

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-09-08, 8 min read, How-to. Original: [https://huggingface.co/blog/train-decision-transformers](https://huggingface.co/blog/train-decision-transformers)

## Summary

Decision Transformers recast reinforcement learning as conditional sequence modeling: a GPT-2 model takes the last K timesteps of return-to-go, state and action tokens and autoregressively predicts actions that achieve a desired return. This walkthrough trains an offline Decision Transformer from scratch on the HalfCheetah expert dataset from the Hugging Face hub. A custom data collator normalizes states, computes returns-to-go, scales rewards by 1000, pads sequences and samples trajectories by length. A subclassed model adds an MSE action loss so the standard transformers Trainer can be used, and training takes about 40 minutes in Colab versus roughly 3 hours in the original paper.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- reinforcement learning basics ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- transformer architecture ([easy posts on System design](https://swe.quest/tags/system-design.md?difficulty=easy))
- PyTorch
- Hugging Face transformers Trainer

## Keywords

Decision Transformer, Hugging Face transformers, GPT-2, MuJoCo HalfCheetah, offline reinforcement learning, Gym, Hugging Face datasets

## Related posts

- [Introducing Decision Transformers on Hugging Face 🤗](https://swe.quest/articles/introducing-decision-transformers-on-hugging-face-53b9486d51c912ca.md): Hugging Face · 2022-03-28 · Medium · 8 min read · Machine learning, Python, Open source
  Decision Transformer, an offline reinforcement learning method that recasts RL as return-conditioned sequence modeling, is now part of the 🤗 transformers library, with nine pretrained checkpoints for Gym continuous-control tasks (Hopper, Walker2D, HalfCheetah).
- [Jack of All Trades, Master of Some, a Multi-Purpose Transformer Agent](https://swe.quest/articles/jack-of-all-trades-master-of-some-a-multi-purpose-transformer-agent-eac8be04880a3ed1.md): Hugging Face · 2024-04-22 · Hard · 8 min read · Machine learning, Model training, AI agents
  JAT (Jack of All Trades) is an open reproduction of DeepMind's Gato: a single GPT-Neo-based Transformer trained by behavioral cloning on a new dataset of expert trajectories from Atari, BabyAI, Meta-World and MuJoCo, plus text and image-caption data.
- [Decision Models in Hugging Face](https://swe.quest/articles/decision-models-in-hugging-face-8e72d537e83c9634.md): Hugging Face · 2026-11-10 · Medium · 16 min read · LLMs, Machine learning, Inference & serving, Python
  Decision models take a state plus a set of typed questions with predefined answers and return structured decisions with probabilities instead of generating text, avoiding schema violations and autoregressive decoding overhead.
- [Turning Database Grading into RL Rewards: Post-Training Agents with ThinkingBox](https://swe.quest/articles/turning-database-grading-into-rl-rewards-post-training-agents-with-thinkingbox-9126975f97c5fba4.md): Hugging Face · 2026-10-22 · Hard · 18 min read · Model training, AI agents, Evaluation, LLMs
  Executable checks on terminal database state, originally built to grade agents in ThinkingBox-Bench, double as binary RL rewards for post-training tool-using agents.
- [User foundation models for Grab](https://swe.quest/articles/user-foundation-models-for-grab-98db1323e8d3fa5a.md): Grab Engineering · 2025-09-26 · Hard · 18 min read · Recommendations & ranking, Model training, Machine learning, ML platforms
  Grab built a transformer-based user foundation model that jointly learns from tabular user attributes and time-series clickstream data to produce general-purpose user embeddings, replacing siloed hand-engineered features.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
