PPO2¶
The Proximal Policy Optimization algorithm combines ideas from A2C (having multiple workers) and TRPO (it uses a trust region to improve the actor).
The main idea is that after an update, the new policy should be not too far form the old policy. For that, ppo uses clipping to avoid too large update.
Note
PPO2 is the implementation of OpenAI made for GPU. For multiprocessing, it uses vectorized environments compared to PPO1 which uses MPI.
Note
PPO2 contains several modifications from the original algorithm not documented by OpenAI: value function is also clipped and advantages are normalized.
Notes¶
 Original paper: https://arxiv.org/abs/1707.06347
 Clear explanation of PPO on Arxiv Insights channel: https://www.youtube.com/watch?v=5P7IxPq8u8
 OpenAI blog post: https://blog.openai.com/openaibaselinesppo/
python m stable_baselines.ppo2.run_atari
runs the algorithm for 40M frames = 10M timesteps on an Atari game. See help (
h
) for more options.
python m stable_baselines.ppo2.run_mujoco
runs the algorithm for 1M frames on a Mujoco environment.
Can I use?¶
 Recurrent policies: ✔️
 Multi processing: ✔️
 Gym spaces:
Space  Action  Observation 

Discrete  ✔️  ✔️ 
Box  ✔️  ✔️ 
MultiDiscrete  ✔️  ✔️ 
MultiBinary  ✔️  ✔️ 
Example¶
Train a PPO agent on CartPolev1 using 4 processes.
import gym
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import SubprocVecEnv
from stable_baselines import PPO2
# multiprocess environment
n_cpu = 4
env = SubprocVecEnv([lambda: gym.make('CartPolev1') for i in range(n_cpu)])
model = PPO2(MlpPolicy, env, verbose=1)
model.learn(total_timesteps=25000)
model.save("ppo2_cartpole")
del model # remove to demonstrate saving and loading
model = PPO2.load("ppo2_cartpole")
# Enjoy trained agent
obs = env.reset()
while True:
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
Parameters¶

class
stable_baselines.ppo2.
PPO2
(policy, env, gamma=0.99, n_steps=128, ent_coef=0.01, learning_rate=0.00025, vf_coef=0.5, max_grad_norm=0.5, lam=0.95, nminibatches=4, noptepochs=4, cliprange=0.2, verbose=0, tensorboard_log=None, _init_setup_model=True, policy_kwargs=None)[source]¶ Proximal Policy Optimization algorithm (GPU version). Paper: https://arxiv.org/abs/1707.06347
Parameters:  policy – (ActorCriticPolicy or str) The policy model to use (MlpPolicy, CnnPolicy, CnnLstmPolicy, …)
 env – (Gym environment or str) The environment to learn from (if registered in Gym, can be str)
 gamma – (float) Discount factor
 n_steps – (int) The number of steps to run for each environment per update (i.e. batch size is n_steps * n_env where n_env is number of environment copies running in parallel)
 ent_coef – (float) Entropy coefficient for the loss caculation
 learning_rate – (float or callable) The learning rate, it can be a function
 vf_coef – (float) Value function coefficient for the loss calculation
 max_grad_norm – (float) The maximum value for the gradient clipping
 lam – (float) Factor for tradeoff of bias vs variance for Generalized Advantage Estimator
 nminibatches – (int) Number of training minibatches per update. For recurrent policies, the number of environments run in parallel should be a multiple of nminibatches.
 noptepochs – (int) Number of epoch when optimizing the surrogate
 cliprange – (float or callable) Clipping parameter, it can be a function
 verbose – (int) the verbosity level: 0 none, 1 training information, 2 tensorflow debug
 tensorboard_log – (str) the log location for tensorboard (if None, no logging)
 _init_setup_model – (bool) Whether or not to build the network at the creation of the instance
 policy_kwargs – (dict) additional arguments to be passed to the policy on creation

action_probability
(observation, state=None, mask=None, actions=None)¶ If
actions
isNone
, then get the model’s action probability distribution from a given observation depending on the action space the output is:
 Discrete: probability for each possible action
 Box: mean and standard deviation of the action output
However if
actions
is notNone
, this function will return the probability that the given actions are taken with the given parameters (observation, state, …) on this model.Warning
When working with continuous probability distribution (e.g. Gaussian distribution for continuous action) the probability of taking a particular action is exactly zero. See http://blog.christianperone.com/2019/01/ for a good explanation
Parameters:  observation – (np.ndarray) the input observation
 state – (np.ndarray) The last states (can be None, used in recurrent policies)
 mask – (np.ndarray) The last masks (can be None, used in recurrent policies)
 actions – (np.ndarray) (OPTIONAL) For calculating the likelihood that the given actions are chosen by the model for each of the given parameters. Must have the same number of actions and observations. (set to None to return the complete action probability distribution)
Returns: (np.ndarray) the model’s action probability

get_env
()¶ returns the current environment (can be None if not defined)
Returns: (Gym Environment) The current environment

learn
(total_timesteps, callback=None, seed=None, log_interval=1, tb_log_name='PPO2')[source]¶ Return a trained model.
Parameters:  total_timesteps – (int) The total number of samples to train on
 seed – (int) The initial seed for training, if None: keep current seed
 callback – (function (dict, dict)) > boolean function called at every steps with state of the algorithm. It takes the local and global variables. If it returns False, training is aborted.
 log_interval – (int) The number of timesteps before logging.
 tb_log_name – (str) the name of the run for tensorboard log
Returns: (BaseRLModel) the trained model

classmethod
load
(load_path, env=None, **kwargs)¶ Load the model from file
Parameters:  load_path – (str or filelike) the saved parameter location
 env – (Gym Envrionment) the new environment to run the loaded model on (can be None if you only need prediction from a trained model)
 kwargs – extra arguments to change the model when loading

predict
(observation, state=None, mask=None, deterministic=False)¶ Get the model’s action from an observation
Parameters:  observation – (np.ndarray) the input observation
 state – (np.ndarray) The last states (can be None, used in recurrent policies)
 mask – (np.ndarray) The last masks (can be None, used in recurrent policies)
 deterministic – (bool) Whether or not to return deterministic actions.
Returns: (np.ndarray, np.ndarray) the model’s action and the next state (used in recurrent policies)

save
(save_path)[source]¶ Save the current parameters to file
Parameters: save_path – (str or filelike object) the save location

set_env
(env)¶ Checks the validity of the environment, and if it is coherent, set it as the current environment.
Parameters: env – (Gym Environment) The environment for learning a policy