ai theory

Reinforcement Learning Basics (15) — PPO Keeps the Policy from Moving Too Far

Junyoung Park · 2024-06-21 · 6 min

Introduction

Policy Gradient increases the probability of good actions. Even when the direction is correct, taking too large a step can cause problems.

If the policy changes substantially while we perform several updates on trajectories collected by the current policy, that data no longer comes from the policy we are updating. The probability of one action may suddenly dominate and erase other good actions.

Proximal Policy Optimization, or PPO, implements a simple idea with a relatively simple objective: move in a good direction, but do not move too far from the old policy.

This article reads the probability ratio and clipping mechanism of PPO-Clip with concrete numbers.

The Probability Ratio between the Old and New Policies

Let πθold\pi_{\theta_{\text{old}}} be the policy that collected the trajectories and πθ\pi_\theta the policy currently being updated.

For a sample (St,At)(S_t,A_t), calculate

rt(θ)=πθ(AtSt)πθold(AtSt).r_t(\theta) = \frac{ \pi_\theta(A_t\mid S_t) }{ \pi_{\theta_{\text{old}}}(A_t\mid S_t) }.

This ratio shows how much the probability of the selected action has changed.

  • rt=1r_t=1: the probability is unchanged.
  • rt=1.2r_t=1.2: it is 20%20\% larger than under the old policy.
  • rt=0.7r_t=0.7: it has fallen to 70%70\% of the old probability.

For example, if an action's probability was 0.40.4 under the old policy and 0.50.5 under the new policy,

rt=0.50.4=1.25.r_t=\frac{0.5}{0.4}=1.25.

The Clipped Objective

The central PPO-Clip objective is

LCLIP(θ)=Et[min(rt(θ)A^t,clip(rt(θ),1ϵ,1+ϵ)A^t)].L^{\text{CLIP}}(\theta) = \mathbb E_t \left[ \min \left( r_t(\theta)\hat A_t, \operatorname{clip} \left( r_t(\theta),1-\epsilon,1+\epsilon \right) \hat A_t \right) \right].

It looks long, but it simply chooses the more conservative of two candidates.

  1. rtA^tr_t\hat A_t: the unconstrained Policy Gradient objective
  2. clip(rt,1ϵ,1+ϵ)A^t\operatorname{clip}(r_t,1-\epsilon,1+\epsilon)\hat A_t: the objective after restricting the ratio to an interval
  3. min\min: select the less optimistic value

With the common choice ϵ=0.2\epsilon=0.2, the reference interval is

[1ϵ,1+ϵ]=[0.8,1.2].[1-\epsilon,1+\epsilon] = [0.8,1.2].
Even for a good action, increasing its probability beyond a fixed interval provides no further reward, reducing the incentive for an excessive update.

When the Advantage Is Positive

A^t>0\hat A_t>0 means the chosen action was better than expected, so we want to increase its probability.

Suppose

A^t=2,rt=1.35,ϵ=0.2.\hat A_t=2, \qquad r_t=1.35, \qquad \epsilon=0.2.

The unconstrained value is

rtA^t=1.35×2=2.7.r_t\hat A_t = 1.35\times2 =2.7.

The clipped value is

clip(1.35,0.8,1.2)×2=1.2×2=2.4.\operatorname{clip}(1.35,0.8,1.2)\times2 = 1.2\times2 =2.4.

The objective therefore uses

min(2.7,2.4)=2.4.\min(2.7,2.4)=2.4.

Once the probability ratio exceeds 1.21.2, increasing it further no longer improves the objective for this sample.

When the Advantage Is Negative

A^t<0\hat A_t<0 means the selected action was worse than expected, so we want to reduce its probability. But we should also prevent an excessive reduction.

If

A^t=2,rt=0.7,ϵ=0.2,\hat A_t=-2, \qquad r_t=0.7, \qquad \epsilon=0.2,

then

rtA^t=0.7×(2)=1.4,r_t\hat A_t = 0.7\times(-2) =-1.4,

while the clipped value is

0.8×(2)=1.6.0.8\times(-2) =-1.6.

Using

min(1.4,1.6)=1.6\min(-1.4,-1.6) =-1.6

removes any additional benefit from pushing rtr_t below 0.80.8.

The min\min can be confusing when the values are negative. Just remember that 1.6-1.6 is smaller than 1.4-1.4 on the number line.

Clipping Does Not Hard-Constrain the Probability

PPO does not force every probability ratio to stay inside [0.8,1.2][0.8,1.2]. Its objective merely removes the incentive for improvement beyond that interval.

Because gradients from other samples share network parameters, some ratios can still cross the boundary. Implementations therefore often monitor an approximate KL divergence and stop an epoch early if it becomes too large.

D^KL(πoldπθ)\widehat{D}_{\mathrm{KL}} \left( \pi_{\text{old}} \parallel \pi_\theta \right)

It is more accurate to view clipping as a mechanism that removes the reward for one sample pushing the policy too far, rather than as a hard constraint.

How PPO Trains One Batch

PPO commonly follows this sequence.

  1. Collect several trajectories with the old policy πθold\pi_{\theta_{\text{old}}}.
  2. Calculate returns and advantages A^t\hat A_t.
  3. Save the old action probability for every sample.
  4. Shuffle the batch into mini-batches.
  5. Optimize the PPO objective for several epochs over the same data.
  6. Treat the updated policy as the next old policy and collect fresh data.
A collected batch is reused over several mini-batches and epochs, while each sample's update incentive is limited relative to its old probability.

What Else Is in the Practical Loss?

An Actor-Critic implementation of PPO does not use only the policy loss.

L=Lpolicy+cvLvalueceH(πθ)L = L_{\text{policy}} + c_vL_{\text{value}} - c_eH(\pi_\theta)
  • LpolicyL_{\text{policy}}: trains the Actor with the clipped objective.
  • LvalueL_{\text{value}}: reduces the Critic's value-prediction error.
  • H(πθ)H(\pi_\theta): encourages entropy so the policy does not collapse onto one action too early.
  • cv,cec_v,c_e: control the relative scale of the terms.

Papers may write an objective to maximize, while code may prepend a minus sign and minimize a loss. When signs differ across implementations, check what each one is maximizing or minimizing.

Why PPO Is Commonly Paired with GAE

PPO is strongly affected by the quality of its advantage estimate. GAE is a common choice.

A^t=δt+(γλ)δt+1+(γλ)2δt+2+\hat A_t = \delta_t + (\gamma\lambda)\delta_{t+1} + (\gamma\lambda)^2\delta_{t+2} +\cdots

where

δt=Rt+1+γV(St+1)V(St).\delta_t = R_{t+1} + \gamma V(S_{t+1}) - V(S_t).

λ\lambda tunes the bias-variance trade-off between short TD estimates and long returns.

Implementations also often normalize advantages within a mini-batch to approximately zero mean and unit standard deviation. This stabilizes gradient scale, but it means that learning uses the relative ordering of advantages in the batch.

What PPO Does Not Solve

PPO is a practical way to stabilize policy updates, not an answer to every RL problem.

  • It still requires continually collecting fresh on-policy data.
  • A poorly designed reward can make it stably optimize the wrong goal.
  • It is sensitive to hyperparameters and implementation details.
  • Clipping alone does not guarantee safety or generalization.

PPO appears again in RLHF. There, too, the central problem is to increase a preference reward without moving too far from a reference policy.

What to Remember

PPO compares the action probabilities of the old and new policies and clips the incentive for an advantage estimate to push the policy too far in one update.

  1. rt=πθ(AtSt)/πold(AtSt)r_t=\pi_\theta(A_t\mid S_t)/\pi_{\text{old}}(A_t\mid S_t).
  2. If A^t>0\hat A_t>0, increase the selected action's probability; if it is negative, decrease it.
  3. PPO-Clip uses the more conservative value before and after clipping.
  4. Clipping is not a hard constraint that forces the ratio into an interval.
  5. Practical PPO is used with a value loss, entropy, and GAE.

References