Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Learning from whole episodes
A REINFORCE agent plays a full game, looks at how well it went, and then makes every move it chose a little more likely if the game went well, or a little less likely if it went badly. Because luck plays a big part in any single game, this signal is noisy. Two simple fixes help a lot: credit each move only with what happened after it, and compare each result with what usually happens instead of with zero.
The same recipe, with a learned value estimate as the comparison point, sits inside the algorithms used to fine-tune chat assistants.
- 1. REINFORCE updates the policy after…
- 2. A move should be credited with…
- 3. Comparing results with what usually happens…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- REINFORCE (Williams 1992): sample episodes with π_θ, then θ ← θ + α Σ_t γ^t G_t ∇log π_θ(A_t|S_t), where G_t is the discounted return from step t.
- Whole-return form: using G_0 on every step is also unbiased, but adds the noise of rewards that came before each action.
- Reward-to-go (causality): an action cannot affect earlier rewards, so only G_t is needed.
- Baseline: subtract any b(S_t) that does not depend on the action A_t (or on anything that action influences): Σ_t γ^t (G_t − b(S_t))∇log π. The mean is unchanged; the variance can drop a lot.
- Common baselines: the batch average return, a leave-one-out average over the other episodes, or a learned value function V(s), which gives actor-critic methods (topic L4-3).
- Score of a sigmoid policy: for π(1|s) = sigmoid(w·φ(s)), ∇_w log π(a|s) = (a − π(1|s))φ(s).
- 1. REINFORCE weights each score by…
- 2. A valid baseline may depend on…
- 3. The score of a sigmoid policy is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.REINFORCE on cart-pole
Exercise 1.
- Cart-pole: push left or right; reward 1 per step; an episode ends when the pole passes 12°, the cart leaves the track, or after 200 steps.
- A linear policy π(right|s) = sigmoid(w·(s, 1)): five parameters.
- Episodes run in parallel; G_t computed backwards; Adam on the gradient estimate.
- Three baselines at θ = 0: none, the batch mean at each step (the episode itself included), and leave-one-out (the other episodes only).
Practice.
- Always use reward-to-go: it is free and never worse.
- Always use a baseline: a learned V(s) is standard; with batches, normalising the advantages to mean 0 and standard deviation 1 is common.
- Keep the baseline independent of the episode it is applied to, or accept a small, known bias.
- Use large batches: the estimate’s noise falls like one over the square root of the number of episodes.
- Adaptive steps help: Adam or gradient clipping keeps the rare huge-return batch from wrecking the policy.
- Tools: Stable-Baselines3 and CleanRL implement REINFORCE-style updates inside A2C and PPO; for teaching, a 50-line NumPy loop like this one is enough.
def reinforce_gradient(F, A, Pr, M, gamma=0.99, baseline="none"):
T = len(M); G = np.zeros_like(M); run = np.zeros(M.shape[1])
for t in range(T - 1, -1, -1):
run = M[t] * (1 + gamma * run); G[t] = run
S, N = (G * M).sum(1, keepdims=True), M.sum(1, keepdims=True)
if baseline == "loo":
G = G - np.where((M > 0) & (N > 1), (S - G * M) / np.maximum(N - 1, 1), 0)
disc = gamma ** np.arange(T)[:, None]
return ((disc * G * M * (A - Pr))[..., None] * F).sum((0, 1)) / M.shape[1]- 1. Reward-to-go is…
- 2. The noise of a batch gradient falls with the batch size n like…
- 3. The standard learned baseline is…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Causality. For t’ < t, the reward r_(t’+1) is fixed once the history up to S_t is fixed, and E[∇log π(A_t|S_t) | history] = 0 (the expected score is zero). So E[r_(t’+1)∇log π(A_t|S_t)] = 0, and dropping the rewards before t from the weight of step t changes nothing in expectation while removing their noise.
Baselines. The same argument works for any b that is fixed given the history up to S_t: E[b ∇log π(A_t|S_t)] = E[b · E[∇log π | history]] = 0.
Self-including baselines are biased. If b_i is the mean of K returns including episode i’s own, then b_i = G_i/K + (others)/K. The other episodes are independent of episode i, so their part has zero mean against episode i’s score, but G_i/K does not: E[(G_i − b_i)·score_i] = (1 − 1/K)E[G_i·score_i]. The gradient shrinks; at step t only the N_t episodes still running enter, so the factor is 1 − 1/N_t, smaller late in the episode. Leaving episode i out, b_i = (Σ_(j≠i) G_j)/(K − 1), removes the bias.
Optimal constant baseline. For one parameter with score s, Var[(R − b)s] = E[s²(R − b)²] − (E[Rs])². Setting the derivative in b to zero gives b* = E[s²R]/E[s²]: the average return weighted by the squared score. It is close to, but not equal to, the mean return.
- 1. Past rewards can be dropped because…
- 2. A batch mean that includes the episode itself…
- 3. The variance-minimising constant baseline weights returns by…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.On cart-pole, how much do reward-to-go and a baseline speed up REINFORCE, and how noisy is each estimator?
History.
- R. J. Williams (1992) introduced REINFORCE, with a reinforcement baseline.
- L. Weaver and N. Tao (2001) derived the optimal baseline for policy-gradient estimates.
- E. Greensmith, P. Bartlett and J. Baxter (2004) analysed variance reduction with baselines and actor-critic methods.
- J. Schulman, P. Moritz, S. Levine, M. Jordan and P. Abbeel (2016) introduced generalised advantage estimation; W. Kool, H. van Hoof and M. Welling (2019) popularised the leave-one-out (“REINFORCE with K samples”) baseline.
Exercise 2.
- Four estimators: the whole return G_0 on every step; reward-to-go γ^t G_t; reward-to-go minus a leave-one-out baseline; and reward-to-go without the γ^t factor (the form most libraries use).
- Four learning runs of each, trained in parallel: 150 batches of 20 episodes, Adam with step 0.03, γ = 0.99.
- Noise at fixed weights: 100 batches of 10 episodes at θ = 0 and at a half-trained θ. Relative spread = √(trace of the covariance) / length of the mean; cosine of a single batch’s estimate with the averaged unbiased gradient.
Seed 0, learning:
| estimator | first batch with mean length ≥ 195 (4 runs) | mean of last 10 batches |
|---|---|---|
| whole return | never, never, never, never | 114.5 |
| reward-to-go | 140, never, never, 145 | 178.6 |
| reward-to-go + leave-one-out baseline | 139, 128, 119, 142 | 189.8 |
| reward-to-go, no γ^t | 130, 130, 146, 139 | 185.7 |
Seed 0, noise of one batch of 10 episodes:
| estimator | relative spread at θ = 0 | cosine at θ = 0 | relative spread, half-trained | cosine, half-trained |
|---|---|---|---|---|
| whole return | 2.79 | 0.298 | 6.82 | 0.086 |
| reward-to-go | 0.91 | 0.762 | 1.36 | 0.599 |
| reward-to-go + baseline | 0.87 | 0.856 | 1.29 | 0.656 |
| reward-to-go, no γ^t | 1.11 | 0.672 | 1.42 | 0.591 |
- The whole return never learned to balance in 12 runs over three seeds (last-10 means 114.5, 142.1 and 123.2). Its relative spread was about three times that of reward-to-go at θ = 0 and five times at the half-trained policy, where long episodes pile up many irrelevant early rewards.
- Reward-to-go learned, but not reliably: 2, 2 and 3 of 4 runs reached 195 within 150 batches on seeds 0, 1 and 2.
- Adding the baseline made all 12 runs reach 195 and gave the highest cosine in every setting. At this small scale its gain over plain reward-to-go is modest, because cart-pole returns are all positive and similar early on; it grows with the spread of returns.
- Dropping γ^t (biased for the discounted objective, since later steps get full weight) learned about as fast as the baseline here, because the goal, balancing for 200 steps, is undiscounted. Libraries use this form for the same reason.
- Exercise 1: at θ = 0 the leave-one-out baseline kept the mean gradient (within 1.3 standard errors) and cut the spread from 9.61 to 8.26; the self-including batch mean cut it to 6.50 but shrank the mean gradient to 0.79 of its size, as the theory predicts for episodes that end at different times. Training with Adam step 0.1, no baseline first reached 195 at batch 59 (last-10 mean 188.6), leave-one-out at batch 52 (199.6).
- Seeds 1 and 2 (last-10 means, whole / reward-to-go / baseline / no γ^t): 142.1 / 184.2 / 192.0 / 188.4 and 123.2 / 185.8 / 189.7 / 185.1. Baseline cosines at θ = 0: 0.900 and 0.880.
Beyond REINFORCE
- Learned critics: replace the Monte Carlo return and baseline with a value network (actor-critic, topic L4-3), trading a little bias for much lower variance.
- Generalised advantage estimation: a tunable mix of one-step and Monte Carlo advantages.
- Trust regions: PPO (topic L4-4) uses these advantages with clipped steps.
- Language models: REINFORCE with a leave-one-out baseline (RLOO) and group-relative baselines (GRPO) fine-tune language models from rewards without a critic.
References
- R. J. Williams (1992). “Simple statistical gradient-following algorithms for connectionist reinforcement learning.” Machine Learning.
- L. Weaver and N. Tao (2001). “The optimal reward baseline for gradient-based reinforcement learning.” UAI.
- E. Greensmith, P. L. Bartlett and J. Baxter (2004). “Variance reduction techniques for gradient estimates in reinforcement learning.” JMLR.
- J. Schulman, P. Moritz, S. Levine, M. Jordan and P. Abbeel (2016). “High-dimensional continuous control using generalized advantage estimation.” ICLR.
- W. Kool, H. van Hoof and M. Welling (2019). “Buy 4 REINFORCE samples, get a baseline for free!” ICLR workshop.