tianshou

thu-mlMIT更新放缓

基于 PyTorch 的深度强化学习库,提供 DQN、DDPG、A2C、模仿学习等算法实现,接口简洁,方便快速搭建和对比实验。

An elegant PyTorch deep reinforcement learning library.

v2.0.1发布于 2026-04-02 20:52 UTC11,054star +2 / 7 天1,350fork同步于 2026-11-10 23:30 UTC

怎么上手

由 README 提炼,执行前请确认
pip install tianshou
硬件要求按 README 说明

README 未说明具体显存或 GPU 型号要求;库基于纯 PyTorch,可随 PyTorch 在 CPU 或 GPU 上运行。

多卡 / 张量并行
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

纯 PyTorch 深度强化学习库,模块化接口兼顾算法研究与工程应用

基于 PyTorch 的深度强化学习库,提供 DQN、DDPG、A2C、模仿学习等算法实现,接口简洁,方便快速搭建和对比实验。

核心能力

  • 分离 Algorithm 与 Policy 抽象,接口类型安全易改
  • 高层 API 支持在自定义环境上直接训练已实现算法
  • 覆盖在线 on/off-policy 与离线 RL,实验性支持多智能体与基于模型 RL
  • 内置 DQN、PPO、SAC、TD3、BCQ、CQL、GAIL 等大量算法
  • 支持同步/异步向量化环境与 EnvPool 加速采样

容易踩的坑

需要 Python >= 3.11;PyPI 版本落后于 master,要最新功能需从源码用 poetry 安装;Atari、MuJoCo、EnvPool 等环境需额外安装对应 extras,且多个 extras 必须在同一条命令中指定。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v2.0.1
  1. v2.0.1最新正式版2026-04-02 · 2026-04-02 20:52 UTC

    This is maintenance release.

    For details, please refer to the change log.

  2. v2.0.02025-12-01 · 2025-12-01 11:30 UTC
    查看更新说明

    The key feature of version 2 is a complete overhaul of the software design of the procedural API (i.e. Tianshou's core implementation), 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.

    This release is brought to you by Applied AI Institute gGmbH.

    Developers:

    • Dr. Dominik Jain (@opcode81)
    • Michael Panchenko (@MischaPanch)

    For details, please refer to the change log.

  3. v2.0.0b2v2.0.0b2 (beta release)pre-release2025-10-22 · 2025-10-22 18:32 UTC
    查看更新说明

    This is the second pre-release of Tianshou v2.0.

    With this version, we provide

    • enhanced compatibility with newer Python versions, particularly 3.12 and 3.13
    • less restrictive dependency constraints, supporting newer versions of numpy and gymnasium in particular

    Please test it and let us know how it works!

  4. v2.0.0b1v2.0.0b1 (beta release)pre-release2025-07-15 · 2025-07-15 08:41 UTC
    查看更新说明

    This is a pre-release (beta) of the 2.0.0 version of tianshou, the full release notes and an updated documentation will be created at the release of the non-beta version.

    The code on master can be considered stable, see the changelog for changes. Only minor changes, and likely no breaking ones, will be added for the full 2.0.0 release.

    The main things missing are an enhanced benchmarking and documentation

  5. v1.2.02025-06-23 · 2025-06-23 12:37 UTC
    查看更新说明

    This is the last major release before version 2.0.0

    It solves the regression in data collection performance, introduces several fixes, and importantly, adds support for determinism testing, which is used to ensure that the refactoring in the upcoming 2.0.0 release does not affect any aspect of training or inference

    Changes/Improvements

    • trainer:
      • Custom scoring now supported for selecting the best model. #1202
    • highlevel:
      • DiscreteSACExperimentBuilder: Expose method with_actor_factory_default #1248 #1250
      • ActorFactoryDefault: Fix parameters for hidden sizes and activation not being passed on in the discrete case (affects with_actor_factory_default method of experiment builders)
      • ExperimentConfig: Do not inherit from other classes, as this breaks automatic handling by jsonargparse when the class is used to define interfaces (as in high-level API examples)
      • AutoAlphaFactoryDefault: Differentiate discrete and continuous action spaces and allow coefficient to be modified, adding an informative docstring (previous implementation was reasonable only for continuous action spaces)
        • Adjust usage in atari_sac_hl example accordingly.
      • NPGAgentFactory, TRPOAgentFactory: Fix optimizer instantiation including the actor parameters (which was misleadingly suggested in the docstring in the respective policy classes; docstrings were fixed), as the actor parameters are intended to be handled via natural gradients internally
    • data:
      • ReplayBuffer: Fix collection of empty episodes being disallowed
      • Collection was slow due to isinstance checks on Protocols and due to Buffer integrity validation. This was solved by no longer performing isinstance on Protocols and by making the integrity validation disabled by default.
    • Tests:
      • We have introduced extensive determinism tests which allow to validate whether training processes deterministically compute the same results across different development branches. This is an important step towards ensuring reproducibility and consistency, which will be instrumental in supporting Tianshou developers in their work, especially in the context of algorithm development and evaluation.

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    PyPI Read the Docs Pytest codecov GitHub issues GitHub stars GitHub forks GitHub license

    [!NOTE] Tianshou version 2 is here!

    We have released the new major version of Tianshou on PyPI.
    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.

    Tianshou (天授) is a reinforcement learning (RL) library based on pure PyTorch and 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.
    2. Convenient high-level interfaces for applications of RL (training an implemented algorithm on a custom environment).
    3. 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:

    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 for REINFORCE/A2C/TRPO/PPO/DDPG/TD3/SAC algorithms
    • Support for vectorized environments (synchronous or asynchronous) for all algorithms (see usage)
    • Support for super-fast vectorized environments based on EnvPool for all algorithms (see usage)
    • Support for recurrent state representations in actor networks and critic networks (RNN-style training for POMDPs) (see usage)
    • Support any type of environment state/action (e.g. a dict, a self-defined class, ...) Usage
    • Support for customized training processes (see usage)
    • 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)
    • Support for logging based on both TensorBoard and W&B
    • Support for multi-GPU training (see usage)
    • Comprehensive documentation, PEP8 code-style checking, type checking and thorough tests

    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 and conda-forge. 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 (which you need to install on your system first)

    git clone [email protected]: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 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 environments1)
    • pybullet (for pybullet environments)
    • robotics (for gymnasium-robotics environments)
    • vizdoom (for ViZDoom environments)
    • envpool (for envpool integration)
    • argparse (in order to be able to run the high level API examples)

    Otherwise, you can install the latest release from PyPI (currently far behind the master) with the following command:

    $ pip install tianshou
    

    If you are using Anaconda or Miniconda, you can install Tianshou from conda-forge:

    $ conda install tianshou -c conda-forge
    

    Alternatively to the poetry install, you can also install the latest source version through GitHub:

    $ pip install git+https://github.com/thu-ml/tianshou.git@master --upgrade
    

    Finally, you may check the installation via your Python console as follows:

    import tianshou
    print(tianshou.__version__)
    

    If no errors are reported, you have successfully installed Tianshou.

    Documentation

    Find example scripts in the test/ and examples/ folders.

    Tutorials and API documentation are hosted on tianshou.readthedocs.io.

    Why Tianshou?

    Comprehensive Functionality

    High Software Engineering Standards

    RL PlatformDocumentationCode CoverageType HintsLast Update
    Stable-Baselines3Documentation Statuscoverage report:heavy_check_mark:GitHub last commit
    Ray/RLlib:heavy_minus_sign:(1):heavy_check_mark:GitHub last commit
    SpinningUp:x::x:GitHub last commit
    Dopamine:x::x:GitHub last commit
    ACME:heavy_minus_sign:(1):heavy_check_mark:GitHub last commit
    Sample Factory:heavy_minus_sign:codecov:x:GitHub last commit
    TianshouRead the Docscodecov:heavy_check_mark:GitHub last commit

    (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 page for more detail.

    Atari and MuJoCo benchmark results can be found in the examples/atari/ and 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).

    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,
                training_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_training_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. Here's a run (with the training time cut short):

    Find many further applications of the high-level API in the examples/ folder; look for scripts ending with _hl.py. Note that most of these examples require the extra argparse (install it by adding --extras argparse when invoking poetry).

    Procedural API

    Let us now consider an analogous example in the procedural API. Find the full script in examples/discrete/discrete_dqn.py.

    First, import the relevant packages:

    import gymnasium as gym
    import tianshou as ts
    from tianshou.algorithm.modelfree.dqn import DiscreteQLearningPolicy
    from tianshou.algorithm.optim import AdamOptimizerFactory
    from tianshou.data import CollectStats
    from tianshou.trainer import OffPolicyTrainerParams
    from tianshou.utils.net.common import Net
    from tianshou.utils.space_info import SpaceInfo
    from torch.utils.tensorboard import SummaryWriter
    

    Define hyper-parameters:

    task = 'CartPole-v1'
    lr, epoch, batch_size = 1e-3, 10, 64
    num_training_envs, num_test_envs = 10, 100
    gamma, n_step, target_freq = 0.9, 3, 320
    buffer_size = 20000
    eps_train, eps_test = 0.1, 0.05
    epoch_num_steps, collection_step_num_env_steps = 10000, 10
    

    Initialize the logger:

    logger = ts.utils.TensorboardLogger(SummaryWriter('log/dqn'))
    

    Create the environments:

    # You can also try SubprocVectorEnv, which will use parallelization
    training_envs = ts.env.DummyVectorEnv([lambda: gym.make(task) for _ in range(num_training_envs)])
    test_envs = ts.env.DummyVectorEnv([lambda: gym.make(task) for _ in range(num_test_envs)])
    

    Create the network, policy, and algorithm:

    # Create the network
    # Note: You can easily define other networks.
    # See https://tianshou.readthedocs.io/en/master/01_tutorials/00_dqn.html#build-the-network
    env = gym.make(task, render_mode="human")
    assert isinstance(env.action_space, gym.spaces.Discrete)
    space_info = SpaceInfo.from_env(env)
    state_shape = space_info.observation_info.obs_shape
    action_shape = space_info.action_info.action_shape
    net = Net(state_shape=state_shape, action_shape=action_shape, hidden_sizes=[128, 128, 128])
    optim = AdamOptimizerFactory(lr=lr)
    
    # Create the policy
    policy = DiscreteQLearningPolicy(
        model=net,
        action_space=env.action_space,
        eps_training=eps_train,
        eps_inference=eps_test
    )
    
    # Create the algorithm with the policy and optimizer factory
    algorithm = DQN(
        policy=policy,
        optim=AdamOptimizerFactory(lr=lr),
        gamma=gamma,
        n_step_return_horizon=n_step,
        target_update_freq=target_freq
    )
    

    Set up the collectors:

    training_collector = ts.data.Collector[CollectStats](
      algorithm,
      training_envs,
      ts.data.VectorReplayBuffer(buffer_size, num_training_envs),
      exploration_noise=True,
    )
    test_collector = ts.data.Collector[CollectStats](
      algorithm,
      test_envs,
      exploration_noise=True,
    ) 
    

    Let's train the model using the algorithm:

    result = algorithm.run_training(
      OffPolicyTrainerParams(
        training_collector=training_collector,
        test_collector=test_collector,
        max_epochs=epoch,
        epoch_num_steps=epoch_num_steps,
        collection_step_num_env_steps=collection_step_num_env_steps,
        test_step_num_episodes=num_test_envs,
        batch_size=batch_size,
        update_step_num_gradient_steps_per_sample=1 / collection_step_num_env_steps,
        stop_fn=lambda mean_rewards: mean_rewards >= env.spec.reward_threshold,
        logger=logger,
        test_in_training=True,
      )
    )
    print(f"Finished training in {result.timing.total_time} seconds")
    

    This is how you could manually save/load the trained policy (it's exactly the same as loading a torch.nn.module):

    torch.save(policy.state_dict(), 'dqn.pth')
    policy.load_state_dict(torch.load('dqn.pth'))
    

    Now let's watch the agent with 35 FPS:

    collector = ts.data.Collector(policy, env, exploration_noise=True)
    collector.collect(n_episode=1, render=1 / 35)
    

    Inspect the data saved in TensorBoard:

    $ tensorboard --logdir log/dqn
    

    Please read the documentation for advanced usage.

    Contributing

    Tianshou is still under development. Further algorithms and features are continuously being added, and we always welcome contributions to help make Tianshou better. If you would like to contribute, please check out this link.

    Citing Tianshou

    If you find Tianshou useful, please cite it in your publications.

    @article{tianshou,
      author  = {Jiayi Weng and Huayu Chen and Dong Yan and Kaichao You and Alexis Duburcq and Minghao Zhang and Yi Su and Hang Su and Jun Zhu},
      title   = {Tianshou: A Highly Modularized Deep Reinforcement Learning Library},
      journal = {Journal of Machine Learning Research},
      year    = {2022},
      volume  = {23},
      number  = {267},
      pages   = {1--6},
      url     = {http://jmlr.org/papers/v23/21-1127.html}
    }
    

    Acknowledgments

    Tianshou is supported by appliedAI Institute for Europe, a non-profit organization committed to providing long-term support and development.

    Tianshou was previously a reinforcement learning platform based on TensorFlow. You can check out the branch priv for more detail. Many thanks to Haosheng Zou's pioneering work for Tianshou before version 0.1.1.

    We would like to thank TSAIL and Institute for Artificial Intelligence, Tsinghua University for providing such an excellent AI research platform.

    Footnotes

    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. ↩