Foundations to Frontier
Tests
L. Reinforcement Learning and Autonomy › L4 Deep RL: Policy-Based

Trust-region methods: TRPO and PPO

A policy-gradient step that is too large can wreck a policy, and the next batch of data is then collected by the wrecked policy. Trust-region methods limit how far each update moves the policy, measured by the KL divergence. TRPO enforces the limit with a natural-gradient step and a line search; PPO approximates it with a clipped objective and simple first-order steps, which lets it reuse each batch for many epochs. This page derives both, builds PPO on 16 parallel cart-poles, and measures what clipping does when a batch is reused 30 times.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Small, safe steps

After each round of practice, a learner could change its habits a lot or a little. Changing a lot based on a few games is risky: some of what looked good was luck, and a bad change means the next practice games are played badly too. Trust-region methods make sure the new way of playing stays close to the old one, so the learner can safely squeeze more out of the same practice games.

Deep reinforcement learning lab
PPO on 16 cart-poles: each batch is 16 × 32 steps with GAE(0.95) advantages, normalised; then 30 full-batch Adam steps (actor step 0.1) on the surrogate clipped at ε = 0.2; 150 batches. Top: episode length (20-episode average; dashed: 200). Bottom: log₁₀ of the KL divergence from the old to the new policy after each update; dashed: 0.01. The last 20 episodes first averaged 195 after 9.1k steps; final 20-episode mean 200.0; largest KL in one update 0.026.
Why engineers care

PPO is the most widely used deep RL algorithm: it trained OpenAI Five for Dota 2 and is the standard optimiser for reinforcement learning from human feedback in chat models.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A trust region limits…
  2. 2. Big updates from little data are risky because…
  3. 3. PPO is widely used in…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Importance ratio: ρ_t(θ) = π_θ(A_t|S_t) / π_old(A_t|S_t), with π_old the policy that collected the batch.
  • Surrogate objective: L(θ) = E[ρ_t(θ)Â_t]. At θ = θ_old its gradient equals the policy gradient.
  • TRPO (Schulman et al. 2015): maximise L(θ) subject to the average KL(π_old ‖ π_θ) ≤ δ. Solved approximately with a natural-gradient direction F⁻¹g (conjugate gradient), scaled to the KL limit, then a backtracking line search.
  • PPO-clip (Schulman et al. 2017): maximise E[min(ρ_tÂ_t, clip(ρ_t, 1 − ε, 1 + ε)Â_t)], typically ε = 0.2, with several epochs of minibatch Adam steps per batch.
  • PPO-penalty: maximise L(θ) − β KL, adapting β to hit a target KL.
  • KL divergence: KL(p ‖ q) = Σ p log(p/q) ≥ 0, zero only when p = q, not symmetric.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The ratio ρ compares…
  2. 2. PPO's typical clip ε is…
  3. 3. TRPO limits…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

PPO on cart-pole

Exercise 1.

  • The A2C set-up of topic L4-3: 16 cart-poles, linear sigmoid actor, critic linear in quadratic features, GAE(0.95).
  • Batches of 16 × 32 steps; advantages normalised to mean 0, standard deviation 1.
  • Several full-batch Adam epochs per batch on the clipped surrogate, whose gradient is Â∇ρ = Âρ(a − p)φ where the clip is inactive and 0 where it is active.
  • The KL divergence from the old to the new policy is recorded after every update.

Practice.

  • Defaults that work widely: ε = 0.2, 3 to 10 epochs, 4 to 32 minibatches, GAE λ = 0.95, γ = 0.99, Adam 3e-4 for networks.
  • Watch the KL and the clip fraction: an approximate KL far above 0.02 to 0.05, or a clip fraction above about 0.3, means steps are too large; many implementations stop the epochs early when the KL passes a target.
  • Normalise advantages and observations; clip gradients to norm 0.5.
  • Value loss: often clipped as well, and weighted against an entropy bonus.
  • Implementation details matter: studies of PPO found that code-level choices (normalisation, learning-rate annealing, initialisation) can matter as much as the clipping itself.
  • Tools: Stable-Baselines3 PPO, CleanRL ppo.py, RLlib, and TRL for language-model fine-tuning.
def surrogate_gradient(F, A, p_old, p_new, adv, eps):
    pi_new = np.where(A == 1, p_new, 1 - p_new)
    pi_old = np.where(A == 1, p_old, 1 - p_old)
    rho = pi_new / pi_old
    clipped = ((adv > 0) & (rho > 1 + eps)) | ((adv < 0) & (rho < 1 - eps))
    g = np.where(clipped, 0.0, adv * rho * (A - p_new))[..., None] * F
    return g, clipped
🐍 Exercise 1: the clipped surrogate and PPO
Implement the gradient of PPO's clipped surrogate for a sigmoid policy, check it against finite differences, and train PPO on parallel cart-poles.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A clip fraction far above 0.3 suggests…
  2. 2. Early stopping of PPO epochs is triggered by…
  3. 3. PPO for language models is in…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Surrogate gradient. ∇ρ_t = ∇π_θ(A_t|S_t)/π_old(A_t|S_t). At θ = θ_old this is ∇log π_θ(A_t|S_t), so ∇L(θ_old) is exactly the policy gradient. Away from θ_old, L is only a local model of the true objective because the states were visited by π_old.

Policy improvement bound (Kakade and Langford 2002; Schulman et al. 2015). J(π_new) ≥ J(π_old) + L(π_new) − C·max_s KL(π_old ‖ π_new)(s), with C = 4εγ/(1 − γ)² and ε the largest advantage magnitude. Maximising the right-hand side never decreases J; TRPO replaces the penalty by a constraint on the average KL.

Quadratic KL. For small d, KL(π_θ ‖ π_(θ+d)) ≈ ½ dᵀF d, with F the Fisher matrix. So the TRPO step solves max gᵀd subject to ½dᵀFd ≤ δ: d = √(2δ / gᵀF⁻¹g) F⁻¹g, a natural-gradient step of fixed KL size.

Clipping. min(ρÂ, clip(ρ)Â) ≤ ρÂ, so the clipped objective is a pessimistic lower bound on the surrogate. Once ρ has moved more than ε in the direction the advantage favours, the gradient of that sample is zero; moves in the unfavoured direction are never hidden. Clipping limits the ratio only for samples in the batch; it does not strictly bound the KL.

📝 Quick check (Level 3)
0/3 answered
  1. 1. At θ = θ_old the surrogate's gradient equals…
  2. 2. The KL divergence near θ is approximately…
  3. 3. Clipping strictly bounds the KL.

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

On cart-pole, what happens when one batch is reused for 30 Adam epochs, with and without clipping, compared with a single step per batch?

History.

  • S. Kakade and J. Langford (2002) proved the conservative policy iteration improvement bound.
  • J. Schulman, S. Levine, P. Abbeel, M. Jordan and P. Moritz (2015) introduced TRPO.
  • J. Schulman, F. Wolski, P. Dhariwal, A. Radford and O. Klimov (2017) introduced PPO.
  • L. Engstrom, A. Ilyas and colleagues (2020) and M. Andrychowicz and colleagues (2021) showed how much PPO’s results depend on implementation details.
  • L. Ouyang and colleagues (2022) used PPO for InstructGPT’s reinforcement learning from human feedback.

Exercise 2.

  • PPO as in Exercise 1 with Adam step 0.1 for the actor; 150 batches of 16 × 32 steps; four learners per setting, all trained at once.
  • Settings: one step per batch with no clipping (A2C-like); 30 epochs without clipping; 30 epochs with ε = 0.1, 0.2 and 0.3.
  • Measured: thousands of environment steps until the last 20 episodes average 195; final last-20 mean; the largest KL in one update; the clip fraction.

Seed 0:

setting thousands of steps to 195 last-20 mean largest KL in one update clip fraction
one step per batch 42.3, 36.5, 53.2, 44.9 200, 190.8, 194.6, 113.4 0.003 to 0.008 0
30 epochs, no clipping 33.7, 25.2, 8.6, 29.5 200, 200, 9.7, 200 2.54 to 3.44 0
30 epochs, ε = 0.1 never, 7.0, 19.1, 11.2 176.7, 200, 159.8, 200 0.006 to 0.010 0.060
30 epochs, ε = 0.2 6.4, 28.6, 8.4, 10.8 200, 200, 200, 200 0.021 to 0.051 0.055
30 epochs, ε = 0.3 11.0, 19.4, 29.4, 20.7 200, 200, 200, 200 0.053 to 0.090 0.045
  • Reusing data pays: with clipping, the median learner on seed 0 reached 195 after 9.6 (ε = 0.2), 11.2 (ε = 0.1) and 20 (ε = 0.3) thousand steps against 43.6 thousand for one step per batch.
  • Without clipping, 30 epochs moved the policy too far: single updates changed it by 2.5 to 16 nats of KL across the three seeds, and 2 of 12 learners collapsed to episodes of about 10 steps (final means 9.7 on seed 0 and 9.2 on seed 1) and never recovered within the run.
  • Clipping kept every update small: across all 36 clipped learners, the largest KL in any update was 0.101. Smaller ε gave smaller updates.
  • Clipping is not a guarantee: a few clipped learners dipped late in training (ε = 0.1: final means 176.7 and 159.8 on seed 0; ε = 0.2: 166.4 on seed 1 and 178.3 on seed 2).
  • Exercise 1: with ε = 0.2 and 10 epochs, four learners all ended at 200.0, and the largest KL in any update was 0.041.
  • Seeds 1 and 2: largest unclipped KLs 2.61, 2.48, 12.01, 5.20 and 3.47, 10.78, 4.48, 16.31; one-step-per-batch thousands of steps to 195: 37.4, 72.8, 47.2, 50.7 and 52.9, 33.2, 49.2, 48.1.
🐍 Exercise 2 (research): on cart-pole, what happens when one batch is reused for 30 Adam epochs, with and without clipping, compared with a single step per batch?
Train PPO on 16 parallel cart-poles with one step per batch, 30 unclipped epochs and 30 clipped epochs at three values of ε, and compare learning speed, stability, KL per update and clip fraction.

Trust regions in practice

  • PPO everywhere: the default for simulated robot locomotion, game agents, and RLHF.
  • Language models: RLHF with PPO adds a KL penalty to a reference model on top of clipping; critic-free variants (RLOO, GRPO) use sample-based baselines instead.
  • Off-policy alternatives: SAC and TD3 (next topic) reuse data through a replay buffer instead of a trust region.
  • Trust regions elsewhere: MPO, V-MPO and natural-gradient optimisers such as K-FAC apply the same KL idea.

References

  1. S. Kakade and J. Langford (2002). “Approximately optimal approximate reinforcement learning.” ICML.
  2. J. Schulman, S. Levine, P. Abbeel, M. Jordan and P. Moritz (2015). “Trust region policy optimization.” ICML.
  3. J. Schulman, F. Wolski, P. Dhariwal, A. Radford and O. Klimov (2017). “Proximal policy optimization algorithms.” arXiv:1707.06347.
  4. L. Engstrom, A. Ilyas, S. Santurkar, D. Tsipras, F. Janoos, L. Rudolph and A. Madry (2020). “Implementation matters in deep policy gradients: a case study on PPO and TRPO.” ICLR.
  5. L. Ouyang and colleagues (2022). “Training language models to follow instructions with human feedback.” NeurIPS.