TL;DR
Proximal policy optimization (PPO) improves a policy with gradient steps that are deliberately small. It takes a batch of recent experience, estimates how much better each action was than expected, and pushes the policy toward the actions that did well. The key detail is a clipped objective: once an action's probability has changed by a set fraction, the objective stops rewarding further change. One noisy batch therefore cannot throw the policy off a cliff.
Where you'll see it: training game-playing and robotic agents with reinforcement learning, and in the reinforcement learning stage of RLHF for language models, where a reward model scores the responses.
Intuition
Learn from a batch, but not too far
Policy gradient methods are simple. Sample some behaviour, see which actions led to good outcomes, and make those actions more likely. The difficulty is the step size. A step that is too small makes training crawl. A step that is too large changes the policy so much that the data you collected no longer describes it, and the next update is built on a broken foundation.
A cook tasting a sauce adds a pinch, not the whole jar. PPO works the same way: move toward what worked, but do not rewrite the policy after one batch of evidence.
Earlier work, TRPO, enforced this limit with a hard constraint on how far the policy could move. That required second-order calculations and a careful line search. PPO gets most of the same effect with a few lines of code and an ordinary optimizer such as Adam. The rest of this lesson explains those few lines.
The picture below shows the core idea. Each curve is the objective for one unit of advantage, plotted against the probability ratio , which is the new probability of an action divided by its old probability. Inside the band from to the objective rises or falls with . Outside it, the curve goes flat in the direction the advantage would reward, so there is nothing left to gain from moving further.
- advantage > 0
- advantage < 0
The loop in one picture
PPO alternates between collecting experience with a frozen copy of the policy and improving the live policy on that experience for several passes.
flowchart LR A[Run current policy and collect experience] --> B[Estimate advantages] B --> C[Compute probability ratio of new to old] C --> D[Clipped objective] D --> E[Several epochs of minibatch gradient steps] E --> A
The math
Setup
Let be the policy with parameters . Recall from the policy gradient lesson that the gradient of the expected return is an expectation of weighted by how good the action was. We estimate that goodness with an advantage , an estimate of from the advantage function lesson. In practice is usually computed with generalized advantage estimation.
We collect data with a frozen copy of the policy, , and then want to improve using that data. The data came from the old policy, so a correction is needed.
The probability ratio
Define the ratio for each sampled action:
At the ratio is exactly . A ratio of means the action is now percent more likely than it was when the data was collected. A ratio of means it is percent less likely.
The importance-weighted policy objective is
Its gradient at equals the ordinary policy gradient. The trouble is that nothing stops the optimizer from pushing far from . For an action with positive advantage the objective keeps growing as grows, so an unconstrained optimizer will make the update as large as the learning rate allows, and a single batch can destroy the policy.
The clipped objective
PPO's fix is to limit the objective. Let be a small number, typically . Define . The clipped surrogate objective is
Read it one sample at a time. There are three cases.
- Positive advantage, . The action was better than expected, so raising its probability helps. The unclipped term grows with until reaches . Above that the clipped term is smaller, the minimum selects it, and it is flat. The gradient is zero for that sample.
- Negative advantage, . Lowering the probability helps. The objective stops improving once falls to . Below that the minimum selects the flat clipped term.
- Moving the wrong way. If the advantage is positive but has fallen below , the unclipped term is the smaller one. The minimum selects it, its gradient is nonzero, and the update pushes the probability back up. The same holds on the negative side.
The minimum makes the objective a pessimistic bound. It never gives credit for a move beyond the band, but it still penalises a move in the wrong direction. Clipping the whole term alone would be worse. For a good action with , a clipped-only objective would sit flat at the boundary and never recover.
The full objective
The policy is not the only thing trained. A value network , an estimate of the value function, is trained to predict returns, and an entropy bonus keeps the policy from collapsing too early. The combined objective, written as something to maximise, is
Here is the return estimate used as the training target, is the entropy of the action distribution in state (large when the policy is spread out, near zero when it is nearly deterministic), and and are weighting constants. Implementations usually minimise the negative of this quantity, which is the same problem with the sign flipped.
The training loop
- Copy the current parameters to . Run the policy for steps and record states, actions, rewards and the old action probabilities.
- Compute advantages and value targets for the batch. Many implementations normalise the advantages to zero mean and unit variance within the batch, which stabilises the step size.
- For epochs, shuffle the batch into minibatches. For each minibatch take one gradient ascent step on .
- Return to step 1.
Several epochs on the same batch is what makes PPO efficient compared with methods that use each sample once. The clip is what makes those repeated passes safe.
Why the clip is only an approximate trust region
TRPO bounded the KL divergence between the old and new policies directly. PPO's clip does not guarantee a KL bound. The gradient stops for samples whose ratio has left the band, but the other samples in the batch still move the shared parameters, and those moves can push clipped samples further out during later epochs. A PPO variant, sometimes called PPO-penalty, adds a KL term to the objective with an adaptive coefficient. Many practical implementations instead track the KL between old and new policies and stop the epochs early once it exceeds a target.
Worked examples
Setting: . In one state, the old policy chose action "wait" with probability . The new policy now gives it . The advantage estimate for "wait" is .
- Ratio: .
- Unclipped term: .
- Clipped ratio: . Clipped term: .
- Minimum: .
- The clipped term won, so this sample contributes no gradient.
Interpretation: "wait" was a good choice, and the policy has already raised its probability by percent, more than the percent band allows in one update. The objective gives no extra credit for raising it further. Other samples in the batch can still move the parameters.
Setting: . The old probability of action "a" was , the new probability is , and the advantage is .
- Ratio: .
- Unclipped term: .
- Clipped ratio: . Clipped term: .
- Minimum: .
- The clipped term won, so the gradient is zero for this sample.
Interpretation: action "a" is bad and its probability has already fallen by percent, twice the allowed percent. Pushing it lower gives no reward.
Setting: . The advantage is , but the ratio has dropped to , so the action became less likely during the update.
- Unclipped term: .
- Clipped ratio: . Clipped term: .
- Minimum: .
- The unclipped term won, so its gradient is nonzero and pushes the probability back up.
Interpretation: the clip does not freeze a sample that has drifted the wrong way. This is what lets the objective correct mistakes from earlier epochs.
Setting: . Context: "The capital of France is". The old policy gave the token " Paris" probability , and the new policy gives . A reward model says the response was better than average, so .
- Ratio: .
- Unclipped term: .
- Clipped ratio: . Clipped term: .
- Minimum: .
- The clipped term won. Further increases to the probability of " Paris" in this context earn nothing in this update.
Interpretation: the probability rose from to , a percent increase. That is larger than one update should make, so the objective stops rewarding it.
The same computation in Python, using the three cases from the worked examples above:
def clipped_term(ratio, advantage, eps=0.2):
unclipped = ratio * advantage
clipped = min(max(ratio, 1 - eps), 1 + eps) * advantage
return min(unclipped, clipped)
print(clipped_term(1.5, 2.0)) # 2.4
print(clipped_term(0.6, -1.0)) # -0.8
print(clipped_term(0.6, 2.0)) # 1.2Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| You want stable on-policy training with little tuning | Nothing serious; PPO is the default for this | Use PPO |
| You need a hard guarantee on how far the policy moves | The clip is a heuristic and does not bound KL | TRPO, or PPO with a KL penalty or early stopping |
| Many epochs on one batch with a large learning rate | Ratios drift outside the band, and most samples end up clipped, so learning stalls | Fewer epochs or a smaller learning rate, and monitor KL |
| Noisy or biased advantage estimates | The clip limits step size but cannot fix a wrong signal | Tune GAE, normalise advantages, check the value function |
| Stale data from many asynchronous workers | Ratios are computed against a policy that is far behind | Fresher data, or an off-policy method designed for it |
| Policy becomes deterministic too early | Exploration disappears | Increase the entropy coefficient |
Two practical warnings. First, PPO's results depend heavily on implementation details such as advantage normalisation, value-function clipping, observation normalisation and weight initialisation. Engstrom and colleagues showed that changing these details can matter as much as the choice between PPO and TRPO. Second, watch the clip fraction, the share of samples where clipping is active. A small fraction is healthy. A large one, such as the 45 percent in the last check-yourself question, means the updates are too big for the data.
History
Trust region methods came first. In 2015 John Schulman and colleagues published trust region policy optimization (TRPO), which maximised a surrogate objective subject to a KL constraint. It worked well but was complicated to implement.
In July 2017 Schulman, Filip Wolski, Prafulla Dhariwal, Alec Radford and Oleg Klimov, then at OpenAI, published PPO. They showed that the clipped objective matched TRPO's stability on continuous control benchmarks with a much simpler algorithm. It spread quickly because it was easy to implement and to scale across many parallel workers.
PPO later became the standard policy optimizer for large reinforcement learning systems, including game-playing agents and the reinforcement learning stage of RLHF for language models, as described in the PPO paper node and in the RLHF lesson.
Check yourself
QuestionRecall: what is the value of the probability ratio when , and why?Show answer
It equals . The numerator and denominator are the same probability, so their ratio is exactly one. This is the starting point of every update: the clipped band is centred on the state the data was collected in.
QuestionComputation: . An action has advantage and ratio . Compute the clipped objective term and say whether its gradient flows.Show answer
Unclipped term: . Clipped ratio: , so the clipped term is . The minimum is , from the unclipped term. Its gradient flows, pushing the probability of the action up toward the band.
QuestionComputation: . An action has advantage and ratio . Compute the clipped objective term and say whether its gradient flows.Show answer
Unclipped term: . Clipped ratio: , so the clipped term is . The minimum is , from the unclipped term. Its gradient flows and pushes the probability back down toward the band.
QuestionWhy: why take the minimum of the clipped and unclipped terms, rather than clipping the ratio and stopping there?Show answer
Clipping alone would give zero gradient to a sample whose ratio had moved the wrong way past the band. Such a sample could never correct itself. The minimum keeps the gradient on wrong-way moves and removes it only for moves that the advantage already rewarded past the limit. The objective therefore never over-claims credit for a large change.
QuestionApplied: training logs show that percent of samples have clipping active in every update, and the KL between the old and new policies keeps rising across epochs. What is happening, and what would you change?Show answer
The updates are too large for the batch. Many samples have been pushed to the boundary, and the shared parameters keep moving them further. Reduce the learning rate or the number of epochs per batch, or stop the epochs early once KL exceeds a target. Also check that the old action probabilities were recorded under the same policy that generated the data, and that advantages are normalised.