- Key idea
- The score function (log-derivative) estimator, control variates and baselines, leave-one-out, and the reparameterisation trick
- Real world
- A cook tweaks a recipe by tasting batches. They can't differentiate "how good does a random batch taste" with respect to the amount of salt. But they can note which batches tasted better than usual and lean the recipe towards whatever those batches had more of.
- In an LLM
- The reward of an answer can't be differentiated with respect to the model's weights: sampling a token is a discrete jump, and the checker is a black box. The policy gradient gets around this by differentiating the log-probability of the sampled answer instead, weighted by how much better than usual it scored.
The problem
RL for LLMs wants to raise the expected reward . Gradient descent needs . But:
- The answer is sampled. A tiny change in the weights doesn't change which token comes out; it changes how likely each token is.
- The reward comes from a unit test, a maths checker or a separate reward model. There's no gradient through it.
So you can't backpropagate through the sample. You need a gradient of the distribution.
The log-derivative trick
The fix is one line of calculus. The gradient of a probability equals the probability times the gradient of its log:
Put that inside the expectation and the gradient becomes another expectation, which can be estimated by sampling:
is called the score function. It's the direction in weight space that makes more likely. The estimate: sample answers, and push each one's log-probability up in proportion to its reward. This is REINFORCE, and every policy-gradient method for LLMs is a variant of it.
Make 10 batches with slightly random salt. Batches that taste better than average had, on average, a bit more salt. Shift the recipe towards that. You never needed "taste" as a smooth function of salt; you only needed to know which random variations you made, and how they turned out.
For a sampled answer,
That's one ordinary backward pass through the tokens it contains, exactly like a supervised fine-tuning step on that answer. RL is "fine-tune on your own samples, weighted by reward".
Baselines are control variates
The score function averages to zero: summed over every possible answer, pushing all of them up in proportion to their probability changes nothing.
So you can subtract any constant from the reward without changing the expected gradient. It does change the variance, enormously. This is a control variate: subtract something with known mean (here zero) that is correlated with your noise.
To estimate a house's value, you could average the prices of nearby sales: very noisy, because houses differ. Or compare each sale to the area's known average and average the differences. Same target, far less noise, because the shared part has been subtracted out.
If every answer gets reward 10 or 11 instead of 0 or 1, REINFORCE without a baseline pushes every sample up hard, and only the small difference between 10 and 11 carries information. With the group mean as baseline, the advantages are exactly what they were for rewards of 0 and 1, whatever the offset.
Which baseline?
| Baseline | Biased? | Notes |
|---|---|---|
| None | no | Very noisy; sensitive to reward offset |
| Learned value (PPO's critic) | no | Needs a second network |
| Group mean, including the sample itself | slightly | Shrinks the gradient by ; GRPO |
| Leave-one-out (RLOO) | no | Mean of the other samples |
| Group mean ÷ group std (GRPO) | yes | Rescales each prompt's gradient; Dr. GRPO removes the std |
A baseline must not depend on the sample it's applied to, or the zero-mean argument breaks. Leave-one-out exists precisely to satisfy that.
Try it
The other way: reparameterisation
If the randomness can be moved outside the model, the gradient can go straight through. Instead of sampling , sample noise and compute . Now is a differentiable function of . This reparameterisation trick gives much lower-variance gradients and powers VAEs and diffusion models.
It doesn't work for picking tokens: a discrete choice has no smooth path. The Gumbel-softmax relaxation fakes one by replacing the hard choice with a soft, temperature-controlled mixture, which is useful inside a network but not for training a model to write text, where the score function remains the standard.
Where this shows up
- REINFORCE, PPO, GRPO, RLOO are all score-function estimators with different baselines: policy gradient, GRPO, fixes.
- Advantage normalisation is a choice of baseline and scale: GRPO fixes.
- DPO avoids the estimator by solving for the optimum in closed form: tilted distributions.
- Gumbel-max sampling, the hard cousin of Gumbel-softmax: sampling text.
The estimator
For a softmax over logits ,
The widget computes this per sample and measures the total variance across 800 groups.
Baselines in papers
RLOO is the group-mean advantage rescaled by , which undoes the shrinkage. GRPO's division by the std weights prompts unequally: prompts with nearly all-same rewards have tiny std and get boosted. Dr. GRPO drops it.
The variance-minimising constant baseline is not the mean reward but a reward average weighted by gradient size:
In practice the mean is close enough.
Reparameterisation and Gumbel-softmax
with i.i.d. Gumbel noise. As , becomes the one-hot Gumbel-max sample from .
Papers
- Williams, Simple statistical gradient-following algorithms for connectionist reinforcement learning (1992): REINFORCE
- Monte Carlo Gradient Estimation in Machine Learning (Mohamed et al., 2019): survey of score-function, reparameterisation and control variates
- Back to Basics: Revisiting REINFORCE Style Optimization for Learning from Human Feedback (Ahmadian et al., 2024): RLOO for LLMs
- Understanding R1-Zero-Like Training (Liu et al., 2025): Dr. GRPO, the std and length biases
- Auto-Encoding Variational Bayes (Kingma & Welling, 2013): reparameterisation
- Categorical Reparameterization with Gumbel-Softmax (Jang et al., 2016) and The Concrete Distribution (Maddison et al., 2016)