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 be the policy that collected the trajectories and the policy currently being updated.
For a sample , calculate
This ratio shows how much the probability of the selected action has changed.
- : the probability is unchanged.
- : it is larger than under the old policy.
- : it has fallen to of the old probability.
For example, if an action's probability was under the old policy and under the new policy,
The Clipped Objective
The central PPO-Clip objective is
It looks long, but it simply chooses the more conservative of two candidates.
- : the unconstrained Policy Gradient objective
- : the objective after restricting the ratio to an interval
- : select the less optimistic value
With the common choice , the reference interval is
When the Advantage Is Positive
means the chosen action was better than expected, so we want to increase its probability.
Suppose
The unconstrained value is
The clipped value is
The objective therefore uses
Once the probability ratio exceeds , increasing it further no longer improves the objective for this sample.
When the Advantage Is Negative
means the selected action was worse than expected, so we want to reduce its probability. But we should also prevent an excessive reduction.
If
then
while the clipped value is
Using
removes any additional benefit from pushing below .
The can be confusing when the values are negative. Just remember that is smaller than on the number line.
Clipping Does Not Hard-Constrain the Probability
PPO does not force every probability ratio to stay inside . 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.
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.
- Collect several trajectories with the old policy .
- Calculate returns and advantages .
- Save the old action probability for every sample.
- Shuffle the batch into mini-batches.
- Optimize the PPO objective for several epochs over the same data.
- Treat the updated policy as the next old policy and collect fresh data.
What Else Is in the Practical Loss?
An Actor-Critic implementation of PPO does not use only the policy loss.
- : trains the Actor with the clipped objective.
- : reduces the Critic's value-prediction error.
- : encourages entropy so the policy does not collapse onto one action too early.
- : 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.
where
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.
- .
- If , increase the selected action's probability; if it is negative, decrease it.
- PPO-Clip uses the more conservative value before and after clipping.
- Clipping is not a hard constraint that forces the ratio into an interval.
- Practical PPO is used with a value loss, entropy, and GAE.