WHY a policy directly? In continuous or huge action spaces, taking argmaxaQ(s,a) is hard. A parameterized policy handles stochasticity, continuous actions, and gives smooth optimization.
We want ∇θJ(θ). The trouble: the expectation is over a distribution that depends on θ. We can't just push the gradient inside — so we use the log-derivative trick.
Step 1 — Write the expectation as an integral over trajectories.J(θ)=∫pθ(τ)R(τ)dτWhy? An expectation is just a probability-weighted sum/integral of the quantity.
Step 2 — Differentiate; gradient moves inside the integral (linear operator).∇θJ=∫∇θpθ(τ)R(τ)dτWhy?R(τ) doesn't depend on θ (rewards come from the environment), only pθ(τ) does.
Step 3 — The log-derivative trick. Note the identity
∇θpθ(τ)=pθ(τ)∇θlogpθ(τ)Why? Because ∇θlogp=p∇θp — just the chain rule on log. Multiply both sides by p.
Substitute:
∇θJ=∫pθ(τ)∇θlogpθ(τ)R(τ)dτ=Eτ[∇θlogpθ(τ)R(τ)]Why this matters: it turned a gradient-of-an-expectation into an expectation of a gradient, which we can estimate by sampling trajectories (Monte Carlo).
Step 4 — Expand logpθ(τ). The trajectory probability is
pθ(τ)=ρ(s0)∏t=0Tπθ(at∣st)P(st+1∣st,at)
Take log (product → sum):
logpθ(τ)=logρ(s0)+∑tlogπθ(at∣st)+∑tlogP(st+1∣st,at)
Now differentiate w.r.t. θ. The environment terms ρ and Pdo not depend on θ, so their gradients vanish!
∇θlogpθ(τ)=∑t=0T∇θlogπθ(at∣st)Why this is huge: We get the gradient without knowing the transition modelP — REINFORCE is model-free.
Why Gt (reward-to-go) instead of full R(τ)? The action at time t can only affect future rewards. Rewards earned beforet are noise as far as at's credit is concerned. Dropping them keeps the estimator unbiased but lowers variance. This is causality.
Why is this still unbiased? For any baseline that doesn't depend on the action:
Ea∼π[∇θlogπθ(a∣s)b(s)]=b(s)∑a∇θπθ(a∣s)=b(s)∇θ=1a∑πθ(a∣s)=b(s)⋅0=0
So the baseline changes variance but adds zero bias. A common choice: b(st)=V^(st), giving the advantageAt=Gt−V^(st).
Imagine training a dog with treats. You can't tell the dog exactly what to do. So you just watch: whenever it does something and then good stuff happens, you say "do that more!" and whenever bad stuff follows, "do that less!" You strengthen the moves that were followed by rewards. REINFORCE does exactly this for a computer: it makes the actions that led to lots of reward more likely, and the ones that led to little reward less likely. To be fair, it only judges an action by what happened after it (not before), and it compares to an "average day" so it can tell a genuinely good move from just a lucky day.
Dekho, REINFORCE ka core idea bahut simple hai: hum directly ek policyπθ(a∣s) seekhte hain — yaani neural net batata hai ki kis state mein kaunsa action lene ka probability kitna ho. Value function ka jhanjhat nahi, seedha action ki probability tune karte hain. Rule ek line mein: jo action ke baad zyada reward mila, uski probability badha do; jiske baad kam reward mila, uski ghata do. Bilkul kutte ko treat dekar train karne jaisa.
Ab maths thoda: hum J(θ)=E[R(τ)] ko maximize karna chahte hain, lekin expectation khud θ pe depend karta hai. Yahan log-derivative trick kaam aata hai — ∇p=p∇logp — isse gradient-of-expectation, expectation-of-gradient ban jaata hai jise hum trajectories sample karke estimate kar sakte hain. Jab hum logpθ(τ) ko todte hain, toh environment ke transition terms P(s′∣s,a) ka θ se koi lena-dena nahi, isliye woh zero ho jaate hain. Result: sirf ∑t∇logπθ(at∣st)Gt bachta hai — matlab model-free, environment ka model jaanne ki zaroorat hi nahi!
Do practical baatein yaad rakho. Pehli: reward-to-goGt use karo (action ke baad wala reward), kyunki action apne se pehle wale reward ke liye responsible nahi — isse variance kam hota hai bina bias aaye. Doosri: ek baselineb(s) (jaise V(s)) minus kar do, taaki signal center ho jaaye — "average se accha tha ya bura?" Ye baseline gradient ko biased nahi karta (proof: b(s)∇∑aπ=b(s)⋅0), sirf variance calm karta hai. Aur haan — code mein minus sign lagana mat bhoolna, kyunki optimizer minimize karta hai par humein J maximize karna hai (gradient ascent).