Why does policy gradient actually work? Levine reinterprets it as a softened policy iteration, proves that maximizing the old policy's advantage under the new policy's trajectories maximizes the true RL objective, and shows that the substitution making this tractable is safe exactly when the new policy stays close to the old one. That turns policy gradient into a constrained optimization problem, enforced with a KL penalty (the route PPO takes) or approximated in closed form by the natural gradient (the route TRPO takes).
This is “one of the more technically nuanced lectures in the course,” and it answers a
question the earlier lectures skipped: why should we expect policy gradients to
improve the policy? The obvious answer — it’s a gradient, and gradient ascent works —
hides most of the structure, and the analysis that reveals it carries over to
actor-critic methods and RL algorithm design generally.
Recap: REINFORCE samples trajectories from the current policy, computes each step’s
reward-to-go (Monte Carlo, or a learned value function from the actor-critic lecture),
and steps along
∇θJ(θ)≈N1∑i∑t∇θlogπθ(ai,t∣si,t)A^i,t.
Conceptually, though, every policy gradient method alternates two steps: estimate the
advantage A^π(st,at) of the current policy, then use the estimate to get
an improved policy π′. That structure is exactly policy iteration from the
value-based methods week — except policy iteration jumps straight to the argmax,
putting probability 1 on the highest-advantage action, while policy gradient “makes a
much gentler update”: grad-log-pi times A^ nudges probability toward
large-advantage actions without going all the way to one and zero.
The improvement lemma
The technical heart of the lecture is one identity. Work with discounted returns where
the discount starts at step one — not the convention used in practice, but it makes the
calculation clean.
Now unpack the right-hand side into per-time-step state–action marginals and
importance-sample the action expectation back to the old policy:
One obstacle remains: the states are still distributed according to pθ′(st),
which we cannot sample from — θ′ is the thing we’re solving for. If we could
replace pθ′(st) with pθ(st) — call the result Aˉ(θ′) —
then differentiating at θ′=θ recovers exactly the policy gradient, and
policy gradient stands revealed as approximate policy iteration. Everything hinges on
one claim: pθ(st) is close to pθ′(st) when πθ is close to
πθ′. That sounds obvious; proving it non-vacuously is the next section’s
work.
Bounding the distribution mismatch
Start with a deterministic πθ and say πθ′ is close to it if it
assigns probability at most ϵ to any action πθ would not have taken.
Then the new policy’s state marginal splits into two terms:
pθ′(st)=(1−ϵ)tpθ(st)+(1−(1−ϵ)t)pmistake(st)
If no mistake was made for t steps — probability (1−ϵ)t — the state
distribution is exactly the old one; otherwise we land in some pmistake
about which we assume nothing, like the tightrope walker from the imitation learning
lecture who falls and can never recover. This is exactly the behavioral cloning
analysis from lecture 2, and it yields the same bound: any total variation divergence
is at most 2, and (1−ϵ)t≥1−ϵt for ϵ∈[0,1], so
∣pθ′(st)−pθ(st)∣≤2(1−(1−ϵ)t)≤2ϵt
“It’s not a great bound, but it is a bound”: the marginals converge as
ϵ→0.
For the general stochastic case — following the trust region policy optimization (TRPO)
paper — define closeness by total variation:
∣πθ′(at∣st)−πθ(at∣st)∣≤ϵ for all st
(an in-expectation version also works). A lemma from prior work does the heavy lifting:
if two distributions have total variation divergence ϵ, there exists a joint
distribution with those marginals under which the two variables agree with probability
1−ϵ. Geometrically: overlay the two distributions as bar graphs; the slivers
where the bars differ have total volume ϵ, so the probability of acting
differently is at most ϵ. That recovers the “probability of a mistake”
structure of the deterministic case, and the same 2ϵt bound.
To turn a bound on distributions into a bound on the objective, bound the expectation
of any function f:
(write pθ′=pθ+(pθ′−pθ) and be maximally pessimistic
on the difference — Levine’s advice: pause and derive this yourself). Taking f to be
the bracketed action expectation above, the tractable objective lower-bounds the true
one up to an error 2ϵtC, with C the largest value the bracket can take.
The importance weights and expectations are over probabilities that sum to one, and an
advantage is a sum of rewards over time, so C is of order Trmax for a finite
horizon and rmax/(1−γ) for an infinite one.
The conclusion: maximizing Aˉ(θ′) — whose derivative is exactly the policy
gradient — is guaranteed to improve the true objective, providedθ′ stays
close to θ. Policy gradient is sound, but only as a constrained procedure.
Enforcing the constraint: KL penalties
Total variation constraints are awkward to impose (the absolute value isn’t
differentiable everywhere). The KL divergence — “basically the most widely used
divergence measure between distributions,” with tractable expressions and closed forms
for common continuous distributions — bounds it:
so constraining DKL(πθ′∥πθ)≤ϵ preserves
every bound above. The practical problem becomes: maximize the importance-sampled
objective Aˉ(θ′) subject to the KL constraint. One principled way in is the
Lagrangian,
L(θ′,λ)=Aˉ(θ′)−λ(DKL(πθ′∥πθ)−ϵ)
optimized by dual gradient descent: alternate maximizing L with respect
to θ′ (even incompletely, for a few gradient steps) with a gradient step on
λ — raise λ when the constraint is violated, lower it when it’s slack.
A more heuristic variant just fixes λ by hand and treats the KL term as a
regularizer penalizing deviation from the old policy. Both work well in practice; this
family includes the original guided policy search method and PPO (proximal policy
optimization).
The natural gradient
The last part trades exactness for simplicity: approximate the constrained problem so
aggressively that a single linear-algebra step solves it. First, Taylor-expand the
objective to first order around θ, giving the trust-region picture: optimize the
linear approximation (the green line), but only inside the region where you trust it
(the red box). The problem becomes
θ′←argmaxθ′∇θAˉ(θ)⊤(θ′−θ)s.t.DKL(πθ′∥πθ)≤ϵ
and the payoff of linearizing is that at θ′=θ the importance weights
cancel, so ∇θAˉ(θ)=∇θJ(θ) — the standard
policy gradient, with any advantage estimator you like plugged in.
Could plain gradient ascent handle the constraint? It does solve a constrained
problem — but the wrong one: argmax∇J⊤(θ′−θ) subject to
∥θ′−θ∥2≤ϵ, a circle in parameter space, with the learning
rate α=ϵ/∥∇θJ(θ)∥2 emerging as its
Lagrange multiplier. But the constraint we want lives in distribution space: some
parameters barely move the policy’s probabilities while others move them enormously,
so a small ball in θ can violate any KL budget. We want an ellipse — squished
along directions where the distribution is sensitive, elongated where it isn’t.
So do to the constraint what we did to the objective: Taylor-expand. The KL’s first
derivative at θ′=θ is zero (it’s flat at its minimum), so go to second
order:
DKL(πθ′∥πθ)≈21(θ′−θ)⊤F(θ′−θ)
With a linear objective and a quadratic constraint, the Lagrangian is a second-order
polynomial with a closed-form maximizer:
θ′=θ+αF−1∇θJ(θ),α=∇θJ(θ)⊤F−1∇θJ(θ)2ϵ
This is the natural gradient: the regular gradient premultiplied by the inverse
Fisher matrix, with α chosen (if you wish) to satisfy the KL budget ϵ
exactly — plug the update into the quadratic constraint and check the algebra.
Practical notes, as Levine gives them: classical natural policy gradient estimates
F from samples and picks α by hand — a good stabilizer in general
(see Peters and Schaal, “Reinforcement learning of motor skills with policy
gradients”). TRPO instead picks ϵ, solves for α, and uses the conjugate
gradient method to compute F−1∇θJ without ever forming the
full matrix. Or skip the approximation entirely and optimize the importance-sampled
objective for many gradient steps with a KL regularizer — the path that leads to PPO.
Check yourself
Derivation-heavy exercises in the lecture’s spirit — pen and paper recommended.
Problem 9.1telescoping improvement lemma
Prove the improvement lemma
J(θ′)−J(θ)=Eτ∼pθ′(τ)[∑t=0∞γtAπθ(st,at)].
Identify (a) the step where the expectation switches from p(s0) to
pθ′(τ) and why it is legal, and (b) where the telescoping happens.
Show solution
Start from J(θ)=Es0∼p(s0)[Vπθ(s0)]. (a) The
quantity inside depends only on s0, and every policy’s trajectory distribution has
the same initial-state marginal p(s0), so the expectation may be taken under
pθ′(τ) without changing its value — this is the step that plants the new
policy’s distribution into an expression about the old policy’s value. (b) Replace
Vπθ(s0) with
∑t=0∞γtVπθ(st)−∑t=1∞γtVπθ(st):
all terms with t≥1 cancel, so the two infinite sums equal Vπθ(s0)
exactly. Flip the sign of J(θ), pair the term at t+1 (which carries an extra
γ) with the term at t, and pull out γt to get
−J(θ)=Epθ′[∑tγt(γVπθ(st+1)−Vπθ(st))].
Adding J(θ′)=Epθ′[∑tγtr(st,at)] and merging
the sums leaves
Epθ′[∑tγt(r(st,at)+γVπθ(st+1)−Vπθ(st))],
and the bracket is the definition of Aπθ(st,at).
Problem 9.2how loose is the bound
Policies πθ and πθ′ satisfy the total variation closeness
condition with ϵ=0.001 at every state, over a horizon T=100 with
rmax=1. (a) Bound the total variation between the state marginals at the final
step. (b) Bound the error term 2ϵtC of the surrogate objective at t=T,
using C=Trmax. (c) Compare (b) to the largest possible return and comment on
how the bound scales with horizon.
Show solution
(a) 2ϵt=2×0.001×100=0.2. (b)
2ϵtC=2×0.001×100×(100×1)=20. (c) The
largest possible return is Trmax=100, so the worst-case error is 20% of the
maximum achievable return even with a policy that deviates with probability only one in
a thousand per step. The culprit is the t⋅C∼T2rmax scaling: one
factor of T from compounding state-distribution drift (the behavioral cloning
argument), another from the advantage being a sum of up to T rewards. The bound is
loose but honest — it vanishes linearly as ϵ→0, which is all the
constrained-update argument needs, and it explains why trust regions must shrink as
horizons grow.
Problem 9.3step sizes satisfy constraints
(a) Show that the gradient ascent step
θ′=θ+α∇θJ(θ) with
α=ϵ/∥∇θJ(θ)∥2 satisfies
∥θ′−θ∥2≤ϵ with equality. (b) Show that the natural gradient
step θ′=θ+αF−1∇θJ(θ) with
α=2ϵ/(∇θJ⊤F−1∇θJ)
satisfies the quadratized KL constraint
21(θ′−θ)⊤F(θ′−θ)≤ϵ with
equality.
Show solution
(a) ∥θ′−θ∥2=α2∥∇J∥2=∥∇J∥2ϵ∥∇J∥2=ϵ.
(b) Write g=∇θJ. Then θ′−θ=αF−1g and
21(αF−1g)⊤F(αF−1g)=21α2g⊤F−1FF−1g=21α2g⊤F−1g
(using symmetry of F). Substituting
α2=2ϵ/(g⊤F−1g) gives
21⋅g⊤F−1g2ϵ⋅g⊤F−1g=ϵ.
In both cases the maximizer of a linear objective over a convex constraint region sits
on the boundary, which is why equality is the right answer — the step uses the entire
budget ϵ in the steepest useful direction.
Problem 9.4Fisher preconditioning by hand
A two-parameter policy has, at the current θ, Fisher matrix
F=diag(100,1) and policy gradient
∇θJ=(1,1)⊤. (a) Compare the directions of the vanilla gradient
step and the natural gradient step. (b) Interpret what
F11=100 says about parameter 1, and explain how this mirrors the
(k,σ) example.
Show solution
(a) The vanilla step moves along (1,1) — equal effort in both coordinates. The
natural step moves along
F−1∇J=(1001⋅1,1⋅1)⊤=(0.01,1)⊤
— almost entirely along parameter 2. (b) A large diagonal Fisher entry means small
changes in that parameter produce large changes in the policy’s action probabilities
(the expected squared grad-log-prob in that direction is big). The natural gradient
therefore takes small steps in high-sensitivity directions and large steps in
low-sensitivity ones — per-direction step sizes chosen by distribution geometry rather
than parameter geometry. In the (k,σ) example, σ plays the role of
parameter 1: as σ→0 it dominates the probabilities, the vanilla gradient
pours all its effort into shrinking it further, and k never reaches −1;
premultiplying by F−1 suppresses the σ direction and boosts k,
pointing the update at the optimum.