5.2.5Deep & Advanced RL

Policy gradient methods

1,605 words7 min readdifficulty · medium6 backlinks

WHY do we even want policy gradients?

Value methods (Q-learning) learn Q(s,a)Q(s,a) and pick argmaxaQ\arg\max_a Q. This breaks when:

  • Actions are continuous — you can't argmax over an infinite set easily.
  • The optimal policy is stochastic — e.g. rock-paper-scissors, or partially observed states where the best move is genuinely random. A greedy value policy is deterministic.
  • You want smooth improvement — small θ\theta changes give small policy changes, which is stable.

WHAT are we optimizing?

Define the objective as expected return of a trajectory τ=(s0,a0,s1,a1,)\tau = (s_0,a_0,s_1,a_1,\dots):

J(θ)=Eτπθ[R(τ)],R(τ)=t=0TγtrtJ(\theta) = \mathbb{E}_{\tau\sim\pi_\theta}\big[R(\tau)\big], \qquad R(\tau)=\sum_{t=0}^{T}\gamma^t r_t

We do gradient ascent: θθ+αθJ(θ)\theta \leftarrow \theta + \alpha\,\nabla_\theta J(\theta).

The whole game is: how do we compute θJ\nabla_\theta J when the reward doesn't obviously depend on θ\theta? Reward comes from the environment; θ\theta only shapes which trajectories we sample. This is the trick.


HOW: deriving the Policy Gradient Theorem from scratch

Write the objective as an integral over trajectories, with Pθ(τ)P_\theta(\tau) the trajectory probability:

J(θ)=Pθ(τ)R(τ)dτJ(\theta) = \int P_\theta(\tau)\,R(\tau)\,d\tau

Step 1 — differentiate. θJ=θPθ(τ)R(τ)dτ\nabla_\theta J = \int \nabla_\theta P_\theta(\tau)\,R(\tau)\,d\tau Why this step? R(τ)R(\tau) has no θ\theta in it (reward is environment's), so only PθP_\theta is differentiated.

Step 2 — the log-derivative trick. Multiply and divide by Pθ(τ)P_\theta(\tau): θPθ=PθθPθPθ=PθθlogPθ(τ)\nabla_\theta P_\theta = P_\theta \frac{\nabla_\theta P_\theta}{P_\theta} = P_\theta\,\nabla_\theta \log P_\theta(\tau) Why? Because logf=ff\nabla \log f = \frac{\nabla f}{f}. This is the crucial identity — it turns the gradient of a probability into an expectation we can sample.

θJ=Pθ(τ)θlogPθ(τ)R(τ)dτ=Eτ ⁣[θlogPθ(τ)R(τ)]\nabla_\theta J = \int P_\theta(\tau)\,\nabla_\theta\log P_\theta(\tau)\,R(\tau)\,d\tau = \mathbb{E}_{\tau}\!\big[\nabla_\theta\log P_\theta(\tau)\,R(\tau)\big]

Step 3 — expand logPθ(τ)\log P_\theta(\tau). The trajectory probability factorizes: Pθ(τ)=ρ(s0)inittP(st+1st,at)dynamicsπθ(atst)policyP_\theta(\tau) = \underbrace{\rho(s_0)}_{\text{init}} \prod_{t}\underbrace{P(s_{t+1}\mid s_t,a_t)}_{\text{dynamics}}\,\underbrace{\pi_\theta(a_t\mid s_t)}_{\text{policy}} Take log → sum. The init and dynamics terms have no θ\theta, so they vanish under θ\nabla_\theta: θlogPθ(τ)=t=0Tθlogπθ(atst)\nabla_\theta\log P_\theta(\tau) = \sum_{t=0}^{T}\nabla_\theta\log\pi_\theta(a_t\mid s_t) Why this matters: we never need the environment's dynamics model! This is model-free.


Reducing variance: causality + baseline

REINFORCE works but has huge variance. Two principled fixes:

1. Causality (reward-to-go). An action at time tt can't affect rewards before tt. So replace the full R(τ)R(\tau) multiplying the tt-th term with only future reward: θJ=E ⁣[tθlogπθ(atst)  ttγttrtGt (reward-to-go)]\nabla_\theta J = \mathbb{E}\!\left[\sum_t \nabla_\theta\log\pi_\theta(a_t\mid s_t)\;\underbrace{\sum_{t'\ge t}\gamma^{t'-t}r_{t'}}_{G_t\ \text{(reward-to-go)}}\right]

2. Baseline subtraction. Subtract any function b(st)b(s_t) that doesn't depend on the action: θJ=E ⁣[tθlogπθ(atst)(Gtb(st))]\nabla_\theta J = \mathbb{E}\!\left[\sum_t \nabla_\theta\log\pi_\theta(a_t\mid s_t)\,\big(G_t - b(s_t)\big)\right] Why is this allowed (unbiased)? Because Eaπ[θlogπθ(as)b(s)]=b(s)θ ⁣aπθ(as)=b(s)θ1=0\mathbb{E}_{a\sim\pi}[\nabla_\theta\log\pi_\theta(a\mid s)\,b(s)] = b(s)\,\nabla_\theta\!\sum_a\pi_\theta(a\mid s)=b(s)\,\nabla_\theta 1 = 0. Subtracting zero changes nothing in expectation but slashes variance.

The best baseline is b(s)=V(s)b(s)=V(s), giving the advantage A(s,a)=GtV(s)Q(s,a)V(s)A(s,a)=G_t-V(s)\approx Q(s,a)-V(s). This is the seed of Actor-Critic.


Steel-manned mistakes


Flashcards

What core problem does the log-derivative trick solve in policy gradients?
It converts θPθ(τ)\nabla_\theta P_\theta(\tau) into Pθ(τ)θlogPθ(τ)P_\theta(\tau)\nabla_\theta\log P_\theta(\tau), turning the gradient of a probability into a samplable expectation.
Why are environment dynamics irrelevant to the policy gradient?
They contain no θ\theta, so θlogP(ss,a)=0\nabla_\theta\log P(s'\mid s,a)=0; only θlogπθ\nabla_\theta\log\pi_\theta survives — making PG model-free.
State the REINFORCE gradient.
θJ=Eτ[tθlogπθ(atst)R(τ)]\nabla_\theta J=\mathbb{E}_\tau[\sum_t \nabla_\theta\log\pi_\theta(a_t\mid s_t)\,R(\tau)].
Why can policy gradients handle continuous actions better than Q-learning?
They parameterize the policy directly and sample actions, avoiding an argmax\arg\max over an infinite action set.
Why is subtracting a baseline b(s)b(s) unbiased?
Because Eaπ[θlogπθ(as)b(s)]=b(s)θaπ=b(s)θ1=0\mathbb{E}_{a\sim\pi}[\nabla_\theta\log\pi_\theta(a\mid s)\,b(s)]=b(s)\nabla_\theta\sum_a\pi=b(s)\nabla_\theta 1=0.
What is the advantage function and why is V(s)V(s) the good baseline?
A(s,a)=Q(s,a)V(s)A(s,a)=Q(s,a)-V(s); using b=Vb=V measures how much better an action is than average, minimizing variance.
Why "reward-to-go" instead of full return per term?
Causality: an action at time tt cannot influence rewards before tt, so those terms are irrelevant noise and are dropped.
Ascent or descent for J(θ)J(\theta)?
Gradient ascent — JJ is a return we maximize.

Recall Feynman: explain to a 12-year-old

Imagine training a dog with treats. You don't tell the dog exactly how to move its legs. You just make the moves that got treats happen more often and the bad moves happen less often. The dog is the policy; the treat is the reward. Policy gradients = "did that trajectory get a big treat? Then make all the choices you made in it a little more likely." The math (log-derivative trick) is just the careful bookkeeping for "how do I nudge the dice so good rolls come up more?"


Connections

  • Actor-Critic methods — baseline becomes a learned critic VϕV_\phi.
  • REINFORCE algorithm — the direct Monte-Carlo estimator.
  • Advantage function and Generalized Advantage Estimation.
  • Trust Region and PPO — control step size for stability.
  • Value-based methods (Q-learning) — the alternative family PG contrasts with.
  • Log-derivative trick / Score function estimator — same idea across ML.
  • Variance reduction in Monte Carlo.

Concept Map

fail on

motivate

parameterize

maximize

gradient ascent

differentiate

turns grad into

factorize trajectory

dynamics vanish

yields

feeds

Value methods Q-learning

Continuous or stochastic needs

Policy gradient methods

Policy pi_theta a given s

Objective J = E of return

Update theta + alpha grad J

Log-derivative trick

Sampleable expectation

Init x dynamics x policy

Model-free result

Policy Gradient Theorem REINFORCE

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Q-learning me hum har action ki value seekhte hain aur phir sabse achha action argmax se choose karte hain. Problem tab aati hai jab actions continuous ho ya best policy random ho — tab argmax kaam nahi karta. Policy gradient me hum seedha policy πθ(as)\pi_\theta(a\mid s) ko ek neural net se parameterize karte hain, aur θ\theta ko aise adjust karte hain ki jo actions zyada reward de rahe hain, wo actions zyada probable ban jaayein. Bilkul dog training jaisa — treat wale moves ko zyada baar karvao.

Ab asli jaadu hai log-derivative trick. Reward R(τ)R(\tau) me toh θ\theta hai hi nahi, phir gradient kaise nikle? Trick: P=PlogP\nabla P = P\,\nabla\log P. Isse gradient ek expectation ban jaata hai jise hum sampled trajectories se estimate kar lete hain. Aur jab logPθ(τ)\log P_\theta(\tau) ko expand karte hain, toh environment ke dynamics aur initial state wale terms me θ\theta hai hi nahi — wo gradient me gayab ho jaate hain. Isliye policy gradient model-free hai: hume environment ka model chahiye hi nahi.

Ek dikkat hai — REINFORCE ka gradient bahut noisy (high variance) hota hai. Do smart fixes: (1) reward-to-go — kyunki action time tt pe reward past ko affect nahi kar sakta, sirf future reward count karo. (2) Baseline b(s)b(s) minus karo — ye mathematically zero add karta hai (unbiased rehta hai) par variance ko kaafi kam kar deta hai. Best baseline hai V(s)V(s), jisse hume advantage A=QVA=Q-V milta hai. Yahi se Actor-Critic ka concept nikalta hai. Yaad rakho: yahan hum gradient ascent karte hain, kyunki reward maximize karna hai, minimize nahi.

Go deeper — visual, from zero

Test yourself — Deep & Advanced RL

Connections