Search

Search papers and lessons

Proximal Policy Optimization (PPO)

Level 4 · 13 min read

Proximal policy optimization improves a policy with deliberately small gradient steps, clipping how far each update can move an action's probability so one noisy batch cannot wreck the policy.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Reinforcement learning
  • Linear algebra
  • Discrete math
  • Sampling
  • Sign in to see your progress

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 rr, which is the new probability of an action divided by its old probability. Inside the band from 0.80.8 to 1.21.2 the objective rises or falls with rr. Outside it, the curve goes flat in the direction the advantage would reward, so there is nothing left to gain from moving further.

00.511.52−2−1012ratio robjective per unit advantage
  • advantage > 0
  • advantage < 0
With ε = 0.2, the objective stops changing once an action's probability has moved more than 20 percent in the rewarded direction. Moves the wrong way are not clipped.

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 πθ(a∣s)\pi_\theta(a \mid s) be the policy with parameters θ\theta. Recall from the policy gradient lesson that the gradient of the expected return is an expectation of ∇θlog⁡πθ(at∣st)\nabla_\theta \log \pi_\theta(a_t \mid s_t) weighted by how good the action was. We estimate that goodness with an advantage A^t\hat{A}_t, an estimate of Aπ(st,at)A_\pi(s_t, a_t) from the advantage function lesson. In practice A^t\hat{A}_t is usually computed with generalized advantage estimation.

We collect data with a frozen copy of the policy, πθold\pi_{\theta_{\text{old}}}, and then want to improve θ\theta 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:

rt(θ)=πθ(at∣st)πθold(at∣st).r_t(\theta) = \frac{\pi_\theta(a_t \mid s_t)}{\pi_{\theta_{\text{old}}}(a_t \mid s_t)}.

At θ=θold\theta = \theta_{\text{old}} the ratio is exactly 11. A ratio of 1.51.5 means the action is now 5050 percent more likely than it was when the data was collected. A ratio of 0.60.6 means it is 4040 percent less likely.

The importance-weighted policy objective is

LPG(θ)=Et[rt(θ) A^t].L^{\text{PG}}(\theta) = \mathbb{E}_t\left[ r_t(\theta)\, \hat{A}_t \right].

Its gradient at θold\theta_{\text{old}} equals the ordinary policy gradient. The trouble is that nothing stops the optimizer from pushing rtr_t far from 11. For an action with positive advantage the objective keeps growing as rtr_t 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 ϵ\epsilon be a small number, typically 0.20.2. Define clip⁡(x,lo,hi)=min⁡(max⁡(x,lo),hi)\operatorname{clip}(x, lo, hi) = \min(\max(x, lo), hi). The clipped surrogate 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}\big(r_t(\theta),\, 1 - \epsilon,\, 1 + \epsilon\big)\, \hat{A}_t \right) \right].

Read it one sample at a time. There are three cases.

  • Positive advantage, A^t>0\hat{A}_t \gt 0. The action was better than expected, so raising its probability helps. The unclipped term grows with rtr_t until rtr_t reaches 1+ϵ1 + \epsilon. Above that the clipped term is smaller, the minimum selects it, and it is flat. The gradient is zero for that sample.
  • Negative advantage, A^t<0\hat{A}_t \lt 0. Lowering the probability helps. The objective stops improving once rtr_t falls to 1−ϵ1 - \epsilon. Below that the minimum selects the flat clipped term.
  • Moving the wrong way. If the advantage is positive but rtr_t has fallen below 1−ϵ1 - \epsilon, 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 rt=0.6r_t = 0.6, 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 Vϕ(s)V_\phi(s), 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

LtTOTAL(θ,ϕ)=Et[LtCLIP(θ)−c1 (Vϕ(st)−Vttarg)2+c2 S[πθ](st)].L^{\text{TOTAL}}_t(\theta, \phi) = \mathbb{E}_t\left[ L^{\text{CLIP}}_t(\theta) - c_1\, \big(V_\phi(s_t) - V^{\text{targ}}_t\big)^2 + c_2\, S[\pi_\theta](s_t) \right].

Here VttargV^{\text{targ}}_t is the return estimate used as the training target, S[πθ](st)S[\pi_\theta](s_t) is the entropy of the action distribution in state sts_t (large when the policy is spread out, near zero when it is nearly deterministic), and c1c_1 and c2c_2 are weighting constants. Implementations usually minimise the negative of this quantity, which is the same problem with the sign flipped.

The training loop

  1. Copy the current parameters to θold\theta_{\text{old}}. Run the policy for TT steps and record states, actions, rewards and the old action probabilities.
  2. Compute advantages A^t\hat{A}_t 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.
  3. For KK epochs, shuffle the batch into minibatches. For each minibatch take one gradient ascent step on LTOTALL^{\text{TOTAL}}.
  4. 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

Example
A good action that already improved too much

Setting: ϵ=0.2\epsilon = 0.2. In one state, the old policy chose action "wait" with probability 0.200.20. The new policy now gives it 0.300.30. The advantage estimate for "wait" is A^=+2\hat{A} = +2.

  1. Ratio: r=0.30/0.20=1.5r = 0.30 / 0.20 = 1.5.
  2. Unclipped term: r A^=1.5×2=3.0r\,\hat{A} = 1.5 \times 2 = 3.0.
  3. Clipped ratio: clip⁡(1.5,0.8,1.2)=1.2\operatorname{clip}(1.5, 0.8, 1.2) = 1.2. Clipped term: 1.2×2=2.41.2 \times 2 = 2.4.
  4. Minimum: min⁡(3.0,2.4)=2.4\min(3.0, 2.4) = 2.4.
  5. 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 5050 percent, more than the 2020 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.

Example
A bad action pushed down too far

Setting: ϵ=0.2\epsilon = 0.2. The old probability of action "a" was 0.500.50, the new probability is 0.300.30, and the advantage is A^=−1\hat{A} = -1.

  1. Ratio: r=0.30/0.50=0.6r = 0.30 / 0.50 = 0.6.
  2. Unclipped term: 0.6×(−1)=−0.60.6 \times (-1) = -0.6.
  3. Clipped ratio: clip⁡(0.6,0.8,1.2)=0.8\operatorname{clip}(0.6, 0.8, 1.2) = 0.8. Clipped term: 0.8×(−1)=−0.80.8 \times (-1) = -0.8.
  4. Minimum: min⁡(−0.6,−0.8)=−0.8\min(-0.6, -0.8) = -0.8.
  5. The clipped term won, so the gradient is zero for this sample.

Interpretation: action "a" is bad and its probability has already fallen by 4040 percent, twice the allowed 2020 percent. Pushing it lower gives no reward.

Example
A good action that moved the wrong way

Setting: ϵ=0.2\epsilon = 0.2. The advantage is A^=+2\hat{A} = +2, but the ratio has dropped to r=0.6r = 0.6, so the action became less likely during the update.

  1. Unclipped term: 0.6×2=1.20.6 \times 2 = 1.2.
  2. Clipped ratio: clip⁡(0.6,0.8,1.2)=0.8\operatorname{clip}(0.6, 0.8, 1.2) = 0.8. Clipped term: 0.8×2=1.60.8 \times 2 = 1.6.
  3. Minimum: min⁡(1.2,1.6)=1.2\min(1.2, 1.6) = 1.2.
  4. 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.

Example
A language model token

Setting: ϵ=0.2\epsilon = 0.2. Context: "The capital of France is". The old policy gave the token " Paris" probability 0.400.40, and the new policy gives 0.520.52. A reward model says the response was better than average, so A^=+0.8\hat{A} = +0.8.

  1. Ratio: r=0.52/0.40=1.3r = 0.52 / 0.40 = 1.3.
  2. Unclipped term: 1.3×0.8=1.041.3 \times 0.8 = 1.04.
  3. Clipped ratio: clip⁡(1.3,0.8,1.2)=1.2\operatorname{clip}(1.3, 0.8, 1.2) = 1.2. Clipped term: 1.2×0.8=0.961.2 \times 0.8 = 0.96.
  4. Minimum: min⁡(1.04,0.96)=0.96\min(1.04, 0.96) = 0.96.
  5. The clipped term won. Further increases to the probability of " Paris" in this context earn nothing in this update.

Interpretation: the probability rose from 0.400.40 to 0.520.52, a 3030 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.2

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
You want stable on-policy training with little tuningNothing serious; PPO is the default for thisUse PPO
You need a hard guarantee on how far the policy movesThe clip is a heuristic and does not bound KLTRPO, or PPO with a KL penalty or early stopping
Many epochs on one batch with a large learning rateRatios drift outside the band, and most samples end up clipped, so learning stallsFewer epochs or a smaller learning rate, and monitor KL
Noisy or biased advantage estimatesThe clip limits step size but cannot fix a wrong signalTune GAE, normalise advantages, check the value function
Stale data from many asynchronous workersRatios are computed against a policy that is far behindFresher data, or an off-policy method designed for it
Policy becomes deterministic too earlyExploration disappearsIncrease the entropy coefficient c2c_2

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 rt(θ)r_t(\theta) when θ=θold\theta = \theta_{\text{old}}, and why?Show answer

It equals 11. 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 [1−ϵ,1+ϵ][1 - \epsilon, 1 + \epsilon] is centred on the state the data was collected in.

QuestionComputation: ϵ=0.2\epsilon = 0.2. An action has advantage A^=+1.5\hat{A} = +1.5 and ratio r=0.7r = 0.7. Compute the clipped objective term and say whether its gradient flows.Show answer

Unclipped term: 0.7×1.5=1.050.7 \times 1.5 = 1.05. Clipped ratio: clip⁡(0.7,0.8,1.2)=0.8\operatorname{clip}(0.7, 0.8, 1.2) = 0.8, so the clipped term is 0.8×1.5=1.20.8 \times 1.5 = 1.2. The minimum is 1.051.05, from the unclipped term. Its gradient flows, pushing the probability of the action up toward the band.

QuestionComputation: ϵ=0.2\epsilon = 0.2. An action has advantage A^=−2\hat{A} = -2 and ratio r=1.4r = 1.4. Compute the clipped objective term and say whether its gradient flows.Show answer

Unclipped term: 1.4×(−2)=−2.81.4 \times (-2) = -2.8. Clipped ratio: clip⁡(1.4,0.8,1.2)=1.2\operatorname{clip}(1.4, 0.8, 1.2) = 1.2, so the clipped term is 1.2×(−2)=−2.41.2 \times (-2) = -2.4. The minimum is −2.8-2.8, 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 4545 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.

Sources