lucidrains/q-transformer
Implementation of Q-Transformer, Scalable Offline Reinforcement Learning via Autoregressive Q-Functions, out of Google Deepmind
Q‑Transformer – offline RL with autoregressive Q‑functions
What it is – A PyTorch implementation of the Q‑Transformer paper (Google DeepMind, 2023). It treats the Q‑function as an autoregressive model that predicts a sequence of discrete actions, enabling scalable offline reinforcement learning for robotic tasks.
Key components
QRoboticTransformer– a vision‑transformer backbone (based on MaxViT) that ingests video frames and optional text instructions, and outputs Q‑values for a discretised action space.QLearner– training loop that performs Q‑learning on a replay‑memory dataset generated by the agent.Agent– runs the model in a user‑provided environment (subclass ofBaseEnvironment) to collect trajectories.ReplayMemoryDataset– a simple dataset wrapper for the stored (state, action, reward, next‑state, done) tuples.
How to get it
pip install q-transformer
Typical workflow
- Define an environment – implement
BaseEnvironmentor use the suppliedMockEnvironmentwhich expects video tensors and instruction embeddings. - Create the model – instantiate
QRoboticTransformerwith vision hyper‑parameters (e.g., a small MaxViT) and specify the number of discrete actions and bins. - Collect data –
Agent(model, environment, ...)()runs episodes, stores transitions in a replay buffer, and writes them to disk. - Train –
QLearner(model, dataset=ReplayMemoryDataset(), ...)()runs Q‑learning (supports dueling heads, n‑step returns, gradient accumulation, etc.). - Infer actions – after training, call
model.get_optimal_actions(video, instructions)to obtain the best discrete action sequence for new video‑instruction pairs.
What makes it special
- Implements the autoregressive Q‑function idea, allowing the model to handle multi‑dimensional discrete action spaces efficiently.
- Includes optional dueling architecture, n‑step returns, and a conservative regularisation placeholder.
- Designed for robotics: the model processes multi‑camera video (
(3, 6, 224, 224)) and language instructions. - The repo tracks a detailed TODO list that mirrors ongoing research directions (e.g., beam‑search decoding, cross‑attention to past actions, gumbel‑based exploration).
Who might use it
- Researchers experimenting with offline RL for manipulation or embodied agents.
- Practitioners who need a ready‑to‑run baseline for discretised robotic control from video and language.
- Anyone looking to reproduce or extend the Q‑Transformer paper (the implementation has been reproduced by Kotb et al., 2024).
Citations – The README provides BibTeX entries for the original Q‑Transformer paper and related works (FlashAttention, continual learning regularisation, etc.).
All details above are taken directly from the repository’s README.
Related
- Dispatch
- Dispatch
- Dispatch
- Dispatch
- Project