Value methods (Q-learning) learn Q(s,a) and pick argmaxaQ. 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 θ changes give small policy changes, which is stable.
Define the objective as expected return of a trajectory τ=(s0,a0,s1,a1,…):
J(θ)=Eτ∼πθ[R(τ)],R(τ)=∑t=0Tγtrt
We do gradient ascent: θ←θ+α∇θJ(θ).
The whole game is: how do we compute ∇θJ when the reward doesn't obviously depend on θ? Reward comes from the environment; θ only shapes which trajectories we sample. This is the trick.
Write the objective as an integral over trajectories, with Pθ(τ) the trajectory probability:
J(θ)=∫Pθ(τ)R(τ)dτ
Step 1 — differentiate.∇θJ=∫∇θPθ(τ)R(τ)dτWhy this step?R(τ) has no θ in it (reward is environment's), so only Pθ is differentiated.
Step 2 — the log-derivative trick. Multiply and divide by Pθ(τ):
∇θPθ=PθPθ∇θPθ=Pθ∇θlogPθ(τ)Why? Because ∇logf=f∇f. This is the crucial identity — it turns the gradient of a probability into an expectation we can sample.
Step 3 — expand logPθ(τ). The trajectory probability factorizes:
Pθ(τ)=initρ(s0)∏tdynamicsP(st+1∣st,at)policyπθ(at∣st)
Take log → sum. The init and dynamics terms have no θ, so they vanish under ∇θ:
∇θlogPθ(τ)=∑t=0T∇θlogπθ(at∣st)Why this matters: we never need the environment's dynamics model! This is model-free.
REINFORCE works but has huge variance. Two principled fixes:
1. Causality (reward-to-go). An action at time t can't affect rewards beforet. So replace the full R(τ) multiplying the t-th term with only future reward:
∇θJ=E∑t∇θlogπθ(at∣st)Gt(reward-to-go)t′≥t∑γt′−trt′
2. Baseline subtraction. Subtract any function b(st) that doesn't depend on the action:
∇θJ=E[∑t∇θlogπθ(at∣st)(Gt−b(st))]Why is this allowed (unbiased)? Because Ea∼π[∇θlogπθ(a∣s)b(s)]=b(s)∇θ∑aπθ(a∣s)=b(s)∇θ1=0. Subtracting zero changes nothing in expectation but slashes variance.
The best baseline is b(s)=V(s), giving the advantageA(s,a)=Gt−V(s)≈Q(s,a)−V(s). This is the seed of Actor-Critic.
What core problem does the log-derivative trick solve in policy gradients?
It converts ∇θPθ(τ) into Pθ(τ)∇θlogPθ(τ), turning the gradient of a probability into a samplable expectation.
Why are environment dynamics irrelevant to the policy gradient?
They contain no θ, so ∇θlogP(s′∣s,a)=0; only ∇θlogπθ survives — making PG model-free.
State the REINFORCE gradient.
∇θJ=Eτ[∑t∇θlogπθ(at∣st)R(τ)].
Why can policy gradients handle continuous actions better than Q-learning?
They parameterize the policy directly and sample actions, avoiding an argmax over an infinite action set.
Why is subtracting a baseline b(s) unbiased?
Because Ea∼π[∇θlogπθ(a∣s)b(s)]=b(s)∇θ∑aπ=b(s)∇θ1=0.
What is the advantage function and why is V(s) the good baseline?
A(s,a)=Q(s,a)−V(s); using b=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 t cannot influence rewards before t, so those terms are irrelevant noise and are dropped.
Ascent or descent for J(θ)?
Gradient ascent — J 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?"
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 πθ(a∣s) ko ek neural net se parameterize karte hain, aur θ 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(τ) me toh θ hai hi nahi, phir gradient kaise nikle? Trick: ∇P=P∇logP. Isse gradient ek expectation ban jaata hai jise hum sampled trajectories se estimate kar lete hain. Aur jab logPθ(τ) ko expand karte hain, toh environment ke dynamics aur initial state wale terms me θ 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 t pe reward past ko affect nahi kar sakta, sirf future reward count karo. (2) Baselineb(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), jisse hume advantageA=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.