REINFORCE and the Policy-Gradient Theorem (Williams 1992; Sutton et al. 1999)
Read first: Reinforcement Learning: An Introduction, Ch. 5–6 — Monte Carlo, Temporal-Difference Learning and Q-learning
TL;DR. Instead of learning values and acting greedily, policy-gradient methods adjust the policy’s parameters directly to increase expected return. The log-derivative trick turns the gradient of an expectation into an expectation you can sample: run the policy, then push up the log-probability of each action in proportion to how good the outcome was. Williams (1992) named this family REINFORCE and proved that it follows the gradient in expectation. Subtracting a baseline that does not depend on the action keeps the estimate unbiased and can cut its variance a lot. Sutton et al. (1999) proved the policy-gradient theorem: the gradient needs action values and the states the policy visits, but not the derivative of the state distribution.
Why it matters now. Every LLM RL method on this site, from PPO in InstructGPT to GRPO, RLOO and DAPO, is a descendant of this rule. Most of their differences are about what multiplies (the return, an advantage, a group-normalised reward) and how far one batch may move the policy.
Key idea
Section titled “Key idea”Objective. With a policy , maximise , where is a whole trajectory.
The log-derivative trick, in plain words. We cannot differentiate through “sample a trajectory”, but we can differentiate its probability. Use :
The probability of a trajectory is a product of start-state, policy and environment terms, so its log is a sum. Only the policy terms depend on :
The environment’s transition probabilities vanish from the gradient. That is why policy gradients need no model of the environment, and why, in an agent, the tool outputs (which the environment produced) are masked out of the loss.
REINFORCE. Williams’s update for a stochastic unit is , where is the characteristic eligibility (today: the score, ) and is a reinforcement baseline that must not depend on the action taken. His Theorem 1 shows that the expected update has a non-negative inner product with the gradient of expected reward, and equals times the gradient when is constant. For episodes with one reward at the end, his Eq. 11 sums the eligibilities over time, . In Sutton and Barto’s per-step form (Eqs. 13.8 and 13.11 are the undiscounted case; their discounted pseudocode in §13.3–13.4 adds the factor ), for the discounted start-state objective with uniformly enumerated episode timesteps:
When this reduces to the usual undiscounted update. The outer weight may instead be absorbed into a discounted timestep-sampling distribution; it cannot simply be dropped. Discounting inside does not replace it. (The worked example below only updates at , where .)
Two refinements are free. Reward-to-go: use (rewards after step ) rather than the whole-episode return, since an action cannot change earlier rewards. Baseline: for any , , so subtracting it leaves the mean unchanged. Spinning Up notes that the most common choice is the on-policy value function , which turns into an estimate of the advantage.
The policy-gradient theorem (Sutton et al. 1999, Theorem 1; S&B Eq. 13.5). For both the average-reward and start-state formulations,
where is the (discounted) distribution of states the policy visits. Changing changes which states you visit, yet that derivative is absent. In the paper’s words, “the effect of policy changes on the distribution of states does not appear.” So sampling states by simply running the policy gives an unbiased gradient estimate. The same paper shows that a learned can replace the true one without bias if it is “compatible” with the policy parameterisation (Theorem 2), and proves convergence to a locally optimal policy for a policy iteration with function approximation (Theorem 3).
Running example, computed exactly
Section titled “Running example, computed exactly”Let with , so π starts at 50/50, and let the agent always answer in E. Then and the true gradient is .
The scores are for search and for answer.
| Trajectory | Prob. | REINFORCE, no baseline | with | with advantage | |
|---|---|---|---|---|---|
| answer, correct | 0.15 | 1 | −0.50 | −0.27 | 0.08 |
| answer, wrong | 0.35 | 0 | 0 | +0.23 | 0.08 |
| search, correct | 0.40 | 0.8 | +0.40 | +0.17 | 0.08 |
| search, wrong | 0.10 | −0.1 | −0.05 | −0.28 | 0.08 |
| mean / variance | 0.08 / 0.095 | 0.08 / 0.042 | 0.08 / 0 |
All three columns are unbiased. The baseline cuts the variance by about 55%. Using the exact advantage, and , removes it completely in this two-action example, which is the motivation for learning a critic on the next page. Note what a single sample can do: with no baseline, a lucky correct direct answer pushes the policy away from searching (−0.5), the wrong direction on average. In contrast, if the “baseline” were action-dependent, say , every column entry would average to 0: the gradient is destroyed.
Translating REINFORCE
Section titled “Translating REINFORCE”LLM token generation. For a prompt and completion , . With a single reward at the end, the REINFORCE gradient is : Williams’s Eq. 11. Each token gets the same scalar here, but that is a property of this estimator, not a claim that every token deserves the same credit. RLOO sets to the mean reward of the other samples for the same prompt; GRPO uses the group mean and also divides by the group’s standard deviation. RLOO’s leave-one-out baseline is independent of the scored sample, so the unbiasedness argument above applies. GRPO’s inclusive group mean is not: it contains the scored sample’s own reward. In plain REINFORCE with i.i.d. samples, subtracting that inclusive mean scales the expected gradient by (RLOO’s factor undoes exactly this shrinkage); sampled-std normalization changes the estimator further.
A search agent. The policy’s actions are the tokens it writes: reasoning, queries and the final answer. Search results are inserted by the environment. By the log-derivative argument their probabilities are not part of , so their tokens are masked out of the loss. A correct final answer then raises the log-probability of every query the agent wrote in that episode, including useless ones. That coarse credit assignment is what a critic, process rewards or per-turn advantages try to fix.
Key results
Section titled “Key results”- Williams 1992, Theorems 1–2: for REINFORCE algorithms the expected weight change lies in a direction of non-decreasing expected reward, for immediate and for episodic reinforcement. The paper also derives the Bernoulli-logistic case, eligibility , and the reinforcement-comparison baseline (an exponential average of past rewards).
- Sutton et al. 1999, Theorem 1: the policy-gradient expression above, for both formulations. Theorem 2: a learned can replace without bias if and is fit to by least squares. Theorem 3: a policy iteration using such approximations converges to a locally optimal policy.
- The 1999 paper also notes that REINFORCE learns much more slowly than methods that use value functions, and argues that learning a value function to reduce gradient variance appears essential for rapid learning. That is the bridge to actor–critic.
How to read these
Section titled “How to read these”Start with Spinning Up’s “Part 3: Intro to policy optimization” for the derivation in modern notation, then S&B §13.1–13.4. Read Williams §1–5 for the original framing (skim the connectionist details) and Sutton et al. §1–2 for the theorem.
Limitations and open questions
Section titled “Limitations and open questions”- High variance. One scalar per episode scaled by every score in it is noisy, especially for long completions or agent episodes with many tool calls.
- On-policy. The gradient is for the policy that generated the data. Reusing a batch for several updates needs importance ratios and some limit on how far the policy moves, which is what TRPO and PPO add.
- Step size has no natural scale. The same parameter change can move some action probabilities a lot and others barely; TRPO addresses this by measuring steps in KL divergence.
- Credit assignment for agents. How best to split a terminal reward across many tool calls without a reliable critic is an open problem.
Connections
Section titled “Connections”- Previous: Monte Carlo, TD and Q-learning. Next: Actor–critic and the advantage.
- Constraining the step: TRPO → PPO.
- REINFORCE-style LLM RL: RLOO · GRPO · Dr. GRPO (on what GRPO’s normalisation does to the objective).
- Courses: Spinning Up · CS285.
- Agents: Long-horizon tool-using agents.
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Derive / compute
In the search-or-answer MDP, let π(search | Q) = σ(θ) with θ = 0, and let the agent always answer in E (γ = 0.9). Derive the exact gradient of J(θ). Then list the single-trajectory REINFORCE estimates for the four possible trajectories with no baseline and with the baseline b = 0.46, and show that both have mean 0.08 but different variances.
What a strong answer covers
- J(θ) = (1 − σ)·0.3 + σ·0.62 = 0.3 + 0.32σ(θ), so dJ/dθ = 0.32·σ(1 − σ) = 0.08 at θ = 0.
- Score of the first action: d/dθ log σ = 1 − σ = 0.5 for search, d/dθ log(1 − σ) = −σ = −0.5 for answer. E's action does not depend on θ.
- No baseline, G·score: answer-correct (p 0.15) −0.5, answer-wrong (0.35) 0, search-correct (0.40) 0.8·0.5 = 0.4, search-wrong (0.10) −0.05. Mean 0.08, variance ≈ 0.095.
- Baseline 0.46, (G − 0.46)·score: −0.27, +0.23, +0.17, −0.28. Mean still 0.08, variance ≈ 0.042, about 55% lower.
- Using the exact advantage (±0.16) instead of G − b gives 0.08 on every trajectory, zero variance in this two-action example.
- Mechanism
Walk through the log-derivative trick for a trajectory. Why do the environment's transition probabilities disappear from the gradient, and what does that imply for the retrieved documents inside a search agent's context during policy-gradient training?
What a strong answer covers
- ∇ of p_θ(τ) equals p_θ(τ)·∇ log p_θ(τ), so ∇J = E over τ of G(τ)·∇ log p_θ(τ): a gradient of an expectation becomes an expectation you can sample.
- log p_θ(τ) = log p(s₀) + Σ log π_θ(a_t | s_t) + Σ log p(s_(t+1) | s_t, a_t). Only the policy terms depend on θ, so the gradient is Σ ∇ log π_θ(a_t | s_t) times the return.
- This is why no model of the environment is needed: the dynamics are sampled, never differentiated.
- Retrieved documents are transitions chosen by the search engine, not actions of the policy. They should be masked out of the log-probability sum; including them would push the model to "generate" tool outputs.
- What if…
Compare four choices for the scalar that multiplies ∇ log π(a_t | s_t): (a) the full return G₀, (b) the reward-to-go G_t, (c) G_t − b(s_t) with a state-dependent baseline, (d) G_t − q(s_t, a_t). Which are unbiased, which reduce variance, and which one breaks the gradient?
What a strong answer covers
- (a) Unbiased but noisy: actions are credited with rewards that came before them.
- (b) Unbiased and lower variance: an action cannot affect past rewards, and those terms have zero expected contribution.
- (c) Unbiased for any b that does not depend on the action, because the expected score is zero (Σ over a of ∇π(a|s) = ∇1 = 0). Williams requires the baseline to be conditionally independent of the action; the value function is the usual choice.
- (d) Biased: subtracting an action-dependent quantity removes the signal. In the toy example the expected gradient becomes 0 instead of 0.08.
- Connect
Williams's episodic REINFORCE (his Eq. 11) uses one reward at the end of the episode and the sum of the characteristic eligibilities over time. Show that sequence-level REINFORCE for an LLM with a verifier reward is the same rule, and explain what RLOO and GRPO change about the baseline.
What a strong answer covers
- For a completion y, log π(y | x) = Σ_t log π(y_t | x, y_<t), so ∇ log π(y | x) is the sum of per-token eligibilities, and (R − b) multiplies all of them: exactly Δw = α(r − b)·Σ_t e(t).
- Every token in a completion receives the same scalar (R − b). That is a property of this estimator, not a claim that every token is equally responsible; a critic or GAE can assign different per-token advantages.
- RLOO uses the mean reward of the other samples for the same prompt as b, which does not depend on this sample's action, so the estimate stays unbiased.
- GRPO uses the inclusive group mean, which contains the scored sample's own reward, so it is not independent of the sample: in plain REINFORCE it scales the expected gradient by (G−1)/G. GRPO also divides by the group standard deviation, which rescales prompts differently and changes the objective's weighting across prompts.
- Critique
A paper reports that REINFORCE with a learned baseline matched PPO on a math-reasoning RLVR benchmark and concludes that "trust regions are unnecessary for LLM RL". What conditions of that experiment would you check before believing the general claim?
What a strong answer covers
- How many gradient steps are taken per batch of samples: with one step per batch the data is on-policy and a trust region has little to do. The benefit appears when the same batch is reused for several epochs.
- Learning rate, batch size and KL-to-reference regularisation: a small step size can act as an implicit trust region.
- Task properties: short completions with a binary verifier and a strong SFT start are a forgiving setting; long agent episodes with tool calls may not be.
- Seeds and variance across runs, and whether training curves (not only final accuracy) are compared, since instability shows up as occasional collapses.
- Research idea
Propose an experiment that tests whether the variance of the policy-gradient estimate, rather than the bias of the advantage, is the bottleneck for RL-training a search agent. State the hypothesis, the manipulation and the outcome that would falsify it.
What a strong answer covers
- Hypothesis: with sparse correctness rewards, learning speed is limited by gradient variance, so lowering variance at fixed bias should speed learning.
- Manipulation: hold everything fixed and vary the number of samples per prompt used for an unbiased leave-one-out baseline (for example 2, 4, 8, 16), measuring the empirical gradient variance at checkpoints.
- A second arm adds a learned critic (lower variance, some bias) to separate the two effects.
- Falsified if accuracy per training sample does not improve as measured variance falls, or if the biased critic arm wins even at matched variance.