<p>To better understand PPO, it is helpful to look at the main contributions of the paper, which are: <strong>(1)</strong> the Clipped Surrogate Objective and <strong>(2)</strong> the use of &quot;multiple epochs of stochastic gradient ascent to perform each policy update&quot;.</p>
<br>
<p>From the original <a href="https://arxiv.org/abs/1707.06347" rel="noreferrer">PPO paper</a>:</p>
<blockquote>
<p>We have introduced [PPO], a family of policy optimization methods that use <strong>multiple epochs of stochastic gradient ascent to perform each policy update</strong>. These methods have the stability and reliability of trust-region [<a href="https://arxiv.org/abs/1502.05477" rel="noreferrer">TRPO</a>] methods but are much simpler to implement, requiring <strong>only a few lines of code change to a vanilla policy gradient implementation</strong>, applicable in more general settings (for example, when using a joint architecture for the policy and value function), and have better overall performance.</p>
</blockquote>
<hr />
<h1 id="the-clipped-surrogate-objective-98d7">1. The Clipped Surrogate Objective</h1>
<p>The Clipped Surrogate Objective is a drop-in replacement for the policy gradient objective that is designed to improve training stability by limiting the change you make to your policy at each step.</p>
<p>For vanilla policy gradients (e.g., REINFORCE) --- which you should be familiar with, or <a href="http://karpathy.github.io/2016/05/31/rl/" rel="noreferrer">familiarize yourself with</a> before you read this --- the objective used to optimize the neural network looks like:</p>
<p><a href="https://i.stack.imgur.com/5VZRT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5VZRT.png" alt="PG objective" /></a></p>
<p>This is the standard formula that you would see in the <a href="http://incompleteideas.net/book/the-book-2nd.html" rel="noreferrer">Sutton book</a>, and <a href="http://karpathy.github.io/2016/05/31/rl/" rel="noreferrer">other</a> <a href="http://rail.eecs.berkeley.edu/deeprlcourse-fa17/index.html" rel="noreferrer">resources</a>, where the A-hat could be the discounted return (as in REINFORCE) or the advantage function (as in <a href="https://arxiv.org/abs/1506.02438" rel="noreferrer">GAE</a>) for example.  By taking a gradient ascent step on this loss with respect to the network parameters, you will incentivize the actions that led to higher reward.</p>
<p>The vanilla policy gradient method uses the log probability of your action (log π(a | s)) to trace the impact of the actions, but you could imagine using another function to do this.  Another such function, introduced in <a href="https://people.eecs.berkeley.edu/%7Epabbeel/cs287-fa09/readings/KakadeLangford-icml2002.pdf" rel="noreferrer">this paper</a>, uses the probability of the action under the <em>current policy</em> (π(a|s)), divided by the probability of the action under your <em>previous policy</em> (π_old(a|s)).  This looks a bit similar to importance sampling if you are familiar with that:</p>
<p><a href="https://i.stack.imgur.com/bCAEy.png" rel="noreferrer"><img src="https://i.stack.imgur.com/bCAEy.png" alt="r eq" /></a></p>
<p>This r(θ) will be greater than 1 when the action is <em>more</em> probable for your <em>current</em> policy than it is for your <em>old</em> policy; it will be between 0 and 1 when the action is less probable for your current policy than for your old.</p>
<p>Now to build an objective function with this r(θ), we can simply swap it in for the log π(a|s) term.  This is what is done in TRPO:</p>
<p><a href="https://i.stack.imgur.com/EMMPa.png" rel="noreferrer"><img src="https://i.stack.imgur.com/EMMPa.png" alt="TRPO objective" /></a></p>
<p><strong>But what would happen here if your action is much more probable (like 100x more) for your current policy?</strong>  r(θ) will tend to be really big and lead to taking big gradient steps that might wreck your policy.  To deal with this and other issues, TRPO adds several extra bells and whistles (e.g., KL Divergence constraints) to limit the amount the policy can change and help guarantee that it is monotonically improving.</p>
<p>Instead of adding all these extra bells and whistles, what if we could build these stabilizing properties into the objective function?  As you might guess, this is what PPO does.  It gains the same performance benefits  as TRPO and avoids the complications by optimizing this simple (but kind of funny looking) Clipped Surrogate Objective:</p>
<p><a href="https://i.stack.imgur.com/zt9mz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zt9mz.png" alt="annotated clipped surrogate" /></a></p>
<p>The first term (blue) inside the minimization is the same (r(θ)A) term we saw in the TRPO objective.  The second term (red) is a version where the (r(θ)) is clipped between (1 - e, 1 + e). (in the paper they state a good value for e is about 0.2, so r can vary between ~(0.8, 1.2)).  Then, finally, the minimization of both of these terms is taken (green).</p>
<p>Take your time and look at the equation carefully and make sure you know what all the symbols mean, and mathematically what is happening.  Looking at the code may also help; here is the relevant section in both the OpenAI <a href="https://github.com/openai/baselines/blob/9fa8e1baf1d1f975b87b369a8082122eac812eb1/baselines/ppo1/pposgd_simple.py#L111-L117" rel="noreferrer">baselines</a> and <a href="https://github.com/unixpickle/anyrl-py/blob/953ad68d6507b83583e342b3210ed98e03a86a4f/anyrl/algos/ppo.py#L149-L155" rel="noreferrer">anyrl-py</a> implementations.</p>
<p>Great.</p>
<p>Next, let's see what effect the L clip function creates. Here is a diagram from the paper that plots the value of the clip objective for when the Advantage is positive and negative:</p>
<p><a href="https://i.stack.imgur.com/F6SxR.png" rel="noreferrer"><img src="https://i.stack.imgur.com/F6SxR.png" alt="Clip intro" /></a></p>
<p>On the left half of the diagram, where  (A &gt; 0), this is where the action had an estimated positive effect on the outcome.  On the right half of the diagram, where (A &lt; 0), this is where the action had an estimated negative effect on the outcome.</p>
<p>Notice how on the left half, the r-value gets clipped if it gets too high.  This will happen if the action became a lot more probable under the current policy than it was for the old policy.  When this happens, we do not want to get greedy and step too far (because this is just a local approximation and sample of our policy, so it will not be accurate if we step too far), and so we clip the objective to prevent it from growing.  (This will have the effect in the backward pass of blocking the gradient --- the flat line causing the gradient to be 0).</p>
<p>On the right side of the diagram, where the action had an estimated <em>negative</em> effect on the outcome, we see that the clip activates near 0, where the action under the current policy is unlikely.  This clipping region will similarly prevent us from updating too much to make the action much less probable after we already just took a big step to make it less probable.</p>
<p>So we see that both of these clipping regions prevent us from getting too greedy and trying to update too much at once and leaving the region where this sample offers a good estimate.</p>
<p><strong>But why are we letting the r(θ) grow indefinitely on the far right side of the diagram?  This seems odd as first, but what would cause r(θ) to grow really large in this case?</strong>  r(θ) growth in this region will be caused by a gradient step that made our action  <em>a lot more probable</em>, and it turning out to make our policy <em>worse</em>.  If that was the case, we would want to be able to undo that gradient step.  And it just so happens that the L clip function allows this.  The function is negative here, so the gradient will tell us to walk the other direction and make the action less probable by an amount proportional to how much we screwed it up.  (Note that there is a similar region on the far left side of the diagram, where the action is good and we accidentally made it less probable.)</p>
<p>These &quot;undo&quot; regions explain why we must include the weird minimization term in the objective function.  They correspond to the unclipped r(θ)A having a lower value than the clipped version and getting returned by the minimization.  This is because they were steps in the wrong direction (e.g., the action was good but we accidentally made it less probable).  If we had not included the min in the objective function, these regions would be flat (gradient = 0) and we would be prevented from fixing mistakes.</p>
<p>Here is a diagram summarizing this:</p>
<p><a href="https://i.stack.imgur.com/gasbI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gasbI.png" alt="L Clip Diagram" /></a></p>
<p>And that is the gist of it.  The Clipped Surrogate Objective is just a drop-in replacement you could use in the vanilla policy gradient.  The clipping limits the effective change you can make at each step in order to improve stability, and the minimization allows us to fix our mistakes in case we screwed it up.  One thing I didn't discuss is what is meant by PPO objective forming a &quot;lower bound&quot; as discussed in the paper.  For more on that, I would suggest <a href="https://youtu.be/gqX8J38tESw?t=14m1s" rel="noreferrer">this part</a> of a lecture the author gave.</p>
<h1 id="multiple-epochs-for-policy-updating-a67s">2. Multiple epochs for policy updating</h1>
<p>Unlike vanilla policy gradient methods, and <em>because of the Clipped Surrogate Objective function</em>, PPO allows you to run multiple epochs of gradient ascent on your samples without causing destructively large policy updates. This allows you to squeeze more out of your data and reduce sample inefficiency.</p>
<p>PPO runs the policy using <em>N</em> parallel actors each collecting data, and then it samples mini-batches of this data to train for <em>K</em> epochs using the Clipped Surrogate Objective function. See full algorithm below (the approximate param values are: <em>K</em> = 3-15, <em>M</em> = 64-4096, <em>T</em> (horizon) = 128-2048):</p>
<p><a href="https://i.stack.imgur.com/a6z3u.png" rel="noreferrer"><img src="https://i.stack.imgur.com/a6z3u.png" alt="PPO Algo" /></a></p>
<p>The parallel actors part was popularized by the <a href="https://arxiv.org/abs/1602.01783" rel="noreferrer">A3C paper</a> and has become a fairly standard way for collecting data.</p>
<p>The newish part is that they are able to run <em>K</em> epochs of gradient ascent on the trajectory samples.  As they state in the paper, it would be nice to run the vanilla policy gradient optimization for multiple passes over the data so that you could learn more from each sample. However, this generally fails in practice for vanilla methods because they take too big of steps on the local samples and this wrecks the policy.  PPO, on the other hand, has the built-in mechanism to prevent too much of an update.</p>
<p>For each iteration, after sampling the environment with π_old (line 3) and when we start running the optimization (line 6), our policy π will be exactly equal to π_old.  So at first, none of our updates will be clipped and we are guaranteed to learn something from these examples.  However, as we update π using multiple epochs, the objective will start hitting the clipping limits, the gradient will go to 0 for those samples, and the training will gradually stop...until we move on to the next iteration and collect new samples.</p>
<p>....</p>
<p>And that's all for now.  If you are interested in gaining a better understanding, I would recommend digging more into the <a href="https://arxiv.org/abs/1707.06347" rel="noreferrer">original paper</a>, trying to implement it yourself, or diving into the <a href="https://github.com/openai/baselines/blob/b29c8020d7ac72256dca48fae85e96b4b3c75ccb/baselines/ppo2/ppo2.py" rel="noreferrer">baselines implementation</a> and playing with the code.</p>
<p>[edit: 2019/01/27]: For a better background and for how PPO relates to other RL algorithms, I would also strongly recommend checking out OpenAI's <a href="https://spinningup.openai.com/en/latest/index.html" rel="noreferrer">Spinning Up resources and implementations</a>.</p>
What is the way to understand Proximal Policy Optimization Algorithm in RL?
<p>I know the basics of Reinforcement Learning, but what terms it's necessary to understand to be able read <a href="https://arxiv.org/abs/1707.06347" rel="noreferrer">arxiv PPO paper</a> ?</p>

<p>What is the roadmap to learn and use <a href="https://blog.openai.com/openai-baselines-ppo/" rel="noreferrer">PPO</a> ?</p>