Search

Search papers and lessons

Trust Region Policy Optimization (TRPO)

Level 4 · 16 min read

Trust region policy optimization improves a policy with the largest step that a surrogate objective still lets us trust, by capping how far the action probabilities may move.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Reinforcement learning
  • Linear algebra
  • Discrete math
  • Information theory
  • Sign in to see your progress

TL;DR

Trust region policy optimization, or TRPO, improves a policy by taking the biggest step that a local approximation can still be trusted for. A policy gradient step can overshoot: one update that looks good on the sampled data can push the policy into a region where the estimate is wrong and performance collapses. TRPO prevents this by putting a hard limit on how far the new policy may move from the old one, measured as the average KL divergence between their action distributions.

Within that limit it maximises a surrogate objective, which is the policy gradient's estimate written so that it is an honest prediction of the true return improvement near the old policy. The result is a method that improves steadily, and that rarely needs hand tuning of the step size.

Where you'll see it: in continuous-control robotics benchmarks where stable learning mattered more than speed and as the conceptual parent of PPO.

Intuition

How far can you trust a map?

Suppose you are hiking with a map that was drawn from one spot. Near where you stand, the map is accurate. Far away, it is a guess. Following the steepest slope on the map is sensible for a few metres and foolish for a few kilometres. A policy gradient method is in the same position. It has a local estimate of how the return changes when the policy changes, and that estimate is only good for small changes.

TRPO draws a circle around the current policy, a trust region, and says: within this circle the map is good enough, so move as far up the hill as you can and stop at the edge. The circle is measured not in parameter space, where a small numerical change can mean a huge change in behaviour, but in the space of action probabilities. Two policies are close when they choose the same actions with nearly the same probabilities, whatever their parameters look like.

That is why the method is careful about its step. Plain gradient ascent with a fixed learning rate has no idea whether the step is safe. TRPO asks the question directly and answers it with a constraint.

flowchart LR
    A[Run current policy and collect episodes] --> B[Estimate advantages]
    B --> C[Compute the surrogate gradient g]
    C --> D[Find direction x with F x = g by conjugate gradient]
    D --> E[Scale step so the KL stays under delta]
    E --> F[Backtrack until the surrogate improves]
    F --> G[Replace the policy]
    G --> A

The math

Setup

Let πold\pi_{\text{old}} be the current policy with parameters θold\theta_{\text{old}}, and let πθ\pi_\theta be a candidate. The advantage Aold(s,a)A_{\text{old}}(s, a) is the advantage of action aa in state ss under πold\pi_{\text{old}}, and the expectations below are over states and actions sampled by running πold\pi_{\text{old}}. The importance ratio is

r(θ)=πθ(a∣s)πold(a∣s),r(\theta) = \frac{\pi_\theta(a \mid s)}{\pi_{\text{old}}(a \mid s)},

which is the same reweighting used in Importance Sampling. At θ=θold\theta = \theta_{\text{old}} it equals one.

The surrogate objective

Define the surrogate

Lθold(θ)=Es,a∼πold ⁣[πθ(a∣s)πold(a∣s) Aold(s,a)].L_{\theta_{\text{old}}}(\theta) = \mathbb{E}_{s, a \sim \pi_{\text{old}}}\!\left[\frac{\pi_\theta(a \mid s)}{\pi_{\text{old}}(a \mid s)}\, A_{\text{old}}(s, a)\right].

Its gradient at θold\theta_{\text{old}} is exactly the policy gradient, since the ratio is one there and its derivative is the score. So LL agrees with the true objective to first order at the old policy. Away from it, the two can disagree, and the question is how far away the agreement holds.

Kakade and Langford's performance difference identity makes this precise. Let η(π)\eta(\pi) be the expected discounted return of π\pi. Then for any new policy π~\tilde\pi,

η(π~)=η(πold)+Eτ∼π~ ⁣[∑t≥0γtAold(st,at)].\eta(\tilde\pi) = \eta(\pi_{\text{old}}) + \mathbb{E}_{\tau \sim \tilde\pi}\!\left[\sum_{t \ge 0} \gamma^t A_{\text{old}}(s_t, a_t)\right].

The surrogate replaces the state distribution of π~\tilde\pi with that of πold\pi_{\text{old}}, which we can sample. The error from that replacement is controlled by how far the policies differ.

The monotonic improvement bound

Schulman and colleagues showed that the error is bounded by a KL penalty. With ε=max⁡s,a∣Aold(s,a)∣\varepsilon = \max_{s,a} |A_{\text{old}}(s,a)| and discount γ\gamma,

η(π~) ≥ Lπold(π~)−C DKLmax⁡(πold,π~),C=4εγ(1−γ)2,\eta(\tilde\pi) \ \ge\ L_{\pi_{\text{old}}}(\tilde\pi) - C\, D_{\text{KL}}^{\max}(\pi_{\text{old}}, \tilde\pi), \qquad C = \frac{4\varepsilon\gamma}{(1-\gamma)^2},

where DKLmax⁡D_{\text{KL}}^{\max} is the largest KL divergence over states. If we maximise the right-hand side, the true return cannot decrease. That is the monotonic improvement guarantee. The constant CC is so large in practice that the penalised step is tiny, so the paper turns the penalty into a constraint on the average KL, which allows much larger steps.

The trust region problem

TRPO solves

max⁡θ Lθold(θ)subject toDˉKL(θold,θ)≤δ,\max_\theta\ L_{\theta_{\text{old}}}(\theta) \quad \text{subject to} \quad \bar D_{\text{KL}}(\theta_{\text{old}}, \theta) \le \delta,

where DˉKL(θold,θ)=Es∼πold[KL(πold(⋅∣s) ∥ πθ(⋅∣s))]\bar D_{\text{KL}}(\theta_{\text{old}}, \theta) = \mathbb{E}_{s \sim \pi_{\text{old}}}\big[\mathrm{KL}(\pi_{\text{old}}(\cdot \mid s)\,\|\,\pi_\theta(\cdot \mid s))\big] is the average KL over states, and δ\delta is a small number such as 0.010.01. The KL is taken in the direction old-to-new, so the expectation is over states and actions that we can sample from the old policy. To second order, either direction gives the same quadratic below.

Making the problem solvable

For a parameter change Δ\Delta that is small, the constraint can be approximated by a quadratic and the objective by a line:

DˉKL(θold,θold+Δ)≈12 Δ⊤F Δ,L(θold+Δ)≈L(θold)+g⊤Δ,\bar D_{\text{KL}}(\theta_{\text{old}}, \theta_{\text{old}} + \Delta) \approx \tfrac12\, \Delta^\top F\, \Delta, \qquad L(\theta_{\text{old}} + \Delta) \approx L(\theta_{\text{old}}) + g^\top \Delta,

where g=∇θLg = \nabla_\theta L is the policy gradient and FF is the Fisher information matrix, the expected outer product of the score, F=E[∇log⁡π ∇log⁡π⊤]F = \mathbb{E}[\nabla \log \pi \, \nabla \log \pi^\top]. Maximising g⊤Δg^\top \Delta on the ellipsoid 12Δ⊤FΔ≤δ\tfrac12 \Delta^\top F \Delta \le \delta gives the update

Δ=2δg⊤F−1g  F−1g.\Delta = \sqrt{\frac{2\delta}{g^\top F^{-1} g}}\; F^{-1} g.

The direction F−1gF^{-1} g is the natural gradient. It measures the geometry of the policy space, so a step that is large in parameter units but small in KL is not penalised for its size. Inverting FF is too expensive for a neural network with millions of parameters, so TRPO solves Fx=gF x = g with conjugate gradient, using about ten iterations. Each iteration needs only the product FvF v, which can be computed with two backward passes, so FF is never formed.

After the step, TRPO runs a backtracking line search. It shrinks the step by a fixed factor until the new policy both satisfies the KL constraint and actually improves the surrogate. If no step passes, the update is rejected.

Worked examples

Example
The constrained optimum for one state

Take a single state with two actions. The old policy is uniform, πold=(0.5,0.5)\pi_{\text{old}} = (0.5, 0.5), and the advantages are A(1)=+1A(1) = +1 and A(2)=−1A(2) = -1. These average to zero under πold\pi_{\text{old}}, as they should. Let the new policy put probability pp on action 1.

  1. The surrogate is L(p)=0.5⋅p0.5⋅1+0.5⋅1−p0.5⋅(−1)=p−(1−p)=2p−1L(p) = 0.5 \cdot \frac{p}{0.5} \cdot 1 + 0.5 \cdot \frac{1-p}{0.5} \cdot (-1) = p - (1 - p) = 2p - 1.
  2. Without a constraint, LL grows as pp grows, so the best choice is p=1p = 1. That is a wild move.
  3. The KL from old to new is KL=0.5ln⁡0.5p+0.5ln⁡0.51−p\mathrm{KL} = 0.5 \ln\frac{0.5}{p} + 0.5 \ln\frac{0.5}{1-p}. At p=0.6p = 0.6 it is 0.02040.0204. At p=0.9p = 0.9 it is 0.5110.511.
  4. Set δ=0.02\delta = 0.02. Solving KL(p)=0.02\mathrm{KL}(p) = 0.02 gives p≈0.599p \approx 0.599, where L=2(0.599)−1=0.198L = 2(0.599) - 1 = 0.198.
  5. Interpretation: the trust region stops the policy at about a sixty percent chance on the better action, not at certainty. The surrogate's promised gain is 0.1980.198, and the KL spent to get it is exactly the budget.
Example
Natural step versus plain gradient step

Two parameters, gradient g=(1,1)g = (1, 1), and Fisher matrix F=diag(1,4)F = \mathrm{diag}(1, 4). Use δ=0.02\delta = 0.02.

  1. Natural direction: F−1g=(1,14)F^{-1} g = (1, \tfrac14). Then g⊤F−1g=1+14=1.25g^\top F^{-1} g = 1 + \tfrac14 = 1.25.
  2. Natural step length factor: 2⋅0.02/1.25=0.032=0.1789\sqrt{2 \cdot 0.02 / 1.25} = \sqrt{0.032} = 0.1789. The step is Δ=(0.1789,0.0447)\Delta = (0.1789, 0.0447).
  3. Check the KL: 12Δ⊤FΔ=12(0.032+4⋅0.002)=0.02\tfrac12 \Delta^\top F \Delta = \tfrac12(0.032 + 4 \cdot 0.002) = 0.02. The constraint is met exactly.
  4. Predicted gain: g⊤Δ=0.1789+0.0447=0.2236g^\top \Delta = 0.1789 + 0.0447 = 0.2236.
  5. Plain gradient step with the same KL budget: Δ=c(1,1)\Delta = c(1, 1) with 12c2(1+4)=0.02\tfrac12 c^2 (1 + 4) = 0.02, so c=0.0894c = 0.0894. Predicted gain g⊤Δ=0.1789g^\top \Delta = 0.1789.
  6. Interpretation: the natural step buys about twenty-five percent more improvement from the same KL, because it moves less along the direction where the policy is most sensitive.

Tradeoffs and failure modes

TRPO is a reliable method, but its machinery is heavy. The table lists the main situations.

SituationProblemBetter choice
Large network, many samples per updateConjugate gradient and line search cost many passes over the batchA first-order method such as PPO
Discrete actions with a small tableConstraint is cheap, but the full machinery is overkillPlain policy gradient with a baseline
Advantage estimates very noisySurrogate is a poor model far from the old policy, and the constraint can be satisfied by tiny movesLower variance advantages with [[generalized-advantage-estimation
Very small δ\deltaLearning is slow and the trust region rarely bindsRaise δ\delta or use an adaptive schedule
Need the exact KL at every stateAverage KL over sampled states is only an estimateAccept the approximation, or check the sampled KL after the step

The method also assumes the Fisher approximation is good. When the quadratic model of KL is poor, the constraint can be violated even after the line search, which is one reason later methods replaced the hard constraint with a clipped objective.

History

Kakade and Langford, in the early 2000s, showed that large policy changes can destroy performance and proposed conservative updates. Shun-ichi Amari had earlier described the natural gradient as the steepest direction in the geometry of probability distributions. Sham Kakade then applied the natural gradient to policies. Schulman, Levine, Moritz, Jordan and Abbeel at Berkeley combined these ideas in 2015 into TRPO, with the conjugate gradient solver and the line search that make it practical for neural networks. It was tested on simulated robotic locomotion and on Atari games, where it learned stable policies without per-task step-size tuning.

TRPO caught on because it was backed by an improvement bound and needed little step-size tuning. Its drawback was complexity: implementing the Fisher-vector products and the line search is fiddly. In 2017 Schulman and colleagues proposed PPO, which keeps the spirit of a trust region but enforces it with a clipped ratio. PPO is far simpler and became the default in most later work.

Check yourself

QuestionRecall: what does the trust region constrain?Show answer

The average KL divergence between the old and new policies, measured over the states visited, not the distance between the parameter vectors. Two parameter settings that produce the same action probabilities are at zero KL, however far apart they are in parameter space.

QuestionCompute: with L(p)=2p−1L(p) = 2p - 1 and KL(p)=0.5ln⁡0.5p+0.5ln⁡0.51−p\mathrm{KL}(p) = 0.5\ln\frac{0.5}{p} + 0.5\ln\frac{0.5}{1-p}, what is KL\mathrm{KL} at p=0.75p = 0.75?Show answer

ln⁡0.50.75=ln⁡0.6667=−0.4055\ln\frac{0.5}{0.75} = \ln 0.6667 = -0.4055 and ln⁡0.50.25=ln⁡2=0.6931\ln\frac{0.5}{0.25} = \ln 2 = 0.6931. The average is 0.5(−0.4055+0.6931)=0.14380.5(-0.4055 + 0.6931) = 0.1438. The KL is about 0.1440.144, well above the budget of 0.020.02, so p=0.75p = 0.75 is outside the trust region and the constrained solver would not choose it.

QuestionWhy: why does TRPO use the natural gradient direction F inverse times g, rather than g itself?Show answer

The constraint is measured in KL, so the step should be measured in the geometry of the distributions. The plain gradient ignores that some parameters change the policy much more than others. Multiplying by the inverse Fisher matrix rescales each direction by how much it changes the policy, so the step is spent where it buys improvement at a fixed KL cost, as the second worked example shows.

QuestionApplied: you run TRPO and the average KL after the step is 0.05 with delta equal to 0.01. What is the likely cause, and what should you check?Show answer

The quadratic approximation of the KL, or the conjugate gradient solve, is inaccurate, so the step was larger than the model predicted. Check the number of conjugate gradient iterations, the damping added to F, and whether the line search actually tests the KL on the new policy rather than only the surrogate. A fix is to shrink the initial step or to enforce the KL check inside the line search.

Sources