# tianshou **Repository Path**: nutquant/tianshou ## Basic Information - **Project Name**: tianshou - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-11 - **Last Updated**: 2025-11-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
--- [](https://pypi.org/project/tianshou/) [](https://github.com/conda-forge/tianshou-feedstock) [](https://tianshou.org/en/master/) [](https://github.com/thu-ml/tianshou/actions) [](https://codecov.io/gh/thu-ml/tianshou) [](https://github.com/thu-ml/tianshou/issues) [](https://github.com/thu-ml/tianshou/stargazers) [](https://github.com/thu-ml/tianshou/network) [](https://github.com/thu-ml/tianshou/blob/master/LICENSE) > ℹ️ **Introducing Tianshou version 2** > > We have released the second beta version 2.0.0b2 of the new major version of Tianshou on PyPI, and we invite you to try it! > Version 2 is a complete overhaul of the software design of the procedural API, in which > * we establish a clear separation between learning algorithms and policies (via the separate abstractions `Algorithm` and `Policy`). > * we provide more well-defined, more usable interfaces with extensive documentation of all algorithm and trainer parameters, > renaming some parameters to make their names more consistent and intuitive. > * the class hierarchy is fully revised, establishing a clear separation between on-policy, off-policy and offline algorithms > at the type level and ensuring that all inheritance relationships are meaningful. > > Because of the extent of the changes, this version is not backwards compatible with previous versions of Tianshou. > For migration information, please see the [change log](CHANGELOG.md). **Tianshou** ([天授](https://baike.baidu.com/item/%E5%A4%A9%E6%8E%88)) is a reinforcement learning (RL) library based on pure PyTorch and [Gymnasium](http://github.com/Farama-Foundation/Gymnasium). Tianshou's main features at a glance are: 1. Modular low-level interfaces for algorithm developers (RL researchers) that are both flexible, hackable and type-safe. 1. Convenient high-level interfaces for applications of RL (training an implemented algorithm on a custom environment). 1. Large scope: online (on- and off-policy) and offline RL, experimental support for multi-agent RL (MARL), experimental support for model-based RL, and more Unlike other reinforcement learning libraries, which may have complex codebases, unfriendly high-level APIs, or are not optimized for speed, Tianshou provides a high-performance, modularized framework and user-friendly interfaces for building deep reinforcement learning agents. One more aspect that sets Tianshou apart is its generality: it supports online and offline RL, multi-agent RL, and model-based algorithms. Tianshou aims at enabling concise implementations, both for researchers and practitioners, without sacrificing flexibility. Supported algorithms include: - [Deep Q-Network (DQN)](https://storage.googleapis.com/deepmind-media/dqn/DQNNaturePaper.pdf) - [Double DQN](https://arxiv.org/pdf/1509.06461.pdf) - [Dueling DQN](https://arxiv.org/pdf/1511.06581.pdf) - [Branching DQN](https://arxiv.org/pdf/1711.08946.pdf) - [Categorical DQN (C51)](https://arxiv.org/pdf/1707.06887.pdf) - [Rainbow DQN (Rainbow)](https://arxiv.org/pdf/1710.02298.pdf) - [Quantile Regression DQN (QRDQN)](https://arxiv.org/pdf/1710.10044.pdf) - [Implicit Quantile Network (IQN)](https://arxiv.org/pdf/1806.06923.pdf) - [Fully-parameterized Quantile Function (FQF)](https://arxiv.org/pdf/1911.02140.pdf) - [Policy Gradient (PG)](https://papers.nips.cc/paper/1713-policy-gradient-methods-for-reinforcement-learning-with-function-approximation.pdf) - [Natural Policy Gradient (NPG)](https://proceedings.neurips.cc/paper/2001/file/4b86abe48d358ecf194c56c69108433e-Paper.pdf) - [Advantage Actor-Critic (A2C)](https://openai.com/blog/baselines-acktr-a2c/) - [Trust Region Policy Optimization (TRPO)](https://arxiv.org/pdf/1502.05477.pdf) - [Proximal Policy Optimization (PPO)](https://arxiv.org/pdf/1707.06347.pdf) - [Deep Deterministic Policy Gradient (DDPG)](https://arxiv.org/pdf/1509.02971.pdf) - [Twin Delayed DDPG (TD3)](https://arxiv.org/pdf/1802.09477.pdf) - [Soft Actor-Critic (SAC)](https://arxiv.org/pdf/1812.05905.pdf) - [Randomized Ensembled Double Q-Learning (REDQ)](https://arxiv.org/pdf/2101.05982.pdf) - [Discrete Soft Actor-Critic (SAC-Discrete)](https://arxiv.org/pdf/1910.07207.pdf) - [Vanilla Imitation Learning](https://en.wikipedia.org/wiki/Apprenticeship_learning) - [Batch-Constrained deep Q-Learning (BCQ)](https://arxiv.org/pdf/1812.02900.pdf) - [Conservative Q-Learning (CQL)](https://arxiv.org/pdf/2006.04779.pdf) - [Twin Delayed DDPG with Behavior Cloning (TD3+BC)](https://arxiv.org/pdf/2106.06860.pdf) - [Discrete Batch-Constrained deep Q-Learning (BCQ-Discrete)](https://arxiv.org/pdf/1910.01708.pdf) - [Discrete Conservative Q-Learning (CQL-Discrete)](https://arxiv.org/pdf/2006.04779.pdf) - [Discrete Critic Regularized Regression (CRR-Discrete)](https://arxiv.org/pdf/2006.15134.pdf) - [Generative Adversarial Imitation Learning (GAIL)](https://arxiv.org/pdf/1606.03476.pdf) - [Prioritized Experience Replay (PER)](https://arxiv.org/pdf/1511.05952.pdf) - [Generalized Advantage Estimator (GAE)](https://arxiv.org/pdf/1506.02438.pdf) - [Posterior Sampling Reinforcement Learning (PSRL)](https://www.ece.uvic.ca/~bctill/papers/learning/Strens_2000.pdf) - [Intrinsic Curiosity Module (ICM)](https://arxiv.org/pdf/1705.05363.pdf) - [Hindsight Experience Replay (HER)](https://arxiv.org/pdf/1707.01495.pdf) Other noteworthy features: - Elegant framework with dual APIs: - Tianshou's high-level API maximizes ease of use for application development while still retaining a high degree of flexibility. - The fundamental procedural API provides a maximum of flexibility for algorithm development without being overly verbose. - State-of-the-art results in [MuJoCo benchmarks](https://github.com/thu-ml/tianshou/tree/master/examples/mujoco) for REINFORCE/A2C/TRPO/PPO/DDPG/TD3/SAC algorithms - Support for vectorized environments (synchronous or asynchronous) for all algorithms (see [usage](https://tianshou.readthedocs.io/en/master/01_tutorials/07_cheatsheet.html#parallel-sampling)) - Support for super-fast vectorized environments based on [EnvPool](https://github.com/sail-sg/envpool/) for all algorithms (see [usage](https://tianshou.readthedocs.io/en/master/01_tutorials/07_cheatsheet.html#envpool-integration)) - Support for recurrent state representations in actor networks and critic networks (RNN-style training for POMDPs) (see [usage](https://tianshou.readthedocs.io/en/master/01_tutorials/07_cheatsheet.html#rnn-style-training)) - Support any type of environment state/action (e.g. a dict, a self-defined class, ...) [Usage](https://tianshou.readthedocs.io/en/master/01_tutorials/07_cheatsheet.html#user-defined-environment-and-different-state-representation) - Support for customized training processes (see [usage](https://tianshou.readthedocs.io/en/master/01_tutorials/07_cheatsheet.html#customize-training-process)) - Support n-step returns estimation and prioritized experience replay for all Q-learning based algorithms; GAE, nstep and PER are highly optimized thanks to numba's just-in-time compilation and vectorized numpy operations - Support for multi-agent RL (see [usage](https://tianshou.readthedocs.io/en/master/01_tutorials/07_cheatsheet.html#multi-agent-reinforcement-learning)) - Support for logging based on both [TensorBoard](https://www.tensorflow.org/tensorboard) and [W&B](https://wandb.ai/) - Support for multi-GPU training (see [usage](https://tianshou.readthedocs.io/en/master/01_tutorials/07_cheatsheet.html#multi-gpu)) - Comprehensive documentation, PEP8 code-style checking, type checking and thorough [tests](https://github.com/thu-ml/tianshou/actions) In Chinese, Tianshou means divinely ordained, being derived to the gift of being born. Tianshou is a reinforcement learning platform, and the nature of RL is not learn from humans. So taking "Tianshou" means that there is no teacher to learn from, but rather to learn by oneself through constant interaction with the environment. “天授”意指上天所授,引申为与生具有的天赋。天授是强化学习平台,而强化学习算法并不是向人类学习的,所以取“天授”意思是没有老师来教,而是自己通过跟环境不断交互来进行学习。 ## Installation Tianshou is currently hosted on [PyPI](https://pypi.org/project/tianshou/) and [conda-forge](https://github.com/conda-forge/tianshou-feedstock). It requires Python >= 3.11. For installing the most recent version of Tianshou, the best way is clone the repository and install it with [poetry](https://python-poetry.org/) (which you need to install on your system first) ```bash git clone git@github.com:thu-ml/tianshou.git cd tianshou poetry install ``` You can also install the dev requirements by adding `--with dev` or the extras for say mujoco and acceleration by [envpool](https://github.com/sail-sg/envpool) by adding `--extras "mujoco envpool"` If you wish to install multiple extras, ensure that you include them in a single command. Sequential calls to `poetry install --extras xxx` will overwrite prior installations, leaving only the last specified extras installed. Or you may install all the following extras by adding `--all-extras`. Available extras are: - `atari` (for Atari environments) - `box2d` (for Box2D environments) - `classic_control` (for classic control (discrete) environments) - `mujoco` (for MuJoCo environments) - `mujoco-py` (for legacy mujoco-py environments[^1]) - `pybullet` (for pybullet environments) - `robotics` (for gymnasium-robotics environments) - `vizdoom` (for ViZDoom environments) - `envpool` (for [envpool](https://github.com/sail-sg/envpool/) integration) - `argparse` (in order to be able to run the high level API examples) [^1]: `mujoco-py` is a legacy package and is not recommended for new projects. It is only included for compatibility with older projects. Also note that there may be compatibility issues with macOS newer than Monterey. Otherwise, you can install the latest release from PyPI (currently far behind the master) with the following command: ```bash $ pip install tianshou ``` If you are using Anaconda or Miniconda, you can install Tianshou from conda-forge: ```bash $ conda install tianshou -c conda-forge ``` Alternatively to the poetry install, you can also install the latest source version through GitHub: ```bash $ pip install git+https://github.com/thu-ml/tianshou.git@master --upgrade ``` Finally, you may check the installation via your Python console as follows: ```python import tianshou print(tianshou.__version__) ``` If no errors are reported, you have successfully installed Tianshou. ## Documentation Find example scripts in the [test/]( https://github.com/thu-ml/tianshou/blob/master/test) and [examples/](https://github.com/thu-ml/tianshou/blob/master/examples) folders. Tutorials and API documentation are hosted on [tianshou.readthedocs.io](https://tianshou.readthedocs.io/). **Important**: The documentation is currently being updated to reflect the changes in Tianshou v2.0.0. Not all features are documented yet, and some parts are outdated (they are marked as such). The documentation will be fully updated when the v2.0.0 release is finalized. ## Why Tianshou? ### Comprehensive Functionality ### High Software Engineering Standards | RL Platform | Documentation | Code Coverage | Type Hints | Last Update | | ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------ | ----------------------------------------------------------------------------------------------------------------- | | [Stable-Baselines3](https://github.com/DLR-RM/stable-baselines3) | [](https://stable-baselines3.readthedocs.io/en/master/?badge=master) | [](https://gitlab.com/araffin/stable-baselines3/-/commits/master) | :heavy_check_mark: |  | | [Ray/RLlib](https://github.com/ray-project/ray/tree/master/rllib/) | [](http://docs.ray.io/en/master/rllib.html) | :heavy_minus_sign:(1) | :heavy_check_mark: |  | | [SpinningUp](https://github.com/openai/spinningup) | [](https://spinningup.openai.com/) | :x: | :x: |  | | [Dopamine](https://github.com/google/dopamine) | [](https://github.com/google/dopamine/tree/master/docs) | :x: | :x: |  | | [ACME](https://github.com/deepmind/acme) | [](https://github.com/deepmind/acme/blob/master/docs/index.md) | :heavy_minus_sign:(1) | :heavy_check_mark: |  | | [Sample Factory](https://github.com/alex-petrenko/sample-factory) | [:heavy_minus_sign:](https://arxiv.org/abs/2006.11751) | [](https://codecov.io/gh/alex-petrenko/sample-factory) | :x: |  | | | | | | | | [Tianshou](https://github.com/thu-ml/tianshou) | [](https://tianshou.readthedocs.io/en/master) | [](https://codecov.io/gh/thu-ml/tianshou) | :heavy_check_mark: |  | (1): it has continuous integration but the coverage rate is not available ### Reproducible, High-Quality Results Tianshou is rigorously tested. In contrast to other RL platforms, **our tests include the full agent training procedure for all of the implemented algorithms**. Our tests would fail once if any of the agents failed to achieve a consistent level of performance on limited epochs. Our tests thus ensure reproducibility. Check out the [GitHub Actions](https://github.com/thu-ml/tianshou/actions) page for more detail. Atari and MuJoCo benchmark results can be found in the [examples/atari/](examples/atari/) and [examples/mujoco/](examples/mujoco/) folders respectively. **Our MuJoCo results reach or exceed the level of performance of most existing benchmarks.** ### Algorithm Abstraction Reinforcement learning algorithms are build on abstractions for - on-policy algorithms (`OnPolicyAlgorithm`), - off-policy algorithms (`OffPolicyAlgorithm`), and - offline algorithms (`OfflineAlgorithm`), all of which clearly separate the core algorithm from the training process and the respective environment interactions. In each case, the implementation of an algorithm necessarily involves only the implementation of methods for - pre-processing a batch of data, augmenting it with necessary information/sufficient statistics for learning (`_preprocess_batch`), - updating model parameters based on an augmented batch of data (`_update_with_batch`). The implementation of these methods suffices for a new algorithm to be applicable within Tianshou, making experimentation with new approaches particularly straightforward. ## Quick Start Tianshou provides two API levels: - the high-level interface, which provides ease of use for end users seeking to run deep reinforcement learning applications - the procedural interface, which provides a maximum of control, especially for very advanced users and developers of reinforcement learning algorithms. In the following, let us consider an example application using the _CartPole_ gymnasium environment. We shall apply the deep Q-network (DQN) learning algorithm using both APIs. ### High-Level API In the high-level API, the basis for an RL experiment is an `ExperimentBuilder` with which we can build the experiment we then seek to run. Since we want to use DQN, we use the specialization `DQNExperimentBuilder`. The high-level API provides largely declarative semantics, i.e. the code is almost exclusively concerned with configuration that controls what to do (rather than how to do it). ```python from tianshou.highlevel.config import OffPolicyTrainingConfig from tianshou.highlevel.env import ( EnvFactoryRegistered, VectorEnvType, ) from tianshou.highlevel.experiment import DQNExperimentBuilder, ExperimentConfig from tianshou.highlevel.params.algorithm_params import DQNParams from tianshou.highlevel.trainer import ( EpochStopCallbackRewardThreshold, ) experiment = ( DQNExperimentBuilder( EnvFactoryRegistered( task="CartPole-v1", venv_type=VectorEnvType.DUMMY, train_seed=0, test_seed=10, ), ExperimentConfig( persistence_enabled=False, watch=True, watch_render=1 / 35, watch_num_episodes=100, ), OffPolicyTrainingConfig( max_epochs=10, epoch_num_steps=10000, batch_size=64, num_train_envs=10, num_test_envs=100, buffer_size=20000, collection_step_num_env_steps=10, update_step_num_gradient_steps_per_sample=1 / 10, ), ) .with_dqn_params( DQNParams( lr=1e-3, gamma=0.9, n_step_return_horizon=3, target_update_freq=320, eps_training=0.3, eps_inference=0.0, ), ) .with_model_factory_default(hidden_sizes=(64, 64)) .with_epoch_stop_callback(EpochStopCallbackRewardThreshold(195)) .build() ) experiment.run() ``` The experiment builder takes three arguments: - the environment factory for the creation of environments. In this case, we use an existing factory implementation for gymnasium environments. - the experiment configuration, which controls persistence and the overall experiment flow. In this case, we have configured that we want to observe the agent's behavior after it is trained (`watch=True`) for a number of episodes (`watch_num_episodes=100`). We have disabled persistence, because we do not want to save training logs, the agent or its configuration for future use. - the training configuration, which controls fundamental training parameters, such as the total number of epochs we run the experiment for (`num_epochs=10`) and the number of environment steps each epoch shall consist of (`epoch_num_steps=10000`). Every epoch consists of a series of data collection (rollout) steps and training steps. The parameter `collection_step_num_env_steps` controls the amount of data that is collected in each collection step and after each collection step, we perform a training step, applying a gradient-based update based on a sample of data (`batch_size=64`) taken from the buffer of data that has been collected. For further details, see the documentation of configuration class. We then proceed to configure some of the parameters of the DQN algorithm itself: For instance, we control the epsilon parameter for exploration. We want to use random exploration during rollouts for training (`eps_training`), but we don't when evaluating the agent's performance in the test environments (`eps_inference`). Furthermore, we configure model parameters of the network for the Q function, parametrising the number of hidden layers of the default MLP factory. Find the script in [examples/discrete/discrete_dqn_hl.py](examples/discrete/discrete_dqn_hl.py). Here's a run (with the training time cut short):