WHY policy gradients at all? In value-based methods (Q-learning) you learn values and act greedily. But for continuous or high-dimensional action spaces (e.g. an aircraft's control-surface deflections), maximizing over actions is painful. Policy methods directly parameterize the action distribution and optimize θ by gradient ascent — no argmax needed.
We want ∇θJ(θ). The trouble: the expectation is over τ, whose distribution depends on θ. We can't just push the gradient inside the expectation naively.
Step 1 — write the expectation as an integral. Let pθ(τ) be the probability of trajectory τ:
J(θ)=∫pθ(τ)R(τ)dτ.Why this step? An expectation is a probability-weighted integral; making it explicit lets us differentiate.
Step 2 — differentiate. Only pθ depends on θ:
∇θJ=∫∇θpθ(τ)R(τ)dτ.
Step 3 — the log-derivative trick. We use the identity
∇θpθ(τ)=pθ(τ)∇θlogpθ(τ),
which is just ∇logf=f∇f rearranged. Why this step? It re-inserts pθ(τ) as a weight, turning the integral back into an expectation we can sample:
∇θJ=∫pθ(τ)∇θlogpθ(τ)R(τ)dτ=Eτ[∇θlogpθ(τ)R(τ)].
Step 4 — expand logpθ(τ). The trajectory probability factorizes:
pθ(τ)=envp(s0)∏t=0Tpolicyπθ(at∣st)env dynamicsp(st+1∣st,at).
Taking log turns products into sums; then ∇θkills every term that doesn't contain θ (the environment terms p(s0) and p(st+1∣st,at)):
∇θlogpθ(τ)=∑t=0T∇θlogπθ(at∣st).Why this matters:we never need a model of the environment. This is the magic of REINFORCE.
Reward-to-go. An action at time t can't affect rewards that already happened. So replace the full R(τ) with the return from t onward:
∇θJ=E∑t∇θlogπθ(at∣st)Gtt′≥t∑γt′−trt′.Why? Removing irrelevant future-independent-of-action reward lowers variance without adding bias.
Baseline. Subtract any function b(st) that doesn't depend on at:
∇θJ=E[∑t∇θlogπθ(at∣st)(Gt−b(st))].
This is unbiased because Ea[∇θlogπθ(a∣s)b(s)]=b(s)∇θ=1a∑πθ(a∣s)=0. A good baseline is b(s)=V(s); then Gt−b(st) is an advantage estimate.
Imagine training a dog. You can't tell it exactly what to do — you just let it try things. When it does a trick and you give a treat, it becomes a bit more likely to do that trick again. REINFORCE is the same: the robot tries random actions, and whichever actions came before a big reward get "voted up" a little. Do this thousands of times and the robot's random guessing slowly turns into skill. The clever math bit (∇logπ) is just how much to turn each knob up so the good action gets more likely.
Dekho, REINFORCE ka core idea bilkul dog-training jaisa hai. Agent ke paas ek policy hoti hai πθ(a∣s) — yaani state s dekh kar action a ka probability distribution. Hum chahte hain expected reward J(θ) maximize karna. Problem yeh hai ki reward ka expectation trajectory τ par hai, aur trajectory ka distribution khud θ par depend karta hai — toh gradient seedha andar nahi ghusa sakte.
Yahan log-derivative trick kaam aata hai: ∇p=p∇logp. Iski wajah se gradient wapas ek expectation ban jaata hai jise hum samples se estimate kar sakte hain. Aur sabse mast baat — jab hum logpθ(τ) expand karte hain, toh environment ke transition terms θ pe depend nahi karte, isliye ∇θ unhe zero kar deta hai. Bache sirf ∑t∇θlogπθ(at∣st). Matlab koi environment model nahi chahiye — pure model-free. Final formula: gradient = (log-prob ka gradient) × (return Gt), aur phir θ ko us direction mein upar le jao (ascent).
Aerospace context mein socho: ek controller jo thrust ya control-surface deflection command karta hai. Gaussian policy use karo — action ka mean μθ(s). Agar mean se zyada thrust dene par acha result mila (positive advantage), toh ∇μlogπ=(a−μ)/σ2 mean ko us achhe action ki taraf shift kar deta hai. Dheere-dheere random guessing skill ban jaati hai.
Do practical tips yaad rakho: (1) reward-to-goGt use karo, full episode reward nahi — variance kam hota hai, bias same. (2) Baselineb(s) subtract karo (jaise V(s)) — yeh unbiased rehta hai kyunki uska expected contribution zero hai, par variance aur kam. Bas yahi 20% concepts 80% performance dila dete hain.
Test yourself — Machine Learning (Aerospace Applications)