ML Systems Lab Open interactive version →
Advanced 60 min read ppotrpotrust regionkl divergenceclipped objective

PPO and TRPO

Trust region, KL constraint, clipped surrogate, entropy bonus, implementation details

You are training a quadruped robot to walk. With vanilla policy gradient, you take a gradient step and the policy changes. If the step is too large, the robot attempts movements far outside the distribution of the collected data — the advantage estimates, computed under the old policy, are completely wrong for the new one. The robot was walking; after one bad update it is lying on the ground producing no useful gradient signal. Recovery is impossible because the next gradient step is also based on wrong estimates. This catastrophic policy collapse is the problem PPO and TRPO solve.

TRPO formalizes the constraint: maximize the expected advantage subject to KL(π_old || π_new) ≤ δ. The KL constraint defines a trust region — updates inside it are theoretically safe, with a guaranteed lower bound on policy improvement. The cost is second-order optimization: computing the natural gradient requires Fisher information matrix-vector products, conjugate gradient, and a line search. Correct, but expensive and complex.

PPO approximates TRPO with a clipped objective: L_CLIP = E[min(r_t A_t, clip(r_t, 1-ε, 1+ε) A_t)] where r_t = π_new/π_old. When A_t > 0 and r_t > 1+ε — the policy is already much more likely to take this good action — the gradient is killed. When A_t < 0 and r_t < 1-ε — the policy has already moved away from this bad action — the gradient is killed again. The clip enforces a soft trust region using only first-order optimization and vanilla Adam.

PPO's clip parameter ε = 0.2 allows up to 20% policy ratio change per step. This is not overly conservative — it prevents catastrophic collapse while still allowing rapid learning across multiple mini-batch epochs per collected batch. The fraction of clipped updates should be 10–30% during stable training. Below 1% means the clip is never activating and providing no stability benefit. Above 90% means the policy is drifting too far and the trust region is already broken.

NOT this: PPO is conservative and learns slowly. PPO with ε = 0.2 is not slow — the alternative (unconstrained large updates) produces policy collapse that wastes entire training runs. PPO's constraint is exactly why it achieves consistent results across random seeds when unconstrained policy gradients do not.

Key points

Takeaway

PPO prevents catastrophic policy collapse by killing gradients when the policy ratio r_t moves outside [1-ε, 1+ε] — ensuring each update stays within a soft trust region where the advantage estimates are still valid.

Recap

Check your understanding

Q1. Which two of the following statements correctly describe when the PPO clip objective kills the gradient?

Q2. You are training PPO on a continuous control task and observe that training is stable for 100 updates, then the policy collapses — mean episode reward drops from +500 to near 0 and never recovers. What happened and how do you diagnose and fix it?

Q3. In RLHF with PPO for an LLM, why is the KL penalty to the SFT model necessary? What happens if you remove it?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →