RLHF (Reinforcement Learning from Human Feedback)
Preferences train a reward model, and the reward trains the policy on a leash. The InstructGPT recipe, and why DPO drops the reward model.
Jump to a section
What InstructGPT had instead of a loss
In OpenAI's InstructGPT study, labelers preferred the outputs of a 1.3B-parameter model to those of the 175B GPT-3, a model with 100 times more parameters. The smaller model had been trained on human preferences; the larger one had only been pretrained.
There is no loss function for "be more helpful". What the team had was people: labelers who wrote example answers and, for each prompt, ranked between 4 and 9 model outputs from best to worst. Reinforcement learning from human feedback (RLHF) is the machinery that turns those rankings into a number a policy can be optimized against.
Three stages, three models
The pipeline trains a reward model as a stand-in for the labelers, then uses reinforcement learning to push the language model toward responses that the stand-in scores highly, while a penalty keeps it close to where it started.
Supervised fine-tuning (SFT). Fine-tune the pretrained model on labeler-written demonstrations. InstructGPT used about 13k training prompts here. The result imitates good answers but has never been told which of two answers is better.
Reward model. Train a model to score a prompt and response with one scalar, using the rankings. InstructGPT's reward model saw about 33k prompts, and each ranking of outputs yields pairwise comparisons.
Policy optimization. Start the policy from the SFT model and update it with PPO to maximize the reward model's score, with a KL penalty against the frozen SFT model. InstructGPT ran this stage on about 31k prompts.
First, learn from demonstrations
Choose one update to see probability move toward the demonstrated outcome. Open the visualization in Studio to change the target.
The visual loads as you reach this section.
Each stage does a different job. SFT imitates, the reward model scores, and RL changes the policy using that score. DPO, covered below, is an alternative to the last two stages, not an extra stage after them.
A reward model learns only differences
A ranking says which response a labeler preferred. It does not say by how much, or on what absolute scale. The Bradley-Terry model turns this into a probability: the chance that beats is . Fitting it means minimizing
A reward gap of 1.7 predicts the preferred response wins about 85% of the time, since .
Only differences enter the loss, so adding any constant to every response for a given prompt changes nothing. The reward model's absolute scores are arbitrary up to that shift. That is fine for RL, which compares responses, but it means a raw score of 3.2 means nothing on its own.
InstructGPT used 6B reward models because 175B reward model training could be unstable. It also put all comparisons from one ranking into the same batch element. Each response appears in of those comparisons, and the paper reports that shuffling them into the dataset as independent examples made the reward model overfit within a single pass.
The leash on the policy
The policy is trained to maximize reward while staying near the reference policy , the SFT model:
This objective has a closed-form optimum: . The reference distribution is reweighted by exponentiated reward, and sets how sharp the reweighting is. Small lets the policy concentrate on whatever the reward model scores highest; large keeps it close to the reference.
That matters because the reward model is a learned proxy. Suppose it gives its top score to a response that exploits one of its flaws:
1from math import exp
2
3
4def kl_optimal_policy(ref_probs, rewards, beta):
5 # pi*(y) is proportional to pi_ref(y) * exp(r(y) / beta)
6 weights = [p * exp(r / beta) for p, r in zip(ref_probs, rewards)]
7 total = sum(weights)
8 return [w / total for w in weights]
9
10
11ref = [0.5, 0.3, 0.2] # reference policy over three responses
12reward = [0.0, 1.0, 3.0] # learned reward; the third response exploits a flaw
13
14loose = kl_optimal_policy(ref, reward, beta=0.25)
15tight = kl_optimal_policy(ref, reward, beta=10.0)
16assert loose[2] > 0.99 # almost all mass on the exploit
17assert abs(tight[0] - 0.454) < 1e-3 # still close to the reference 0.5
18assert all(abs(a - b) < 1e-6 for a, b in zip(kl_optimal_policy(ref, reward, 1e9), ref))PPO does not compute this optimum; it approaches it with sampled rollouts. InstructGPT applied the KL as a per-token penalty inside the reward, which summed over a sampled response gives . PPO's clipped surrogate discourages large changes in the policy ratio within one update, but clipping is not a bound on how far the policy drifts over many updates. That job belongs to .
The setup holds four networks: the policy, the frozen reference, the reward model, and a value model that estimates expected reward for PPO's advantages. InstructGPT initialized its value model from the reward model.
DPO keeps the preferences and drops the reward model
Direct Preference Optimization starts from the closed-form optimum above. Solving it for the reward gives , where normalizes over responses. Substituting into the Bradley-Terry loss, cancels in the difference:
The policy is now trained directly on preference pairs with a classification-style loss. Standard offline DPO needs no separately fitted reward model, no value model and no rollouts; the reference log-probabilities can be computed once and stored. It is still gradient-based training of network weights, not a closed-form solution.
What DPO gives up is the online loop. PPO samples fresh responses from the current policy and can optimize any reward, including non-differentiable ones such as test results. Offline DPO learns only from the pairs in its dataset. The derivation shares PPO's optimum in theory, but on finite data the two methods do not produce the same model.
In code
Two pieces of a PPO-based setup: the reward model loss and a sequence-level KL-shaped reward. This is not a full PPO implementation.
1import torch
2import torch.nn.functional as F
3from torch import Tensor
4
5
6def reward_model_loss(r_chosen: Tensor, r_rejected: Tensor) -> Tensor:
7 # Bradley-Terry: preferred reward should exceed rejected reward
8 return -F.logsigmoid(r_chosen - r_rejected).mean()
9
10
11def kl_shaped_reward(
12 reward: Tensor, logp_policy: Tensor, logp_ref: Tensor, beta: float = 0.1
13) -> Tensor:
14 # Inputs have shape (batch,): one reward and summed response log-prob per sequence.
15 # Sequences are sampled from the policy; the expectation of this log-ratio is KL.
16 kl = logp_policy - logp_ref # an individual sample may be negative
17 return reward - beta * kl
18
19
20r_chosen, r_rejected = torch.tensor([2.1]), torch.tensor([0.4])
21assert reward_model_loss(r_chosen, r_rejected) < reward_model_loss(r_rejected, r_chosen)Where it goes wrong
Reward hacking. The policy optimizes the reward model, not the labelers. Any pattern the reward model over-scores, the policy will find and amplify, and a small lets it go further. Watch reward-model score and held-out human preference together; when the first rises and the second stalls, the policy is exploiting the proxy.
Shortcuts in the labels. If labelers tended to prefer longer or more confident answers, the reward model learns length and tone as cheap proxies for quality, and RL produces more of both. A high reward on the training distribution does not establish factuality or safety on new prompts.
Disagreement treated as signal. Comparisons avoid asking labelers for a shared numeric scale, but labelers still disagree and carry their own biases. The reward model averages over them, so the policy is tuned to the labeler pool, not to every user.
The alignment tax. InstructGPT's PPO models regressed on some public NLP benchmarks. Mixing pretraining gradients into the PPO updates (the paper's PPO-ptx variant) reduced those regressions.
Misdescribed methods. Not every preference method runs an online reward-model rollout loop. When comparing methods, identify which quantities are learned, which are fixed, and where the preference data enters.
Sources and further reading
- Ouyang et al., Training language models to follow instructions with human feedback: SFT, preference rewards, and PPO-based training.
- Rafailov et al., Direct Preference Optimization: preference-loss derivation and its assumptions.
Keep going
Ask your own question about RLHF (Reinforcement Learning from Human Feedback), or start a guided session on it: six short pages, from the big picture to code you can run.