Topic 44 of 62 · Post-training
Policy gradient and REINFORCE
HardWhat is the policy gradient, and how does REINFORCE estimate it for an LLM completion?
1 · Start with the intuition
Think of the language model as a policy that chooses one token at a time. When you score a finished completion with a unit test, a math verifier, or a reward model, you cannot backpropagate through discrete token sampling or through a Python test runner. The policy-gradient identity (the log-derivative trick) gives you a clean way around that: sample completions from the model, score them, and nudge the log-probability of each sampled completion up or down in proportion to how much better or worse it scored than a baseline.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Sample rollouts online
Draw one or more completions y ~ πθ(· | x) from the current policy.
Next, we will look at: Score & subtract a baseline. Move on when this operation makes sense.
Visualizing the behavior
Where this sits: the RL-for-LLMs progression
Weight on ∇ log π for four completions, rewards [1, 0, 0, 1]
DerivedNo baseline: Ψ = R
Mean baseline: Ψ = R − 0.5
3 · The governing equation
∇θ E_{y~πθ}[R(x, y)] = E_{y~πθ} [ (R(x, y) − b(x)) · ∇θ log πθ(y | x) ]
where log πθ(y | x) = Σₜ log πθ(yₜ | x, y_{<t})
and E_{y~πθ}[ b(x) · ∇θ log πθ(y | x) ] = 0 (zero bias for any action-independent b)
Group mean that includes rollout i (G rollouts): E[ĝ] = ((G − 1)/G) · ∇θ E[R]Notation: x is the prompt, y = (y₁, …, y_T) is a sampled completion, R(x, y) is the scalar reward, and b(x) is a baseline that depends on the prompt (or other independent rollouts for that prompt) but never on y itself.
Where does ∇θ E[R] = E[R · ∇θ log πθ] come from? Write the expectation as a sum Σ_y πθ(y|x) R(x,y), move the gradient inside the sum, and use the calculus identity ∇π = π · ∇(log π). Notice two crucial practical facts: (1) the reward R(x,y) is treated as a detached scalar weight—you never differentiate through the reward function; and (2) without a baseline b(x), a binary reward R ∈ {0, 1} gives zero gradient weight to failed rollouts (0 · ∇ log π = 0) instead of pushing their probability down! Subtracting a baseline centers the weights so bad rollouts get negative weights and good rollouts get positive weights, slashing variance. The two common group baselines are not equivalent, though. A leave-one-out (RLOO) baseline excludes the current rollout, so with independent rollouts it does not depend on that rollout’s action and the estimator stays unbiased. A plain group mean includes the current rollout’s own reward, and for G independent rollouts the averaged estimator’s expectation is (G − 1)/G times the true gradient: 75% for G = 4. The direction is preserved in this basic setting, but the estimator is biased.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Saying “policy gradient backpropagates through the reward model” (that is direct reward backpropagation, not REINFORCE—in policy gradient R is a detached scalar!) or claiming “REINFORCE is biased” (it is unbiased; its weakness is high variance across long sequences).
In practice · implementation & engineering rule of thumb
Subtract a baseline from raw rewards before multiplying by log-probabilities—without one, 0-reward failures get zero push-down. A leave-one-out baseline keeps the estimate unbiased; a group mean that includes the rollout itself shrinks the expected gradient by (K − 1)/K.
# Leave-One-Out (RLOO) baseline for K completions of the same prompt: # rewards: [B, K], seq_logprobs: [B, K] baseline = (rewards.sum(dim=-1, keepdim=True) - rewards) / (rewards.size(-1) - 1) advantages = (rewards - baseline).detach() loss = -(advantages * seq_logprobs).mean()
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy does subtracting a baseline not bias the gradient?
Show follow-up reasoning
For a fixed state, E_a[∇ log π(a|s)] = Σₐ π(a|s) ∇ log π(a|s) = ∇ Σₐ π(a|s) = ∇ 1 = 0. Multiplying by any b that does not depend on a keeps the expectation at zero, so only the variance changes.