Skip to content

REINFORCE and the Policy-Gradient Theorem (Williams 1992; Sutton et al. 1999)

Skip →GAE (advantage estimation)
main track #4corefundamentalpaperrubric 12/121992 · Machine Learning 8(3–4) (Williams); NIPS 12, 1999 (Sutton et al.) · ~75 min

Ronald J. Williams (1992); Richard S. Sutton, David McAllester, Satinder Singh, Yishay Mansour (1999)

Read first: Reinforcement Learning: An Introduction, Ch. 5–6 — Monte Carlo, Temporal-Difference Learning and Q-learning

Status

TL;DR. Instead of learning values and acting greedily, policy-gradient methods adjust the policy’s parameters θ\theta 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 ∇log⁡π\nabla \log \pi (the return, an advantage, a group-normalised reward) and how far one batch may move the policy.

Objective. With a policy πθ(a∣s)\pi_\theta(a \mid s), maximise J(θ)=Eτ∼πθ[G(τ)]J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}[G(\tau)], where τ\tau 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 ∇p=p ∇log⁡p\nabla p = p\,\nabla \log p:

∇θJ(θ)=∑τG(τ) ∇θpθ(τ)=∑τpθ(τ) G(τ) ∇θlog⁡pθ(τ)=Eτ[G(τ) ∇θlog⁡pθ(τ)].\nabla_\theta J(\theta) = \sum_\tau G(\tau)\,\nabla_\theta p_\theta(\tau) = \sum_\tau p_\theta(\tau)\, G(\tau)\, \nabla_\theta \log p_\theta(\tau) = \mathbb{E}_{\tau}\left[G(\tau)\, \nabla_\theta \log p_\theta(\tau)\right].

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 θ\theta:

∇θlog⁡pθ(τ)=∑t∇θlog⁡πθ(At∣St).\nabla_\theta \log p_\theta(\tau) = \sum_t \nabla_\theta \log \pi_\theta(A_t \mid S_t).

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 Δw=α (r−b) e\Delta w = \alpha\,(r - b)\,e, where e=∂ln⁡g/∂we = \partial \ln g / \partial w is the characteristic eligibility (today: the score, ∇log⁡π\nabla \log \pi) and bb 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 α\alpha times the gradient when α\alpha is constant. For episodes with one reward at the end, his Eq. 11 sums the eligibilities over time, Δw=α (r−b)∑te(t)\Delta w = \alpha\,(r - b) \sum_t e(t). 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 γt\gamma^t), for the discounted start-state objective with uniformly enumerated episode timesteps:

θ←θ+α γt (Gt−b(St)) ∇θlog⁡πθ(At∣St).\theta \leftarrow \theta + \alpha\,\gamma^t\,\big(G_t - b(S_t)\big)\,\nabla_\theta \log \pi_\theta(A_t \mid S_t).

When γ=1\gamma = 1 this reduces to the usual undiscounted update. The outer weight γt\gamma^t may instead be absorbed into a discounted timestep-sampling distribution; it cannot simply be dropped. Discounting inside GtG_t does not replace it. (The worked example below only updates at t=0t = 0, where γ0=1\gamma^0 = 1.)

Two refinements are free. Reward-to-go: use GtG_t (rewards after step tt) rather than the whole-episode return, since an action cannot change earlier rewards. Baseline: for any b(s)b(s), Ea∼π[∇log⁡π(a∣s) b(s)]=b(s) ∇∑aπ(a∣s)=b(s) ∇1=0\mathbb{E}_{a \sim \pi}[\nabla \log \pi(a \mid s)\, b(s)] = b(s)\, \nabla \sum_a \pi(a \mid s) = b(s)\,\nabla 1 = 0, so subtracting it leaves the mean unchanged. Spinning Up notes that the most common choice is the on-policy value function Vπ(s)V^\pi(s), which turns Gt−V(St)G_t - V(S_t) 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,

∇θJ(θ)=∑sdπ(s)∑a∇θπθ(a∣s) Qπ(s,a),\nabla_\theta J(\theta) = \sum_s d^{\pi}(s) \sum_a \nabla_\theta \pi_\theta(a \mid s)\, Q^{\pi}(s, a),

where dπd^\pi is the (discounted) distribution of states the policy visits. Changing θ\theta 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 QQ 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).

One REINFORCE update, step by step Four boxes: sample a trajectory from the policy; compute the score, the gradient of log probability, for each action the policy took; weight it by return minus baseline; average and take a gradient step. Below, a search-agent trajectory: the question and retrieved documents come from the environment and get no score, while the query and the answer are policy actions and get a score. In the toy example, search then correct gives 0.5 times 0.8 minus 0.46, which is 0.17, and the average over trajectories equals the true gradient 0.08. 1. Sample τ ~ π_θ 2. Score each policy action ∇ log π(a_t | s_t) 3. Weight by G_t − b(s_t) 4. Average θ ← θ + α · ĝ A search-agent trajectory: what gets a score? question query (search) retrieved docs answer prompt: no score policy: scored environment: masked policy: scored Toy, θ = 0: search then correct → score +0.5 × (0.8 − 0.46) = +0.17 Average over the four trajectories = 0.08 = dJ/dθ (unbiased); variance 0.095 → 0.042
Original sketch. One REINFORCE update on the search-or-answer MDP: sample a trajectory, compute the score of each policy action, multiply by (return − baseline), average. Tool outputs are environment transitions and get no score.

Let π(search∣Q)=σ(θ)\pi(\text{search} \mid Q) = \sigma(\theta) with θ=0\theta = 0, so π starts at 50/50, and let the agent always answer in E. Then J(θ)=0.3 (1−σ)+0.62 σ=0.3+0.32 σ(θ)J(\theta) = 0.3\,(1 - \sigma) + 0.62\,\sigma = 0.3 + 0.32\,\sigma(\theta) and the true gradient is 0.32 σ(1−σ)=0.080.32\,\sigma(1-\sigma) = 0.08.

The scores are ddθlog⁡σ=1−σ=+0.5\frac{d}{d\theta}\log\sigma = 1 - \sigma = +0.5 for search and ddθlog⁡(1−σ)=−σ=−0.5\frac{d}{d\theta}\log(1-\sigma) = -\sigma = -0.5 for answer.

TrajectoryProb.G0G_0REINFORCE, no baselinewith b=0.46b = 0.46with advantage A(Q,a)A(Q,a)
answer, correct0.151−0.50−0.270.08
answer, wrong0.3500+0.230.08
search, correct0.400.8+0.40+0.170.08
search, wrong0.10−0.1−0.05−0.280.08
mean / variance0.08 / 0.0950.08 / 0.0420.08 / 0

All three columns are unbiased. The baseline cuts the variance by about 55%. Using the exact advantage, A(Q,search)=0.62−0.46=+0.16A(Q, \text{search}) = 0.62 - 0.46 = +0.16 and A(Q,answer)=−0.16A(Q, \text{answer}) = -0.16, 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 q(Q,a)q(Q, a), every column entry would average to 0: the gradient is destroyed.

LLM token generation. For a prompt xx and completion yy, log⁡πθ(y∣x)=∑tlog⁡πθ(yt∣x,y<t)\log \pi_\theta(y \mid x) = \sum_t \log \pi_\theta(y_t \mid x, y_{<t}). With a single reward R(x,y)R(x, y) at the end, the REINFORCE gradient is (R−b) ∇∑tlog⁡πθ(yt∣x,y<t)(R - b)\,\nabla \sum_t \log \pi_\theta(y_t \mid x, y_{<t}): 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 bb 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 GG i.i.d. samples, subtracting that inclusive mean scales the expected gradient by (G−1)/G(G-1)/G (RLOO’s G/(G−1)G/(G-1) 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 ∇log⁡pθ(τ)\nabla \log p_\theta(\tau), 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.

  • 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 (y−p) x(y - p)\,x, 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 fwf_w can replace QπQ^\pi without bias if ∂fw/∂w=∇π/π\partial f_w/\partial w = \nabla \pi / \pi and fwf_w is fit to QπQ^\pi 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.

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.

  • 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.

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Done with this page?

Status