Proximal Policy Optimization Algorithms
Reuse each batch of experience for several gradient steps, but clip the objective so the new policy can't drift far from the one that collected the data.
Start here (no maths needed)
PPO teaches a computer by trial and error, but only lets it change its habits a little at a time, so it doesn't undo what it already learned.
Think of it like this
Think of training a dog with treats. When the dog sits and gets a treat, it starts sitting more often. A good trainer doesn't expect the dog to change everything after one treat. They reward small improvements, again and again. If you got too excited after one good moment and changed your whole training plan, you might confuse the dog. PPO is the careful trainer: learn from what just happened, but only take small steps.
How it works
- The program (the "policy") tries the task many times and records what happened.
- For each action, it works out: was this better or worse than expected?
- It makes actions that went well a bit more likely, and ones that went badly a bit less likely.
- It sets a limit on how much any action's chance can change in one round. This is the "clip".
- Throw away the old records, try the task again with the improved policy, and repeat.
Why robots care
Many walking robots, like four-legged robot dogs, learn to walk this way. Thousands of copies practise at the same time in a computer simulation. They fall over, get up, and slowly improve. Then the learned skill is moved to the real robot.
Try it
- Try learning to throw a paper ball into a bin from a new spot. After each throw, change your aim only a little. Then try changing it a lot after each throw. Which way gets you there faster without wild misses?
- In the demo further down, drag the Ratio r slider. Where the line goes flat, the program has already changed enough, and it stops pushing. Then change Clip ε to make the "allowed change" bigger or smaller.
Words to know
- Reinforcement learning
- Learning by trying things and getting rewards or penalties.
- Policy
- The robot's "rulebook" for what to do in each situation.
- Advantage
- How much better (or worse) an action turned out than expected.
- Clip
- A limit on how big a change is allowed in one step.
- Simulation
- A computer version of the world where robots can practise safely.
Going deeper: everything below is the full version, with some maths. On a first read, skim the headings and the "Why it matters for robotics" box, and come back for the rest later.
1. Summary
TL;DR
Plain policy gradient uses each sample once. Optimising on the same data for many steps tends to make destructively large policy updates. TRPO fixed this with a hard KL constraint, but it needs second-order optimisation and is awkward to combine with shared policy/value networks. PPO gets most of TRPO's stability with ordinary first-order SGD. It takes the ratio between the new and old action probabilities and clips it to [1 − ε, 1 + ε], then takes the more pessimistic of the clipped and unclipped objectives. The update then has no incentive to push the ratio past the clip. Several epochs of minibatch Adam on each batch become safe. It's a few lines of change to a policy-gradient implementation, and it became the default on-policy RL algorithm.
- Loop: N parallel actors each collect T steps. Compute advantages with GAE. Run K epochs of minibatch updates on the clipped loss. Repeat.
- Clipping beat the alternatives: on 7 MuJoCo tasks, ε = 0.2 scored best (0.82 normalised) and no clipping or penalty scored −0.39, worse than a random policy.
- An adaptive KL penalty is the paper's second variant. It worked, but slightly worse than clipping.
- Continuous control: PPO outperformed TRPO, CEM, A2C and others on almost all MuJoCo tasks at one million steps, and learned 3D humanoid running and steering in Roboschool.
- Atari: mixed, honestly reported. PPO won more games than ACER on learning speed, and fewer on final score.
2. The core idea
r_t(θ) = π_θ(a_t | s_t) / π_θold(a_t | s_t)
L^CLIP(θ) = Ê_t[ min( r_t(θ)·Â_t , clip(r_t(θ), 1−ε, 1+ε)·Â_t ) ]
L(θ) = Ê_t[ L^CLIP_t(θ) − c1·(V_θ(s_t) − V_t^targ)² + c2·S[π_θ](s_t) ]
Â_t = δ_t + (γλ)·δ_{t+1} + … + (γλ)^(T−t−1)·δ_{T−1}
δ_t = r_t + γ·V(s_{t+1}) − V(s_t)
r_t(θ)is the probability ratio. It equals 1 before any update. Above 1 means the new policy likes action at more than the policy that took it.Â_tis the advantage estimate: how much better the action was than the critic expected.clip(…)caps the ratio at 1 ± ε. The paper uses ε = 0.2.min(…)makes the objective a pessimistic lower bound. Changes that would improve the objective beyond the clip get no credit. Changes that make it worse always count.c1weights the value-function (critic) loss when policy and value share a network.c2weights an entropy bonusSthat keeps exploration alive.δ_tis the one-step TD error. GAE sums them with decay γλ. λ trades bias (small λ) against variance (λ = 1 gives the full Monte Carlo return minus the baseline). Note that rt in δt is the reward, not the ratio. The paper reuses the letter.
MuJoCo settings from the paper: horizon T = 2048, Adam step size 3 × 10−4, 10 epochs, minibatch 64, γ = 0.99, λ = 0.95.
3. Deep dive
What the clip does, case by case
Look at one timestep. If the advantage is positive, the update wants to raise the action's probability, so r grows. Once r passes 1 + ε, the objective goes flat and the gradient for that sample is zero. If the advantage is negative, the update lowers the probability. Once r drops below 1 − ε, it's flat again. But if r has moved the wrong way (say the ratio went up on a bad action), the unclipped term is smaller and the min keeps it, so the gradient still pulls it back. Clipping only removes the incentive to overshoot. It's not a hard wall.
Interactive: the clipped objective for one sample
Left: advantage  = +1. Right:  = −1. Dashed: unclipped r·Â. Solid: LCLIP. The marker shows the ratio r you pick. Flat stretches mean zero gradient.
Where PPO sits on the RL ladder
Module 02's RL ladder climbs Bellman → REINFORCE → A2C → GAE → PPO. Each step fixes the previous one's main weakness. REINFORCE is unbiased but noisy. A baseline and a critic (A2C) cut variance. GAE tunes the bias-variance trade-off. PPO's contribution is sample reuse: K epochs on each batch instead of one step. TRPO had the same goal with a KL constraint solved by conjugate gradient. PPO swaps that for a clip and plain Adam, which also makes it easy to share layers between policy and critic or add auxiliary losses.
It's still on-policy. Once θ has moved, old data no longer represents the current policy, so each batch is discarded after its K epochs. That's why PPO pairs naturally with fast, massively parallel simulators. Off-policy methods (SAC, DQN) reuse data longer and are usually more sample-efficient on real hardware.
From MuJoCo to language models
PPO later became the RL step in RLHF for language models: InstructGPT optimised a learned reward model with PPO plus a KL penalty to the starting model. That's the direct line to Day 30, whose 2017 experiments actually used TRPO and A2C. Newer LLM recipes often replace PPO's critic with group-relative baselines (GRPO) or skip RL for DPO. The clipped-ratio idea survives in most of them. Compare the multi-teacher on-policy distillation in Module 05, another way to train a model on its own samples.
4. Why it matters for robotics
- PPO is the workhorse of sim-trained legged locomotion. Quadruped and humanoid walking policies are commonly trained with PPO + GAE across thousands of parallel simulated robots, then transferred to hardware. For example, Rudin et al. (2021) trained ANYmal walking policies in minutes on one GPU with massively parallel PPO.
- Continuous actions are native. The policy outputs the mean (and a learned standard deviation) of a Gaussian over joint targets or torques, exactly as in the paper's MuJoCo setup with a 2×64 tanh MLP.
- Stability matters more than peak sample efficiency in sim. Sim data is cheap. A method that rarely diverges and needs little tuning across tasks is worth more than a few times better sample efficiency.
- Dexterous manipulation too: OpenAI's in-hand cube rotation with a Shadow hand trained with PPO under heavy domain randomization.
- Limits: on-policy data hunger makes PPO a poor fit for learning directly on a physical robot. There, off-policy RL, imitation learning or fine-tuning a sim-trained policy are more common.
5. How to study it
- Read the paper (it's 12 pages). Focus on sections 3 (clipping), 5 (algorithm) and 6.1 (the ablation table). About 1.5 h.
- Re-derive the four cases of the clip (positive or negative advantage, ratio above or below the clip) with the demo above.
- In the 30u30 Day 29 folder, read
implementation.py: actor-critic network, rollout buffer with GAE, the PPO loss and the training loop. Runtrain_minimal.pyon a small gym task. - Work through
notebook.ipynb, then the exercises in order: clipped loss, GAE, actor-critic, ε ablation, PPO versus vanilla policy gradient. - Stretch: train a MuJoCo or Isaac locomotion task with an existing PPO implementation and watch how the clip fraction and KL behave during training.
6. Self-check
Why not just run many gradient steps on the plain policy-gradient loss?
The loss is only valid near the policy that collected the data. Repeated steps on it push the policy far away, often making destructively large updates. The paper's no-clipping baseline scored below a random policy on average.
With  > 0 and r = 1.5, ε = 0.2, what's the gradient of LCLIP for that sample?
Zero. The clipped term 1.2·Â is smaller than 1.5·Â, so the min selects the clipped term, which doesn't depend on θ.
With  < 0 and r = 1.5, is the gradient zero?
No. Because  is negative, r·Â = 1.5 is lower than the clipped 1.2Â, so min picks the unclipped term and the gradient pushes r back down. Clipping only blocks overshooting in the helpful direction.
What does λ in GAE control?
The bias-variance trade-off of the advantage estimate. λ = 0 uses only the one-step TD error (low variance, more bias). λ = 1 uses the full return (unbiased given the baseline, high variance).
Why is PPO a good match for GPU physics simulators?
It's on-policy and stable, so it benefits from huge, fresh batches of parallel experience and doesn't need a replay buffer. Simulators can supply that data cheaply.
7. Further reading
- The original paper (Schulman et al., 2017) and the GAE paper (Schulman et al., 2015)
- Learning to Walk in Minutes Using Massively Parallel Deep RL (Rudin et al.), PPO for legged robots
- 30u30 Day 29: implementation, notebook, cheatsheet, exercises
- Next: Day 30, Deep Reinforcement Learning from Human Preferences
- Course link: Module 07, After pre-training: where PPO and RLHF sit in today's post-training pipeline, plus a KL-leash / reward-hacking demo