Lecture 05 · Part 1

Policy Gradients


The first actual RL algorithm of the course. Levine differentiates the RL objective directly — one identity turns an intractable gradient into something you can estimate from rollouts — then spends the rest of the lecture on why the naive estimator barely works and what fixes it: causality and reward-to-go, baselines, importance sampling for off-policy data, the pseudo-loss trick for autodiff, and the natural gradient.

The objective, and how to evaluate it from rollouts

Policy gradients are “in some ways kind of the simplest reinforcement learning algorithm”: directly differentiate the RL objective and do gradient ascent on the policy parameters. The policy πθ(atst)\pi_\theta(a_t \mid s_t) — a network with weights θ\theta — induces, together with the unknown dynamics, a trajectory distribution by the chain rule:

pθ(τ)=p(s1)t=1Tπθ(atst)p(st+1st,at)p_\theta(\tau) = p(s_1) \prod_{t=1}^{T} \pi_\theta(a_t \mid s_t)\, p(s_{t+1} \mid s_t, a_t)

with τ=(s1,a1,,sT,aT)\tau = (s_1, a_1, \ldots, s_T, a_T). The objective is J(θ)=Eτpθ(τ)[tr(st,at)]J(\theta) = \mathbb{E}_{\tau \sim p_\theta(\tau)}\left[\sum_t r(s_t, a_t)\right], and this lecture works with the finite-horizon version.

Crucially, model-free RL never assumes we know p(s1)p(s_1) or the transition probabilities — only that we can run the policy, which samples from them. That is already enough to evaluate J(θ)J(\theta): roll out NN trajectories and average their total rewards. The whole game is doing the same for the derivative.

One identity makes the gradient estimable

Write r(τ)r(\tau) for the total reward of a trajectory, so J(θ)=pθ(τ)r(τ)dτJ(\theta) = \int p_\theta(\tau)\, r(\tau)\, d\tau and θJ(θ)=θpθ(τ)r(τ)dτ\nabla_\theta J(\theta) = \int \nabla_\theta p_\theta(\tau)\, r(\tau)\, d\tau — useless as written, since θpθ(τ)\nabla_\theta p_\theta(\tau) touches the unknown dynamics. Then comes what Levine calls “basically the only piece of mathematical cleverness in this whole derivation”:

What the gradient is actually doing

For a discrete policy, θlogπθ\nabla_\theta \log \pi_\theta is exactly the gradient you’d compute in supervised maximum likelihood — except maximum likelihood raises the log probability of every observed action, while the policy gradient weights each trajectory’s log-likelihood gradient by its reward. High-reward trajectories get their probabilities pushed up; low-reward ones get pushed down.

For continuous actions, take πθ(atst)=N(fNN(st),Σ)\pi_\theta(a_t \mid s_t) = \mathcal{N}(f_{\text{NN}}(s_t), \Sigma); the log-probability is a quadratic in f(st)atf(s_t) - a_t, and its gradient 12Σ1(f(st)at)dfdθ-\tfrac{1}{2}\Sigma^{-1}(f(s_t) - a_t)\frac{df}{d\theta} backpropagates through the mean network. And a notable freebie: the derivation never used the Markov property, so the same equation holds verbatim in partially observed settings — replace sts_t with oto_t and run it.

The catch is variance. With three samples and rewards drawn as bars on a number line, where the distribution moves depends alarmingly on things that shouldn’t matter: add a constant to every reward — which provably changes no optimal policy — and a finite-sample gradient estimate moves differently. In expectation the estimator is always correct; for any finite NN it is noisy enough that “a lot of advances in policy gradient algorithms basically revolve around different ways to reduce their variance.”

Two variance reducers: causality and baselines

Causality. Distribute the reward sum into the grad-log-pi sum and each action’s term multiplies rewards from all time steps — including the past, which the action provably cannot affect (“this is not an assumption… the only way this would not be true is if you had time travel”). Those past-reward terms cancel in expectation, so drop them:

θJ(θ)1Ni=1Nt=1Tθlogπθ(ai,tsi,t)Q^i,t,Q^i,t=t=tTr(si,t,ai,t)\nabla_\theta J(\theta) \approx \frac{1}{N}\sum_{i=1}^{N}\sum_{t=1}^{T} \nabla_\theta \log \pi_\theta(a_{i,t} \mid s_{i,t})\, \hat{Q}_{i,t}, \qquad \hat{Q}_{i,t} = \sum_{t'=t}^{T} r(s_{i,t'}, a_{i,t'})

Q^i,t\hat{Q}_{i,t} is the reward-to-go — a single-sample estimate of the Q-function, foreshadowing actor-critic. Fewer terms in the sum means smaller numbers means lower variance; the estimator stays unbiased. You always use this.

Baselines. Subtract bb from the reward before weighting, so better-than-average trajectories go up and worse-than-average go down regardless of whether raw rewards are all positive:

Going off-policy with importance sampling

Policy gradients are the classic on-policy algorithm: the expectation is under pθ(τ)p_\theta(\tau), so every gradient step demands fresh samples — painful when deep nets need many small steps and samples are expensive. Importance sampling relaxes this. For samples from any pˉ(τ)\bar{p}(\tau):

Exp[f(x)]=Exq ⁣[p(x)q(x)f(x)]\mathbb{E}_{x \sim p}[f(x)] = \mathbb{E}_{x \sim q}\!\left[\frac{p(x)}{q(x)} f(x)\right]

and in the trajectory ratio the unknown initial-state and transition terms cancel, leaving only the product of policy ratios tπθ(atst)πθ(atst)\prod_t \frac{\pi_{\theta'}(a_t \mid s_t)}{\pi_\theta(a_t \mid s_t)} — computable. The trouble: a product of TT numbers each below 1 shrinks exponentially, so the importance weights blow variance up “exponentially fast.” The escape hatch is to write the weight over state–action marginals and ignore the state-marginal ratio, keeping only the current action’s ratio. That’s no longer the exact gradient, but the error is bounded when θ\theta' stays close to θ\theta — the insight behind practical algorithms like TRPO and PPO, developed properly in the advanced policy gradients lecture.

Implementing it: the pseudo-loss trick

Naively computing θlogπθ\nabla_\theta \log \pi_\theta per state–action pair means ~10,000 million-entry vectors. Instead, give your autodiff package a graph whose gradient is the policy gradient:

Practice notes, verbatim spirit: the gradient is far noisier than supervised learning, so batch sizes run in the thousands to tens of thousands; learning rates are hard, Adam is “okay-ish” and the right starting point; budget for much more hyperparameter tuning than supervised learning has taught you to expect.

Why step size is broken, and the natural gradient

Gradient ascent is really a constrained problem: maximize the linearized objective within an ϵ\epsilon-ball — but that ball lives in parameter space, and parameters affect the policy unevenly. Constrain instead in distribution space with a parameterization- independent divergence: require DKL(πθπθ)ϵD_{KL}(\pi_{\theta'} \| \pi_\theta) \le \epsilon. The second-order expansion of the KL is a quadratic form under the Fisher information matrix F=Eπθ ⁣[logπθlogπθ]\mathbf{F} = \mathbb{E}_{\pi_\theta}\!\left[\nabla \log \pi_\theta\, \nabla \log \pi_\theta^\top\right] — estimable from the same samples — and the update becomes

θ=θ+αF1θJ(θ)\theta' = \theta + \alpha\, \mathbf{F}^{-1} \nabla_\theta J(\theta)

the natural gradient. With the preconditioner in place the vector field points at the optimum. Natural policy gradient picks α\alpha; TRPO picks ϵ\epsilon and derives α\alpha, using conjugate gradient to get F1J\mathbf{F}^{-1}\nabla J without forming F\mathbf{F}.

Check yourself

Derivation-style exercises in the lecture’s spirit — work them on paper.

Problem 5.1 log-gradient derivation

Derive, from scratch, why θJ(θ)=Epθ(τ)[θlogpθ(τ)r(τ)]\nabla_\theta J(\theta) = \mathbb{E}_{p_\theta(\tau)}[\nabla_\theta \log p_\theta(\tau)\, r(\tau)] requires no knowledge of the dynamics, starting from J(θ)=pθ(τ)r(τ)dτJ(\theta) = \int p_\theta(\tau) r(\tau) d\tau. Point to the exact step where the transition probabilities disappear.

Show solution

Push θ\nabla_\theta inside the integral, then apply θpθ=pθθlogpθ\nabla_\theta p_\theta = p_\theta \nabla_\theta \log p_\theta to get pθ(τ)θlogpθ(τ)r(τ)dτ\int p_\theta(\tau)\, \nabla_\theta \log p_\theta(\tau)\, r(\tau)\, d\tau, an expectation under pθp_\theta. Expanding logpθ(τ)=logp(s1)+t[logπθ(atst)+logp(st+1st,at)]\log p_\theta(\tau) = \log p(s_1) + \sum_t [\log \pi_\theta(a_t \mid s_t) + \log p(s_{t+1} \mid s_t, a_t)], the dynamics disappear at the differentiation step: θlogp(s1)=0\nabla_\theta \log p(s_1) = 0 and θlogp(st+1st,at)=0\nabla_\theta \log p(s_{t+1} \mid s_t, a_t) = 0 because neither depends on θ\theta. Only tθlogπθ(atst)\sum_t \nabla_\theta \log \pi_\theta(a_t \mid s_t) survives, and the dynamics now appear only in the sampling distribution — handled by rolling out the policy.

Problem 5.2 baseline variance

A one-step problem has two trajectories under the current policy: probability 0.5 each, rewards 101 and 99, with θlogπθ=+g\nabla_\theta \log \pi_\theta = +g for the first and g-g for the second. Compute the single-sample gradient estimator’s variance with baseline b=0b=0 and with b=100b = 100 (the mean reward). What happens with b=99b = 99?

Show solution

With b=0b=0: the estimator is +101g+101g or 99g-99g with equal probability; mean =g= g. Variance =E[X2]g2=12(1012+992)g2g2=10000g2= \mathbb{E}[X^2] - g^2 = \tfrac{1}{2}(101^2 + 99^2)g^2 - g^2 = 10000\,g^2. With b=100b=100: estimator is +g+g or g-g… mean =g12(1)+...= g \cdot \tfrac{1}{2}(1) + ... — careful: values are (101100)g=g(101-100)g = g and (99100)g=g-(99-100)g = g; the estimator is always gg, variance 00, mean unchanged at gg. With b=99b = 99: values 2g2g and 00, mean still gg, variance 12(4g2)g2=g2\tfrac{1}{2}(4g^2) - g^2 = g^2. Same expectation every time — exactly the unbiasedness theorem — but variance swings from 104g210^4 g^2 to 00 depending on bb.

Problem 5.3 importance-weight blowup

Your new policy agrees with the old one everywhere except it assigns ratio πθ/πθ=0.9\pi_{\theta'}/\pi_\theta = 0.9 at every one of T=100T = 100 time steps. What is the trajectory importance weight, and what does it do to the effective sample size? Why does keeping only the per-step action ratio avoid this?

Show solution

The full weight is 0.91002.7×1050.9^{100} \approx 2.7 \times 10^{-5}. With weights that small on every sample (and occasionally enormous ones when ratios exceed 1), the variance of the weighted estimator explodes — effectively almost none of your samples carry information, which is the “variance goes to infinity exponentially fast” failure. Writing the gradient over state–action marginals and dropping the state-marginal ratio leaves a single factor of 0.90.9 per term rather than a product over all past steps, so the weight no longer decays exponentially in TT. The price is bias — acceptable, with bounded error, only while θ\theta' stays close to θ\theta, which is the constraint TRPO/PPO enforce.

Problem 5.4 natural gradient intuition

In the (k,σ)(k, \sigma) example, the vanilla gradient’s σ\sigma-component dominates as σ0\sigma \to 0. Explain in one or two sentences per item: (a) why this happens, (b) why no single learning rate fixes it, and (c) what property of the KL constraint makes the natural gradient immune.

Show solution

(a) The Gaussian log-density scales as 1/σ21/\sigma^2, so /σ\partial/\partial\sigma terms grow like 1/σ31/\sigma^31/σ41/\sigma^4 while /k\partial/\partial k stays moderate — after normalization the σ\sigma direction swallows the step. (b) A rate small enough to keep the σ\sigma updates stable makes progress in kk negligible; a rate that moves kk overshoots in σ\sigma — the conditioning problem lives in the geometry, not the scalar. (c) The KL divergence measures change between distributions, independent of parameterization; expanding it gives the Fisher matrix as the local metric, and F1\mathbf{F}^{-1} rescales each direction by how much it actually changes the policy — big-effect parameters get small steps and vice versa, which is exactly the per-parameter learning rate you wanted.