Registry / ai-ml / tianshou

tianshou

JSON →
library2.0.1pypypiunverified

A library for deep reinforcement learning, providing a modular and flexible framework for implementing and benchmarking RL algorithms. Current version is 2.0.1, released in 2025. The library follows a steady release cadence, with major version 2.0 overhauling the procedural API and separating algorithms from policies.

pip install tianshou
INSTALL
IMPORT
SIG · TIANSHOU
T
tianshou
ai-mlpythonv2.0.1
Install
83.0s avg
Import
Disk
5274MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 86.3s
py 3.11
✕ build_error
✓ 88.2s
py 3.12
✕ build_error
✓ 80.7s
py 3.13
✕ build_error
✓ 76.9s
py 3.9
✕ build_error
✕ timeout
5274MB installed
● package 5274MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Policy
from tianshu.policy import Policy
from tianshou.policy import Policy

Train a DQN agent on CartPole-v1 using off-policy training.

import gymnasium as gym from tianshou.data import Collector, VectorReplayBuffer from tianshou.env import DummyVectorEnv from tianshou.policy import DQNPolicy from tianshou.trainer import offpolicy_trainer import torch env = gym.make('CartPole-v1') train_envs = DummyVectorEnv([lambda: gym.make('CartPole-v1') for _ in range(10)]) test_envs = DummyVectorEnv([lambda: gym.make('CartPole-v1') for _ in range(10)]) state_shape = env.observation_space.shape or env.observation_space.n action_shape = env.action_space.shape or env.action_space.n policy = DQNPolicy( state_shape=state_shape, action_shape=action_shape, model=torch.nn.Linear(state_shape, action_shape), optim=torch.optim.Adam(policy_network.parameters(), lr=1e-3), ).to('cpu') buffer = VectorReplayBuffer(total_size=20000, buffer_num=len(train_envs)) collector = Collector(policy, train_envs, buffer) test_collector = Collector(policy, test_envs) def stop_fn(reward): return reward >= 195 result = offpolicy_trainer( policy=policy, train_collector=collector, test_collector=test_collector, max_epoch=10, step_per_epoch=1000, step_per_collect=10, episode_per_test=5, batch_size=64, stop_fn=stop_fn, update_per_step=1, ) print(f'Finished training in {result.timing.total_time_seconds}s')
Debug
Known issues
breakingv2.0.0 completely overhauled the API. `Policy` and `Algorithm` abstractions are separated; many imports changed. Old code using `from tianshou import Policy` will break.
fix
Update imports to use submodules (e.g., `from tianshou.policy import ...`) and adopt the new `Algorithm`/`Policy` split.
affects: <2.0.0
breakingIn v2.0.0, `ReplayBuffer` was moved from `tianshou.data` (already existed) but some internal APIs changed. The `VectorReplayBuffer` is now the recommended buffer for vectorized environments.
fix
Use `from tianshou.data import VectorReplayBuffer` and ensure buffer API calls are updated.
affects: <2.0.0
gotchaTianshou 2.0.0+ requires Python 3.11+ and drops support for older Python versions. Also, `gymnasium` is used instead of `gym`.
fix
Upgrade to Python 3.11+ and install `gymnasium` if you rely on custom environments.
affects: >=2.0.0
deprecatedThe `trainer` module's API (e.g., `offpolicy_trainer`) is being replaced in future versions by a more streamlined high-level interface. Check documentation for upcoming changes.
fix
Consider using the high-level `experiment` package for new projects.
affects: >=2.0.0
Upgrade
Version history
2.0.1latest on PyPI · released Apr 2, 2026
Audit
Dependencies
gymnasiumrequiredEnvironment interface
torchrequiredNeural network backend
numpyrequiredNumerical operations
tensorboardoptionalLogging and visualization
Agent activity
24 hits · last 30 days
node
22
OpenAI (training)
1
Resources
tianshou — pip install tianshou · libregistry