Stable Baselines is a set of improved implementations of reinforcement learning algorithms based on OpenAI Baselines.
You can read a detailed presentation of Stable Baselines in the Medium article.
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.
This toolset is a fork of OpenAI Baselines, with a major structural refactoring, and code cleanups:
- Unified structure for all algorithms
- PEP8 compliant (unified code style)
- Documented functions and classes
- More tests & more code coverage
- Main differences with OpenAI Baselines
- Usage
- Prerequisites
- Virtual environment
- Installation
- Testing the installation
- Subpackages
- How To Contribute
- Bonus
Most of 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 PPO2 on a cartpole environment:
import gym
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import PPO2
env = gym.make('CartPole-v1')
env = DummyVecEnv([lambda: env]) # The algorithms require a vectorized environment to run
model = PPO2(MlpPolicy, env, verbose=1)
model.learn(total_timesteps=10000)
obs = env.reset()
for i in range(1000):
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
Or just train a model with a one liner if the environment is registed in Gym:
from stable_baselines.common.policies import MlpPolicy
from stable_baselines import PPO2
model = PPO2(MlpPolicy, 'CartPole-v1').learn(10000)
All the following examples can be executed online using Google colab notebooks:
Name | Refactored(1) | Recurrent | Box |
Discrete |
MultiDiscrete |
MultiBinary |
Multi Processing |
---|---|---|---|---|---|---|---|
A2C | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
ACER | ✔️ | ✔️ | ❌ (5) | ✔️ | ❌ | ❌ | ✔️ |
ACKTR | ✔️ | ✔️ | ❌ (5) | ✔️ | ❌ | ❌ | ✔️ |
DDPG | ✔️ | ❌ | ✔️ | ❌ | ❌ | ❌ | ❌ |
DeepQ | ✔️ | ❌ | ❌ | ✔️ | ❌ | ❌ | ❌ |
GAIL (2) | ✔️ | ✔️ | ✔️ | ❌ | ❌ | ❌ | ✔️ (4) |
HER (3) | ❌ (5) | ❌ | ✔️ | ❌ | ❌ | ❌ | ❌ |
PPO1 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ (4) |
PPO2 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
TRPO | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ (4) |
(1): Whether or not the algorithm has be refactored to fit the BaseRLModel
class.
(2): Only implemented for TRPO.
(3): Only implemented for DDPG.
(4): Multi Processing with MPI.
(5): TODO, in project scope.
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.
here are a few barebones examples of how to use this library:
import gym
from stable_baselines.common.policies import MlpPolicy, MlpLstmPolicy, MlpLnLstmPolicy, \
CnnPolicy, CnnLstmPolicy, CnnLnLstmPolicy
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import ACKTR
env = gym.make('CartPole-v1')
# Vectorize the environment, as some models requires it.
# But all the models can use vectorized environments
env = DummyVecEnv([lambda: env])
model = ACKTR(MlpPolicy, env, gamma=0.5, verbose=1)
model.learn(total_timesteps=25000)
model.save(save_path="acktr_env")
obs = env.reset()
while True:
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
Training a RL agent on Atari games is straightforward thanks to make_atari_env
helper function. It will do all the preprocessing and multiprocessing for you.
from stable_baselines.common.cmd_util import make_atari_env
from stable_baselines.common.policies import MlpPolicy, MlpLstmPolicy, MlpLnLstmPolicy, \
CnnPolicy, CnnLstmPolicy, CnnLnLstmPolicy
from stable_baselines.common.vec_env import DummyVecEnv, VecFrameStack
from stable_baselines import A2C
# There already exists an environment generator that will make and wrap atari environments correctly.
env = make_atari_env('BreakoutNoFrameskip-v4', num_env=8, seed=0)
# Stack 4 frames
env = VecFrameStack(env, n_stack=4)
model = A2C(CnnPolicy, env, verbose=1)
model.learn(total_timesteps=25000)
model.save(save_path="a2c_env")
obs = env.reset()
while True:
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
from stable_baselines.common.cmd_util import make_atari_env
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import DeepQ, models
# There already exists an environment generator that will make and wrap atari environments correctly
# Here we set the num_env to 1, as DeepQ does not support multi-environments
env = make_atari_env('MsPacmanNoFrameskip-v4', num_env=1, seed=0)
# Here deepq does not use the standard Actor-Critic policies
model = DeepQ(models.cnn_to_mlp(convs=[(32, 8, 4), (64, 4, 2), (64, 3, 1)], hiddens=[64]), env, verbose=1)
model.learn(total_timesteps=5000)
model.save(save_path="deepq_env")
obs = env.reset()
while True:
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
import gym
import numpy as np
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import SubprocVecEnv
from stable_baselines.common import set_global_seeds
from stable_baselines import ACKTR
def make_env(env_id, rank, seed=0):
"""
Utility function for multiprocessed env.
:param env_id: (str) the environment ID
:param num_env: (int) the number of environment you wish to have in subprocesses
:param seed: (int) the inital seed for RNG
:param rank: (int) index of the subprocess
"""
def _init():
env = gym.make(env_id)
env.seed(seed + rank)
return env
set_global_seeds(seed)
return _init
env_id = "CartPole-v1"
num_cpu = 4 # Number of processes to use
# Create the vectorized environment
env = SubprocVecEnv([make_env(env_id, i) for i in range(num_cpu)])
model = ACKTR(MlpPolicy, env, verbose=1)
model.learn(total_timesteps=25000)
obs = env.reset()
for _ in range(1000):
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
You can define a custom callback function that will be called inside the agent. This could be useful when you want to monitor training, for instance display live learning curve in Tensorboard (or in Visdom) or save the best agent.
import os
import gym
import numpy as np
import matplotlib.pyplot as plt
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env.dummy_vec_env import DummyVecEnv
from stable_baselines.bench import Monitor
from stable_baselines.results_plotter import load_results, ts2xy
from stable_baselines import DDPG
from stable_baselines.ddpg.noise import AdaptiveParamNoiseSpec
best_mean_reward, n_steps = -np.inf, 0
def callback(_locals, _globals):
"""
Callback called at each step (for DQN an others) or after n steps (see ACER or PPO2)
:param _locals: (dict)
:param _globals: (dict)
"""
global n_steps, best_mean_reward
# Print stats every 1000 calls
if (n_steps + 1) % 1000 == 0:
# Evaluate policy performance
x, y = ts2xy(load_results(log_dir), 'timesteps')
if len(x) > 0:
mean_reward = np.mean(y[-100:])
print(x[-1], 'timesteps')
print("Best mean reward: {:.2f} - Last mean reward per episode: {:.2f}".format(best_mean_reward, mean_reward))
# New best model, you could save the agent here
if mean_reward > best_mean_reward:
best_mean_reward = mean_reward
# Example for saving best model
print("Saving new best model")
_locals['self'].save(log_dir + 'best_model.pkl')
n_steps += 1
return False
# Create log dir
log_dir = "/tmp/gym/"
os.makedirs(log_dir, exist_ok=True)
# Create and wrap the environment
env = gym.make('LunarLanderContinuous-v2')
env = Monitor(env, log_dir, allow_early_resets=True)
env = DummyVecEnv([lambda: env])
# Add some param noise for exploration
param_noise = AdaptiveParamNoiseSpec(initial_stddev=0.2, desired_action_stddev=0.2)
model = DDPG(MlpPolicy, env, param_noise=param_noise, memory_limit=int(1e6), verbose=0)
# Train the agent
model.learn(total_timesteps=200000, callback=callback)
You can also make custom policies to train with:
import gym
from stable_baselines.common.policies import FeedForwardPolicy
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import ppo2
# Custom MLP policy of 3 layers of 256, 64 and 16
class CustomPolicy(FeedForwardPolicy):
def __init__(self, *args, **kwargs):
super(CustomPolicy, self).__init__(*args, **kwargs, layers=[256, 64, 16], type="mlp")
env = gym.make('LunarLander-v2')
env = DummyVecEnv([lambda: env])
model = PPO2(CustomPolicy, env, verbose=1)
model.learn(total_timesteps=100000)
obs = env.reset()
while True:
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
By default, images are scaled (dividing by 255) but not other type of input. For that, a wrapper exists and will compute a running average and standard deviation of input features (it can do the same for rewards).
import gym
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import DummyVecEnv, VecNormalize
from stable_baselines import PPO2
env = DummyVecEnv([lambda: gym.make("Reacher-v2")])
# Automatically normalize the input features
env = VecNormalize(env, norm_obs=True, norm_reward=False,
clip_obs=10.)
model = PPO2(MlpPolicy, env)
model.learn(total_timesteps=2000)
# Don't forget to save the running average when saving the agent
log_dir = "/tmp/"
model.save(log_dir + "ppo_reacher")
env.save_running_average(log_dir)
You can also move from one environment to an other for continuous learning (PPO2 on DemonAttack-v0
, then transferred on SpaceInvaders-v0
):
from stable_baselines.common.cmd_util import make_atari_env
from stable_baselines.common.policies import MlpPolicy, MlpLstmPolicy, MlpLnLstmPolicy, \
CnnPolicy, CnnLstmPolicy, CnnLnLstmPolicy
from stable_baselines import PPO2
# There already exists an environment generator that will make and wrap atari environments correctly
env = make_atari_env('DemonAttackNoFrameskip-v4', num_env=8, seed=0)
model = PPO2(CnnPolicy, env, verbose=1)
model.learn(total_timesteps=10000)
obs = env.reset()
for i in range(1000):
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
# The number of environments must be identical when changing environments
env = make_atari_env('SpaceInvadersNoFrameskip-v4', num_env=8, seed=0)
# change env
model.set_env(env)
model.learn(total_timesteps=10000)
obs = env.reset()
while True:
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
Baselines requires python3 (>=3.5) with the development headers. You'll also need system packages CMake, OpenMPI and zlib. Those can be installed as follows
sudo apt-get update && sudo apt-get install cmake libopenmpi-dev python3-dev zlib1g-dev
Installation of system packages on Mac requires Homebrew. With Homebrew installed, run the follwing:
brew install cmake openmpi
From the general python package sanity perspective, it is a good idea to use virtual environments (virtualenvs) to make sure packages from different projects do not interfere with each other. You can install virtualenv (which is itself a pip package) via
pip install virtualenv
Virtualenvs are essentially folders that have copies of python executable and all python packages. To create a virtualenv called venv with python3, one runs
virtualenv /path/to/venv --python=python3
To activate a virtualenv:
. /path/to/venv/bin/activate
More thorough tutorial on virtualenvs and options can be found here
Install the Stable Baselines package
Using pip from pypi:
pip install stable-baselines
From source:
pip install git+https://github.com/hill-a/stable-baselines
Some of the baselines examples use MuJoCo (multi-joint dynamics in contact) physics simulator, which is proprietary and requires binaries and a license (temporary 30-day license can be obtained from www.mujoco.org). Instructions on setting up MuJoCo can be found here
All unit tests in baselines can be run using pytest runner:
pip install pytest pytest-cov
pytest --cov-config .coveragerc --cov-report html --cov-report term --cov=.
To cite this repository in publications:
@misc{stable-baselines,
author = {Hill, Ashley and Raffin, Antonin and Traore, Rene and Dhariwal, Prafulla and Hesse, Christopher and Klimov, Oleg and Nichol, Alex and Plappert, Matthias and Radford, Alec and Schulman, John and Sidor, Szymon and Wu, Yuhuai},
title = {Stable Baselines},
year = {2018},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/hill-a/stable-baselines}},
}
To any interested in making the baselines better, there is still some documentation that needs to be done. If you want to contribute, please open an issue first and then propose your pull request.
Nice to have (for the future):
- Continuous actions support for ACER
- Continuous actions support for ACKTR
- Html documentation (see issue #11)
Make a gif of a trained agent (you need to install imageio):
import imageio
import numpy as np
from stable_baselines.common.policies import MlpPolicy
from stable_baselines import A2C
model = A2C(MlpPolicy, "LunarLander-v2").learn(100000)
images = []
obs = model.env.reset()
img = model.env.render(mode='rgb_array')
for i in range(350):
images.append(img)
action, _ = model.predict(obs)
obs, _, _ ,_ = model.env.step(action)
img = model.env.render(mode='rgb_array')
imageio.mimsave('lander_a2c.gif', [np.array(img[0]) for i, img in enumerate(images) if i%2 == 0], fps=29)