RL from Human Feedback (RLHF) and DPO¶
TL;DR
A pre-trained language model predicts text; it does not try to be helpful. RLHF fixes that in three stages: fine-tune on good examples (SFT), train a reward model from human comparisons ("reply A is better than reply B"), then use RL (usually PPO) to make the model produce replies the reward model likes, while a KL penalty keeps it close to where it started. DPO reaches the same goal with a single supervised-style loss on the comparisons: no reward model and no RL loop.
Level: advanced · Prerequisites: Policy gradients, Actor-critic (PPO). Knowing what a language model does ("predict the next token") helps.
Why should I care?¶
This is where the whole RL series pays off. In the first lesson the chatbot row of the table said: state = the conversation so far, action = the next word, reward = a score for the finished reply. This lesson fills in that row.
- It is how chat assistants are aligned. OpenAI's InstructGPT paper (Ouyang et al., 2022) showed that labellers preferred replies from a 1.3-billion-parameter model fine-tuned with RLHF over replies from the 175-billion-parameter GPT-3 it started from. The same recipe, or a close relative, is used to fine-tune most chat assistants.
- It turns "be helpful" into something you can optimise. Nobody can write a formula for a good answer. But people can easily say which of two answers is better. RLHF learns a reward from those judgements.
- It shows RL's classic failure modes in a modern setting. Reward hacking, distribution shift and the need for a "leash" on the policy all show up, and the fixes reuse ideas from PPO and from the value-function lessons.
Step 1 — The problem: a text predictor is not an assistant¶
A pre-trained language model has read a huge amount of text and learned one thing: given some text, predict the next token (a word or a piece of a word). Ask it a question and it continues the text in whatever way is most typical of the internet. That might be an answer. It might also be three more questions, a forum argument, or a confident wrong answer.
| Prompt: "Why is the sky blue? Explain for a 10-year-old." | |
|---|---|
| What a pre-trained model might write | "Why is grass green? Why is the sea salty? 50 science questions kids ask…" (it continues the list it thinks it is reading) |
| What we want | "Sunlight is made of all colours. Air bounces blue light around much more than red, so blue comes at you from every part of the sky." |
Supervised fine-tuning (SFT) is the obvious first fix: collect prompts with good human-written answers and train the model to imitate them (ordinary next-token cross-entropy). It helps a lot. But it has limits:
- Writing ideal answers is slow and expensive, and the model can only be as good as the demonstrations.
- Imitation never says what is bad. The model sees good answers, not "this answer is wrong", "this one is rude", "this one is long-winded".
- "Helpful, honest, harmless" is hard to write as a reward function. You can't code
def reward(answer): ...for helpfulness.
The key observation behind RLHF (Christiano et al., 2017) is that comparing is much easier than writing or scoring. Show a person two replies and ask "which is better?". They can answer in seconds, and they don't need to agree on an absolute scale. Those comparisons are the training signal.
Step 2 — A language model is a policy¶
To use RL, we need the familiar pieces from lesson 1: states, actions, a policy and a reward. A language model already is a policy. Generating a reply token by token is an episode:
The probability of a whole reply \(y = (y_1, \dots, y_T)\) is the product of the token probabilities, so its log-probability is a sum:
That is exactly the \(\log \pi_\theta(a_t \mid s_t)\) term from the policy gradients lesson, summed over the episode. So REINFORCE, actor-critic and PPO all apply without change. The hard part is the reward.
What to notice
- The "environment" is trivial: appending a token is deterministic, and nothing pushes back. All the difficulty sits in the reward, which is given only once per reply.
- Because the reward is per reply, it is common to treat the whole reply as one big action. That simpler view (a "contextual bandit", like the bandits lesson with a prompt as context) is the one the rest of this lesson uses, and it is the view DPO is built on.
Step 3 — The RLHF recipe in three stages¶
The pipeline popularised by InstructGPT (Ouyang et al., 2022), building on Christiano et al. (2017) and Stiennon et al. (2020), has three stages. Click through it:
What to notice
- Humans appear only once, as labellers of comparisons. Everything after that is automatic: the reward model stands in for the humans during RL, so the policy can be scored millions of times.
- The SFT model plays two roles in stage 3: it is the starting point of the policy and the reference \(\pi_{\text{ref}}\) that the KL penalty measures distance from.
Step 4 — Learning a reward from comparisons (Bradley–Terry)¶
The labellers give us triples \((x, y_w, y_l)\): a prompt, the winning (preferred) reply and the losing one. We want a reward model \(r_\phi(x, y)\) that outputs one number per reply. How do we turn "A beat B" into a training signal for numbers?
The Bradley–Terry model (from 1952, originally for ranking chess players and sports teams) says: the probability that A beats B depends only on the difference of their scores, squashed through the sigmoid \(\sigma(z) = 1/(1+e^{-z})\):
Training the reward model is then ordinary maximum likelihood, i.e. logistic regression on the score difference:
Try it: set the reward model's two scores and see what it predicts and how much loss it pays for each possible human choice.
What to notice
- Equal scores mean a coin flip: \(\Delta = 0\) gives \(P = 0.5\) and loss \(\log 2 \approx 0.693\) either way. That is where training starts.
- Getting it right pays little, getting it wrong costs a lot. At \(\Delta = 2\) the model says \(P = 0.881\); if humans chose A the loss is only 0.127, but if they chose B it is 2.127. At \(\Delta = 4\) the loss for a correct prediction is down to 0.018, so confident, correct pairs stop contributing much.
- Add +1 to both: nothing changes. Rewards are only defined up to a constant. A reward model's absolute numbers mean nothing; only differences between replies to the same prompt do. (This shift-freedom is exactly what makes DPO possible in Step 6.)
- Humans disagree and are noisy, so a perfect reward model would not hit 100% accuracy. In the code below, the true hidden preference itself only predicts 84.8% of held-out human choices.
In practice the reward model is a copy of the SFT model with the next-token output layer swapped for a single scalar output. In the toy version below, each "reply" is just three features (helpful, correct, length) and the reward model is linear. The hidden human taste ignores length:
import torch
import torch.nn.functional as F
torch.manual_seed(0)
X = torch.randn(500, 3) # 500 responses, features: helpful, correct, length
w_true = torch.tensor([1.5, 2.0, 0.0]) # the hidden "human taste": length doesn't matter
r_true = X @ w_true
# 5000 comparisons: the labeller prefers a over b with probability sigmoid(r_a - r_b)
a, b = torch.randint(0, 500, (5000,)), torch.randint(0, 500, (5000,))
a_wins = torch.rand(5000) < torch.sigmoid(r_true[a] - r_true[b])
chosen, rejected = torch.where(a_wins, a, b), torch.where(a_wins, b, a)
train, test = slice(0, 4000), slice(4000, 5000)
def reward_model_loss(r_chosen, r_rejected):
"""Bradley-Terry: -log P(chosen beats rejected) = -log sigmoid(r_w - r_l)."""
return -F.logsigmoid(r_chosen - r_rejected).mean()
reward_model = torch.nn.Linear(3, 1, bias=False) # in real RLHF: a transformer with a scalar head
opt = torch.optim.Adam(reward_model.parameters(), lr=0.05)
for step in range(301):
loss = reward_model_loss(reward_model(X[chosen[train]]).squeeze(-1),
reward_model(X[rejected[train]]).squeeze(-1))
opt.zero_grad(); loss.backward(); opt.step()
if step % 100 == 0:
print(f"step {step:3d} loss {loss.item():.3f}")
with torch.no_grad():
r = reward_model(X).squeeze(-1)
acc = (r[chosen[test]] > r[rejected[test]]).float().mean().item()
acc_true = (r_true[chosen[test]] > r_true[rejected[test]]).float().mean().item()
print("learned weights:", [round(v, 2) for v in reward_model.weight[0].tolist()])
print(f"held-out accuracy: reward model {acc:.3f}, true reward {acc_true:.3f}")
step 0 loss 0.774
step 100 loss 0.334
step 200 loss 0.334
step 300 loss 0.334
learned weights: [1.41, 1.86, 0.05]
held-out accuracy: reward model 0.850, true reward 0.848
From nothing but "A beat B" labels, the reward model recovered the hidden taste (\([1.41, 1.86, 0.05]\) vs the true \([1.5, 2.0, 0.0]\)) and predicts held-out human choices as well as the true reward does. With a real network and real text, it is never this clean: the reward model sees a limited set of comparisons and has to generalise to replies unlike any it was trained on. That is the weak spot Step 5 deals with.
Step 5 — RL fine-tuning with a KL leash¶
Now the RL stage. The policy \(\pi_\theta\) (initialised from the SFT model) writes replies, the reward model scores them, and we maximise
The first term is "get a high score". The second is a leash: the KL divergence \(\mathrm{KL}(\pi \| \pi_{\text{ref}}) = \mathbb{E}_{y \sim \pi}\big[\log \tfrac{\pi(y \mid x)}{\pi_{\text{ref}}(y \mid x)}\big]\) measures how far the policy has drifted from the SFT model, and \(\beta\) sets how strongly drifting is punished.
The optimiser is usually PPO from the actor-critic lesson. In InstructGPT-style implementations the KL term is folded into the reward, \(r_\phi(x, y) - \beta \log \frac{\pi_\theta(y \mid x)}{\pi_{\text{ref}}(y \mid x)}\), often spread over tokens as a per-token penalty, and a value head (the critic) estimates advantages. So during training four networks are in play: the policy, the frozen reference, the frozen reward model and the critic.
Why the leash? The reward model is only a proxy for what people want. It was trained on replies from the SFT model, so it is only trustworthy near the SFT model's replies. A policy that is pushed hard to maximise the proxy will find the places where the proxy is wrong: replies the reward model over-rates. This is reward hacking or reward over-optimisation, an instance of Goodhart's law ("when a measure becomes a target, it ceases to be a good measure"). Gao et al. (2022) measured it directly: as RL pushes the policy further from its start, the learned reward keeps rising while the true quality first rises and then falls.
The KL-regularised objective has a beautiful exact solution. For one prompt, the best possible policy is
Reweight the reference model by the exponentiated reward. (Why: a few lines of algebra show \(\mathbb{E}_\pi[r] - \beta\,\mathrm{KL}(\pi \| \pi_{\text{ref}}) = \beta \log Z(x) - \beta\,\mathrm{KL}(\pi \| \pi^*)\). The first term doesn't depend on \(\pi\), and a KL is zero only when the two distributions are equal, so \(\pi = \pi^*\) is the maximum.) We can't compute \(Z(x)\) for a real model, since it sums over every possible reply. That is why RLHF needs RL to approximate \(\pi^*\). But in a toy with five candidate replies we can compute it exactly.
Try it: the prompt is "Why is the sky blue?", asked by a 10-year-old. The SFT model \(\pi_{\text{ref}}\) spreads its probability over five replies: A a simple correct answer, B a correct but technical one, C a wrong one ("it reflects the ocean"), D a refusal, and E gushing flattery ("What a WONDERFUL question!!!") with no real answer. The reward model got E badly wrong: it scores it 3.0, the highest of all, while its true quality is −0.5. Lower \(\beta\) (loosen the leash) and watch.
What to notice
- Large \(\beta\) = tight leash. At \(\beta = 10\), \(\pi^*\) is almost \(\pi_{\text{ref}}\) (KL 0.012) and the true quality barely moves from the starting model's −0.275 to −0.112.
- Moderate \(\beta\) helps for real. At \(\beta = 1\) the policy shifts mass from C and D to A (0.391), and the true quality rises to 0.715. Scanning \(\beta\), true quality peaks at 0.717 around \(\beta \approx 1.05\).
- Small \(\beta\) = reward hacking. At \(\beta = 0.25\) the policy puts 0.947 of its mass on the flattery reply E. The reward-model score climbs to 2.945, but true quality falls to −0.368, worse than the model we started with. As \(\beta \to 0\) the policy collapses onto E entirely (score 3.0, quality −0.5). The purple and green curves on the right are Goodhart's law in one picture.
- Tick "fix the reward model's mistake on E": now the two curves move together, and loosening the leash keeps helping (true quality 1.976 at \(\beta = 0.25\), nearly all mass on A). The KL penalty is insurance against the reward model's errors, which in real life you can't see.
- Notice \(\pi^*\) can only move mass between replies that \(\pi_{\text{ref}}\) already gives some probability to: a reply with \(\pi_{\text{ref}} = 0\) stays at 0. RLHF sharpens and re-ranks what the SFT model can already say. It doesn't teach it things it could never produce.
Step 6 — DPO: skip the reward model and the RL loop¶
RLHF with PPO works, but it is heavy: two training stages, four networks in memory, sampling replies inside the training loop, and PPO's many hyperparameters. Direct Preference Optimization (DPO; Rafailov et al., 2023) asks: since we know the exact form of the optimal policy, can we skip straight to it?
The trick is to read the closed form from Step 5 backwards. Take logs and solve for the reward:
Any policy implies a reward: \(\beta\) times its log-ratio to the reference, plus a term that depends only on the prompt. Now plug this into the Bradley–Terry model from Step 4. Bradley–Terry only uses differences of rewards for the same prompt, so the troublesome \(\beta \log Z(x)\) cancels:
Replace \(\pi^*\) by our trainable policy \(\pi_\theta\) and fit it to the human comparisons by maximum likelihood, exactly as we fitted the reward model:
That's it: the reward model loss with the reward replaced by \(\beta \log \pi_\theta / \pi_{\text{ref}}\). The policy is the reward model ("your language model is secretly a reward model" is the paper's subtitle). Training needs only the log-probabilities of the two fixed replies under \(\pi_\theta\) and \(\pi_{\text{ref}}\): no sampling, no critic, no reward model. Its gradient pushes \(\log \pi_\theta(y_w)\) up and \(\log \pi_\theta(y_l)\) down, weighted by \(\sigma(\hat r_l - \hat r_w)\), i.e. by how badly the implicit rewards \(\hat r = \beta \log \pi_\theta / \pi_{\text{ref}}\) currently rank the pair.
Try it: the same five replies and the same \(\pi_{\text{ref}}\) as above. The training data are 200 human comparisons (20 for each pair of replies), drawn from the hidden true quality. Each click runs plain gradient descent (learning rate 1) on the DPO loss.
What to notice
- Step 0: the policy equals \(\pi_{\text{ref}}\), every implicit reward is 0 and the loss is \(\log 2 = 0.693\), just like an untrained reward model.
- With \(\beta = 1\): after 10 steps A has grown from 0.15 to 0.377 and C has shrunk from 0.30 to 0.126. After 200 steps the policy is \([0.65, 0.268, 0.028, 0.039, 0.015]\) and the loss has levelled off at 0.388.
- It lands on the green ticks. Fit a Bradley–Terry reward model \(\hat r\) to the same 200 comparisons and compute \(\pi_{\text{ref}} \exp(\hat r/\beta)/Z\): you get \([0.651, 0.267, 0.028, 0.039, 0.015]\), the same policy. DPO and "reward model + exact KL-regularised RL" reach the same answer on this toy, which is the claim of the DPO paper.
- \(\beta\) is still the leash. \(\beta = 0.5\) ends further from \(\pi_{\text{ref}}\) (A at 0.884, KL 1.485, true quality 1.877); \(\beta = 2\) stays closer (A at 0.405, KL 0.295, true quality 0.773).
- The flattery reply E never gets a chance here: humans compared it directly and it lost most of its comparisons, and there is no separate reward model to fool. That doesn't make DPO immune to over-optimisation, though; with few or one-sided comparisons it can also drift far from \(\pi_{\text{ref}}\) in ways the data don't justify.
Step 7 — RLHF with PPO vs DPO, and what came next¶
| RLHF with PPO | DPO | |
|---|---|---|
| What is trained | a reward model, then the policy (plus a critic) | the policy only |
| Networks in memory during training | policy, reference, reward model, critic | policy, reference |
| Data used while training the policy | fresh replies sampled from the current policy (on-policy) | a fixed dataset of (prompt, winner, loser) |
| Loss | PPO's clipped policy-gradient objective on \(r - \beta\,\mathrm{KL}\) | a logistic loss on log-probability ratios |
| Strengths | can keep exploring and score new replies; a reward model can be reused | simple, stable, cheap; ordinary supervised training code |
| Weak spots | complex and sensitive to hyperparameters; reward hacking of the reward model | can't score replies outside its dataset; depends heavily on the quality and coverage of the comparisons |
Neither one is "the winner": both are widely used, and they solve the same objective in theory. Other well-known variations change one ingredient each:
- Who labels? In RLAIF, an AI model produces the preference labels instead of people. Constitutional AI (Bai et al., 2022) does this guided by a short written list of principles.
- Which reward? For maths or code, the reward can come from a checker (is the final answer right? do the tests pass?) instead of a learned model, which removes one source of reward hacking.
- Which RL algorithm? GRPO (Shao et al., 2024, in the DeepSeekMath paper) drops PPO's critic: it samples a group of replies per prompt and uses the group's average reward as the baseline, the same baseline idea as in REINFORCE.
- Which preference loss? Many DPO variants (IPO, KTO, ...) change the loss, for example to cope with noisy labels or with single "thumbs up / thumbs down" ratings instead of pairs.
Putting it all together¶
Both routes start from the same human comparisons and the same SFT model, and aim for the same KL-regularised optimum \(\pi^* \propto \pi_{\text{ref}} \exp(r/\beta)\):
Here is DPO on the five-reply toy from the widget, with a compact dpo_loss. In a real model, logp_w would be the sum of token log-probabilities of the winning reply (Step 2), computed by the policy, and ref_logp_w the same under the frozen SFT model:
import itertools
import torch
import torch.nn.functional as F
torch.manual_seed(0)
names = ["A simple", "B technical", "C wrong", "D refuses", "E flattery"]
ref = torch.tensor([0.15, 0.20, 0.30, 0.30, 0.05]) # π_ref: the SFT model's probabilities
quality = torch.tensor([2.0, 1.0, -1.5, -1.0, -0.5]) # hidden human taste (never shown to DPO)
# 20 human comparisons for every pair of responses
winners, losers = [], []
for i, j in itertools.combinations(range(5), 2):
for _ in range(20):
i_wins = torch.rand(()) < torch.sigmoid(quality[i] - quality[j])
winners.append(i if i_wins else j); losers.append(j if i_wins else i)
W, L = torch.tensor(winners), torch.tensor(losers)
def dpo_loss(logp_w, logp_l, ref_logp_w, ref_logp_l, beta):
"""-log sigmoid( beta * [log π/π_ref (y_w) - log π/π_ref (y_l)] )"""
h = beta * ((logp_w - ref_logp_w) - (logp_l - ref_logp_l))
return -F.logsigmoid(h).mean()
beta = 1.0
theta = ref.log().clone().requires_grad_(True) # the policy's logits; start at π_ref
opt = torch.optim.SGD([theta], lr=1.0)
for step in range(201):
logp = torch.log_softmax(theta, dim=0)
loss = dpo_loss(logp[W], logp[L], ref.log()[W], ref.log()[L], beta)
opt.zero_grad(); loss.backward(); opt.step()
if step in (0, 10, 50, 200):
print(f"step {step:3d} loss {loss.item():.3f} π = {[round(p, 3) for p in logp.exp().tolist()]}")
step 0 loss 0.693 π = [0.15, 0.2, 0.3, 0.3, 0.05]
step 10 loss 0.448 π = [0.377, 0.313, 0.126, 0.147, 0.038]
step 50 loss 0.390 π = [0.592, 0.293, 0.041, 0.054, 0.019]
step 200 loss 0.388 π = [0.65, 0.268, 0.028, 0.039, 0.015]
For real models, Hugging Face's TRL library implements reward-model training, PPO, DPO and GRPO trainers on top of transformers.
Common misconceptions¶
\"RLHF teaches the model new knowledge.\"
Mostly it doesn't. The optimal policy \(\pi_{\text{ref}} \exp(r/\beta)/Z\) only reweights replies the starting model could already produce. RLHF changes which of its possible behaviours the model prefers (helpful, honest, on-topic, polite), not what it knows. Knowledge comes from pre-training.
\"The reward model knows what humans want.\"
It is a learned approximation from a limited set of comparisons, and it can be confidently wrong on replies unlike its training data, such as the flattery reply in the widget. Optimise it too hard and you get reward hacking. That's why there is a KL penalty, and why teams keep collecting fresh comparisons.
\"DPO isn't really RL, so it has nothing to do with this course.\"
DPO is trained like supervised learning, but it solves the same KL-regularised RL objective as RLHF; its loss is derived from that objective's exact solution. On the toy above it reaches the same policy as "reward model + optimal RL". It is RL's answer, computed in closed form.
\"A smaller β is always better because the model improves more.\"
A smaller \(\beta\) lets the policy move further from \(\pi_{\text{ref}}\), and the proxy reward always goes up. Whether the true quality goes up depends on whether the reward (or the preference data) is right in the region the policy moves into. In the KL widget, true quality peaks at a moderate \(\beta\) and then falls.
\"Rewards from the reward model are on a meaningful absolute scale.\"
Bradley–Terry only constrains differences: adding a constant to every score changes nothing. A reward of 3.2 means nothing alone; it only means something compared with other replies to the same prompt.
Check yourself¶
1. In the RL view of a language model, what are the state, the action, the episode and the reward?
State: the prompt plus all tokens generated so far. Action: the next token (from the whole vocabulary). Episode: generating one complete reply. Reward: in RLHF, a single score from the reward model for the finished reply (plus a KL penalty), so rewards are sparse and delayed.
2. The reward model gives reply A a score of 3 and reply B a score of 1. What probability does Bradley–Terry give to humans preferring A, and what is the loss if they actually preferred B?
\(\Delta = 3 - 1 = 2\), so \(P(A \succ B) = \sigma(2) = 0.881\). If humans chose B, the loss is \(-\log \sigma(-2) = 2.127\): a confident wrong prediction is punished heavily.
3. What would happen in RLHF if you set β = 0?
Nothing would stop the policy from drifting as far as it likes to maximise the reward model's score. It would find replies the reward model over-rates (reward hacking) and could collapse onto a few of them, losing fluency and diversity. In the KL widget, very small \(\beta\) puts essentially all mass on the flattery reply E, with true quality −0.5, worse than the starting model.
4. Why does the partition function Z(x) disappear in the DPO loss, and why does that matter?
DPO writes the reward as \(\beta \log \frac{\pi(y \mid x)}{\pi_{\text{ref}}(y \mid x)} + \beta \log Z(x)\). Bradley–Terry uses only the difference of two rewards for the same prompt, so the \(\beta \log Z(x)\) terms cancel. That matters because \(Z(x)\) is a sum over every possible reply and can't be computed; without the cancellation the DPO loss would be intractable.
5. Give one advantage of RLHF with PPO over DPO, and one advantage of DPO over RLHF with PPO.
PPO: it is on-policy, so it scores fresh replies from the current model and can improve beyond the replies in a fixed dataset; the reward model can also be reused. DPO: it is much simpler and cheaper, with no reward model, no critic and no sampling during training, and it trains stably with ordinary supervised code.
Further reading¶
- Paul Christiano et al., Deep Reinforcement Learning from Human Preferences (2017). The original idea: learn a reward from comparisons of short clips, then run RL on it.
- Nisan Stiennon et al., Learning to Summarize from Human Feedback (2020). The reward model + PPO + KL recipe applied to language.
- Long Ouyang et al., Training Language Models to Follow Instructions with Human Feedback (2022). InstructGPT: SFT → reward model → PPO.
- Rafael Rafailov et al., Direct Preference Optimization: Your Language Model is Secretly a Reward Model (2023). The DPO derivation in full.
- Leo Gao, John Schulman & Jacob Hilton, Scaling Laws for Reward Model Overoptimization (2022). Goodhart's law measured.
- Yuntao Bai et al., Constitutional AI: Harmlessness from AI Feedback (2022). AI-generated preference labels guided by written principles.
- Nathan Lambert, Reinforcement Learning from Human Feedback (free online book). A thorough, practical overview.
Next lessons: this is the last lesson of the reinforcement-learning series. Continue with the Generative AI section of the roadmap, starting with Large language models (coming soon).