Ask a pretrained model to explain the moon landing to a 6 year old, and it mostly writes more questions. In this toy, people rate its replies −0.87 on average. Instruction tuning lifts that to 0.09, and preference tuning at β = 0.6 lifts it to 0.37.
Switch between the three stages and press Ask. Then drag β down to 0.1, and watch the model pile onto a padded answer that the reward model loves and people do not. Then we build each stage from zero: instruction tuning, a reward model, RLHF and DPO.
You need a little Python, what a probability is and the next-word loss of a language model (Lecture 4). We build the rest from zero.
Lecture 8, "Post-training (RLHF, SFT, DPO)", of Stanford CS224N, Natural Language Processing with Deep Learning (Winter 2026, Diyi Yang and Yejin Choi; this lecture’s slides are Diyi Yang’s). Its readings are InstructGPT (Ouyang et al., 2022), Flan (Chung et al., 2022), AlpacaFarm (Dubois et al., 2023), "How Far Can Camels Go?" (Wang et al., 2023) and DPO (Rafailov et al., 2023). Before: Lecture 7, Pretraining. Next: Lecture 9, Efficient Adaptation.
Illustrative toy. The six kinds of reply, their chances, the reward model’s scores and people’s ratings (from −2 to +2) are invented to show the mechanism. The preference-tuned model is the exact best leashed model of Chapter 6. The prompt and GPT-3’s habit of replying with more questions come from the InstructGPT reading.
Chapter 0
Count what follows an instruction on the web, and see why a pretrained model replies with another question
A pretrained model learned one job in Lecture 7: predict the next word of web text. Give it "Stanford University is located in", and it fills the blank well. Give it an instruction, and it may not follow it.
The InstructGPT paper (Ouyang et al., 2022), this lecture’s first reading, shows GPT-3 doing this:
Explain the moon landing to a 6 year old in a few sentences.
Explain the theory of gravity to a 6 year old.
Explain the theory of relativity to a 6 year old in a few sentences.
Explain the big bang theory to a 6 year old.
Why? On the web, a line like the prompt often sits in a list of exercises. So the line after it is usually another exercise. The model copies what usually comes next, because that is exactly what its training rewarded.
Teal numbers count what follows the prompt in a toy corpus of 1,000 lines. Blue bars are the probabilities a model trained on that corpus gives. Switch the prompt, then sample three replies.
Illustrative toy corpus. We made up the counts; the pattern, a list of exercises after an exercise, is the one in the InstructGPT example above.
A model trained on these counts gives each continuation its share: P = count / total. After the instruction, 220 of the 1,000 lines answer it, so P(answer) = 220 / 1,000 = 0.22. Ask three times, and the chance that no reply answers is 0.78 × 0.78 × 0.78 = 0.47. Almost half the time, a user who asks three times gets no answer.
Post-training closes this gap in two steps, and the lecture follows them. Instruction tuning shows the model examples of good answers. Preference tuning (RLHF or DPO) then teaches it which of two answers people like better. It works: InstructGPT’s labelers preferred a 1.3B-parameter model tuned this way to the 175B GPT-3, which has more than 100 times as many parameters.
Symptom: the model replies with more questions, or with text copied from a web page.
Measure: the share of replies that try to answer: 22% in the toy.
Fix: fine-tune on instructions paired with good answers (Chapter 1).
Chapter 1
Fine-tune on instruction and answer pairs, and mask the prompt so the loss grades only the answer
Instruction tuning, also called supervised fine-tuning (SFT), is the lecture’s first step. Collect pairs of an instruction and a good output across many tasks. Fine-tune the pretrained model on them with the same next-word loss. Then test it on tasks it never saw.
Scale matters here, as in pretraining. Flan (Chung et al., 2022) fine-tuned on 1.8K tasks: Flan-PaLM 540B beat the plain PaLM 540B by 9.4 points on average and reached 75.2% on five-shot MMLU. The lecture also shows cheaper routes. Alpaca fine-tuned LLaMA 7B on 52K examples written by another model. LIMA (Zhou et al., 2023) used only 1,000 curated examples on a 65B model, and people rated its answers equal to or better than GPT-4’s in 43% of cases.
One detail decides what the model learns: which tokens the loss grades. Call the prompt x and the answer y, with T tokens y1 to yT. The model, with weights θ, gives each answer token a probability pθ.
Yellow chips are the prompt; warm chips are the answer. Each pink bar is a token’s loss, minus the log of its probability. Turn the mask off and on.
Illustrative probabilities for one example part-way through fine-tuning. The answer is a shortened version of the human demonstration on the lecture’s slide.
L-S-F-T equals minus one over T, times the sum over t from one to T, of log p-theta of y-t given x and y-before-t.
Feed the prompt x and the answer y through the model. For each answer token yt, take minus the log of its probability. Average over the T answer tokens. The prompt is read, but never graded.
From the toy: the 10 answer tokens have probabilities 0.30, 0.20, 0.25, 0.40, 0.35, 0.80, 0.90, 0.85, 0.70 and 0.60. Their losses add up to 7.52, so LSFT = 7.52 / 10 = 0.75. Grade the 9 prompt tokens too, and the average becomes 1.06, with 63% of it from the prompt.
In code, the mask is one number. Set the label of every prompt token to −100, and PyTorch’s cross_entropy(..., ignore_index=-100) skips it (the Field Guide has the code).
Symptom: the tuned model starts writing new user questions, and the loss stays high even when the answers are good.
Measure: the share of the loss that comes from prompt tokens: 63% in the toy.
Fix: mask the prompt, so every gradient step improves the answer.
Chapter 2
Grade two models with the token loss and with an expected reward, and see which grade notices how an answer is wrong
The lecture names two limits of instruction tuning beyond its cost. First, open-ended tasks have no single right answer: "Write me a story about a dog and her pet grasshopper." Second, the token loss penalizes every mistake the same. If the reference says "Avatar is a fantasy TV show", a model that writes "adventure" pays the same loss as one that writes "musical".
The reason is in Chapter 1’s formula. The loss looks only at the probability of the reference token. Where the rest of the probability goes, to a near miss or to nonsense, does not enter it.
So grade whole answers instead. For one prompt (we drop x for now), give each answer y a reward R(y): a number from a person, higher is better. The lecture’s example is a news story about a small earthquake, with three summaries:
Each model spreads its probability over the three summaries; y1 is the reference. Pick a model, then drag the share it gives y1.
Illustrative. Summaries y1 and y2 and their rewards come from the lecture’s slides; R = 4.1 for y3 is one of the slide’s guesses. Model A puts the rest of its probability 2 : 3 on y2 and y3, model B 3 : 2.
What do we maximize? Call the model’s probability of answer y pθ(y). Its expected reward J is the reward of each answer, weighted by how often the model writes it:
J of theta equals the expected value, over y drawn from p-theta, of R of y; that equals the sum over y of p-theta of y times R of y.
Multiply each answer’s reward R(y) by its probability pθ(y). Add the products. The total J is the reward you expect from one answer.
From the device: model A gives the three summaries 0.5, 0.2 and 0.3, so J = 0.5 × 8.0 + 0.2 × 1.2 + 0.3 × 4.1 = 5.47. Model B gives 0.5, 0.3 and 0.2: J = 5.18. Both have the same loss on the reference, −log 0.5 = 0.69.
Same loss, different value: J sees where the missing probability went. The catch is that R comes from a person, so there is no formula to take its gradient through. Chapter 3 fixes that.
Symptom: two checkpoints tie on the fine-tuning loss, but users clearly prefer one.
Measure: the expected reward: 5.47 against 5.18 here.
Fix: optimize a reward on whole answers (Chapter 3), with a model of people’s judgment (Chapter 4).
Chapter 3
Sample answers, weight each one by its reward, step, and then clip the step with PPO
We want to climb J(θ). Reinforcement learning calls the model a policy. A person gave the reward, so we cannot backpropagate through it. The log-derivative trick moves the gradient onto the model’s own probabilities:
∇θ log pθ(y) = ∇θpθ(y) / pθ(y), so ∇θpθ(y) = pθ(y) ∇θ log pθ(y)
∇θ J = Σy R(y) ∇θpθ(y) = Σy pθ(y) R(y) ∇θ log pθ(y) = Ey∼pθ[R(y) ∇θ log pθ(y)]
∇θ is the gradient: the direction that raises a value fastest. The result is an average, so samples estimate it. Draw m answers, and step by α. This is REINFORCE (Williams, 1992):
Theta becomes theta plus alpha, times one over m, times the sum over i from one to m, of R of y-i times grad-theta of log p-theta of y-i.
Sample m answers. Scale each one’s more-likely direction by its reward R(yi). Average, multiply by α, and add to θ.
A softmax over three logits gives 0.5, 0.3 and 0.2. One sample, y1 with R = 8.0: the gradient of log p(y1) with respect to the logits is (1 − 0.5, −0.3, −0.2). Times 8.0 and α = 0.05, the logits move by (0.20, −0.12, −0.08), and p(y1) rises from 0.50 to 0.575.
Each step samples 4 summaries and pushes each by its reward. Step a few times, then turn on the baseline.
Illustrative toy: a softmax over Chapter 2’s summaries, α = 0.05, computed live. The baseline is the exact expected reward.
Every reward here is positive, so even the poor y2 goes up when sampled: 0.300 to 0.314. Subtract a baseline, the average reward 5.18, and its weight is −3.98: the same sample lowers it to 0.256.
One large step can still wreck a language model. PPO (Schulman et al., 2017) limits each update. Call a sample’s new over old probability ρ, and how much better than expected it scored the advantage A:
L-clip equals the expected value of the minimum of rho times A, and clip of rho between one minus epsilon and one plus epsilon, times A.
For each sample, compute ρ times A, and again with ρ clipped to 1 ± ε. Keep the smaller. Past the clip, the term stops changing, so it stops pushing.
With made-up numbers and ε = 0.2: a good answer with A = +2 is now 1.5 times as likely: min(3.0, 2.4) = 2.4. A bad answer with A = −2 is now half as likely: min(−1.0, −1.6) = −1.6. Both values are flat in θ, so neither sample pushes further.
Symptom: probabilities swing, and poor answers creep up.
Measure: in the toy, watch y2 after a step that samples it, with and without the baseline.
Fix: a baseline (in PPO, a learned value model) and a clipped update.
Chapter 4
Turn people’s picks into scores with the Bradley-Terry model, and fit the scores with one loss
Chapter 3 needs a reward for millions of samples. The lecture names two problems. A person in the loop is too slow, so train a model of their judgment, the reward model RMφ. And ratings are noisy and uncalibrated: is y3 worth 4.1, 6.6 or 3.2? Picking the better of two answers is more reliable.
The Bradley-Terry model (1952) gives each answer a score: the winner beats the loser with probability σ(score difference). The sigmoid σ(z) = 1 / (1 + e−z) maps a difference to a probability; 0 gives 0.5.
Fit the scores so that the picks people made are as likely as possible: maximize each pick’s log-likelihood, or minimize minus it. For a prompt x, a winner yw and a loser yl from the comparison data D:
J-R-M of phi equals minus the expected value, over x, y-w and y-l drawn from D, of log sigma of R-M-phi of x and y-w, minus R-M-phi of x and y-l.
For each comparison, score the winner yw and the loser yl, and subtract. Pass the difference through σ, take the log and flip the sign. Average over all comparisons.
With made-up scores 1.5 and 0.5: σ(1.0) = 0.731, so the loss is −log 0.731 = 0.313. Equal scores give 0.693; backwards scores, a gap of −1.0, give 1.313. The gradient widens the gap at rate 1 − σ, here 0.269.
Two careful labelers each ranked four replies: 6 comparisons each. Train from zero. Then add a rushed labeler who likes padding, and train again.
Illustrative toy: one score per reply, fitted by gradient descent on the loss above with a small weight penalty (0.01). The rushed labeler ranks the padded reply first. Computed live.
InstructGPT’s labelers ranked 4 to 9 answers per prompt; K answers give K(K − 1)/2 comparisons, from 6 to 36. Its reward model was a 6B copy of the SFT model with one score as output, trained on 33k prompts. The lecture’s advice: check the reward model on held-out human picks first. Stiennon et al. (2020) found that doubling the data added about 1.1 points of accuracy; doubling the model, about 1.8.
Symptom: after tuning, answers get longer and gushier, and people like them less.
Measure: the score of a padded reply against a short true one. With the rushed labeler, padding climbs from −0.67 to 0.26.
Fix: test the reward model on held-out picks, audit labelers, and write clear guidelines.
Chapter 5
Charge the model for drifting from where it started, and watch the reward model’s score and people’s rating part ways
RLHF (reinforcement learning from human feedback; Christiano et al., 2017; Stiennon et al., 2020) puts the pieces together. Start from an instruction-tuned model and freeze a copy of it: the reference, pref. Train the reward model of Chapter 4. Then copy the reference into a trainable model pθ and optimize it with RL.
The reward model is only a model of people, and it has blind spots. An optimizer that chases its score finds them: padding, flattery, confident made-up facts. The lecture calls this reward hacking. So RLHF charges a price whenever pθ gives an answer more probability than the reference did. β, the leash, scales the price.
R of x and y equals R-M-phi of x and y, minus beta times log of p-theta of y given x, over p-ref of y given x.
Score the answer with the reward model. Find how much more likely the tuned model makes it than the reference did, take the log, and multiply by β. Subtract that price from the score.
The hero at β = 0.6: the padded reply has RM = 1.6. The tuned model gives it 0.346 against the reference’s 0.10, a ratio of 3.46, and log 3.46 = 1.24. The price is 0.6 × 1.24 = 0.74, so R = 1.6 − 0.74 = 0.86.
Across is the drift, the KL from the reference. Blue is the reward model’s average score; yellow is people’s average rating. Drag β from tight to loose.
Illustrative: the hero’s toy, with the best leashed model for each β (Chapter 6). The shape matches what Stiennon et al. (2020, Figure 5) measured with real labelers.
Stiennon et al. saw this with real summaries. Light optimization made them better by labelers’ judgment. Further optimization made true preference fall, until the reward model was anti-correlated with people. The lecture adds a quieter case: chatbots rewarded for sounding authoritative learn to sound that way regardless of truth.
InstructGPT ran this with PPO, β = 0.02 and a clip of 0.2. Labelers preferred the 175B InstructGPT to GPT-3 85 ± 3% of the time, and to GPT-3 with a few-shot prompt 71 ± 4%. The cost is four networks at once: the policy, the reference, the reward model and the value model.
Symptom: the reward model’s score keeps climbing while the answers get longer and gushier.
Measure: people’s rating of fresh samples: 0.37 at β = 0.6, 0.01 at β = 0.1 in the toy.
Fix: raise β, stop earlier, or improve the reward model where the policy found its blind spot.
Chapter 6
Tilt the reference toward high reward in three steps, and find the one number that every answer then earns
RLHF climbs the reward model’s score minus β times the KL, and PPO climbs it step by sampled step. But which model sits at the top? The lecture’s key fact: this objective has a closed-form answer, a formula with no search.
Call the best model p*. Take the reference, and multiply each answer’s probability by e raised to its score over β. Then divide everything by one number, Z(x), the partition function, so that the probabilities add up to 1.
p-star of y given x equals one over Z of x, times p-ref of y given x, times e to the R-M-phi of x and y over beta.
For each answer, divide its score by β and raise e to that power. Multiply by the reference probability. Add up all the products to get Z(x), and divide each product by Z(x).
The hero’s five replies at β = 0.6: the tilts are 0.19, 5.29, 2.72, 14.39 and 1.95. Times the reference’s 0.15, 0.30, 0.30, 0.10 and 0.15, the weights are 0.028, 1.588, 0.816, 1.439 and 0.292. They sum to Z = 4.16, so p*(padded) = 1.439 / 4.16 = 0.346.
Grey outlines are the reference. Press Next step to tilt, then to divide by Z. Then change β.
Illustrative: the hero’s five replies, the reference probabilities and the reward model’s scores. Computed exactly.
Write the objective for one prompt as a sum: Σy p(y) [ RM(y) − β log( p(y) / pref(y) ) ].
Since RM(y) = β log eRM(y)/β, the bracket is −β log( p(y) / (pref(y) eRM(y)/β) ).
Multiply and divide inside the log by Z, and use p* from the formula: the bracket is −β log( p(y) / p*(y) ) + β log Z.
Summed with weights p(y), the objective is β log Z − β KL(p ‖ p*). A KL is never negative and is 0 only when p = p*. So p* wins, and its objective is β log Z.
A check: at p*, every answer earns the same leashed reward, RM(y) − β log(p*(y) / pref(y)) = β log Z. In the toy, 0.6 × log 4.16 = 0.86, the same 0.86 that Chapter 5 found for the padded reply.
Then why run PPO? Because Z(x) sums over every sentence the model could write, far too many to add up. Chapter 7 shows how DPO never needs Z.
Symptom: the tuned model puts nearly all its probability on one reply.
Measure: at β = 0.1 the padded reply’s tilt is e16, and p* gives it 0.99.
Fix: a larger β flattens the tilt, back toward the reference.
Chapter 7
Solve the formula for the reward, put it into Bradley-Terry, and watch Z cancel
This is the lecture’s longest section. PPO fits a value function, samples online and is sensitive to its settings. DPO (direct preference optimization; Rafailov et al., 2023) removes the RL step in three moves.
Move 1: solve for the reward. Take the log of Chapter 6’s formula:
RM(x, y) = β log( p*(y | x) / pref(y | x) ) + β log Z(x)
Any model pθ defines a reward this way, its implied reward: β log(pθ / pref), plus a term for the prompt alone.
Move 2: put it into Bradley-Terry. Chapter 4’s loss uses only the difference of two rewards for the same prompt. Both carry the same β log Z(x), so it cancels.
Move 3: train on that loss. With pθ in place of p*, the reward model’s loss becomes a loss on the language model itself:
J-D-P-O of theta equals minus the expected value, over x, y-w and y-l drawn from D, of log sigma of beta log of p-theta of y-w over p-ref of y-w, minus beta log of p-theta of y-l over p-ref of y-l.
Run the tuned model and the frozen reference on both answers; sum each answer’s token log-probabilities. Subtract to get each log ratio, times β. Take the winner’s implied reward minus the loser’s, pass it through σ, take the log and flip the sign.
With made-up numbers and β = 0.1: the picked answer’s log ratio is +0.5 nats, the rejected one’s −0.2. The margin is 0.1 × 0.7 = 0.07, σ(0.07) = 0.517, and the loss is −log 0.517 = 0.659, down from 0.693.
Drag how far the model has moved each answer from the reference. Then drag log Z.
Illustrative values. Rafailov et al. used β = 0.1 by default and 0.5 for TL;DR.
So DPO needs two models, the policy and the frozen reference, not four, and no sampling. On TL;DR summarization it beat reference summaries about 61% of the time, against PPO’s 57%, from the same starting model with GPT-4 as judge. The lecture notes that open-source models now almost all use DPO, with variants such as KTO and IPO.
Each pair pushes the picked answer up and the rejected one down, with weight σ(loser’s implied reward − winner’s). A pair ranked wrong weighs nearly 1. At our margin of 0.07 the weight is 0.48; a pair learned to a margin of 3 has 0.05 left.
Symptom: the loss falls, yet the picked answers get less likely.
Measure: track the picked answers’ log-probability, not only the margin. Lowering both, the loser faster, still cuts the loss.
Fix: watch both log-probabilities, use fresher pairs, and stop when held-out picks stop improving.
Chapter 8
Measure how often labelers agree, compare people with an AI judge, and ask whose taste the model learns
Every method so far learns from picks. Someone has to make them. InstructGPT hired about 40 contractors. Its training labelers agreed with each other 72.6 ± 1.5% of the time, and held-out labelers 77.3 ± 1.3%. So about one comparison in four splits two careful people.
Disagreement caps what a reward model can show. Here is a simple model of it. Say each labeler, on each pair, agrees with a hidden consensus with chance a, independently. Two labelers then agree when both match it or both miss it:
agreement = a × a + (1 − a) × (1 − a)
Solve for 72.6%, and a = 0.836: 0.836 × 0.836 + 0.164 × 0.164 = 0.726. Even a perfect reward model, one that knows the consensus, agrees with a fresh labeler only 83.6% of the time. A majority vote of three labelers reaches 92.8%.
Drag how often one labeler matches the consensus. Pink bars show what you can measure; the marks are agreement rates from InstructGPT and AlpacaFarm.
Illustrative model with independent labelers. The marks are reported rates: InstructGPT’s 72.6% and 77.3%, and AlpacaFarm’s 66% for people and 65% for its simulated annotator, each against a human majority.
Can a model make the picks? AlpacaFarm (Dubois et al., 2023), a reading for this lecture, built simulated annotators from GPT-4 prompts. They cost $6 per 1,000 outputs, 50 times less than crowdworkers. They agreed with a human majority 65% of the time, close to the 66% between people. Methods trained on its feedback ranked in the same order as methods trained on human feedback (Spearman correlation 0.98). Both kinds of judge also liked longer outputs: best-of-16 sampling doubled the average length from 278 to 570 characters.
The data also decides what the model is good at. "How Far Can Camels Go?" (Wang et al., 2023) fine-tuned models from 6.7B to 65B parameters on 12 instruction datasets. No single dataset, or mix, was best on every evaluation. Preference-based evaluations also missed capability differences that benchmarks showed.
Last, whose preferences? The lecture notes that labels often come from overseas, low-wage workers, and that annotators’ biases can enter the model. Ryan, Held and Yang (2024) found that alignment created disparities between English dialects and between countries’ opinions (the lesson on that paper).
Symptom: the reward model’s held-out accuracy stalls near 70%, and more training does not help.
Measure: the labelers’ own agreement rate. At 72.6%, the ceiling against one labeler is 83.6%.
Fix: clearer guidelines, several labels per pair with a majority vote, and labelers who represent the users.
Chapter 9
Train RLHF and DPO on the same picks, side by side, and see what each one inherits from the labelers
Here is the whole preference stage on the moon prompt. Labelers compare pairs of the five replies. RLHF fits a reward model to the picks with Bradley-Terry, then runs a policy gradient with the KL price, sampling 64 replies per step. DPO trains on the same picks with its one loss. The grey outlines show Chapter 6’s closed form for the fitted reward: the model both roads aim at.
Choose the leash and the labelers, then press Train. Watch the yellow line: people’s average rating of each model’s replies as it trains.
Illustrative toy, computed live. Labelers pick a reply with Bradley-Terry odds on their own taste (people’s rating, plus a bonus for length when they like length); pairs come from the reference model, and the toy uses the exact expected share of picks. RLHF fits the reward model, then takes 600 policy-gradient steps of 64 samples each; DPO takes 600 full-batch steps. Step sizes scale with β, so every leash trains in the same number of steps.
With careful labelers at β = 0.6, both roads end on the target, where people rate the replies 0.86 on average, up from the reference’s 0.09. RLHF starts late, because it fits a reward model first, and wobbles, because it samples. DPO slides there smoothly. This is the DPO paper’s claim in miniature: same objective, same answer, without the RL loop.
Now switch to labelers who like length. People’s rating climbs at first, while the short story and the padded reply both gain. Then the padded reply takes over, and the rating falls: the target is 0.28 at β = 0.6 and 0.01 at β = 0.2, for both roads. DPO removes the reward model and the sampling. It does not remove the labelers’ taste, because its implied reward fits the picks just as a reward model would.
Symptom: switching from PPO to DPO does not fix long, gushing answers.
Measure: people’s rating of each method’s replies on the same picks. With length-loving labelers at β = 0.6, both end near the target’s 0.28.
Fix: fix the data. Audit the picks for length and style bias, or control for length when you compare.
Chapter 10
Keep the pipeline, the numbers and the code, and see where the field went next
A pretrained model predicts web text, so it often answers an instruction with more instructions. Instruction tuning fine-tunes it on good answers, with the prompt masked. Preference tuning then teaches it which of two answers people like better: RLHF with a reward model and a policy gradient on a KL leash, or DPO with one loss on pairs. Both aim at the same closed-form model, and both inherit the labelers’ taste.
| Stage | Data | Loss | Models in memory | Watch for |
|---|---|---|---|---|
| Instruction tuning (SFT) | instruction and answer pairs | next-token loss on the answer only | 1 | an unmasked prompt; copying noisy answers |
| Reward model | people’s picks between answers | −log σ(score of winner − score of loser) | 1 | low held-out accuracy; labeler bias |
| RLHF (PPO) | prompts, plus the reward model | clipped policy gradient on reward − β × drift | 4: policy, reference, reward, value | reward hacking; unstable runs |
| DPO | prompts with a picked and a rejected answer | −log σ(β × difference of log ratios) | 2: policy, reference | falling log-probability of picked answers; stale pairs |
| Number | Value | Where |
|---|---|---|
| Toy: people’s average rating, pretrained, SFT, preference-tuned at β = 0.6 | −0.87, 0.09, 0.37 | Hero |
| InstructGPT 1.3B against GPT-3 175B | 1.3B preferred, with over 100 times fewer parameters | Chapter 0 |
| Flan-PaLM 540B over PaLM 540B (1.8K tasks) | +9.4 points on average; 75.2% on five-shot MMLU | Chapter 1 |
| LIMA: 1,000 examples, 65B model, equal or better than GPT-4 | 43% of cases | Chapter 1 |
| Bradley-Terry loss at a score gap of 1.0, 0 and −1.0 | 0.313, 0.693, 1.313 | Chapter 4 |
| InstructGPT: β, PPO clip, reward model size | 0.02, 0.2, 6B | Chapter 5 |
| InstructGPT 175B preferred to GPT-3, to few-shot GPT-3 | 85 ± 3%, 71 ± 4% | Chapter 5 |
| DPO against PPO on TL;DR (win rate against references) | about 61% against 57% | Chapter 7 |
| InstructGPT labeler agreement, training and held-out | 72.6 ± 1.5%, 77.3 ± 1.3% | Chapter 8 |
| AlpacaFarm simulated annotator: cost, agreement with people | $6 per 1,000 (50 times cheaper); 65% against 66% | Chapter 8 |
The core as code: the masked SFT loss, the reward model loss and the DPO loss, in PyTorch.
python import torch, torch.nn.functional as F def sft_loss(logits, input_ids, prompt_len): labels = input_ids.clone() labels[:, :prompt_len] = -100 # mask the prompt: graded tokens are the answer only return F.cross_entropy(logits[:, :-1].reshape(-1, logits.size(-1)), labels[:, 1:].reshape(-1), ignore_index=-100) def reward_model_loss(r_win, r_lose): # Bradley-Terry: -log sigma(r_w - r_l) return -F.logsigmoid(r_win - r_lose).mean() def seq_logp(model, input_ids, prompt_len): # sum of the answer's token log-probabilities logp = model(input_ids).logits[:, :-1].log_softmax(-1) tok = logp.gather(-1, input_ids[:, 1:, None]).squeeze(-1) return tok[:, prompt_len - 1:].sum(-1) def dpo_loss(pi_w, pi_l, ref_w, ref_l, beta=0.1): # inputs: seq_logp of policy and frozen reference margin = beta * ((pi_w - ref_w) - (pi_l - ref_l)) # implied reward of winner minus loser; log Z cancels return -F.logsigmoid(margin).mean() # the toy's DPO step: beta 0.1, picked +0.5 nats over the reference, rejected -0.2 print(dpo_loss(torch.tensor([0.5]), torch.tensor([-0.2]), torch.zeros(1), torch.zeros(1))) # 0.6588
The lecture ends on open problems. RLHF still needs costly data, so newer work uses AI feedback (Bai et al., 2022) or fine-tunes models on their own outputs. For maths and code, where an answer can be checked, GRPO (Shao et al., 2024) brings RL back: it replaces the value model with the average reward of a group of samples for the same prompt. And some limits of large models, such as hallucination, may not be solvable with RLHF at all.
Now teach it: press Present or Teach. Explain why the token loss cannot tell a near miss from nonsense, how Bradley-Terry turns picks into scores, and why Z cancels in DPO. Then go back to the hero, set β to 0.2, and predict people’s rating before you look.