The model. A language model is a policy: at each prefix it gives a distribution over the next token, and a completion is a path through this tree. Here every prefix has its own softmax over four logits, 21 prefixes in all; a real model computes the same softmaxes with one shared network, which is what lets it generalize. Its log-probability of a completion is the sum along the path, log π(y) = log π(y1) + log π(y2|y1) + log π(y3|y1y2). The reward arrives only at the end, so each completion is treated as one action. The lower plot uses a square-root scale so that rare completions stay visible.
REINFORCE. You cannot backpropagate through sampling, but ∇Ey~π[r(y)] = Ey~π[r(y) ∇log π(y)], so sampling a batch and stepping along Σi ri ∇log π(yi) climbs the expected reward. That is supervised fine-tuning on the model’s own samples, each weighted by its reward. With rewards of 0 and 1 it is exactly “keep the samples that worked and fine-tune on them.” Every sampled token is pushed up at its prefix, by the log-softmax gradient, one-hot minus probabilities.
Why a baseline. Subtracting any b that does not depend on the sample leaves the gradient unbiased, because E[∇log π] = 0. It changes the noise. Raise add to every reward: the objective’s gradient is unchanged, but REINFORCE now pushes every sample up and relies on small differences between big pushes, and the right-hand chart shows its steps pointing in nearly random directions. The baseline learner subtracts a running average of past rewards and does not notice the shift. Weight r − b is the advantage: better than usual goes up, worse goes down.
GRPO. DeepSeek’s GRPO uses the batch itself as the baseline: sample a group of completions for the same prompt and give each the advantage (ri − mean)/std. Two consequences are visible here. A group whose rewards are all equal teaches nothing; the status line counts those steps. And dividing by the std rescales the reward by 1/std, which amplifies rare successes (try one rare completion) but changes the objective: with more a’s, GRPO overshoots the dashed target, and unchecking the box brings it back. Real GRPO also takes several steps per batch with PPO’s clipped probability ratio; with one step per batch, as here, the ratio is 1 and the clipping does nothing. The KL term’s gradient is computed exactly in this demo; real implementations estimate it from the samples. PPO, the original RLHF algorithm, replaces the group baseline with a learned value network, a second model as big as the first.
The dashed target. The objective E[r] − β·KL(π‖π
ref) has a closed-form maximizer, π*(y) ∝ π
ref(y) exp(r(y)/β): the pretrained model, reweighted toward reward. Every learner here is a noisy way to climb to it. Lower β and π* piles onto the rewarded completions, in the proportions π
ref already gave them; raise it and π* stays near π
ref. At β = 0 nothing holds the policy back, and it collapses onto whichever rewarded completion it happens to find. Inverting this formula is how
DPO gets rid of the sampling.