PPO Algorithm › Trust Region Methods

Michael BrenndoerferDecember 27, 20258 min read
Language AI HandbookAll chapters
Part 37 of 67Alignment and RLHF
Chapter 278PPO Algorithm › Trust Region Methods
Reading section 3 of 14 · Trust Region Methods · 8 minSection 3 of 14 · 8 min

Book contents

Section 3 of 14 · 8 min read

PPO Algorithm › Trust Region MethodsOne section · Sections in this chapter3 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The insight behind trust region methods is to constrain how much the policy can change in each update. Rather than blindly following the gradient wherever it leads, we optimize the policy subject to a constraint that keeps the new policy "close" to the old one. This approach recognizes a basic tension in optimization: we want to improve the policy as quickly as possible. However, our confidence in the improvement direction decreases the further we move from where we collected our data. Gradients computed from sampled trajectories accurately describe performance improvement near the current policy, but extrapolating far from observed data becomes unreliable.

Think of a trust region as the area on a topographic map that your GPS unit has surveyed at high resolution. Within that region, the contour lines are accurate and you can safely navigate. Outside that region, the map was generated from coarser satellite data and might have cliffs, valleys, or impassable terrain that looks navigable on the map. You would not plan a route that takes you far outside your high-resolution zone. Similarly, the policy gradient is an accurate local guide only within the neighborhood of states and actions you have recently observed. Venturing far outside that neighborhood means relying on gradient extrapolations that may be wildly wrong.

Trust Region

A trust region is a neighborhood around the current parameters within which a local approximation of the objective function is trusted to be accurate. Optimization proceeds by maximizing this approximation within the trust region, then updating the region based on how well the approximation matched reality. If the approximation was good, the trust region expands. If it was poor, the region shrinks. This adaptive mechanism maintains alignment between the model and reality throughout training.

The key insight behind all trust region methods is that gradient estimates are only reliable near the data they were computed from. Moving the policy far from the current policy means the state-action distribution shifts, and the advantages computed from old data no longer accurately reflect the new policy's performance. The trust region constraint formalizes the boundary beyond which you should not venture without collecting new data. Enforcing this boundary ensures that the objective function improvement you see during optimization corresponds to actual performance improvement in the environment.

Out[4]:
Visualization
Contour plot of an objective function in two-dimensional parameter space with a circular green trust region centered on the current policy. A red dashed arrow points outside the circle showing the unconstrained gradient direction, and a solid green arrow points to the constrained new policy inside the circle.
Trust region constrains policy updates to a local neighborhood where gradient estimates remain reliable. The blue dot marks the current policy at the center of the shaded green circle (trust region boundary). The red dashed arrow shows where an unconstrained gradient would lead (potentially too far), while the green arrow shows the constrained update that respects the trust region and reaches a new policy (green square). This visualization demonstrates the core principle: gradient estimates are only reliable near the current policy, so updates must stay within the trust region to maintain stability.

Trust Region Policy Optimization (TRPO), PPO's predecessor, formalizes this using KL divergence as a constraint. The KL divergence measures how different two probability distributions are, which makes it a natural choice for measuring policy similarity. Policies that assign similar probabilities to actions have low KL divergence, while policies that behave very differently have high KL divergence. This makes KL divergence a precise, mathematically grounded way to define "how much the policy has changed."

The TRPO objective addresses the policy update problem by maximizing expected advantage while explicitly constraining how much the policy can change. It maximizes the expected advantage weighted by the importance sampling ratio, which allows us to use data from the old policy to evaluate the new policy, while constraining the KL divergence between old and new policies:

max⁡θEs,a∼πθold[πθ(a∣s)πθold(a∣s)Aπθold(s,a)]subject toEs[DKL(πθold(⋅∣s)∥πθ(⋅∣s))]≤δ\begin{aligned} \max_\theta \quad & \mathbb{E}_{s, a \sim \pi_{\theta_{\text{old}}}}\left[\frac{\pi_\theta(a|s)}{\pi_{\theta_{\text{old}}}(a|s)} A^{\pi_{\theta_{\text{old}}}}(s, a)\right] \\ \text{subject to} \quad & \mathbb{E}_s\left[D_{\text{KL}}\left(\pi_{\theta_{\text{old}}}(\cdot|s) \| \pi_\theta(\cdot|s)\right)\right] \leq \delta \end{aligned}

To understand how this constrained optimization problem balances improvement against stability, examine each component:

  • θ\theta: parameters of the new policy being optimized
  • θold\theta_{\text{old}}: parameters of the old policy from which data was collected
  • Es,a∼πθold\mathbb{E}_{s, a \sim \pi_{\theta_{\text{old}}}}: expectation over states and actions sampled from trajectories collected using the old policy
  • ss: a state sampled from the state distribution under the old policy
  • aa: an action sampled from the old policy in state ss
  • πθ(a∣s)\pi_\theta(a|s): probability of action aa in state ss under the new policy
  • πθold(a∣s)\pi_{\theta_{\text{old}}}(a|s): probability of action aa in state ss under the old policy
  • πθ(a∣s)πθold(a∣s)\frac{\pi_\theta(a|s)}{\pi_{\theta_{\text{old}}}(a|s)}: the importance sampling ratio, which reweights data from the old policy to evaluate the new policy
  • Aπθold(s,a)A^{\pi_{\theta_{\text{old}}}}(s, a): advantage function computed under the old policy, measuring how much better action aa is than average in state ss
  • Es\mathbb{E}_s: expectation over states from the state distribution under the old policy
  • DKL(πθold∥πθ)D_{\text{KL}}(\pi_{\theta_{\text{old}}} \| \pi_\theta): Kullback-Leibler divergence measuring how much the new policy distribution differs from the old policy distribution
  • δ\delta: maximum allowed KL divergence, the trust region radius

This constraint ensures the new policy πθ\pi_\theta does not diverge too far from the old policy πθold\pi_{\theta_{\text{old}}} in terms of the probability distributions over actions. The ratio πθ(a∣s)πθold(a∣s)\frac{\pi_\theta(a|s)}{\pi_{\theta_{\text{old}}}(a|s)} is called the importance sampling ratio and allows us to evaluate the new policy using data collected from the old policy. This ratio acts as a correction factor: if the new policy is twice as likely to take an action as the old policy, we weight that action's contribution twice as heavily to account for the fact that it would occur more frequently under the new policy.

Why TRPO Works But Is Complex

TRPO guarantees monotonic improvement under certain conditions, meaning the policy never gets worse than the previous version. This guarantee provides the stability that vanilla policy gradients lack. However, the mathematical machinery required to enforce this guarantee comes with significant computational costs: computing Fisher information matrices and solving linear systems.

Solving the constrained optimization problem requires computing second-order derivatives, specifically the Fisher information matrix, and performing conjugate gradient optimization to solve a system of linear equations. This makes TRPO computationally expensive and difficult to implement correctly. The Fisher information matrix has size n×nn \times n where nn is the number of policy parameters, so even storing it is impractical for large neural networks. Computing the matrix-vector product required by conjugate gradient solvers requires special tricks involving second-order automatic differentiation. Computing the Fisher information matrix across multiple workers in distributed settings adds significant overhead.

PPO achieves similar stability guarantees with a first-order method by replacing the hard constraint with a penalty built directly into the objective function. Instead of solving a constrained optimization problem, PPO modifies the objective itself to discourage excessive policy changes. This transformation from constraint to penalty makes PPO dramatically simpler to implement while preserving the needed benefits of trust region methods. The resulting algorithm fits cleanly into any standard deep learning training loop, with no exotic optimization machinery required.