Proximal Policy Optimization¶
Table of Contents
Background¶
(Previously: Background for TRPO)
PPO is motivated by the same question as TRPO: how can we take the biggest possible improvement step on a policy using the data we currently have, without stepping so far that we accidentally cause performance collapse? Where TRPO tries to solve this problem with a complex secondorder method, PPO is a family of firstorder methods that use a few other tricks to keep new policies close to old. PPO methods are significantly simpler to implement, and empirically seem to perform at least as well as TRPO.
There are two primary variants of PPO: PPOPenalty and PPOClip.
PPOPenalty approximately solves a KLconstrained update like TRPO, but penalizes the KLdivergence in the objective function instead of making it a hard constraint, and automatically adjusts the penalty coefficient over the course of training so that it’s scaled appropriately.
PPOClip doesn’t have a KLdivergence term in the objective and doesn’t have a constraint at all. Instead relies on specialized clipping in the objective function to remove incentives for the new policy to get far from the old policy.
Here, we’ll focus only on PPOClip (the primary variant used at OpenAI).
Quick Facts¶
 PPO is an onpolicy algorithm.
 PPO can be used for environments with either discrete or continuous action spaces.
 The Spinning Up implementation of PPO supports parallelization with MPI.
Key Equations¶
PPOclip updates policies via
typically taking multiple steps of (usually minibatch) SGD to maximize the objective. Here is given by
in which is a (small) hyperparameter which roughly says how far away the new policy is allowed to go from the old.
This is a pretty complex expression, and it’s hard to tell at first glance what it’s doing, or how it helps keep the new policy close to the old policy. As it turns out, there’s a considerably simplified version [1] of this objective which is a bit easier to grapple with (and is also the version we implement in our code):
where
To figure out what intuition to take away from this, let’s look at a single stateaction pair , and think of cases.
Advantage is positive: Suppose the advantage for that stateaction pair is positive, in which case its contribution to the objective reduces to
Because the advantage is positive, the objective will increase if the action becomes more likely—that is, if increases. But the min in this term puts a limit to how much the objective can increase. Once , the min kicks in and this term hits a ceiling of . Thus: the new policy does not benefit by going far away from the old policy.
Advantage is negative: Suppose the advantage for that stateaction pair is negative, in which case its contribution to the objective reduces to
Because the advantage is negative, the objective will increase if the action becomes less likely—that is, if decreases. But the max in this term puts a limit to how much the objective can increase. Once , the max kicks in and this term hits a ceiling of . Thus, again: the new policy does not benefit by going far away from the old policy.
What we have seen so far is that clipping serves as a regularizer by removing incentives for the policy to change dramatically, and the hyperparameter corresponds to how far away the new policy can go from the old while still profiting the objective.
You Should Know
While this kind of clipping goes a long way towards ensuring reasonable policy updates, it is still possible to end up with a new policy which is too far from the old policy, and there are a bunch of tricks used by different PPO implementations to stave this off. In our implementation here, we use a particularly simple method: early stopping. If the mean KLdivergence of the new policy from the old grows beyond a threshold, we stop taking gradient steps.
When you feel comfortable with the basic math and implementation details, it’s worth checking out other implementations to see how they handle this issue!
[1]  See this note for a derivation of the simplified form of the PPOClip objective. 
Exploration vs. Exploitation¶
PPO trains a stochastic policy in an onpolicy way. This means that it explores by sampling actions according to the latest version of its stochastic policy. The amount of randomness in action selection depends on both initial conditions and the training procedure. Over the course of training, the policy typically becomes progressively less random, as the update rule encourages it to exploit rewards that it has already found. This may cause the policy to get trapped in local optima.
Documentation¶

spinup.
ppo
(env_fn, actor_critic=<function mlp_actor_critic>, ac_kwargs={}, seed=0, steps_per_epoch=4000, epochs=50, gamma=0.99, clip_ratio=0.2, pi_lr=0.0003, vf_lr=0.001, train_pi_iters=80, train_v_iters=80, lam=0.97, max_ep_len=1000, target_kl=0.01, logger_kwargs={}, save_freq=10)[source]¶ Parameters:  env_fn – A function which creates a copy of the environment. The environment must satisfy the OpenAI Gym API.
 actor_critic –
A function which takes in placeholder symbols for state,
x_ph
, and action,a_ph
, and returns the main outputs from the agent’s Tensorflow computation graph:Symbol Shape Description pi
(batch, act_dim) Samples actions from policy givenstates.logp
(batch,) Gives log probability, according tothe policy, of taking actionsa_ph
in statesx_ph
.logp_pi
(batch,) Gives log probability, according tothe policy, of the action sampled bypi
.v
(batch,) Gives the value estimate for statesinx_ph
. (Critical: make sureto flatten this!)  ac_kwargs (dict) – Any kwargs appropriate for the actor_critic function you provided to PPO.
 seed (int) – Seed for random number generators.
 steps_per_epoch (int) – Number of steps of interaction (stateaction pairs) for the agent and the environment in each epoch.
 epochs (int) – Number of epochs of interaction (equivalent to number of policy updates) to perform.
 gamma (float) – Discount factor. (Always between 0 and 1.)
 clip_ratio (float) – Hyperparameter for clipping in the policy objective. Roughly: how far can the new policy go from the old policy while still profiting (improving the objective function)? The new policy can still go farther than the clip_ratio says, but it doesn’t help on the objective anymore. (Usually small, 0.1 to 0.3.)
 pi_lr (float) – Learning rate for policy optimizer.
 vf_lr (float) – Learning rate for value function optimizer.
 train_pi_iters (int) – Maximum number of gradient descent steps to take on policy loss per epoch. (Early stopping may cause optimizer to take fewer than this.)
 train_v_iters (int) – Number of gradient descent steps to take on value function per epoch.
 lam (float) – Lambda for GAELambda. (Always between 0 and 1, close to 1.)
 max_ep_len (int) – Maximum length of trajectory / episode / rollout.
 target_kl (float) – Roughly what KL divergence we think is appropriate between new and old policies after an update. This will get used for early stopping. (Usually small, 0.01 or 0.05.)
 logger_kwargs (dict) – Keyword args for EpochLogger.
 save_freq (int) – How often (in terms of gap between epochs) to save the current policy and value function.
Saved Model Contents¶
The computation graph saved by the logger includes:
Key  Value 

x 
Tensorflow placeholder for state input. 
pi 
Samples an action from the agent, conditioned on states in x . 
v 
Gives value estimate for states in x . 
This saved model can be accessed either by
 running the trained policy with the test_policy.py tool,
 or loading the whole saved graph into a program with restore_tf_graph.
References¶
Relevant Papers¶
 Proximal Policy Optimization Algorithms, Schulman et al. 2017
 High Dimensional Continuous Control Using Generalized Advantage Estimation, Schulman et al. 2016
 Emergence of Locomotion Behaviours in Rich Environments, Heess et al. 2017
Why These Papers?¶
Schulman 2017 is included because it is the original paper describing PPO. Schulman 2016 is included because our implementation of PPO makes use of Generalized Advantage Estimation for computing the policy gradient. Heess 2017 is included because it presents a largescale empirical analysis of behaviors learned by PPO agents in complex environments (although it uses PPOpenalty instead of PPOclip).
Other Public Implementations¶
 Baselines
 ModularRL (Caution: this implements PPOpenalty instead of PPOclip.)
 rllab (Caution: this implements PPOpenalty instead of PPOclip.)
 rllib (Ray)