5.2.6 · HinglishDeep & Advanced RL

REINFORCE algorithm

1,834 words8 min readRead in English

5.2.6 · AI-ML › Deep & Advanced RL


HUM kya optimize kar rahe hain?

Policy seedha kyun? Continuous ya bahut bade action spaces mein lena mushkil hai. Ek parameterized policy stochasticity, continuous actions handle karti hai, aur smooth optimization deti hai.


KAISE: Policy Gradient scratch se derive karna

Hum chahte hain . Dikkat yeh hai: expectation ek aisi distribution par hai jo par depend karti hai. Hum gradient andar directly nahin daal sakte — isliye hum log-derivative trick use karte hain.

Step 1 — Expectation ko trajectories par ek integral ki tarah likho. Kyun? Ek expectation bas quantity ka probability-weighted sum/integral hai.

Step 2 — Differentiate karo; gradient integral ke andar jaata hai (linear operator). Kyun? par depend nahin karta (rewards environment se aate hain), sirf karta hai.

Step 3 — Log-derivative trick. Yeh identity note karo: Kyun? Kyunki — bas par chain rule. Dono sides ko se multiply karo.

Substitute karo: Yeh kyun important hai: iska matlab ek expectation-ka-gradient ek gradient-ki-expectation mein badal gaya, jise hum trajectories sample karke estimate kar sakte hain (Monte Carlo).

Step 4 — expand karo. Trajectory probability hai: Log lo (product → sum): Ab ke w.r.t. differentiate karo. Environment ke terms aur par depend nahin karte, isliye unke gradients zero ho jaate hain! Yeh kyun badi baat hai: Hum gradient paate hain transition model jaane bina — REINFORCE model-free hai.

(reward-to-go) kyun, poora kyun nahin? Time par action sirf future rewards ko affect kar sakta hai. se pehle mile rewards ke credit ke nazariye se noise hain. Unhe hatane se estimator unbiased rehta hai lekin variance kam hoti hai. Yeh causality hai.

Figure — REINFORCE algorithm

Baseline: variance reduction

Yeh unbiased kyun hai? Kisi bhi baseline ke liye jo action par depend na kare: Isliye baseline variance change karta hai lekin zero bias add karta hai. Ek common choice: , jo advantage deta hai.


Algorithm (Monte Carlo, episodic)

Initialize θ (policy network), optional value net for baseline
repeat:
    Sample an episode τ = (s0,a0,r0,...,sT) using π_θ
    for t = 0 ... T:
        G_t = Σ_{k=t..T} γ^{k-t} r_k          # reward-to-go
        A_t = G_t - b(s_t)                     # optional baseline
    Loss  = - Σ_t log π_θ(a_t|s_t) * A_t       # minus: grad ASCENT
    θ ← θ + α ∇_θ ( -Loss )                    # via autograd + optimizer
until converged

Minus sign kyun? Optimizers minimize karte hain. Hum maximize karna chahte hain, isliye minimize karte hain.


Worked Examples


Common Mistakes (Steel-manned)


Active Recall

Recall Feynman: 12-saal ke bachche ko samjhao

Socho tum ek kutte ko treats se train kar rahe ho. Tum kutte ko exactly bata nahin sakte kya karna hai. Toh tum bas dekhte ho: jab bhi woh kuch karta hai aur phir achhi cheezein hoti hain, tum kaho "aur karo!" aur jab buri cheezein follow hoti hain, "yeh mat karo!" Tum un moves ko strengthen karte ho jo rewards ke baad aaye. REINFORCE exactly yahi karta hai computer ke liye: jo actions bahut saara reward layi unhe aur likely banata hai, aur jo thoda reward layi unhe kam likely. Fairly ke liye, yeh kisi action ko sirf wahi judge karta hai jo uske baad hua (pehle wala nahin), aur ek "average din" se compare karta hai taaki woh genuinely achhe move aur sirf lucky din mein fark kar sake.


Flashcards

REINFORCE objective kya hai?
Policy ke under expected return, .
REINFORCE policy-gradient formula batao.
.
Log-derivative trick kya hai?
, jo gradient-of-expectation ko expectation-of-gradient mein convert karne ke liye use hota hai.
REINFORCE model-free kyun hai?
mein transition terms aur start-state par depend nahin karte, isliye woh vanish ho jaate hain; sirf bachta hai.
(reward-to-go) kya hai aur ise kyun use karte hain?
; ise use karne se (poore return ki jagah) causally-irrelevant past rewards drop hote hain, variance kam hoti hai aur unbiased rehta hai.
Baseline subtract karna gradient ko bias kyun nahin karta?
Kyunki .
Advantage kya hai?
: ek action ka return state ke average se kitna better tha.
REINFORCE loss mein minus sign kyun?
Optimizers minimize karte hain; hum maximize karna chahte hain, isliye minimize karte hain taaki gradient ascent ho.
Vanilla REINFORCE ki main weakness kya hai?
High variance (Monte-Carlo returns) aur sample inefficiency; help ke liye baselines / Actor-Critic chahiye.

Connections

  • Policy Gradient Methods — REINFORCE iska foundational instance hai.
  • Actor-Critic Methods — Monte-Carlo ko ek learned critic se replace karta hai (baseline + bootstrapping).
  • Advantage Function weighting ke roop mein use hota hai.
  • Value Function V(s) — ek natural baseline choice.
  • Monte Carlo Methods — REINFORCE returns full-episode MC estimates hain.
  • Log-Derivative Trick — core mathematical identity.
  • Softmax Policy / Gaussian Policy ki common parameterizations.
  • Variance Reduction in RL — baselines, reward-to-go, control variates.

Concept Map

maximizes

gradient needs

converts to

estimated by

requires

since P not on theta

implies

yields

feeds

used to

improves

Parameterized policy pi_theta a given s

Objective J theta max expected return

Log-derivative trick

Expectation of a gradient

Monte Carlo sampling of trajectories

Expand log p_theta tau

Env terms rho and P drop out

Model-free

REINFORCE gradient

Nudge theta to raise good actions