5.2.6 · D4Deep & Advanced RL

Exercises — REINFORCE algorithm

2,292 words10 min readBack to topic

Level 1 — Recognition

Recall Solution

Answer: (b). Why (b): the log-derivative trick turns into , so the surviving factor is — the log, not the bare probability. That rules out (a). Why not (c): the weight is the return following , , not the single-step reward . An action is credited with everything that comes after it, not just the immediate payoff.

Recall Solution

Only survives. (start-state distribution) and (environment transitions) come from the world, not from . Their gradient w.r.t. is exactly zero. This is precisely why REINFORCE is model-free.


Level 2 — Application

Recall Solution

Work backwards using (cheaper than re-summing each time).

  • .
  • .
  • .
  • .

Answers: . Why backwards: each reuses the already-computed , so the whole sweep is instead of .

Recall Solution

Write . Differentiate w.r.t. : Why this shape is beautiful: if we sampled and got positive advantage, the update pushes hardest when is small (lots of room to grow) and gently when is already near 1. Self-limiting, no manual clipping needed.

Recall Solution

Update rule: . From L2.2, . Answer: (and unchanged at ). New — nudged up, exactly as the positive return demands.


Level 3 — Analysis

Recall Solution

Pull out (it doesn't depend on ) and convert the expectation to a sum, then run the log-derivative trick backwards: Swap sum and gradient (finite sum, linear): The key fact: probabilities sum to , a constant, whose gradient is . This is the whole reason baselines are "free" — variance drops, bias stays zero. See figure.

Figure — REINFORCE algorithm
Recall Solution
  • No baseline: weights are and . Both positive — both log-probs pushed up. The learner must resolve which is better from a tiny vs difference riding on huge magnitudes → high variance, slow.
  • With : advantages are , . Now is pushed up, pushed down explicitly. The contrast is exposed; magnitudes are small and centered.

Why it works: the baseline removes the common-mode offset () that carried no information about which action was better. See figure.

Figure — REINFORCE algorithm
Recall Solution

Full return is . The naive estimator would multiply by ; reward-to-go multiplies by , dropping the earned before . The past reward is a constant with respect to 's distribution, so — by exactly the L3.1 argument with — its expected contribution to is zero. Removing it changes nothing in expectation but shrinks the weight's variance. Answer: ; unbiased, lower variance. ✅


Level 4 — Synthesis

Recall Solution

The second term has no , so it drops. Differentiate the first w.r.t. : Interpretation of sign: if the sampled action was above the mean () and the advantage was positive, the update moves toward (upward) — "we liked doing more than average, so aim higher." If , it pulls down. The magnitude scales the surprise by how tight the policy is.

Recall Solution

Why the sum: the gradient is a sum over timesteps, and is linear, so summing the terms before differentiating gives the right total. Why is treated as a constant: in REINFORCE the advantage weight is a number computed from the rollout, not differentiated through — autograd sees it as a coefficient. Why the leading minus: optimizers minimize. We want to maximize , i.e. ascend. Minimizing = ascending . Drop the minus and you'd descend, making good actions less likely.

Recall Solution
  • Advantage of bootstrapping: using instead of the full sampled dramatically lowers variance (one random reward + a learned estimate, versus a long noisy sum). It also allows online, per-step updates instead of waiting for the episode to end.
  • Disadvantage: it introduces bias, because is an imperfect estimate; the update is only as correct as the critic. REINFORCE's Monte-Carlo is unbiased but high-variance. This is the classic bias–variance trade-off of policy gradient methods.

Level 5 — Mastery

Recall Solution
  1. Write expectation as an integral: . Tool: definition of expectation — a probability-weighted integral.
  2. Push gradient inside: . Tool: linearity of the gradient; is -free (rewards come from the environment).
  3. Log-derivative trick: . Why: converts into something that keeps the factor , so the integral becomes an expectation again → samplable. Gives .
  4. Factor the trajectory probability: ; take log (product→sum), differentiate. Tool: turns products into sums; environment terms are -free so vanish → . This is where model-freeness appears.
  5. Apply causality: an action at cannot influence rewards before ; those contribute zero in expectation (L3.1 argument), so replace by the reward-to-go for each term. Result:
Recall Solution

Baseline . Advantages: , . Start . Gradients (from L2.2 shapes):

  • Sampled : ; .
  • Sampled : ; .

Averaged gradient (weight each by its advantage, divide by ):

Update : Answer: , . The rewarding action climbed, fell — and thanks to the baseline the two moved by equal and opposite amounts, cleanly separating the winner.


Recall Self-test checklist

Which fact justifies dropping from the gradient? ::: does not depend on , so → model-free. What is the exponent inside the reward-to-go ? ::: (relative to the current step, not absolute). What single property makes a baseline unbiased? ::: it must not depend on the action . Why the leading minus sign in the loss? ::: optimizers minimize; minimizing performs gradient ascent on .