Skip to content

Guide · Open source

How to use tianshou: install and train a reinforcement learning agent

Updated · written with AI from the project's own documentation and checked against our catalogue data

Tianshou is an open-source reinforcement learning library built on PyTorch and Gymnasium. It provides ready-made algorithms such as DQN, PPO, SAC and TD3, so you can train agents that learn by interacting with an environment. This guide explains what it is good for, how to install it, and how its high-level and low-level interfaces fit together.

Tianshouthu-ml/tianshou · ★ 11k · MIT

Quick start

  1. 1Check your Python version. Confirm you have Python 3.11 or newer and that PyTorch is available in your environment.
  2. 2Install tianshou. Run pip install tianshou to install the library from PyPI.
  3. 3Pick an algorithm. Choose a supported algorithm such as DQN, PPO or SAC that matches your action space.
  4. 4Prepare a Gymnasium environment. Use a Gymnasium-style environment, either a built-in one or your own.
  5. 5Train with the high-level or procedural API. Use the high-level interface for standard training, or the procedural API when you need to modify the algorithm.
  6. 6Review the documentation. Read the cheatsheet at tianshou.readthedocs.io for vectorized environments, logging and multi-agent examples.

What is tianshou and who is it for?

Tianshou is a reinforcement learning (RL) library based on pure PyTorch and Gymnasium. It offers modular low-level interfaces for algorithm developers and higher-level interfaces for applying an implemented algorithm to a custom environment.

It is best suited to researchers who want to implement or modify RL algorithms, practitioners training a supported algorithm on their own Gymnasium-style environment, students studying how common algorithms are built, and developers who want vectorized or parallel environment sampling.

  • Requires Python 3.11 or newer and PyTorch.
  • Covers online (on- and off-policy) and offline RL, with experimental support for multi-agent and model-based RL.
  • The README is a library reference, not a beginner course.

Install tianshou with pip

Tianshou is published on PyPI and conda-forge and requires Python 3.11 or newer. Install it with pip from PyPI.

For the most recent version, the project's README suggests cloning the repository and installing from source, using poetry.

pip install tianshou git clone https://github.com/thu-ml/tianshou.git

Choose an algorithm for your problem

Tianshou supports a long list of algorithms. Common starting points include DQN for discrete actions, and PPO, SAC and TD3 for continuous control. Its family also includes Double DQN, Rainbow, A2C, TRPO, DDPG, REDQ and imitation learning methods such as GAIL, plus offline methods such as CQL and TD3+BC.

Pick the algorithm whose assumptions match your action space and whether you can collect new experience during training.

  • DQN, Double DQN, Dueling DQN, Rainbow: Q-learning family.
  • PPO, A2C, TRPO: policy gradient methods.
  • DDPG, TD3, SAC: off-policy methods for continuous actions.
  • CQL, BCQ, TD3+BC: offline RL from fixed datasets.

Use the high-level interface or the procedural API

The high-level interface maximizes ease of use for applying existing algorithms to your own environment. The procedural API gives more flexibility for algorithm development without being overly verbose.

Version 2 separates learning algorithms from policies through the Algorithm and Policy abstractions, and it is not backwards compatible with earlier versions. Check the change log before migrating older code.

  • Use the high-level API to train a supported algorithm quickly.
  • Use the procedural API when you need to change how an algorithm works.
  • Consult the CHANGELOG.md file in the repository when migrating from an older release.

Speed up sampling and extend your setup

Tianshou supports synchronous or asynchronous vectorized environments for all algorithms, so you can sample experience in parallel. It also supports EnvPool-based vectorized environments, multi-GPU training, and logging to TensorBoard or W&B.

Other documented options include recurrent networks for partially observable problems, custom state and action types, and customized training processes. The cheatsheet in the documentation shows how to use each one.

  • Parallel sampling with vectorized environments.
  • EnvPool integration for faster environments.
  • RNN-style training for POMDPs.
  • Logging with TensorBoard or Weights & Biases.
  • Multi-GPU training and multi-agent RL (experimental areas noted in the README).

What people use it for

  • Training a PPO or SAC agent on a custom Gymnasium environment.
  • Implementing or modifying a new RL algorithm in PyTorch for research.
  • Studying how DQN, PPO or SAC are implemented in a readable codebase.
  • Running vectorized or EnvPool-based environment sampling to speed up experiments.
  • Training offline agents from fixed datasets with CQL or TD3+BC.

Frequently asked questions

Is tianshou free to use?

Yes. Tianshou is released under the MIT licence, which the repository's LICENSE file covers. Check the licence file for the full terms.

What are the requirements for tianshou?

Tianshou requires Python 3.11 or newer and PyTorch. It is built on PyTorch and Gymnasium, so you need an environment compatible with the Gymnasium interface.

How do I install tianshou?

Run pip install tianshou to install from PyPI. You can also clone the repository from GitHub to install the latest version.

What is tianshou alternative?

Comparable projects named in the analysis include Stable-Baselines3, RLlib, CleanRL and Dopamine. Each has a different focus, so compare them against your needs.

Is tianshou good for beginners?

Not really. The README is a library reference rather than a guided course, so beginners may want a introductory RL course first.

Why did my tianshou code break after upgrading?

Version 2 is not backwards compatible with earlier versions because of a major redesign. Read the CHANGELOG.md file in the repository and update your code to the new Algorithm and Policy abstractions.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Tianshou suits researchers, students and practitioners who want readable PyTorch implementations of many RL algorithms and fast vectorized sampling. Beginners looking for a guided course, teams needing a non-Python stack, or anyone who needs a frozen API should pick another option such as Stable-Baselines3 or CleanRL.

Install options & downloads