Skip to main content
Open In Colab This section explains PPO on the two smallest environments that still show what it does: a single state with two actions, and a trajectory of two states. Both are small enough that every quantity PPO computes can be plotted.

From REINFORCE to PPO

REINFORCE follows the gradient of the expected return, ∇θJ(θ)=E[∑tGt ∇θlog⁡πθ(at∣st)].\nabla_\theta J(\theta) = \mathbb{E}\bigl[ \sum_{t} G_t\, \nabla_\theta \log \pi_\theta(a_t \mid s_t) \bigr]. Its steps can be too large. One unlucky sample can push the policy far from where it was, and the policy can collapse onto one action or diverge. PPO keeps each update close to the policy that collected the data, an approximation of a trust region, by working with probability ratios.

The PPO objective

PPO measures how far the new policy has moved from the old one with the probability ratio rt(θ)=πθ(at∣st)πθold(at∣st),r_t(\theta) = \frac{\pi_\theta(a_t \mid s_t)}{\pi_{\theta_{old}}(a_t \mid s_t)}, and maximizes the clipped surrogate objective LCLIP(θ)=Et[min⁡(rt(θ)A^t, clip(rt(θ),1−ϵ,1+ϵ) A^t)],L^{CLIP}(\theta) = \mathbb{E}_t[ \min\big( r_t(\theta) \hat A_t,\ \mathrm{clip}(r_t(\theta), 1 - \epsilon, 1 + \epsilon)\, \hat A_t \big) ], where A^t\hat A_t estimates the advantage qπ(st,at)−vπ(st)q_\pi(s_t, a_t) - v_\pi(s_t) of the action taken: how much better it was than the average action in that state. The clipped term only matters on one side of the window, and which side depends on the sign of the advantage. For a good action (A^t>0\hat A_t > 0) the objective stops growing once rt>1+ϵr_t > 1 + \epsilon. For a bad action (A^t<0\hat A_t < 0) it stops growing once rt<1−ϵr_t < 1 - \epsilon. In both cases the gradient becomes zero, so the policy stops moving further in the direction the sample favors. On the other side of the window the unclipped term is active and the gradient is the usual one.

Single-state example

The policy chooses between two actions. It is written with one logit ϕ\phi, π(a1)=σ(ϕ)=11+e−ϕ,π(a2)=1−σ(ϕ),\pi(a_1) = \sigma(\phi) = \frac{1}{1 + e^{-\phi}}, \qquad \pi(a_2) = 1 - \sigma(\phi), which keeps the policy a valid distribution for every ϕ\phi and keeps the score bounded: ∇ϕlog⁡π(a1)=1−π(a1)\nabla_\phi \log \pi(a_1) = 1 - \pi(a_1) and ∇ϕlog⁡π(a2)=−π(a1)\nabla_\phi \log \pi(a_2) = -\pi(a_1). Action a1a_1 earns a reward of 1 and a2a_2 earns 0, so the best policy has π(a1)=1\pi(a_1) = 1. The value of the state under the old policy is its expected reward, vold=πold(a1)v_{old} = \pi_{old}(a_1), so the advantage of a sample is A^=R−πold(a1)\hat A = R - \pi_{old}(a_1). It is positive for a1a_1 and negative for a2a_2, which gives both actions a gradient. Each outer iteration freezes πold\pi_{old}, samples one action under it, and takes four mini-epochs of gradient ascent on the clipped surrogate. The gradient of the surrogate with respect to ϕ\phi is A^ r ∇ϕlog⁡π(a)\hat A\, r\, \nabla_\phi \log \pi(a) while the unclipped term is active, and zero otherwise.
Output from cell 3 Single-state PPO across mini-epochs: (a) the probability of the rewarded action, (b) the sampled importance ratio and the clip window, (c) the clipped and unclipped surrogate objectives of each sample, (d) the advantage of each sample. Each outer iteration starts with the ratio at 1, because the new policy equals the old one, and the mini-epochs move it away. A sample of a1a_1 has a positive advantage and pushes the ratio up; a sample of a2a_2 has a negative advantage and pushes the ratio of a2a_2 down. While π(a1)\pi(a_1) is small, the advantage of a1a_1 is large and the ratio reaches the upper bound within a few mini-epochs. From then on the clipped term is active, the gradient is zero, and the remaining mini-epochs of that iteration leave the policy alone. In this run that happened in 8% of the mini-epochs. Clipping does not bound the size of a step. It decides, before each step, whether to take it. The last step before the bound can still overshoot it. In panel (b) the largest ratio is about 1.22, beyond 1+ϵ=1.21 + \epsilon = 1.2, and the smallest is 0.65, well below 1−ϵ=0.81 - \epsilon = 0.8. The large downward overshoots come from samples of a2a_2 late in training, when the score −π(a1)-\pi(a_1) and the advantage −πold(a1)-\pi_{old}(a_1) are both close to −1-1 and a single step moves the policy a long way. Panel (d) shows why the advantage matters: as π(a1)\pi(a_1) grows, the expected reward voldv_{old} grows with it, so a rewarded sample carries less and less advantage and the updates shrink on their own.

Two-state example

The second environment adds a trajectory. The agent acts in state 1, then in state 2, with logits ϕ1\phi_1 and ϕ2\phi_2. The reward is 1 when the action in state 2 is a1a_1 and 0 otherwise, so the action in state 1 has no effect on the reward and the best policy in state 1 is any policy. The example compares two ways of crediting the reward:
  • Shared reward. Both decisions use the trajectory reward, A^1=A^2=R\hat A_1 = \hat A_2 = R. This is what REINFORCE does without a baseline.
  • True advantages. In state 1 both actions lead to the same expected reward, so q(s1,a)=v(s1)q(s_1, a) = v(s_1) for both actions and A^1=0\hat A_1 = 0. In state 2 the advantage is A^2=R−πold(a1∣s2)\hat A_2 = R - \pi_{old}(a_1 \mid s_2).
A critic that has learned the values supplies the second kind of credit.
Output from cell 5 Two-state PPO: (a) and (b) the policies of both states in one run with each kind of credit, (c) the sampled gradient on the state-1 logit in the same run, (d) the final probability of a1a_1 in state 1 in 20 runs with each kind of credit. With the shared reward, every rewarded trajectory pushes up the probability of whatever action was taken in state 1. Each push makes that action more likely to be sampled again, so it collects the next reward too, and the policy in state 1 collapses onto one action. The expected gradient on ϕ1\phi_1 is zero, since the action in state 1 has no effect on the reward, but a single run does not follow the expectation: the feedback loop amplifies the first few random samples. Panel (d) shows the result over 20 seeds. The runs end near 0 or near 1, and which one depends only on the seed. State 1 loses its exploration for a reason that has nothing to do with the task. A baseline does not fix this. Subtracting a constant bb from the reward leaves a per-sample gradient (R−b)∇log⁡π(a1∣s1)(R - b)\nabla \log \pi(a_1 \mid s_1) that is still nonzero whenever R≠bR \neq b. The true advantage does fix it: in state 1 both actions have the same value, so A^1=0\hat A_1 = 0 and the gradient on ϕ1\phi_1 is exactly zero in every sample. Panels (b) and (c) show π(a1∣s1)\pi(a_1 \mid s_1) staying at 0.5, while state 2 still learns. Estimating such advantages is the job of a critic.

Summary

PPO writes the policy-gradient update with probability ratios between the new and the old policy, and stops following a sample once its ratio leaves the clip window in the direction the sample favors. The single-state example shows that clipping decides whether to take a step. It does not bound the size of the step. The two-state example shows that the update is only as good as its advantage estimates: with the trajectory reward as the advantage, an action that does not matter still collapses onto one choice. Key references: (Schulman et al., 2017; Sutton & Barto, 2018)

References

  • Schulman, J., Wolski, F., Dhariwal, P., Radford, A., Klimov, O. (2017). Proximal Policy Optimization Algorithms. arXiv [cs.LG]. Link
  • Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link

Found an error on this page? Report an issue.