5.6.18Machine Learning (Aerospace Applications)

Policy gradient — REINFORCE

1,615 words7 min readdifficulty · medium3 backlinks

WHAT is REINFORCE?

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 θ\theta by gradient ascent — no argmax needed.


HOW: deriving the gradient from scratch

We want θJ(θ)\nabla_\theta J(\theta). The trouble: the expectation is over τ\tau, whose distribution depends on θ\theta. We can't just push the gradient inside the expectation naively.

Step 1 — write the expectation as an integral. Let pθ(τ)p_\theta(\tau) be the probability of trajectory τ\tau: J(θ)=pθ(τ)R(τ)dτ.J(\theta)=\int p_\theta(\tau)\,R(\tau)\,d\tau . Why this step? An expectation is a probability-weighted integral; making it explicit lets us differentiate.

Step 2 — differentiate. Only pθp_\theta depends on θ\theta: θJ=θpθ(τ)R(τ)dτ.\nabla_\theta J=\int \nabla_\theta p_\theta(\tau)\,R(\tau)\,d\tau .

Step 3 — the log-derivative trick. We use the identity θpθ(τ)=pθ(τ)θlogpθ(τ),\nabla_\theta p_\theta(\tau)=p_\theta(\tau)\,\nabla_\theta \log p_\theta(\tau), which is just logf=ff\nabla \log f = \frac{\nabla f}{f} rearranged. Why this step? It re-inserts pθ(τ)p_\theta(\tau) as a weight, turning the integral back 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 4 — expand logpθ(τ)\log p_\theta(\tau). The trajectory probability factorizes: pθ(τ)=p(s0)envt=0Tπθ(atst)policy  p(st+1st,at)env dynamics.p_\theta(\tau)=\underbrace{p(s_0)}_{\text{env}}\prod_{t=0}^{T}\underbrace{\pi_\theta(a_t\mid s_t)}_{\text{policy}}\;\underbrace{p(s_{t+1}\mid s_t,a_t)}_{\text{env dynamics}}. Taking log\log turns products into sums; then θ\nabla_\theta kills every term that doesn't contain θ\theta (the environment terms p(s0)p(s_0) and p(st+1st,at)p(s_{t+1}\mid s_t,a_t)): θ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 a model of the environment. This is the magic of REINFORCE.

Figure — Policy gradient — REINFORCE

Reward-to-go and baselines (the 20% that gives 80%)

Reward-to-go. An action at time tt can't affect rewards that already happened. So replace the full R(τ)R(\tau) with the return from tt onward: θJ=E ⁣[tθlogπθ(atst)  ttγttrtGt].\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}\right]. Why? Removing irrelevant future-independent-of-action reward lowers variance without adding bias.

Baseline. Subtract any function b(st)b(s_t) that doesn't depend on ata_t: θ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]. This is unbiased because Ea[θlogπθ(as)b(s)]=b(s)θ ⁣aπθ(as)=1=0.\mathbb{E}_{a}[\nabla_\theta\log\pi_\theta(a\mid s)\,b(s)]=b(s)\,\nabla_\theta\!\underbrace{\sum_a\pi_\theta(a\mid s)}_{=1}=0. A good baseline is b(s)=V(s)b(s)=V(s); then Gtb(st)G_t-b(s_t) is an advantage estimate.


Worked examples


Common mistakes (steel-manned)


Recall Feynman: explain to a 12-year-old

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π\nabla\log\pi) is just how much to turn each knob up so the good action gets more likely.


Active recall

What objective does REINFORCE maximize?
Expected return J(θ)=Eτπθ[R(τ)]J(\theta)=\mathbb{E}_{\tau\sim\pi_\theta}[R(\tau)].
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|s_t))\,R(\tau)].
What is the log-derivative (score-function) trick?
θpθ=pθθlogpθ\nabla_\theta p_\theta=p_\theta\nabla_\theta\log p_\theta, which turns a gradient of an integral back into a sampleable expectation.
Why is REINFORCE model-free?
logpθ(τ)\log p_\theta(\tau) splits into env terms (no θ\theta) + tlogπθ(atst)\sum_t\log\pi_\theta(a_t|s_t); θ\nabla_\theta kills the env terms, so no transition model is needed.
What is "reward-to-go" and why use it?
Gt=ttγttrtG_t=\sum_{t'\ge t}\gamma^{t'-t}r_{t'}; using it instead of full R(τ)R(\tau) removes reward independent of the action, lowering variance without bias.
Why does subtracting a baseline b(s)b(s) keep the estimate unbiased?
Because Ea[θlogπθ(as)b(s)]=b(s)θaπθ=b(s)θ1=0\mathbb{E}_a[\nabla_\theta\log\pi_\theta(a|s)\,b(s)]=b(s)\nabla_\theta\sum_a\pi_\theta=b(s)\nabla_\theta 1=0.
Is REINFORCE gradient ascent or descent?
Ascent: θθ+αθJ\theta\leftarrow\theta+\alpha\nabla_\theta J (or minimize logπG-\log\pi\cdot G).
For a Gaussian policy, what is μlogπθ(as)\nabla_\mu\log\pi_\theta(a|s)?
(aμ)/σ2(a-\mu)/\sigma^2.
For softmax, what is θklogπ\nabla_{\theta_k}\log\pi of the chosen class kk?
1πθ(k)1-\pi_\theta(k) (and πθ(j)-\pi_\theta(j) for a non-chosen jj).
Main practical weakness of vanilla REINFORCE?
High gradient variance → slow, noisy learning; mitigated by reward-to-go and baselines.

Connections

  • Value-based methods (Q-learning) — the alternative that learns values, not policies.
  • Actor-Critic methods — uses a learned V(s)V(s) as the baseline/critic.
  • Advantage function A(s,a) — what Gtb(st)G_t-b(s_t) estimates.
  • Softmax and log-softmax gradients — reused for discrete policies.
  • Gaussian policies for continuous control — thrust/deflection commands in aerospace.
  • Monte Carlo estimation and variance reduction — why baselines help.
  • Gradient ascent / stochastic optimization — the update engine.

Concept Map

generates

scored by

averaged into

directly optimizes

avoids argmax over actions

written as

differentiate then apply

yields

expand log p_theta

gradient kills

so model-free

keeps policy terms

drives

updates

Policy pi_theta a given s

Trajectory tau rollout

Return R of tau

Objective J theta expected return

Policy gradient approach

J as integral over p_theta

Log-derivative trick

Sampleable expectation

Factorize p_theta of tau

Environment terms dropped

REINFORCE gradient

Gradient ascent on theta

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, REINFORCE ka core idea bilkul dog-training jaisa hai. Agent ke paas ek policy hoti hai πθ(as)\pi_\theta(a\mid s) — yaani state ss dekh kar action aa ka probability distribution. Hum chahte hain expected reward J(θ)J(\theta) maximize karna. Problem yeh hai ki reward ka expectation trajectory τ\tau par hai, aur trajectory ka distribution khud θ\theta par depend karta hai — toh gradient seedha andar nahi ghusa sakte.

Yahan log-derivative trick kaam aata hai: p=plogp\nabla p = p\,\nabla\log p. 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θ(τ)\log p_\theta(\tau) expand karte hain, toh environment ke transition terms θ\theta pe depend nahi karte, isliye θ\nabla_\theta unhe zero kar deta hai. Bache sirf tθlogπθ(atst)\sum_t\nabla_\theta\log\pi_\theta(a_t\mid s_t). Matlab koi environment model nahi chahiye — pure model-free. Final formula: gradient == (log-prob ka gradient) ×\times (return GtG_t), aur phir θ\theta 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)\mu_\theta(s). Agar mean se zyada thrust dene par acha result mila (positive advantage), toh μlogπ=(aμ)/σ2\nabla_\mu\log\pi=(a-\mu)/\sigma^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-go GtG_t use karo, full episode reward nahi — variance kam hota hai, bias same. (2) Baseline b(s)b(s) subtract karo (jaise V(s)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)

Connections