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
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.
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:
The advantage 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 makes the direction much clearer.
GAE: generalised advantage estimation
The one-step TD error is . GAE weights the TD errors of the following steps by :
If the episode ends after , the recursion is cut there. uses one step only (high bias, low variance); equals the Monte Carlo return minus (low bias, high variance); the default is 0.95. The critic's training target is .
The clipped objective
Training the new policy on data collected by the old one, the probability ratio is . PPO's objective:
With a positive advantage, once exceeds there is no more push; with a negative one, once falls below . Taking the smaller of the two guarantees only "conservative" improvement. The total loss adds the value loss 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
| Setting | 10,000 steps | 30,000 | 50,000 | 70,000 |
|---|---|---|---|---|
| Default (ε = 0.2, 4 epochs) | 182.2 | 343.6 | 500 | 500 |
| ε = 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. Fill a rollout
Default settings, 512 steps. Look at "How it is computed" below.
2. The first update
Go to step 530 (training in progress). Look at the GAE table and the clipped objective.
3. Learning
Run to 30,000 steps and open "Returns".
4. Full marks
Run to 50,000 steps.
5. Clipping too loosely
Set the clip ε to 0.5 and run 70,000 steps.
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
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
- Schulman et al., Proximal Policy Optimization Algorithms, 2017.
- Schulman et al., High-Dimensional Continuous Control Using Generalized Advantage Estimation, 2015: GAE.
- Schulman et al., Trust Region Policy Optimization, 2015: TRPO.
- Huang et al., The 37 Implementation Details of Proximal Policy Optimization, ICLR blog track, 2022.
- Raffin et al., Stable-Baselines3, JMLR, 2021: this page follows the same approach.