Proximal policy optimization (PPO)

Proximal policy optimization trained live on CartPole: rollouts, GAE advantages, the clipped objective with every ratio, actor and critic, and the policy and value drawn as maps and 3D surfaces.

Updated

  • Policy gradients with an actor and a critic
  • Generalized advantage estimation (GAE)
  • The probability ratio and the clipped objective
  • Epochs of minibatch updates on one rollout
  • Entropy, KL and the clip fraction
A > 0A < 0ρ = π_θ / π_oldmin(ρA, clip(ρ, 1 − ε, 1 + ε)A)
Preparing the lab…

Intuition

Adjust the action probabilities directly

DQN asks "how much is this action worth"; PPO asks "should this action be taken more or less often". The actor network gives, for each state, the probabilities of pushing left or right; the agent pushes according to them and records what happens. Afterwards: if things went better than expected after a step, that step's action is made a little more likely; if worse, a little less.

The three small labs below really train in a background thread of your browser (rollouts of 512 steps, 4 epochs per update in minibatches of 64, learning rate 0.001, clip 0.2). On top is the current cart and pole; below, each bar is one episode's return and the dashed line is 475.

10,000 stepsPreparing…
30,000 stepsPreparing…
50,000 stepsPreparing…

Average return of the last 20 episodes: 182.2 at 10,000 steps, 343.6 at 30,000, 500 (the maximum) at 50,000. These are timeline steps: while collecting, a step is one environment step; while training, a step is one minibatch. Of every 544 steps 512 are collection, so 50,000 steps are about 47,000 environment steps.

Math

Policy gradient and advantage

To maximise expected return, the policy-gradient theorem gives the direction of improvement:

∇θJ=E[ ∇θlog⁡πθ(a∣s)  A(s,a) ]\nabla_\theta J = \mathbb{E}\big[\, \nabla_\theta \log \pi_\theta(a \mid s)\; A(s, a) \,\big]

The advantage A(s,a)A(s, a) says how much better this action was than the average in this state. Using raw returns without subtracting the average gives huge variance; subtracting the critic's estimate V(s)V(s) makes the direction much clearer.

GAE: generalised advantage estimation

The one-step TD error is δt=rt+γV(st+1)−V(st)\delta_t = r_t + \gamma V(s_{t+1}) - V(s_t). GAE weights the TD errors of the following steps by (γλ)k(\gamma\lambda)^k:

At=δt+γλ At+1A_t = \delta_t + \gamma\lambda\, A_{t+1}

If the episode ends after tt, the recursion is cut there. λ=0\lambda = 0 uses one step only (high bias, low variance); λ=1\lambda = 1 equals the Monte Carlo return minus VV (low bias, high variance); the default is 0.95. The critic's training target is Rt=At+V(st)R_t = A_t + V(s_t).

The clipped objective

Training the new policy on data collected by the old one, the probability ratio is ρt=πθ(at∣st)/πold(at∣st)\rho_t = \pi_\theta(a_t \mid s_t) / \pi_{\text{old}}(a_t \mid s_t). PPO's objective:

Lclip=E[min⁡(ρtA^t, clip(ρt,1−ε,1+ε) A^t)]L^{\text{clip}} = \mathbb{E}\big[\min\big(\rho_t \hat A_t,\ \mathrm{clip}(\rho_t, 1-\varepsilon, 1+\varepsilon)\,\hat A_t\big)\big]

With a positive advantage, once ρ\rho exceeds 1+ε1+\varepsilon there is no more push; with a negative one, once ρ\rho falls below 1−ε1-\varepsilon. Taking the smaller of the two guarantees only "conservative" improvement. The total loss adds the value loss 0.5 E[(R−V)2]0.5\,\mathbb{E}[(R - V)^2] and subtracts an entropy bonus (0 by default); advantages are standardised within each minibatch; both networks' gradients are clipped together to norm 0.5, with Adam updates.

How much to clip

Setting10,000 steps30,00050,00070,000
Default (ε = 0.2, 4 epochs)182.2343.6500500
ε = 0.5—430.25—299.2
10 epochs—500——

ε = 0.5 is ahead at 30,000 steps (430.25 against 343.6), but within 50,000 steps it peaks at 459.1, never meets the bar, and keeps falling afterwards: with too loose a clip, one update can spoil the policy. Training each rollout for 10 epochs (default 4) reaches the maximum by 30,000 steps: the same data is used more thoroughly.

How it is computed, with real numbers

Once the lab above has loaded, this panel works through the same formulas with its current numbers.

Experiment

Follow the lesson

Course

Lesson 1: rollouts, advantages and clipping

5 steps · each one: set up and run → look → answer

  1. 1. Fill a rollout

    Default settings, 512 steps. Look at "How it is computed" below.

    Why has the policy not changed once in these 512 steps?
  2. 2. The first update

    Go to step 530 (training in progress). Look at the GAE table and the clipped objective.

    How are Aₜ and δₜ related in the GAE table?
  3. 3. Learning

    Run to 30,000 steps and open "Returns".

    The last 20 episodes average 343.6. How does PPO's curve differ from DQN's?
  4. 4. Full marks

    Run to 50,000 steps.

    An average of 500 is the maximum. Roughly how many environment steps are 50,000 timeline steps?
  5. 5. Clipping too loosely

    Set the clip ε to 0.5 and run 70,000 steps.

    It reaches 430.25 at 30,000 steps but falls to 299.2 by 70,000. Why?

Try these settings

Exercises

Exercises

Exercise 1: tighten the clip

With clip ε = 0.5 the best within 50,000 steps is 459.1. Change the settings so that the last 20 episodes average at least 475 within 50,000 steps.

Hint

Set the clip back to 0.2: reached at step 42,500.

  • Average return of the last 20 episodes ≥ 475
  • at most 50000 steps

Exercise 2: faster

The defaults reach only 343.6 by 30,000 steps. Change the settings to reach 475 within 30,000 steps.

Hint

Raise the number of training epochs per rollout from 4 to 10: full marks by 30,000 steps.

  • Average return of the last 20 episodes ≥ 475
  • at most 30000 steps

Quiz

What does PPO's “clipping” clip?
With GAE's λ = 1, the advantage equals…
Is PPO on-policy or off-policy?

Further reading

Frequently asked questions

What is the difference between PPO and DQN?

DQN learns action values Q and takes the best action (value-based, off-policy, discrete actions only); PPO learns the action probability distribution directly (policy gradient, on-policy, for discrete and continuous actions alike). PPO usually trains more steadily; DQN often uses samples more efficiently on discrete problems.

Why is PPO so widely used?

It is simple to implement, not very sensitive to hyperparameters, works with discrete and continuous actions, and trains stably. It is the default algorithm in many reinforcement-learning libraries and is often used in the RLHF (reinforcement learning from human feedback) stage of large language models.

What is the advantage function?

A(s, a) = Q(s, a) − V(s): how much better this action is than the average in this state. Using advantages rather than raw returns in the policy gradient greatly reduces variance.

What is the entropy bonus for?

Subtracting a small multiple of the policy's entropy from the loss encourages the policy to stay somewhat random, avoiding settling on one action too early and stopping exploration. It is 0 by default on this page.

How is PPO related to TRPO?

TRPO bounds each update with a KL-divergence constraint and needs second-order optimisation, which is complex; PPO approximates the same effect with a clipped probability ratio and needs only first-order optimisation, which is far simpler. This page's "How it is computed" shows the approximate KL of the latest minibatch, so you can see how much each update changes.

Further reading