Simple Algorithm
Artificial Intelligence · Deep Layer · 28 min

Trust Regions: TRPO & PPO

Stable policy updates with constraints.

A policy-gradient step that is slightly too large can wreck an on-policy agent: the worse policy collects worse data, and training may never recover. TRPO proved that a KL penalty makes every step an improvement, then relaxed the proof into a trust region; PPO replaced the trust region with a clipped ratio and became the default, for reasons that turned out to lie partly in its implementation details.

What you’ll learn

  • Prove the performance-difference lemma, and explain why the surrogate objective matches the true objective to first order.
  • State and prove TRPO's monotonic-improvement bound, and derive the natural-gradient trust-region step.
  • Analyze PPO's clipped objective case by case, and explain why it does not strictly enforce a trust region.
  • Summarize what large ablation studies found about which implementation details matter.

Why the step size is the hard part

In supervised learning a bad gradient step is corrected by the next minibatch. In on-policy RL the next minibatch comes from the policy you just damaged. And the size that matters is measured in policy space, not parameter space: a small change to the logits of a nearly deterministic softmax can move a lot of probability, while a large change elsewhere moves almost none. To control steps, you need to know how a policy change affects the return.

DerivationProof, and the surrogate objective
  1. Eτ∼π~[∑tγtAπ(st,at)]=Eτ∼π~[∑tγtRt+1−Vπ(s0)]=J(π~)−J(π)\E_{\tau\sim\tilde\pi}\Big[\sum_t\gamma^tA_\pi(s_t,a_t)\Big]=\E_{\tau\sim\tilde\pi}\Big[\sum_t\gamma^tR_{t+1}-V_\pi(s_0)\Big]=J(\tilde\pi)-J(\pi)

    Since Aπ(st,at)=E[Rt+1+γVπ(st+1)−Vπ(st)∣st,at]A_\pi(s_t,a_t)=\E\big[R_{t+1}+\gamma V_\pi(s_{t+1})-V_\pi(s_t)\mid s_t,a_t\big], the discounted sum along a trajectory of π~\tilde\pi telescopes, and E[Vπ(s0)]=J(π)\E[V_\pi(s_0)]=J(\pi):

  2. Lπ(π~)=J(π)+∑sρπ(s)∑aπ~(a∣s) Aπ(s,a)=J(π)+∑sρπ(s) Ea∼π(⋅∣s)[π~(a∣s)π(a∣s) Aπ(s,a)]L_\pi(\tilde\pi)=J(\pi)+\sum_s\rho_\pi(s)\sum_a\tilde\pi(a\mid s)\,A_\pi(s,a)=J(\pi)+\sum_s\rho_\pi(s)\,\E_{a\sim\pi(\cdot\mid s)}\Big[\frac{\tilde\pi(a\mid s)}{\pi(a\mid s)}\,A_\pi(s,a)\Big]

    The right side needs states visited by the new policy, which we have not run. Replace ρπ~\rho_{\tilde\pi} by ρπ\rho_\pi to get the surrogate; an importance ratio on the action lets it be estimated from data of π\pi:

  3. Lπθ0(πθ0)=J(πθ0),∇θLπθ0(πθ)∣θ=θ0=∇θJ(πθ)∣θ=θ0L_{\pi_{\theta_0}}(\pi_{\theta_0})=J(\pi_{\theta_0}),\qquad\nabla_\theta L_{\pi_{\theta_0}}(\pi_\theta)\big|_{\theta=\theta_0}=\nabla_\theta J(\pi_\theta)\big|_{\theta=\theta_0}

    At π~=πθ0\tilde\pi=\pi_{\theta_0} the two objectives agree in value, because advantages average to zero under the policy, and in gradient, because differentiating the importance ratio at θ0\theta_0 reproduces the policy-gradient theorem. A small enough step that improves LL improves JJ; the question is how small.

Kakade and Langford's conservative policy iteration answered for mixtures. Moving to πnew=(1−α)π+απ′\pi_{\text{new}}=(1-\alpha)\pi+\alpha\pi' guarantees J(πnew)≥Lπ(πnew)−2ϵγ(1−γ)2α2J(\pi_{\text{new}})\ge L_\pi(\pi_{\text{new}})-\frac{2\epsilon\gamma}{(1-\gamma)^2}\alpha^2, with ϵ=max⁡s∣Ea∼π′Aπ(s,a)∣\epsilon=\max_s\abs{\E_{a\sim\pi'}A_\pi(s,a)}. The gain in LL is linear in α\alpha and the penalty quadratic, so a small enough mixture always improves. Mixtures of neural policies are awkward, though, which is where TRPO comes in.

TRPO: a lower bound you can climb

DerivationProof by coupling
  1. J(π~)−Lπ(π~)=∑tγt(Est∼π~Aˉ(st)−Est∼πAˉ(st))J(\tilde\pi)-L_\pi(\tilde\pi)=\sum_t\gamma^t\Big(\E_{s_t\sim\tilde\pi}\bar A(s_t)-\E_{s_t\sim\pi}\bar A(s_t)\Big)

    Let Aˉ(s)=Ea∼π~Aπ(s,a)\bar A(s)=\E_{a\sim\tilde\pi}A_\pi(s,a). Subtracting the surrogate from the lemma leaves only the difference between the two state distributions:

  2. ∣Aˉ(s)∣=∣E[Aπ(s,a~)−Aπ(s,a)]∣≤Pr⁡(a~≠a)⋅2ϵ≤2αϵ\abs{\bar A(s)}=\big|\E\big[A_\pi(s,\tilde a)-A_\pi(s,a)\big]\big|\le\Pr(\tilde a\ne a)\cdot2\epsilon\le2\alpha\epsilon

    Couple the two policies so that in every state they pick the same action except with probability DTV≤αD_{\mathrm{TV}}\le\alpha. Since Ea∼πAπ(s,a)=0\E_{a\sim\pi}A_\pi(s,a)=0, Aˉ(s)\bar A(s) is the expected change in advantage when the coupled actions differ:

  3. ∣Est∼π~Aˉ(st)−Est∼πAˉ(st)∣≤2(1−(1−α)t)⋅2αϵ\Big|\E_{s_t\sim\tilde\pi}\bar A(s_t)-\E_{s_t\sim\pi}\bar A(s_t)\Big|\le2\big(1-(1-\alpha)^t\big)\cdot2\alpha\epsilon

    Run the coupled policies from the same start. Their states at time tt can differ only if their actions disagreed at some earlier step, which has probability at most 1−(1−α)t1-(1-\alpha)^t:

  4. ∣J(π~)−Lπ(π~)∣≤4αϵ∑tγt(1−(1−α)t)=4ϵγ α2(1−γ)(1−γ+γα)≤4ϵγ α2(1−γ)2\big|J(\tilde\pi)-L_\pi(\tilde\pi)\big|\le4\alpha\epsilon\sum_t\gamma^t\big(1-(1-\alpha)^t\big)=\frac{4\epsilon\gamma\,\alpha^2}{(1-\gamma)(1-\gamma+\gamma\alpha)}\le\frac{4\epsilon\gamma\,\alpha^2}{(1-\gamma)^2}

    Sum the geometric series:

The bound is a minorizer: Mi(π)=Lπi(π)−C DKLmax⁡(πi,π)M_i(\pi)=L_{\pi_i}(\pi)-C\,D_{\mathrm{KL}}^{\max}(\pi_i,\pi) lies below JJ everywhere and touches it at πi\pi_i. Maximizing it therefore guarantees improvement, J(πi+1)≥Mi(πi+1)≥Mi(πi)=J(πi)J(\pi_{i+1})\ge M_i(\pi_{i+1})\ge M_i(\pi_i)=J(\pi_i): a minorize–maximize (MM) algorithm. Gradient descent on a smooth function is the simplest instance. If ∇f\nabla f is LL-Lipschitz, f(θ−η∇f)≤f(θ)−η(1−Lη2)∥∇f∥2f(\theta-\eta\nabla f)\le f(\theta)-\eta\big(1-\tfrac{L\eta}{2}\big)\norm{\nabla f}^2, a quadratic upper bound whose best step is η=1/L\eta=1/L and which guarantees a decrease for every η<2/L\eta<2/L.

Predict before you look

For f(x,y)=x2+y2f(x,y)=x^2+y^2 the gradient is 22-Lipschitz (L=2L=2). Gradient descent with η=1/L=0.5\eta=1/L=0.5: how many steps does it need to reach the minimum from any start?

Show the answer

1

The update is θ←θ−0.5⋅2θ=0\theta\leftarrow\theta-0.5\cdot2\theta=0: one step. On a quadratic the upper bound is exact, so its minimizer is the true minimizer. Try it in the widget, then try steps that the bound does not certify.

Check your understanding

With γ=0.99\gamma=0.99, how large is TRPO's penalty coefficient C=4ϵγ/(1−γ)2C=4\epsilon\gamma/(1-\gamma)^2 relative to the largest advantage ϵ\epsilon?

  1. About 4ϵ4\epsilon
  2. About 400ϵ400\epsilon
  3. About 40,000ϵ40{,}000\epsilon
  4. About 4,000,000ϵ4{,}000{,}000\epsilon
Show the answer

C. About 40,000ϵ40{,}000\epsilon

4×0.99/0.012=39,6004\times0.99/0.01^2=39{,}600. With so large a penalty the steps the theory certifies are tiny, so practical TRPO keeps the logic but replaces the penalty by a constraint on the average KL divergence, with a radius chosen by hand.

Practical TRPO solves max⁡θLθold(θ)\max_\theta L_{\theta_{\text{old}}}(\theta) subject to DˉKL=Es∼ρold[DKL(πθold(⋅∣s) ∥ πθ(⋅∣s))]≤δ\bar D_{\mathrm{KL}}=\E_{s\sim\rho_{\text{old}}}\big[\KL\big(\pi_{\theta_{\text{old}}}(\cdot\mid s)\,\|\,\pi_\theta(\cdot\mid s)\big)\big]\le\delta, with δ=0.01\delta=0.01 in the original experiments. Linearize the objective, L≈g⊤(θ−θold)L\approx g^\top(\theta-\theta_{\text{old}}), and expand the constraint to second order, DˉKL≈12(θ−θold)⊤F(θ−θold)\bar D_{\mathrm{KL}}\approx\tfrac12(\theta-\theta_{\text{old}})^\top F(\theta-\theta_{\text{old}}) with FF the Fisher information: the solution is a natural-gradient step, θ=θold+2δ/(g⊤F−1g) F−1g\theta=\theta_{\text{old}}+\sqrt{2\delta/(g^\top F^{-1}g)}\,F^{-1}g. Using the average rather than the maximum KL was later supported by a bound stated with expected divergences (Achiam, Held, Tamar & Abbeel, 2017).

text
g ← ∇_θ L(θ) at θ_old                  # policy gradient with advantage estimates
s ← conjugate_gradient(F, g)           # s ≈ F⁻¹g from Fisher–vector products alone
β ← sqrt(2δ / (sᵀ F s))                # largest step with ½·ΔᵀFΔ = δ
for j = 0, 1, 2, …:                    # backtracking line search
    θ ← θ_old + 0.5^j · β · s
    if KL(θ_old, θ) ≤ δ and L(θ) > L(θ_old): break

One TRPO update. Conjugate gradient needs only products FvFv, each about as costly as a gradient, so FF is never formed; the line search enforces the true constraint and an actual improvement of the surrogate.

PPO: clipping the probability ratio

TRPO's second-order machinery is complicated, and awkward with architectures that share parameters between policy and value function. PPO (Schulman et al., 2017) keeps a first-order optimizer and removes the incentive to move far instead. With the probability ratio rt(θ)=πθ(at∣st)/πθold(at∣st)r_t(\theta)=\pi_\theta(a_t\mid s_t)/\pi_{\theta_{\text{old}}}(a_t\mid s_t) and advantages A^t\hat A_t from generalized advantage estimation, it maximizes

LCLIP(θ)=E^t[min⁡(rt(θ) A^t, clip⁡(rt(θ),1−ϵ,1+ϵ) A^t)]L^{\text{CLIP}}(\theta)=\hat\E_t\Big[\min\Big(r_t(\theta)\,\hat A_t,\ \operatorname{clip}\big(r_t(\theta),1-\epsilon,1+\epsilon\big)\,\hat A_t\Big)\Big]
ϵ=0.2\epsilon=0.2 in the paper. The minimum makes LCLIPL^{\text{CLIP}} a pessimistic bound on the unclipped surrogate E^t[rtA^t]\hat\E_t[r_t\hat A_t], which is TRPO's LL without the constant; the two agree to first order at rt=1r_t=1.
AdvantageRatioActive termGradient of the sample
A^t>0\hat A_t>0rt≤1+ϵr_t\le1+\epsilonrtA^tr_t\hat A_traises the action's probability
A^t>0\hat A_t>0rt>1+ϵr_t>1+\epsilon(1+ϵ)A^t(1+\epsilon)\hat A_tzero: no reward for raising a good action further
A^t<0\hat A_t<0rt≥1−ϵr_t\ge1-\epsilonrtA^tr_t\hat A_tlowers the action's probability
A^t<0\hat A_t<0rt<1−ϵr_t<1-\epsilon(1−ϵ)A^t(1-\epsilon)\hat A_tzero: no reward for lowering a bad action further
Clipping never blocks a correction: a good action whose ratio fell below 1−ϵ1-\epsilon, or a bad one whose ratio rose above 1+ϵ1+\epsilon, keeps its full gradient. Only moves that would improve the objective beyond the band are ignored.
text
for iteration = 1, 2, …:
    run π_θold for T steps in each of N environments
    compute advantages Â_t by GAE(γ, λ) with the critic V_φ;  returns R̂_t ← Â_t + V_φ(s_t)
    for epoch = 1, …, K:
        for each minibatch M of the N·T samples:
            r ← π_θ(a | s) / π_θold(a | s)
            L_clip ← mean over M of min(r·Â, clip(r, 1 − ε, 1 + ε)·Â)
            L_V ← mean over M of (V_φ(s) − R̂)²
            gradient step to increase  L_clip − c₁·L_V + c₂·(mean entropy of π_θ)
    θold ← θ

PPO reuses each batch for several epochs; the paper's MuJoCo runs used 2,048 steps per iteration, 10 epochs and minibatches of 64, with GAE (λ=0.95\lambda=0.95). A variant with an adaptive KL penalty instead doubles or halves the penalty coefficient when the measured KL drifts from a target; the clipped version did better in the paper's benchmarks.

What actually matters

Engstrom et al. (2020) dissected the reference implementations of PPO and TRPO. Beyond the clipped loss, the PPO code contained "code-level optimizations": value-function clipping, reward scaling by a running estimate of the return's standard deviation, orthogonal initialization with layer scaling, learning-rate annealing, observation normalization and clipping, tanh activations, and global gradient-norm clipping. They concluded that these optimizations account for most of PPO's gain in reward over TRPO, and that they change how the algorithms behave, including how well the trust region is maintained. Ilyas et al. (2020) added that at typical batch sizes the gradient estimates correlate poorly with the true gradient, and that the surrogate's landscape can differ markedly from the true reward landscape.

Andrychowicz et al. (2021) trained over 250,000 agents to measure more than fifty such choices in on-policy actor–critic methods. Among their recommendations: use the PPO clipped loss, always normalize observations, initialize the policy's last layer with small weights so that the initial policy barely depends on the observation, use GAE with λ≈0.9\lambda\approx0.9, and tune the discount factor per environment. The lesson generalizes beyond PPO: in deep RL, details that papers relegate to appendices can matter as much as the headline objective.

PPO's most visible deployment is fine-tuning language models from human feedback, where the "environment" is a learned reward model and a KL penalty to a reference model adds a second leash. Fine-Tuning, RLHF & Prompting and RLHF & Preference Optimization cover it, together with critic-free REINFORCE-style variants (RLOO, GRPO) that replace the value network with the average reward of several samples per prompt.

Key takeaways

  • Performance-difference lemma: J(π~)−J(π)=∑sρπ~(s)∑aπ~(a∣s)Aπ(s,a)J(\tilde\pi)-J(\pi)=\sum_s\rho_{\tilde\pi}(s)\sum_a\tilde\pi(a\mid s)A_\pi(s,a); the surrogate swaps in the old state visitation ρπ\rho_\pi and matches JJ to first order.
  • TRPO's theorem, J(π~)≥Lπ(π~)−4ϵγ(1−γ)2(DTVmax⁡)2≥Lπ(π~)−C DKLmax⁡J(\tilde\pi)\ge L_\pi(\tilde\pi)-\frac{4\epsilon\gamma}{(1-\gamma)^2}(D_{\mathrm{TV}}^{\max})^2\ge L_\pi(\tilde\pi)-C\,D_{\mathrm{KL}}^{\max}, is proved by coupling; maximizing this minorizer is an MM algorithm with monotone improvement.
  • Practical TRPO constrains the average KL to δ\delta and steps along the natural gradient F−1gF^{-1}g (by conjugate gradient) with size 2δ/g⊤F−1g\sqrt{2\delta/g^\top F^{-1}g}, checked by a line search.
  • PPO's clipped surrogate is a pessimistic bound on rtA^tr_t\hat A_t that zeroes the gradient only for moves that would improve the objective beyond the band; it does not strictly enforce a trust region.
  • Implementation details (normalization, initialization, schedules, value clipping) matter as much as the objective, as large ablation studies showed.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

The surrogate Lπ(π~)L_\pi(\tilde\pi) differs from the exact expression J(π~)=J(π)+∑sρπ~(s)∑aπ~(a∣s)Aπ(s,a)J(\tilde\pi)=J(\pi)+\sum_s\rho_{\tilde\pi}(s)\sum_a\tilde\pi(a\mid s)A_\pi(s,a) only in:

  1. The advantages, which it estimates for π~\tilde\pi instead of π\pi
  2. The state weighting, ρπ\rho_\pi instead of ρπ~\rho_{\tilde\pi}
  3. The discount factor
  4. Nothing: the two are equal
Show the answer

B. The state weighting, ρπ\rho_\pi instead of ρπ~\rho_{\tilde\pi}

Everything else is exact. Using the old policy's state visitation is what makes LL computable from old data, and the TRPO theorem bounds the price of that substitution by a quadratic in the policies' divergence.

Question 2

In PPO with ϵ=0.2\epsilon=0.2, a sample has A^t<0\hat A_t<0 and probability ratio rt=1.5r_t=1.5. What does it contribute to the gradient of LCLIPL^{\text{CLIP}}?

  1. Nothing: its ratio is outside the clip range
  2. A push to raise rtr_t further
  3. A push to lower rtr_t, since the unclipped term rtA^tr_t\hat A_t is the minimum
  4. A push toward rt=1+ϵr_t=1+\epsilon exactly, and then nothing
Show the answer

C. A push to lower rtr_t, since the unclipped term rtA^tr_t\hat A_t is the minimum

min⁡(1.5A^t, 1.2A^t)=1.5A^t\min(1.5\hat A_t,\,1.2\hat A_t)=1.5\hat A_t because A^t<0\hat A_t<0, so the unclipped branch is active and its gradient A^t∇rt\hat A_t\nabla r_t lowers the probability of this bad action. Clipping removes only the incentive to move further in the direction that improves the objective; it never blocks a correction.

Question 3

What is the direction of a TRPO step, and how is it computed?

  1. The natural gradient F−1gF^{-1}g, computed by conjugate gradient from Fisher–vector products
  2. The plain gradient gg, scaled to the trust-region radius
  3. The Newton direction H−1gH^{-1}g of the true objective JJ
  4. The clipped-ratio gradient of PPO
Show the answer

A. The natural gradient F−1gF^{-1}g, computed by conjugate gradient from Fisher–vector products

Linearizing the surrogate and approximating the average KL by 12Δ⊤FΔ\tfrac12\Delta^\top F\Delta gives a quadratically constrained linear program whose solution is along F−1gF^{-1}g. Conjugate gradient approximates it without forming FF, and a line search checks the true KL and the improvement.

Question 4

What did Engstrom et al. (2020) find about PPO's advantage over TRPO?

  1. It comes entirely from the clipped objective
  2. It disappears with proper hyperparameter tuning of TRPO's KL radius alone
  3. It comes from PPO's use of more environment samples
  4. Much of it comes from code-level optimizations such as value-function clipping, reward scaling, orthogonal initialization and learning-rate annealing
Show the answer

D. Much of it comes from code-level optimizations such as value-function clipping, reward scaling, orthogonal initialization and learning-rate annealing

They concluded that these optimizations are responsible for most of PPO's gain in reward over TRPO and fundamentally change how the algorithms behave, including how well the trust region is actually maintained.

Further reading

  • S. Kakade & J. Langford, "Approximately optimal approximate reinforcement learning," ICML (2002).
  • J. Schulman, S. Levine, P. Abbeel, M. I. Jordan & P. Moritz, "Trust region policy optimization," ICML (2015).
  • J. Schulman, F. Wolski, P. Dhariwal, A. Radford & O. Klimov, "Proximal policy optimization algorithms," arXiv:1707.06347 (2017).
  • L. Engstrom, A. Ilyas, S. Santurkar, D. Tsipras, F. Janoos, L. Rudolph & A. Madry, "Implementation matters in deep RL: A case study on PPO and TRPO," ICLR (2020).
  • A. Agarwal, N. Jiang, S. M. Kakade & W. Sun, Reinforcement Learning: Theory and Algorithms (monograph draft, 2019–2022).