Skip to content
BIASED

Reinforcement learning for language models

KL-regularized RLHF, the π* optimum, token-level PPO, GRPO, and verifiable rewards.

From fixed weights to RL

Multi-agent systems built agents around a model whose weights never changed: prompts, tools, memory and structure did the learning. This section changes the weights. The tool is the policy gradient of Policy gradients and actor-critic; the policy is the language model of The language model as a policy, read at the token level: state st=(x,y<t)s_t=(x,y_{<t}), action the next token, reward when the response is complete. The NLP course’s instruction-tuning note introduced reward models, RLHF and DPO; this topic derives the objective those methods optimize and how PPO and GRPO approach it.

The KL-regularized objective and π*

Maximizing expected reward alone puts all mass on the best-scored response. That lands on what the reward model knows least about, kills diversity, and forgets pretraining.

Key idea. Maximize reward minus β\beta times KL from a frozen reference πref\pi_{\text{ref}} (usually the SFT model):

Jβ(π)=Ex∼D[Ey∼π(⋅∣x)[r(x,y)]−β DKL(π(⋅∣x) ∥ πref(⋅∣x))].J_\beta(\pi)=\mathbb E_{x\sim\mathcal D}\Big[\mathbb E_{y\sim\pi(\cdot\mid x)}[r(x,y)]-\beta\,D_{\mathrm{KL}}\big(\pi(\cdot\mid x)\,\|\,\pi_{\text{ref}}(\cdot\mid x)\big)\Big].

Rewriting yields optimum π⋆(y∣x)∝πref(y∣x)exp⁡(r(x,y)/β)\pi^\star(y\mid x)\propto\pi_{\text{ref}}(y\mid x)\exp(r(x,y)/\beta), worth βlog⁡Z(x)\beta\log Z(x). Large β\beta keeps the reference; small β\beta approaches plain reward max. Solving for rr and substituting into Bradley–Terry cancels βlog⁡Z\beta\log Z and gives DPO on the policy alone. ZZ sums over every response, so PPO and GRPO approach π⋆\pi^\star by sampling.

Worked example with πref=(0.50,0.30,0.15,0.05)\pi_{\text{ref}}=(0.50,0.30,0.15,0.05), r=(0,0.5,1.0,1.2)r=(0,0.5,1.0,1.2): at β=0.5\beta=0.5, π⋆≈(0.168,0.274,0.373,0.185)\pi^\star\approx(0.168,0.274,0.373,0.185) — answer 3 wins over the higher-scored answer 4 because of reference probability.

PPO for language models

The sequence log-ratio is a sum of token log-ratios. PPO therefore charges a KL penalty at every token and the reward-model score at the last:

rt=−βlog⁡πθ(yt∣st)πref(yt∣st)+1[t=T] rϕ(x,y).r_t=-\beta\log\frac{\pi_\theta(y_t\mid s_t)}{\pi_{\text{ref}}(y_t\mid s_t)}+\mathbb 1[t=T]\,r_\phi(x,y).

Summed with γ=1\gamma=1, the return matches the bracket of JβJ_\beta. A value head VψV_\psi predicts remaining return; GAE turns TD errors into advantages for the clipped objective. Four large models sit in memory: policy, reference, reward model, critic. Credit assignment is hard: only the last token carries rϕr_\phi.

Worked example: log-ratios (0.2,−0.1,0.5,0.0)(0.2,-0.1,0.5,0.0), β=0.1\beta=0.1, score 1.01.0, values (0.6,0.5,0.7,0.9)(0.6,0.5,0.7,0.9) then 00, λ=0.95\lambda=0.95 → advantages ≈(0.301,0.443,0.245,0.100)\approx(0.301,0.443,0.245,0.100).

GRPO — the group as baseline

GRPO (Shao et al., 2024) drops the critic. Sample GG responses per prompt, standardize rewards within the group, give every token of response ii the same advantage

A^i,t=ri−mean⁡(r)std⁡(r).\hat A_{i,t}=\frac{r_i-\operatorname{mean}(r)}{\operatorname{std}(r)}.

KL moves into the loss via D^=u−log⁡u−1\hat D=u-\log u-1 with u=πref/πθu=\pi_{\text{ref}}/\pi_\theta. All-right or all-wrong groups give zero advantage. With r=(1,0,0,1,0,0)r=(1,0,0,1,0,0): mean 1/31/3, std ≈0.471\approx0.471, correct answers get A^≈+1.414\hat A\approx+1.414, wrong −0.707-0.707.

DeepSeekMath-Instruct 7B with GRPO: GSM8K 82.9%→88.2%82.9\%\to88.2\%, MATH 46.8%→51.7%46.8\%\to51.7\%. Maj@K rose; Pass@K did not — RL sharpened answers the model could already produce.

Verifiable rewards

A learned reward is the weakest link: optimization finds where it is wrong. For checkable answers, RLVR replaces it with a program: boxed maths equality, unit tests in a sandbox, format rules. DeepSeek-R1-Zero used rule-based accuracy and format rewards alone after neural reward models were hacked at scale.

False negatives add noise; false positives are optimized. A lenient verifier that checks only the last token of a three-token answer can report pass rate 0.9590.959 while exact accuracy is 0.6060.606. Agentic versions of this failure — editing the tests instead of the code — return in Training agents with reinforcement learning.

Overoptimizing the proxy

Optimizing a proxy too hard makes the true objective worse (Goodhart). Gao, Schulman and Hilton (2023): gold score rises then falls while the proxy keeps rising. Best-of-nn moves log⁡n−(n−1)/n\log n-(n-1)/n nats from the sampling policy (best-of-16 ≈1.84\approx1.84). Defences: limit β\beta or stop early on a held-out eval the proxy does not see; retrain the reward on current samples; use a verifier wherever answers are checkable.

Limits and next

Outcome rewards are sparse; hard prompts teach nothing until the policy succeeds by chance; β\beta is a guess; writing and advice still need learned rewards.

Everything here trains better single responses. Next: reasoning models and test-time compute — let the chain grow under verifiable rewards, and treat tokens at inference as a resource next to training compute.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.