Skip to content
Ahmed Haroon
← Reinforcement Learning

Advanced Policy Optimization

Reusing On-Policy Data

Vanilla policy gradients estimate

∇θJ(θ)=Es∼dπθ, a∼πθ(⋅∣s)[∇θlog⁡πθ(a∣s)Aπθ(s,a)].\nabla_\theta J(\theta) =\mathbb E_{ s\sim d^{\pi_\theta},\, a\sim\pi_\theta(\cdot\mid s) } \left[ \nabla_\theta\log\pi_\theta(a\mid s) A^{\pi_\theta}(s,a) \right].

The estimator is on-policy in two ways: states follow the current policy's occupancy dπθd^{\pi_\theta}, and actions follow πθ(⋅∣s)\pi_\theta(\cdot\mid s).

Suppose a batch was collected by πθk\pi_{\theta_k} and one gradient step produces

θk+1=θk+αg^k.\theta_{k+1}=\theta_k+\alpha\widehat g_k.

The old state-action pairs did not come from πθk+1\pi_{\theta_{k+1}}. Reusing them naively substitutes the wrong sampling distribution into the next gradient estimate.

This is expensive because reliable policy gradients need large trajectory batches. We want an outer loop that collects data and an inner loop that performs several optimization steps:

collect once⟶estimate advantages once⟶optimize several times.\text{collect once} \longrightarrow \text{estimate advantages once} \longrightarrow \text{optimize several times}.

Every inner step makes the batch more off-policy. Advanced policy optimization asks how to reuse it without letting the candidate policy drift so far that the old data becomes misleading.

Importance Sampling

Let pp be a target distribution and qq a behavior distribution. If q(x)>0q(x)>0 wherever p(x)>0p(x)>0, then

Ex∼p[f(x)]=Ex∼q[p(x)q(x)f(x)].\boxed{ \mathbb E_{x\sim p}[f(x)] =\mathbb E_{x\sim q} \left[\frac{p(x)}{q(x)}f(x)\right] }.

The likelihood ratio corrects for sampling from the wrong distribution.

For a trajectory,

pθ(τ)=p(s0)∏t=0H−1πθ(at∣st)P(st+1∣st,at).p_\theta(\tau) =p(s_0)\prod_{t=0}^{H-1} \pi_\theta(a_t\mid s_t) P(s_{t+1}\mid s_t,a_t).

Comparing a target policy πθ\pi_\theta with a behavior policy πβ\pi_\beta, the initial-state and transition factors cancel:

pθ(τ)pβ(τ)=∏t=0H−1πθ(at∣st)πβ(at∣st).\boxed{ \frac{p_\theta(\tau)}{p_\beta(\tau)} =\prod_{t=0}^{H-1} \frac{\pi_\theta(a_t\mid s_t)} {\pi_\beta(a_t\mid s_t)} }.

Define the per-step ratio

ρt(θ)=πθ(at∣st)πβ(at∣st).\rho_t(\theta) =\frac{\pi_\theta(a_t\mid s_t)} {\pi_\beta(a_t\mid s_t)}.

Then the exact off-policy objective is

J(θ)=Eτ∼pβ[(∏t=0H−1ρt(θ))R(τ)].J(\theta) =\mathbb E_{\tau\sim p_\beta} \left[ \left(\prod_{t=0}^{H-1}\rho_t(\theta)\right) R(\tau) \right].

The exact off-policy gradient

Applying the likelihood-ratio identity gives

∇θJ(θ)=Eτ∼pβ[(∏j=0H−1ρj(θ))(∑t=0H−1∇θlog⁡πθ(at∣st))R(τ)].\boxed{ \nabla_\theta J(\theta) =\mathbb E_{\tau\sim p_\beta} \left[ \left(\prod_{j=0}^{H-1}\rho_j(\theta)\right) \left( \sum_{t=0}^{H-1} \nabla_\theta\log\pi_\theta(a_t\mid s_t) \right) R(\tau) \right] }.
See derivation

Write the target expectation under behavior trajectories:

J(θ)=Eτ∼pβ[pθ(τ)pβ(τ)R(τ)].J(\theta) =\mathbb E_{\tau\sim p_\beta} \left[ \frac{p_\theta(\tau)}{p_\beta(\tau)}R(\tau) \right].

Differentiate:

∇θJ(θ)=Eτ∼pβ[∇θpθ(τ)pβ(τ)R(τ)]=Eτ∼pβ[pθ(τ)pβ(τ)∇θlog⁡pθ(τ)R(τ)].\begin{aligned} \nabla_\theta J(\theta) &=\mathbb E_{\tau\sim p_\beta} \left[ \frac{\nabla_\theta p_\theta(\tau)}{p_\beta(\tau)} R(\tau) \right]\\ &=\mathbb E_{\tau\sim p_\beta} \left[ \frac{p_\theta(\tau)}{p_\beta(\tau)} \nabla_\theta\log p_\theta(\tau) R(\tau) \right]. \end{aligned}

Only the policy factors depend on θ\theta:

∇θlog⁡pθ(τ)=∑t∇θlog⁡πθ(at∣st).\nabla_\theta\log p_\theta(\tau) =\sum_t\nabla_\theta\log\pi_\theta(a_t\mid s_t).

Substituting the trajectory ratio gives the result.

Causality lets the score at time tt use reward-to-go and only the prefix ratio:

∇θJ(θ)=Eτ∼pβ[∑t=0H−1(∏j=0tρj(θ))∇θlog⁡πθ(at∣st)Gt].\boxed{ \nabla_\theta J(\theta) =\mathbb E_{\tau\sim p_\beta} \left[ \sum_{t=0}^{H-1} \left(\prod_{j=0}^{t}\rho_j(\theta)\right) \nabla_\theta\log\pi_\theta(a_t\mid s_t) G_t \right] }.

The prefix corrects both the current action and the state distribution created by all preceding actions.

Why exact correction is impractical

Even modest deviations multiply across time:

log⁡∏j=0tρj=∑j=0tlog⁡ρj.\log\prod_{j=0}^{t}\rho_j =\sum_{j=0}^{t}\log\rho_j.

A few trajectories receive enormous weights while most receive almost zero. The estimator remains unbiased but has unusably high variance.

For normalized weights wiw_i, the effective sample size

ESS=(∑iwi)2∑iwi2\mathrm{ESS} =\frac{\left(\sum_iw_i\right)^2}{\sum_iw_i^2}

collapses when a few samples dominate.

The practical surrogate retains only the current action ratio:

LIS(θ′)=Es∼dπβ, a∼πβ[πθ′(a∣s)πβ(a∣s)Aπβ(s,a)].\boxed{ L^{\mathrm{IS}}(\theta') =\mathbb E_{ s\sim d^{\pi_\beta},\, a\sim\pi_\beta } \left[ \frac{\pi_{\theta'}(a\mid s)}{\pi_\beta(a\mid s)} A^{\pi_\beta}(s,a) \right] }.

This deliberately ignores state-distribution mismatch. It is useful only while the candidate policy remains close to the behavior policy.

Policy Improvement from Old Advantages

The right justification for several old-batch updates is approximate policy iteration. We evaluate the old policy, then fit a new policy that prefers its positive-advantage actions.

For policies π\pi and π′\pi', the performance-difference lemma states

J(π′)−J(π)=Eτ∼π′[∑t=0∞γtAπ(st,at)].\boxed{ J(\pi')-J(\pi) =\mathbb E_{\tau\sim\pi'} \left[ \sum_{t=0}^{\infty}\gamma^tA^\pi(s_t,a_t) \right] }.
See derivation

Start with

J(π′)−J(π)=J(π′)−Eτ∼π′[Vπ(s0)].J(\pi')-J(\pi) =J(\pi')- \mathbb E_{\tau\sim\pi'}[V^\pi(s_0)].

The value term can be written as a telescoping series:

−Vπ(s0)=∑t=0∞γt[γVπ(st+1)−Vπ(st)].-V^\pi(s_0) =\sum_{t=0}^{\infty}\gamma^t \left[ \gamma V^\pi(s_{t+1})-V^\pi(s_t) \right].

Therefore,

J(π′)−J(π)=Eτ∼π′[∑t=0∞γt(rt+γVπ(st+1)−Vπ(st))].\begin{aligned} J(\pi')-J(\pi) &=\mathbb E_{\tau\sim\pi'} \left[ \sum_{t=0}^{\infty}\gamma^t \left( r_t+\gamma V^\pi(s_{t+1})-V^\pi(s_t) \right) \right]. \end{aligned}

Taking the conditional expectation over st+1s_{t+1},

r(st,at)+γE[Vπ(st+1)∣st,at]−Vπ(st)=Aπ(st,at).r(s_t,a_t)+\gamma\mathbb E[V^\pi(s_{t+1})\mid s_t,a_t] -V^\pi(s_t) =A^\pi(s_t,a_t).

Substitution proves the identity.

Using the normalized discounted occupancy

dπ(s)=(1−γ)∑t=0∞γtPr⁡(st=s∣π),d^\pi(s) =(1-\gamma)\sum_{t=0}^{\infty} \gamma^t\Pr(s_t=s\mid\pi),

we can write

J(π′)−J(π)=11−γEs∼dπ′, a∼π′[Aπ(s,a)].J(\pi')-J(\pi) =\frac1{1-\gamma} \mathbb E_{ s\sim d^{\pi'},\, a\sim\pi' }[A^\pi(s,a)].

At each fixed state, change the action distribution back to π\pi:

Ea∼π′[Aπ(s,a)]=Ea∼π[π′(a∣s)π(a∣s)Aπ(s,a)].\mathbb E_{a\sim\pi'}[A^\pi(s,a)] =\mathbb E_{a\sim\pi} \left[ \frac{\pi'(a\mid s)}{\pi(a\mid s)}A^\pi(s,a) \right].

The only remaining new-policy quantity is its state distribution. Replacing dπ′d^{\pi'} with dπd^\pi yields the tractable surrogate

Lπ(π′)=11−γEs∼dπ, a∼π[π′(a∣s)π(a∣s)Aπ(s,a)].\boxed{ L_\pi(\pi') =\frac1{1-\gamma} \mathbb E_{ s\sim d^\pi,\, a\sim\pi } \left[ \frac{\pi'(a\mid s)}{\pi(a\mid s)}A^\pi(s,a) \right] }.

The old advantage is exact. The approximation is freezing the state visitation distribution.

Several inner updates are reasonable while the old advantages remain informative and dπ′d^{\pi'} remains close to dπd^\pi.

Controlling State-Distribution Shift

At a state, total-variation distance between two policies is

DTV(π′(⋅∣s),π(⋅∣s))=12∑a∣π′(a∣s)−π(a∣s)∣.D_{\mathrm{TV}} \left(\pi'(\cdot\mid s),\pi(\cdot\mid s)\right) =\frac12\sum_a |\pi'(a\mid s)-\pi(a\mid s)|.

Assume

max⁡sDTV(π′(⋅∣s),π(⋅∣s))≤α.\max_sD_{\mathrm{TV}} \left(\pi'(\cdot\mid s),\pi(\cdot\mid s)\right) \leq\alpha.

The policies can be coupled to choose the same action at each shared state with probability at least 1−α1-\alpha. The probability of no disagreement for tt decisions is at least (1−α)t(1-\alpha)^t, so

DTV(ptπ′,ptπ)≤1−(1−α)t≤tα.D_{\mathrm{TV}}(p_t^{\pi'},p_t^\pi) \leq1-(1-\alpha)^t \leq t\alpha.

Thus small action-distribution changes control state-distribution changes over a limited horizon.

For a bounded function ff,

∣Ep′[f]−Ep[f]∣≤2DTV(p′,p)∥f∥∞.\left| \mathbb E_{p'}[f]-\mathbb E_p[f] \right| \leq2D_{\mathrm{TV}}(p',p)\|f\|_\infty.

Combining these ideas gives the structural bound

true improvement≥old-data surrogate improvement−penalty for policy change.\boxed{ \text{true improvement} \geq \text{old-data surrogate improvement} -\text{penalty for policy change} }.

For the infinite-horizon discounted case, the penalty contains

∑t=0∞tγt=γ(1−γ)2,\sum_{t=0}^{\infty}t\gamma^t =\frac{\gamma}{(1-\gamma)^2},

so long effective horizons make distribution shift especially costly.

Pinsker's inequality connects total variation to KL divergence:

DTV(p,q)≤12DKL(p∥q).\boxed{ D_{\mathrm{TV}}(p,q) \leq\sqrt{\frac12D_{\mathrm{KL}}(p\|q)} }.

We can therefore control the approximation error by keeping the candidate policy close in KL.

KL Trust Regions

Euclidean parameter distance is a poor measure of policy change. A tiny parameter step can drastically alter action probabilities, while a large step in an insensitive direction may barely change them.

KL divergence measures change in the represented distributions:

DKL(p∥q)=Ex∼p[log⁡p(x)q(x)].D_{\mathrm{KL}}(p\|q) =\mathbb E_{x\sim p} \left[\log\frac{p(x)}{q(x)}\right].

It is nonnegative but asymmetric:

DKL(p∥q)≠DKL(q∥p).D_{\mathrm{KL}}(p\|q) \neq D_{\mathrm{KL}}(q\|p).

We usually average statewise KL under old-policy states:

D‾KL(θ′,θ)=Es∼dπθ[DKL(πθ(⋅∣s) ∥ πθ′(⋅∣s))].\boxed{ \overline D_{\mathrm{KL}}(\theta',\theta) =\mathbb E_{s\sim d^{\pi_\theta}} \left[ D_{\mathrm{KL}} \left( \pi_\theta(\cdot\mid s) \,\|\, \pi_{\theta'}(\cdot\mid s) \right) \right] }.

The trust-region problem is

max⁡θ′Et[πθ′(at∣st)πθ(at∣st)A^t]subject toD‾KL(θ′,θ)≤δ..\boxed{ \begin{aligned} \max_{\theta'}\quad &\mathbb E_t \left[ \frac{\pi_{\theta'}(a_t\mid s_t)} {\pi_\theta(a_t\mid s_t)} \widehat A_t \right]\\ \text{subject to}\quad &\overline D_{\mathrm{KL}}(\theta',\theta)\leq\delta. \end{aligned} }.

A simpler penalized objective is

Lpen(θ′)=Lθ(θ′)−βD‾KL(θ′,θ).L_{\mathrm{pen}}(\theta') =L_\theta(\theta') -\beta\overline D_{\mathrm{KL}}(\theta',\theta).

Large β\beta makes updates conservative. Small β\beta permits more movement. Because the useful scale changes during training, we can adapt β\beta:

β←[β+ηβ(D‾KL−δ)]+.\boxed{ \beta\leftarrow \left[ \beta+\eta_\beta \left(\overline D_{\mathrm{KL}}-\delta\right) \right]_+ }.

If measured KL is too large, policy movement becomes more expensive. If it is too small, the penalty relaxes.

Proximal Policy Optimization

PPO approximately prevents the updated policy from moving too far from the policy that generated the batch.

Let

ρt(θ)=πθ(at∣st)πold(at∣st)=exp⁡(log⁡πθ(at∣st)−log⁡πold(at∣st)).\rho_t(\theta) =\frac{\pi_\theta(a_t\mid s_t)} {\pi_{\mathrm{old}}(a_t\mid s_t)} =\exp \left( \log\pi_\theta(a_t\mid s_t) -\log\pi_{\mathrm{old}}(a_t\mid s_t) \right).

The raw importance-weighted surrogate is

LIS(θ)=Et[ρt(θ)A^t].L^{\mathrm{IS}}(\theta) =\mathbb E_t[\rho_t(\theta)\widehat A_t].

For A^t>0\widehat A_t>0, it rewards increasing ρt\rho_t without limit. For A^t<0\widehat A_t<0, it rewards decreasing ρt\rho_t without limit. Repeated updates can therefore move far beyond the region where the old advantage is reliable.

The clipped objective

PPO uses the pessimistic lower envelope

LCLIP(θ)=Et[min⁡(ρt(θ)A^t,clip⁡(ρt(θ),1−ϵ,1+ϵ)A^t)].\boxed{ L^{\mathrm{CLIP}}(\theta) =\mathbb E_t \left[ \min\left( \rho_t(\theta)\widehat A_t, \operatorname{clip} (\rho_t(\theta),1-\epsilon,1+\epsilon) \widehat A_t \right) \right] }.

When A>0A>0,

ℓ(ρ,A)={ρA,ρ≤1+ϵ,(1+ϵ)A,ρ>1+ϵ.\ell(\rho,A) = \begin{cases} \rho A,&\rho\leq1+\epsilon,\\ (1+\epsilon)A,&\rho>1+\epsilon. \end{cases}

The sample stops rewarding further increases beyond 1+ϵ1+\epsilon. Moving in the harmful direction still receives the unclipped penalty.

When A<0A<0,

ℓ(ρ,A)={(1−ϵ)A,ρ<1−ϵ,ρA,ρ≥1−ϵ.\ell(\rho,A) = \begin{cases} (1-\epsilon)A,&\rho<1-\epsilon,\\ \rho A,&\rho\geq1-\epsilon. \end{cases}

The sample stops rewarding further probability reduction below 1−ϵ1-\epsilon, while increasing the bad action remains penalized.

This is why PPO uses

min⁡(ρA,clip⁡(ρ)A)\min(\rho A,\operatorname{clip}(\rho)A)

rather than simply replacing ρ\rho by its clipped value.

Clipping does not enforce

1−ϵ≤ρt≤1+ϵ1-\epsilon\leq\rho_t\leq1+\epsilon

for every sample. It removes the incentive for excessive movement in one direction; it does not pull an out-of-range ratio back. Shared network parameters can also change unsampled probabilities.

At the start of the update, ρt=1\rho_t=1, so clipped and unclipped objectives have the same gradient. After policy movement, clipping introduces deliberate bias in exchange for lower variance and safer reuse.

Advantages and value targets

PPO is usually paired with Generalized Advantage Estimation:

δtV=rt+γ(1−dt)Vϕ(st+1)−Vϕ(st),\delta_t^V =r_t+\gamma(1-d_t)V_\phi(s_{t+1})-V_\phi(s_t), A^t=∑l=0H−t−1(γλ)lδt+lV.\widehat A_t =\sum_{l=0}^{H-t-1} (\gamma\lambda)^l\delta_{t+l}^V.

The matching value target is

V^ttarget=A^t+Vϕ(st).\widehat V_t^{\mathrm{target}} =\widehat A_t+V_\phi(s_t).

Old log-probabilities, advantages, and value targets are computed once and frozen throughout the inner epochs. Recomputing the denominator with the updated policy would collapse the ratio toward one and destroy the surrogate's meaning.

The complete loss

A common minimization loss is

J(θ,ϕ)=−LCLIP(θ)+cVLV(ϕ)−cHEt[H(πθ(⋅∣st))].\boxed{ \mathcal J(\theta,\phi) =-L^{\mathrm{CLIP}}(\theta) +c_VL_V(\phi) -c_H\mathbb E_t[\mathcal H(\pi_\theta(\cdot\mid s_t))] }.

where

LV(ϕ)=Et[(Vϕ(st)−V^ttarget)2].L_V(\phi) =\mathbb E_t \left[ (V_\phi(s_t)-\widehat V_t^{\mathrm{target}})^2 \right].

The entropy bonus discourages premature collapse of the action distribution.

Clipped PPO\textbf{Clipped PPO}Initialize actor πθ and critic Vϕ.Repeat:θold←θ,collect trajectories with πθold,store log⁡πθold(at∣st),compute and freeze A^t and V^ttarget,for several epochs:shuffle the batch into minibatches,ρt←exp⁡(log⁡πθ(at∣st)−log⁡πold(at∣st)),optimize the clipped actor objective, value loss, and entropy,measure KL and stop early if it becomes too large.collect a fresh batch with the updated policy.\begin{aligned} &\text{Initialize actor }\pi_\theta\text{ and critic }V_\phi.\\[0.25em] &\text{Repeat:}\\[0.25em] &\qquad \theta_{\mathrm{old}}\leftarrow\theta,\\[0.25em] &\qquad \text{collect trajectories with }\pi_{\theta_{\mathrm{old}}},\\[0.25em] &\qquad \text{store }\log\pi_{\theta_{\mathrm{old}}}(a_t\mid s_t),\\[0.25em] &\qquad \text{compute and freeze }\widehat A_t \text{ and }\widehat V_t^{\mathrm{target}},\\[0.25em] &\qquad \text{for several epochs:}\\[0.25em] &\qquad\quad \text{shuffle the batch into minibatches},\\[0.25em] &\qquad\quad \rho_t\leftarrow \exp(\log\pi_\theta(a_t\mid s_t)-\log\pi_{\mathrm{old}}(a_t\mid s_t)),\\[0.25em] &\qquad\quad \text{optimize the clipped actor objective, value loss, and entropy},\\[0.25em] &\qquad\quad \text{measure KL and stop early if it becomes too large}.\\[0.25em] &\qquad \text{collect a fresh batch with the updated policy}. \end{aligned}

An empirical old-to-new KL estimate is

D^KL≈1B∑t[log⁡πold(at∣st)−log⁡πθ(at∣st)].\widehat D_{\mathrm{KL}} \approx\frac1B\sum_t \left[ \log\pi_{\mathrm{old}}(a_t\mid s_t) -\log\pi_\theta(a_t\mid s_t) \right].

Clipping approximates a trust region; monitoring KL and stopping inner optimization early remain important safeguards.

Natural Policy Gradients

The ordinary gradient is the steepest-ascent direction under a Euclidean parameter constraint:

max⁡δg⊤δsubject toδ⊤δ≤ε,\max_\delta g^\top\delta \quad\text{subject to}\quad \delta^\top\delta\leq\varepsilon,

where g=∇θJ(θ)g=\nabla_\theta J(\theta). Natural gradients instead measure local distance by policy KL.

For θ′=θ+δ\theta'=\theta+\delta, the local KL expansion is

DKL(pθ∥pθ+δ)≈12δ⊤F(θ)δ,\boxed{ D_{\mathrm{KL}} (p_\theta\|p_{\theta+\delta}) \approx\frac12\delta^\top F(\theta)\delta },

where

F(θ)=Ex∼pθ[∇θlog⁡pθ(x)∇θlog⁡pθ(x)⊤]\boxed{ F(\theta) =\mathbb E_{x\sim p_\theta} \left[ \nabla_\theta\log p_\theta(x) \nabla_\theta\log p_\theta(x)^\top \right] }

is the Fisher information matrix.

See derivation

Taylor-expand the candidate log-density:

log⁡pθ+δ(x)≈log⁡pθ(x)+δ⊤∇θlog⁡pθ(x)+12δ⊤∇θ2log⁡pθ(x)δ.\log p_{\theta+\delta}(x) \approx\log p_\theta(x) +\delta^\top\nabla_\theta\log p_\theta(x) +\frac12\delta^\top \nabla_\theta^2\log p_\theta(x)\delta.

Substitute into KL:

DKL(pθ∥pθ+δ)≈−δ⊤E[∇θlog⁡pθ(x)]−12δ⊤E[∇θ2log⁡pθ(x)]δ.\begin{aligned} D_{\mathrm{KL}}(p_\theta\|p_{\theta+\delta}) &\approx -\delta^\top \mathbb E[\nabla_\theta\log p_\theta(x)]\\ &\quad- \frac12\delta^\top \mathbb E[\nabla_\theta^2\log p_\theta(x)] \delta. \end{aligned}
Epθ[∇θlog⁡pθ(x)]=0.\mathbb E_{p_\theta}[\nabla_\theta\log p_\theta(x)]=0.

The Fisher identity is

−E[∇θ2log⁡pθ(x)]=E[∇θlog⁡pθ(x)∇θlog⁡pθ(x)⊤].-\mathbb E[\nabla_\theta^2\log p_\theta(x)] =\mathbb E \left[ \nabla_\theta\log p_\theta(x) \nabla_\theta\log p_\theta(x)^\top \right].

Substitution yields the quadratic approximation.

For policies, we average the statewise Fisher under current-policy states:

F(θ)=Es∼dπθ, a∼πθ[∇θlog⁡πθ(a∣s)∇θlog⁡πθ(a∣s)⊤].F(\theta) =\mathbb E_{ s\sim d^{\pi_\theta},\, a\sim\pi_\theta } \left[ \nabla_\theta\log\pi_\theta(a\mid s) \nabla_\theta\log\pi_\theta(a\mid s)^\top \right].

Linearize the objective and quadratically approximate KL:

max⁡δg⊤δsubject to12δ⊤Fδ≤ε.\begin{aligned} \max_\delta\quad &g^\top\delta\\ \text{subject to}\quad &\frac12\delta^\top F\delta\leq\varepsilon. \end{aligned}

The solution is

δ∗=2εg⊤F−1gF−1g.\boxed{ \delta^* =\sqrt{ \frac{2\varepsilon}{g^\top F^{-1}g} } F^{-1}g }.
See derivation

Form the Lagrangian

L(δ,λ)=g⊤δ−λ(12δ⊤Fδ−ε).\mathcal L(\delta,\lambda) =g^\top\delta -\lambda \left(\frac12\delta^\top F\delta-\varepsilon\right).

Stationarity gives

g−λFδ=0,g-\lambda F\delta=0,

so

δ=1λF−1g.\delta=\frac1\lambda F^{-1}g.

The optimum uses the full KL budget:

12λ2g⊤F−1g=ε.\frac1{2\lambda^2}g^\top F^{-1}g=\varepsilon.

Solving for λ\lambda and substituting yields the stated step.

The direction

∇~θJ=F−1∇θJ\widetilde\nabla_\theta J =F^{-1}\nabla_\theta J

is the natural policy gradient. Directions that strongly change action probabilities have large Fisher curvature and are shrunk. Directions that barely change the policy can move farther in parameter space.

Trust Region Policy Optimization

TRPO approximates the natural-gradient trust-region step without constructing or inverting the Fisher matrix.

At the current policy, estimate the surrogate gradient gg and solve

Fx=g.Fx=g.

For a network with millions of parameters, explicitly forming FF is impossible. Automatic differentiation can instead compute Fisher-vector products

Fv=∇θ′[∇θ′D^KL(θ′)⊤v]θ′=θFv =\nabla_{\theta'} \left[ \nabla_{\theta'}\widehat D_{\mathrm{KL}}(\theta')^\top v \right]_{\theta'=\theta}

without materializing the matrix.

Conjugate gradients approximately solves the damped system

(F+ηI)x=g.\boxed{ (F+\eta I)x=g }.

Damping improves conditioning and prevents extreme movement along poorly estimated low-curvature directions.

Scale the direction to the quadratic KL radius:

δquad=2δKLx⊤Fxx.\delta_{\mathrm{quad}} =\sqrt{ \frac{2\delta_{\mathrm{KL}}}{x^\top Fx} }x.

This step is based on local approximations and a finite-sample Fisher estimate. TRPO therefore performs backtracking line search. It tries

θcandidate=θ+αδquad,α∈{1,ξ,ξ2,…},\theta_{\mathrm{candidate}} =\theta+\alpha\delta_{\mathrm{quad}}, \qquad \alpha\in\{1,\xi,\xi^2,\ldots\},

and accepts the first candidate that improves the measured surrogate while satisfying the empirical KL limit.

TRPO\textbf{TRPO}Collect trajectories with πθ.Estimate fixed advantages and the surrogate gradient g.Define v↦(F+ηI)v using Fisher-vector products.Use conjugate gradients to approximately solve (F+ηI)x=g.Scale x to the quadratic KL radius.Backtrack along the step until:the surrogate improves, andD^KL≤δKL.Accept the candidate and collect a new batch.\begin{aligned} &\text{Collect trajectories with }\pi_\theta.\\[0.25em] &\text{Estimate fixed advantages and the surrogate gradient }g.\\[0.25em] &\text{Define }v\mapsto(F+\eta I)v\text{ using Fisher-vector products}.\\[0.25em] &\text{Use conjugate gradients to approximately solve }(F+\eta I)x=g.\\[0.25em] &\text{Scale }x\text{ to the quadratic KL radius}.\\[0.25em] &\text{Backtrack along the step until:}\\[0.25em] &\qquad \text{the surrogate improves, and}\\[0.25em] &\qquad \widehat D_{\mathrm{KL}}\leq\delta_{\mathrm{KL}}.\\[0.25em] &\text{Accept the candidate and collect a new batch}. \end{aligned}

TRPO more faithfully enforces a KL trust region. PPO replaces the second-order solve and line search with ordinary minibatch optimization and approximate clipping.

PropertyTRPOPPO
Policy controlExplicit empirical KL checkPessimistic ratio clipping
OptimizerConjugate gradients and line searchSGD or Adam
Batch reuseOne large constrained stepSeveral minibatch epochs
Constraint fidelityHigherApproximate
Implementation complexityHighModerate

Practical Diagnosis

PPO is usually the practical default when standard minibatch optimization, accelerator efficiency, and simple distributed rollout collection matter. TRPO is useful when tighter control over policy movement justifies curvature estimation and line search.

The main PPO diagnostics are:

D^KL(πold,πθ),\widehat D_{\mathrm{KL}}(\pi_{\mathrm{old}},\pi_\theta), clipfrac⁡=1B∑t1[∣ρt−1∣>ϵ],\operatorname{clipfrac} =\frac1B\sum_t \mathbb 1[|\rho_t-1|>\epsilon],

policy entropy, value loss, and explained variance

EV⁡=1−Var⁡(V^target−Vϕ)Var⁡(V^target).\operatorname{EV} =1- \frac{ \operatorname{Var}(\widehat V^{\mathrm{target}}-V_\phi) }{ \operatorname{Var}(\widehat V^{\mathrm{target}}) }.
SymptomLikely causeUseful response
KL rises sharply within an epochLearning rate or epoch count is too largeStop early and reduce inner optimization
Most samples are clippedBatch is already too off-policyUse fewer epochs or smaller updates
KL is tiny and clip fraction is near zeroUpdates are overly conservativeIncrease learning rate or reuse cautiously
Entropy collapses earlyPolicy becomes deterministic too quicklyIncrease entropy regularization or reduce actor updates
Ratios are unstableOld log-probabilities or policy scale are wrongStore the denominator once and inspect log-ratios
Policy metrics look healthy but returns failCritic advantages are unreliableInspect value loss and explained variance
Value loss decreases while policy degradesCritic is overfitting a stale batchReduce critic epochs and collect fresh data

Before trusting a PPO run, verify that old log-probabilities are stored and frozen, advantages and value targets are computed once, terminal and truncation bootstrapping are correct, ratios use log-probability differences, and inner optimization stops when KL becomes excessive.

The full progression is

J(π′)−J(π)=11−γEdπ′,π′[Aπ]⏟exact policy improvement\underbrace{ J(\pi')-J(\pi) =\frac1{1-\gamma} \mathbb E_{d^{\pi'},\pi'}[A^\pi] }_{\text{exact policy improvement}} ⇓correct actions and freeze states\Downarrow\quad\text{correct actions and freeze states} Lπ(π′)=11−γEdπ,π[π′πAπ]⏟old-data surrogate\underbrace{ L_\pi(\pi') =\frac1{1-\gamma} \mathbb E_{d^\pi,\pi} \left[\frac{\pi'}{\pi}A^\pi\right] }_{\text{old-data surrogate}} ⇓control policy movement\Downarrow\quad\text{control policy movement} max⁡π′Lπ(π′)subject toD‾KL(π,π′)≤δ⏟trust-region update.\underbrace{ \max_{\pi'}L_\pi(\pi') \quad\text{subject to}\quad \overline D_{\mathrm{KL}}(\pi,\pi')\leq\delta }_{\text{trust-region update}}.

PPO approximates this constraint with clipped ratios or an adaptive KL penalty. Natural gradients use the Fisher geometry of policy space. TRPO combines that geometry with conjugate gradients and line search. All three aim to extract more learning from each batch without trusting old advantages beyond their local region.