Stable Baselines3 (SB3) is a set of reliable implementations of reinforcement learning algorithms in PyTorch. It is the next major version of Stable Baselines.
You can read a detailed presentation of Stable Baselines3 in the v1.0 blog post or our JMLR paper.
These algorithms will make it easier for the research community and industry to replicate, refine, and identify new ideas, and will create good baselines to build projects on top of. We expect these tools will be used as a base around which new ideas can be added, and as a tool for comparing a new approach against existing ones. We also hope that the simplicity of these tools will allow beginners to experiment with a more advanced toolset, without being buried in implementation details.
Note: Despite its simplicity of use, Stable Baselines3 (SB3) assumes you have some knowledge about Reinforcement Learning (RL). You should not utilize this library without some practice. To that extent, we provide good resources in the documentation to get started with RL.
The performance of each algorithm was tested (see Results section in their respective page), you can take a look at the issues #48 and #49 for more details.
Features | Stable-Baselines3 |
---|---|
State of the art RL methods | ✔️ |
Documentation | ✔️ |
Custom environments | ✔️ |
Custom policies | ✔️ |
Common interface | ✔️ |
Dict observation space support |
✔️ |
Ipython / Notebook friendly | ✔️ |
Tensorboard support | ✔️ |
PEP8 code style | ✔️ |
Custom callback | ✔️ |
High code coverage | ✔️ |
Type hints | ✔️ |
Please take a look at the Roadmap and Milestones.
A migration guide from SB2 to SB3 can be found in the documentation.
Documentation is available online: https://stable-baselines3.readthedocs.io/
Stable-Baselines3 has some integration with other libraries/services like Weights & Biases for experiment tracking or Hugging Face for storing/sharing trained models. You can find out more in the dedicated section of the documentation.
RL Baselines3 Zoo is a training framework for Reinforcement Learning (RL).
It provides scripts for training, evaluating agents, tuning hyperparameters, plotting results and recording videos.
In addition, it includes a collection of tuned hyperparameters for common environments and RL algorithms, and agents trained with those settings.
Goals of this repository:
- Provide a simple interface to train and enjoy RL agents
- Benchmark the different Reinforcement Learning algorithms
- Provide tuned hyperparameters for each environment and RL algorithm
- Have fun with the trained agents!
Github repo: https://github.com/DLR-RM/rl-baselines3-zoo
Documentation: https://stable-baselines3.readthedocs.io/en/master/guide/rl_zoo.html
We implement experimental features in a separate contrib repository: SB3-Contrib
This allows SB3 to maintain a stable and compact core, while still providing the latest features, like Recurrent PPO (PPO LSTM), Truncated Quantile Critics (TQC), Quantile Regression DQN (QR-DQN) or PPO with invalid action masking (Maskable PPO).
Documentation is available online: https://sb3-contrib.readthedocs.io/
Note: Stable-Baselines3 supports PyTorch >= 1.11
Stable Baselines3 requires Python 3.7+.
To install stable-baselines on Windows, please look at the documentation.
Install the Stable Baselines3 package:
pip install stable-baselines3[extra]
Note: Some shells such as Zsh require quotation marks around brackets, i.e. pip install 'stable-baselines3[extra]'
(More Info).
This includes an optional dependencies like Tensorboard, OpenCV or atari-py
to train on atari games. If you do not need those, you can use:
pip install stable-baselines3
Please read the documentation for more details and alternatives (from source, using docker).
Most of the code in the library tries to follow a sklearn-like syntax for the Reinforcement Learning algorithms.
Here is a quick example of how to train and run PPO on a cartpole environment:
import gym
from stable_baselines3 import PPO
env = gym.make("CartPole-v1")
model = PPO("MlpPolicy", env, verbose=1)
model.learn(total_timesteps=10_000)
vec_env = model.get_env()
obs = vec_env.reset()
for i in range(1000):
action, _states = model.predict(obs, deterministic=True)
obs, reward, done, info = vec_env.step(action)
vec_env.render()
# VecEnv resets automatically
# if done:
# obs = env.reset()
env.close()
Or just train a model with a one liner if the environment is registered in Gym and if the policy is registered:
from stable_baselines3 import PPO
model = PPO("MlpPolicy", "CartPole-v1").learn(10_000)
Please read the documentation for more examples.
All the following examples can be executed online using Google Colab notebooks:
- Full Tutorial
- All Notebooks
- Getting Started
- Training, Saving, Loading
- Multiprocessing
- Monitor Training and Plotting
- Atari Games
- RL Baselines Zoo
- PyBullet
Name | Recurrent | Box |
Discrete |
MultiDiscrete |
MultiBinary |
Multi Processing |
---|---|---|---|---|---|---|
ARS1 | ❌ | ✔️ | ✔️ | ❌ | ❌ | ✔️ |
A2C | ❌ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
DDPG | ❌ | ✔️ | ❌ | ❌ | ❌ | ✔️ |
DQN | ❌ | ❌ | ✔️ | ❌ | ❌ | ✔️ |
HER | ❌ | ✔️ | ✔️ | ❌ | ❌ | ❌ |
PPO | ❌ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
QR-DQN1 | ❌ | ❌ | ✔️ | ❌ | ❌ | ✔️ |
RecurrentPPO1 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
SAC | ❌ | ✔️ | ❌ | ❌ | ❌ | ✔️ |
TD3 | ❌ | ✔️ | ❌ | ❌ | ❌ | ✔️ |
TQC1 | ❌ | ✔️ | ❌ | ❌ | ❌ | ✔️ |
TRPO1 | ❌ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
Maskable PPO1 | ❌ | ❌ | ✔️ | ✔️ | ✔️ | ✔️ |
1: Implemented in SB3 Contrib GitHub repository.
Actions gym.spaces
:
Box
: A N-dimensional box that containes every point in the action space.Discrete
: A list of possible actions, where each timestep only one of the actions can be used.MultiDiscrete
: A list of possible actions, where each timestep only one action of each discrete set can be used.MultiBinary
: A list of possible actions, where each timestep any of the actions can be used in any combination.
All unit tests in stable baselines3 can be run using pytest
runner:
pip install pytest pytest-cov
make pytest
You can also do a static type check using pytype
and mypy
:
pip install pytype mypy
make type
Codestyle check with ruff
:
pip install ruff
make lint
We try to maintain a list of projects using stable-baselines3 in the documentation, please tell us if you want your project to appear on this page ;)
To cite this repository in publications:
@article{stable-baselines3,
author = {Antonin Raffin and Ashley Hill and Adam Gleave and Anssi Kanervisto and Maximilian Ernestus and Noah Dormann},
title = {Stable-Baselines3: Reliable Reinforcement Learning Implementations},
journal = {Journal of Machine Learning Research},
year = {2021},
volume = {22},
number = {268},
pages = {1-8},
url = {http://jmlr.org/papers/v22/20-1364.html}
}
Stable-Baselines3 is currently maintained by Ashley Hill (aka @hill-a), Antonin Raffin (aka @araffin), Maximilian Ernestus (aka @ernestum), Adam Gleave (@AdamGleave), Anssi Kanervisto (@Miffyli) and Quentin Gallouédec (@qgallouedec).
Important Note: We do not provide technical support, or consulting and do not answer personal questions via email. Please post your question on the RL Discord, Reddit, or Stack Overflow in that case.
To any interested in making the baselines better, there is still some documentation that needs to be done. If you want to contribute, please read CONTRIBUTING.md guide first.
The initial work to develop Stable Baselines3 was partially funded by the project Reduced Complexity Models from the Helmholtz-Gemeinschaft Deutscher Forschungszentren, and by the EU's Horizon 2020 Research and Innovation Programme under grant number 951992 (VeriDream).
The original version, Stable Baselines, was created in the robotics lab U2IS (INRIA Flowers team) at ENSTA ParisTech.
Logo credits: L.M. Tenkes