Worked examples — REINFORCE algorithm
Before we start, one reminder of the single rule everything below obeys:
The whole game: the sign of decides the direction, and decides which dials move.
We first need the two policy families that appear, so we build them once — before the matrix names them.

The scenario matrix
Every REINFORCE question is one (or a blend) of these cells. Each example below is tagged with the cell it hits.
| # | Cell | What's special | Example |
|---|---|---|---|
| A | Positive return, discrete action | push chosen action up | Ex 1 |
| B | Negative return, discrete action | push chosen action down | Ex 2 |
| C | Zero / degenerate signal | gradient vanishes — no learning | Ex 3 |
| D | Continuous action (Gaussian) | mean and spread both move | Ex 4 |
| E | Baseline centering | same returns, opposite pushes | Ex 5 |
| F | Reward-to-go vs full return | causality kills past rewards | Ex 6 |
| G | Discounting | far-future reward shrinks | Ex 7 |
| H | Real-world word problem | full multi-step episode end to end | Ex 8 |
| I | Exam twist: baseline that hurts | wrong constant raises variance | Ex 9 |
Example 1 — Cell A: positive return, discrete action
-
Compute the log-gradient for each logit. Using the softmax formula with taken action : Why this step? The update only touches of the action we actually took. The softmax gradient tells us it raises 's score and lowers 's in equal-and-opposite amounts (the magenta and violet arrows in the figure above).
-
Multiply by and , then add to each logit. Why this step? The gradient ascent rule; the positive keeps the sign so goes up.
-
Recompute . Why this step? We want to confirm the probability actually rose, not just the logit.
Example 2 — Cell B: negative return, discrete action
-
Log-gradients are identical to Ex 1 (they depend on the policy, not the reward): . Why this step? The direction of "push up" is baked into ; the reward only supplies a sign multiplier.
-
The negative flips the sign. Why this step? means "do that less," so the chosen action's score drops.
-
Recompute: .
Example 3 — Cell C: zero / degenerate signal
-
Case (i), . Update . No dial moves. Why this step? The reward is the magnitude of the push; a zero reward means "no evidence," so REINFORCE stays put.
-
Case (ii), . Softmax gradient for the taken action: . Why this step? Once an action has probability 1, the log-gradient vanishes — the policy is saturated and cannot learn, even from real reward. This is the "premature determinism" trap: exploration died.
-
Contrast the two zeros. In (i) the signal is zero; in (ii) the gradient is zero. Same result (no update) but different cause. Why this step? Diagnosing which zero you hit tells you the fix: (i) needs a nonzero reward or a baseline that centers; (ii) needs entropy/exploration to keep off the boundary.
-
Why a baseline can rescue case (i) — the formal reason. A single reward of looks like "no evidence" only relative to a baseline of . But the true signal is , not alone. If the average return in this state is (rewards are usually positive here) and this time we got the unusually bad , then — a nonzero, negative signal that correctly says "this action did worse than usual, push it down." So the baseline does not create signal from nothing; it reveals that "" is only uninformative against a zero reference, and informative against the true average. This is why centering is the fix for case (i) but not case (ii) (where the gradient itself is zero and no baseline can revive it). Why this step? To show precisely how subtracting can turn a into a nonzero update — it is the contrast , not , that drives learning.
Example 4 — Cell D: continuous action (Gaussian policy), mean AND spread
-
Compute the -gradient of the log. Why this step? Gaussian log-prob is ; its -slope is . It points from the mean toward the sampled action (the magenta arrow on the bell in the figure above).
-
Apply the -update. Why this step? Positive says "that action was good," so the mean slides toward the good action .
-
Compute the -gradient of the log. Why this step? Cell D promises both parameters move. This term compares how far the action landed from the mean against the current spread .
-
Apply the -update. Why this step? The rewarded action was far from the mean, so REINFORCE widens the spread — it says "good things happen out here too, explore more broadly." Had the rewarded action been close in (), the -gradient would be negative and the spread would shrink, sharpening the policy.
Example 5 — Cell E: baseline centering
-
No baseline. Weights are and — both positive, so both actions get pushed up. Why this step? Raw returns don't say which action is relatively better; both look great.
-
With baseline . Now is pushed up, pushed down. Why this step? Subtracting the average exposes the contrast. This is Variance Reduction in RL and, crucially, it does not bias the gradient (the baseline term has zero expectation).
-
Check the advantages are balanced. Add the two advantages directly: — the baseline redistributed the pushes symmetrically around zero without inventing net bias. Why this step? Summing the advantages (not the raw returns) is the clean sanity check that centering is balanced.
Example 6 — Cell F: reward-to-go vs full return
-
Full return . Naively every action, including , is weighted by 15. Why this step? This is the "clean derivation" form — and it's noisy.
-
Reward-to-go for . . Why this step? Action can only influence rewards from time 1 onward. The was already collected — by causality it carries zero expected credit for , so dropping it lowers variance with no bias.
-
Reward-to-go for . (here it equals full return since is first). Why this step? Confirms reward-to-go and full return agree at and diverge only for later actions.
Example 7 — Cell G: discounting
-
Apply . Why this step? Discounting multiplies each future reward by per step of delay, so the earlier you stand, the more the far reward is shrunk.
-
Compare to . There all three equalled 10; discounting spreads them . Why this step? Shows is a knob on patience: small makes distant rewards nearly invisible to early actions.
-
Effect on updates. 's push is scaled by , 's by — the action nearest the reward gets the strongest credit. Why this step? Ties the number back to learning: discounting focuses credit near the payoff.
Example 8 — Cell H: real-world word problem (full episode, end to end)
-
Reward-to-go with . Only is nonzero: Why this step? Same discounting as Ex 7; only the future matters to each action.
-
Subtract the baseline to get the advantage: Why this step? Centers each step's credit; all three beat the "average day" of 0.5.
-
Write the log-gradient factor for each taken action. For its own logit the softmax gradient is : Why this step? The update is not just the sign of — it is for each taken action's own logit (and for every other logit). We show that full factor here.
-
Assemble each taken-logit update : Why this step? All three : because the episode succeeded, every action rises. Had been , then and reach's logit would drop — that is exactly the "forecast" trap.
Example 9 — Cell I: exam twist — a baseline that hurts
-
Bias check. Any baseline independent of the action leaves the gradient unbiased: . So is still unbiased. Why this step? Reconfirms the theorem from the parent note — bias depends on independence from the action, not on the value.
-
Effective weights. With : every advantage becomes . The estimator now multiplies each by 101 instead of by the centered value . Why this step? The good baseline (the mean) gives advantages — minimal-variance. The bad baseline inflates them to 101, blowing up variance.
-
Conclusion. Correct direction (unbiased) but terrible variance — learning becomes jittery and slow. Why this step? Drives home the real purpose of a baseline: it's a variance tool, and the near-optimal choice is mean return, per Value Function V(s).
Wrap-up recall
Recall Which cell is each question?
Positive , discrete, action goes up ::: Cell A (Ex 1) already, update is zero ::: Cell C (Ex 3, saturation) Gaussian mean slides toward rewarded action, spread widens ::: Cell D (Ex 4) Two positive returns, only one should rise ::: Cell E, baseline (Ex 5) Past reward dropped from ::: Cell F, causality (Ex 6) Far reward shrinks by per step ::: Cell G (Ex 7) Wrong-value baseline stays unbiased but jitters ::: Cell I (Ex 9)
See also: Policy Gradient Methods for the family this belongs to, and Hinglish version.