How a model learns what people prefer when nobody can write the reward down. Comparisons, the Bradley-Terry reward model, PPO with four models in memory, and the ways an optimizer games a learned reward, with every loss, gradient, and number checked.
Chapter 16 · Section 16.1 · 45 min

Why learn from preferences

Try to write the reward function for "a good answer". A user asks how to fix a memory leak in a Python service. The best reply is correct, which you could check if you knew the answer. It is also the right length for this user, admits what it does not know, does not lecture, uses code where code helps, and is polite without fawning. Each of those is a judgment, several of them conflict, and the weights change from one prompt to the next. Nobody has ever written that function, and nobody will.

Yet anyone can look at two replies and say which one is better. That asymmetry, between how hard it is to specify what we want and how easy it is to recognize it, is the whole reason this chapter exists. Reinforcement learning from human feedback (RLHF) turns a pile of "this one is better than that one" judgments into a reward function, then uses reinforcement learning to make a language model produce what that reward function likes.

This section explains why we would go to that trouble when supervised fine-tuning already works, why the judgments should be comparisons rather than scores, where the method came from, and the three-stage pipeline the rest of the chapter takes apart piece by piece.

What demonstrations cannot teach

Supervised fine-tuning (Chapter 14) trains a model on prompts xx paired with written demonstrations yy, by maximizing log⁡πθ(y∣x)\log \pi_\theta(y \mid x), the log-probability that the model πθ\pi_\theta with parameters θ\theta assigns to the demonstration. (We write the model as πθ\pi_\theta, a policy, because from §16.4 on we will treat it as one, in the sense of §15.1.) SFT is cheap, stable, and the right first step. It also has four limits that no amount of extra demonstration data removes.

It can only copy. The best an imitator can do is match the distribution of its demonstrators, flaws included. If the people writing demonstrations produce answers of uneven quality, the model learns the average of that unevenness. A demonstration says "write like this". It never says "this part was the good part".

It never sees its own mistakes. Every training token in SFT comes from someone else. The model is never shown one of its own outputs and told it was bad, so it gets no signal about the errors it actually makes, which are not the errors a human makes. This is the exposure problem of teacher forcing (§3.5) in a new form.

It teaches the model to claim what the demonstrator knew. When a labeler writes a confident, correct answer to a question the model cannot answer, SFT trains the model to produce confident answers to such questions, whether or not the knowledge is there. John Schulman argued, in a 2023 talk at Berkeley, that this is one source of hallucination. Imitation copies the form of knowledgeable answers without the knowledge behind them. A training signal that judges the model's own answers can instead reward "I am not sure" when the model is unsure.

Writing is expensive, judging is cheap. An excellent demonstration of a 400-word legal summary can take an expert half an hour. Choosing the better of two summaries takes a few minutes, and it can be done by people who could not have written either one. For the hardest tasks we care about, the skills to judge an answer are more common than the skills to produce it.

The last point is the deepest one. It is the same asymmetry that makes checking a proof easier than finding it. If a judge's signal can be turned into training signal, a model can be pushed past its demonstrations, toward answers that judges prefer but nobody wrote down.

How much a noisy judge is worth

Let's make that claim quantitative with the simplest possible model. Suppose each answer a model can write has a true quality q∼N(0,1)q \sim \mathcal{N}(0, 1), so the average answer has quality 0. A judge does not see qq. It sees a noisy score s=q+es = q + e with independent noise e∼N(0,σe2)e \sim \mathcal{N}(0, \sigma_e^2). We sample nn answers and keep the one the judge scores highest. How good is the answer we keep?

The pair (q,s)(q, s) is jointly Gaussian, with Var⁡(s)=1+σe2\operatorname{Var}(s) = 1 + \sigma_e^2 and Cov⁡(q,s)=1\operatorname{Cov}(q, s) = 1. For jointly Gaussian variables the conditional mean is linear, E[q∣s]=Cov⁡(q,s)Var⁡(s) s=s1+σe2\mathbb{E}[q \mid s] = \frac{\operatorname{Cov}(q,s)}{\operatorname{Var}(s)}\,s = \frac{s}{1+\sigma_e^2}. Choosing by ss only tells us about qq through ss, so the expected quality of the winner is E[max⁡isi]/(1+σe2)\mathbb{E}[\max_i s_i]/(1+\sigma_e^2). The winning score is the largest of nn Gaussians with standard deviation 1+σe2\sqrt{1+\sigma_e^2}, so E[max⁡isi]=1+σe2 mn\mathbb{E}[\max_i s_i] = \sqrt{1+\sigma_e^2}\, m_n, where mnm_n is the expected maximum of nn standard normals. Putting it together,

E[ q of the judge’s pick ]=mn1+σe2.\mathbb{E}[\,q \text{ of the judge's pick}\,] = \frac{m_n}{\sqrt{1+\sigma_e^2}} .

In plain words, picking the best of nn by a noisy judge buys you mnm_n standard deviations of quality, shrunk by a factor that depends only on how noisy the judge is.

Python · cell 1

Even a judge whose noise is as large as the whole spread of quality (σe=1\sigma_e = 1) picks, from 16 samples, an answer 1.25 standard deviations above the model's average, better than about 89% of what the model writes (Φ(1.25)≈0.89\Phi(1.25) \approx 0.89). A sharper judge (σe=0.5\sigma_e = 0.5) gets 1.58. This is best-of-n sampling, and it already beats imitation without any training. RLHF goes one step further and trains the model so that it writes such answers on the first try, instead of paying for nn samples at every query. We will meet best-of-n again in §16.5, where the same arithmetic explains why optimizing a judge too hard backfires.

Warm-up

How much does picking the best of n help?

Implement expected_max_normal(n), the expected value of the largest of nn independent standard normal draws, by numerical integration of E[max⁡]=∫x n φ(x) Φ(x)n−1 dx\mathbb{E}[\max] = \int x\, n\,\varphi(x)\,\Phi(x)^{n-1}\,dx, where φ\varphi and Φ\Phi are the standard normal density and CDF. Then implement pick_quality(n, sigma), the expected true quality of the best of nn answers chosen by a judge with Gaussian noise of standard deviation sigma, using the formula above.

Hidden tests, hints, and AI help, from $1.99 a month.

Comparisons are easier than ratings

If judgments are the signal, what form should they take? The obvious design asks annotators to rate each answer, say from 1 to 7. The design every lab converged on asks them to compare answers instead. There are three reasons, one statistical and two human.

Comparisons cancel each annotator's private scale. Annotator Ana is generous and gives most answers a 6. Annotator Ben is harsh and gives most a 3. If Ana happens to rate the answers to one prompt and Ben the answers to another, averaging raw scores says the first prompt's answers are better, when it only means Ana rated them. Model annotator aa's rating of an answer with quality qq as s=caq+ba+noises = c_a q + b_a + \text{noise}, with a personal leniency bab_a and a personal spread ca>0c_a > 0. The offset bab_a and the scale cac_a shift and stretch everything Ana says, but they never change her order. Whenever she says s1>s2s_1 > s_2, the sign of ca(q1−q2)c_a(q_1 - q_2), and so the order of the true qualities (up to noise), is the same for her as for anyone. A comparison keeps exactly the information that survives every annotator's private monotone rescaling, and throws away exactly the part that does not.

People are more consistent at comparing. Ask someone whether a reply deserves a 5 or a 6 and the answer depends on what they read just before, how tired they are, and what they think a 6 means. Ask which of two replies is better and most of that context drops out. This is an old finding of psychophysics, formalized as Thurstone's law of comparative judgment (1927), which modeled each stimulus as producing a noisy internal value and each judgment as a comparison of two such values. Christiano et al. (2017) reported the same thing for RL, that they "found it much easier for humans to provide consistent comparisons than consistent absolute scores".

Comparisons calibrate themselves as the model improves. An absolute scale has a ceiling. Once most answers deserve a 7, ratings stop distinguishing anything. A comparison between two good answers is still informative, so the signal stays useful as the model gets better, which is exactly when we need it most.

The cost is information. A 7-point rating carries up to log⁡27≈2.8\log_2 7 \approx 2.8 bits, a binary comparison at most 1 bit, and a comparison only tells you about a difference. Let's measure the trade-off. In the simulation below, 60 annotators each rate 8 of 40 answers on a 1 to 7 scale, with personal offsets of varying spread. We estimate each answer's quality two ways, by averaging its raw ratings, and by turning each annotator's ratings into within-annotator comparisons (ties dropped) and counting the fraction it wins. Then we score each estimate by the fraction of answer pairs it puts in the right order.

Python · cell 2

Read the table honestly. When every annotator uses the scale the same way (offset spread 0), averaging ratings is slightly better, because ratings carry more bits and our win-fraction estimate is crude. As soon as annotators differ, the average of raw ratings degrades steadily, while the comparison-based estimate barely moves. Real annotator pools always differ, and the comparison-based estimate can be improved further by fitting a proper model to the comparisons instead of counting wins. That model is Bradley-Terry, and §16.3 derives it. The widget lets you vary the annotators and watch both estimates.

When do comparisons recover the order of answers better than averaged ratings?
60 simulated annotators each rate 8 of 40 answers from 1 to 7, with a personal leniency, a personal spread, and noise. The ratings are averaged per answer, or turned into within-annotator comparisons whose win fractions are counted.
Loading interactive figure…

Ratings are not useless, and modern pipelines often collect both. Llama 2's annotators picked the better of two responses and also said by how much, on a four-level scale from "significantly better" to "negligibly better or unsure" (Touvron et al. 2023). UltraFeedback asked GPT-4 for 1 to 5 scores on four aspects and derived comparisons from them (Cui et al. 2023). And Wang et al. (2024) pointed out that there was no evidence either way on whether reward models trained on ratings or on comparisons are better, because no public dataset had both for the same responses. They built one and proposed a reward model that uses both. The comparison is the backbone, and extra detail rides along.

Core

Comparisons that cancel annotator offsets

You are given three equal-length arrays describing a rating job, annotator[k], item[k], and rating[k] (annotator aa gave item ii the rating ss). Implement within_annotator_pairs(annotator, item, rating) that returns an integer array of shape (P, 2) whose rows are (winner, loser) item ids, one row for every pair of items rated by the same annotator with different ratings. Ties produce no row, and items rated by different annotators are never compared. Order the rows by annotator id, then by the positions of the two ratings in the input (earlier first), so the output is deterministic.

Hidden tests, hints, and AI help, from $1.99 a month.

Where RLHF came from

The idea that an agent can learn a reward from people is older than language models. TAMER had humans press buttons to reward or punish an agent's recent actions and learned a model of that signal (Knox and Stone 2009). Preference-based policy learning asked people to compare whole trajectories in small domains with hand-coded features (Akrour et al. 2011), and Wirth et al. (2017) surveyed a whole literature of preference-based RL. These methods worked on small problems. The step that made the idea scale was deep learning on both sides, a neural reward model and a neural policy.

Language came next, at OpenAI, in three papers that each added one piece.

Ziegler et al. (2019) fine-tuned the 774M GPT-2 with about 5,000 human comparisons for stylistic continuation (positive sentiment, vivid description) and 60,000 for summarization. Labelers picked the best of four samples. The reward was the learned score minus a penalty βlog⁡(πθ(y∣x)/πref(y∣x))\beta\log\big(\pi_\theta(y\mid x)/\pi_{\text{ref}}(y\mid x)\big), with a coefficient β>0\beta > 0, for drifting from the pretrained model πref\pi_{\text{ref}}, the KL penalty that every later system kept (§16.4). Two findings were prophetic. The summarizers learned to be "smart copiers", lifting whole sentences from the source, which labelers rewarded because copied sentences are accurate. And a refactoring bug flipped the sign of the reward, so the model optimized for the worst text it could write while staying fluent, which the authors only noticed after training ended. Optimizers find whatever the reward actually says.

Stiennon et al. (2020) scaled this to Reddit TL;DR summarization with 1.3B and 6.7B models and 64,832 comparisons collected by carefully trained labelers. Their 1.3B RLHF model's summaries were preferred to the human reference summaries 61% of the time, and it outperformed a supervised model ten times its size. They also showed, for the first time clearly, that pushing the policy against the reward model eventually made summaries worse by human judgment while the reward model's score kept climbing. That is overoptimization, the subject of §16.5.

Ouyang et al. (2022), the InstructGPT paper, applied the recipe to general instruction following on prompts that customers sent to the OpenAI API. Labelers preferred outputs of the 1.3B InstructGPT model to those of the 175B GPT-3, a model more than 100 times larger. The 175B InstructGPT was preferred to the 175B GPT-3 85 ± 3% of the time. ChatGPT, released that November, used the same methods with differences in data collection, and RLHF became the default way to turn a pretrained model into an assistant.

In parallel, Anthropic trained helpful and harmless assistants with RLHF and released their comparison data (Bai et al. 2022), and found that online, iterated rounds of fresh comparisons of the latest model mattered, the same lesson Christiano et al. learned on Atari. DeepMind, Meta, and others followed. Llama 2 (Touvron et al. 2023) described the most detailed open recipe of that era, with over 1.4 million comparisons and five rounds of reward modeling and RL.

The three-stage pipeline

InstructGPT's recipe has three stages, and every later variant is a modification of it. Here they are with InstructGPT's numbers.

stagedatawhat is trainedInstructGPT
1. supervised fine-tuningprompts with written demonstrationsthe policy πSFT\pi_{\text{SFT}}, by maximum likelihoodabout 13k prompts
2. reward modelingprompts, KK sampled responses each, a human ranking of thema reward model rϕ(x,y)∈Rr_\phi(x, y) \in \R from the comparisons33k prompts, KK from 4 to 9
3. reinforcement learningprompts only, no labelsthe policy πθ\pi_\theta, initialized at πSFT\pi_{\text{SFT}}, by PPO on the learned reward minus a KL penalty31k prompts

Stage 3 maximizes, over prompts xx from a dataset D\mathcal{D} and responses yy that the policy samples itself,

J(θ)=Ex∼D,  y∼πθ(⋅∣x)[rϕ(x,y)]  −  β Ex∼D[DKL(πθ(⋅∣x) ∥ πref(⋅∣x))],J(\theta) = \mathbb{E}_{x\sim\mathcal{D},\; y\sim\pi_\theta(\cdot\mid x)}\big[r_\phi(x, y)\big] \;-\; \beta\, \mathbb{E}_{x\sim\mathcal{D}}\Big[D_{\mathrm{KL}}\big(\pi_\theta(\cdot\mid x)\,\Vert\,\pi_{\text{ref}}(\cdot\mid x)\big)\Big],

with the reference policy πref=πSFT\pi_{\text{ref}} = \pi_{\text{SFT}} frozen and a penalty strength β>0\beta > 0 (InstructGPT used β=0.02\beta = 0.02). In plain words, write responses the reward model likes, but stay close to the model you started from, because the reward model was trained on that model's kind of output and cannot be trusted far from it.

Three properties of this design are worth fixing in your mind now, because each gets its own section.

  • The reward model is a learned proxy. It was fit to a few tens of thousands of human comparisons, so it is wrong in places, and RL is an optimizer that searches for those places (§16.5).
  • The policy is trained on its own samples. That is what separates stage 3 from SFT. The model gets feedback on the errors it actually makes, which is the cure for the second and third limits of imitation above (§16.4).
  • Stage 2 does not need the model to write well, only to be compared. The comparisons are of the model's own samples, so the data must be refreshed as the model changes, the on-policy problem of §16.2.
signalwhat the annotator doescost per examplewhat it can teachmain failure
demonstrations (SFT)writes the answerhigh, needs a skilled writerthe format and style of good answerscaps at the demonstrator, copies confident tone without knowledge
absolute ratingsscores one answer, say 1 to 7lowwhich answers are good, in principleannotator offsets and drift, ceiling effects
comparisons (RLHF)picks the better of two or ranks a fewlowwhich of the model's own answers are better, beyond the demonstratorthe learned reward is a proxy that can be gamed
verifiable checks (RLVR)nobody, a program checksalmost zerocorrectness where a checker existsonly for checkable tasks, and checkers can be gamed too

The last row is where the field went next. When an answer can be checked by a program, as for math with a known answer or code with tests, the reward model can be replaced by the checker. That is RL with verifiable rewards, the engine of reasoning models (Chapter 18). And when the RL stage is replaced by a classification loss on the comparisons themselves, you get direct preference optimization (Chapter 17), which starts from the very objective J(θ)J(\theta) above. Preferences remain the signal for everything a program cannot check, which is most of what people ask for.

Core

Win rate with ties and an error bar

Human evaluations of RLHF models report numbers like "preferred 85 ± 3% of the time". Implement win_rate(judgments) where judgments is a list of strings, each "A" (model A's response preferred), "B", or "tie". Count a tie as half a win for each side. Return (rate, se), the win rate of A and its standard error, computed as the sample standard deviation (with ddof=1) of the per-judgment scores (1, 0.5, or 0) divided by n\sqrt{n}. (A win rate is a mean, so its interval follows §0.11, and when both models answer the same prompts the comparison is paired.)

Hidden tests, hints, and AI help, from $1.99 a month.

The real way to turn comparisons into scores

Counting wins, as our simulation did, ignores whom each answer beat. Beating a strong answer should count for more than beating a weak one. The principled fix gives every answer a score rir_i, models the probability that ii beats jj as σ(ri−rj)\sigma(r_i - r_j), and fits the scores by maximum likelihood. That is the Bradley-Terry model, the foundation of §16.3. Here it is fit with autograd to the same kind of within-annotator comparisons, and compared with counting.

🔥 In PyTorchfor a machine with PyTorch

The fit orders 87.4% of the pairs correctly against 86.0% for counting wins. The gain is small here because every answer met a random sample of opponents. It grows when the schedule is lopsided and some answers only ever meet strong ones, which is the normal situation when the responses being compared come from models of different quality. The tiny L2 term pins down the scores, which the likelihood alone leaves free to shift by any constant, a fact that will matter for reward models.

Read more

Watch next

🎬 Reinforcement Learning from Human Feedback: From Zero to chatGPT · Hugging FaceNathan Lambert's overview of the whole pipeline and its history, a good map before the details.
🎬 Training AI Without Writing A Reward Function, with Reward Modelling · Robert Miles AI SafetyThe Christiano et al. (2017) backflip experiment explained, and why learning the reward from comparisons is the key idea.
🎬 Post-Training and RLHF Overview | Post-Training Book Course, Lecture 1 · Nathan LambertWhere RLHF sits in modern post-training, from the author of the standard book on it.