Module 21 · Part V · From Model to Assistant
Preference Learning: RL, RLHF and DPO
A Reinforcement Learning Primer, Policy Gradients, PPO, Reward Models, RLHF, and Direct Preference Optimization
By the end you can explain RLHF and DPO from first principles, including the RL vocabulary.
Check yourself ▸
Before you start, can you already…
- ☐implement a decoding loop with a KV cache and choose a decoding strategy for a task. (Module 12)
- ☐use the rules for expectation and variance, read conditional probability notation, and explain cross-entropy, perplexity and KL divergence. (Module 3)
If any is shaky, the linked module teaches it.
Instruction tuning (section 20.1) teaches by imitation, so it cannot make a model better than its examples, which are expensive to write. It is far easier for people to say which of two answers is better. This module turns such judgments into training, using a small piece of reinforcement learning (RL), which learns from scores rather than correct answers, then builds the reward model, the RLHF loop, and DPO, the shortcut that removes most of that machinery. Module 22 uses the same tools to train reasoning.
21.1 A Reinforcement Learning Primer
- An agent takes actions in an environment, each changing the state it sees next.
- A policy is its rule for acting: a distribution over actions given the state.
- A reward scores an outcome. An episode runs from start to finish, and its return is its total reward.
- The goal is to adjust the policy to maximize the expected return.
Each has a language-model counterpart:
| RL | language model |
|---|---|
| state | the prompt plus the tokens generated so far |
| action | the next token |
| policy | the model's next-token distribution |
| episode | generating one complete response to a prompt |
| reward | a score for the finished response, usually given once at the end |
So a language model already is a policy: is the product of its next-token probabilities (section 3.3). The objective is the expected reward of its responses:
reads "the response is sampled from the model". Unlike supervised learning, there is no correct answer to imitate, only a score for whatever the model produced, so it must explore and learn from how its responses scored.
21.2 Policy GradientsRun this section
averages over responses sampled from the model itself, and sampling has no derivative. The log-derivative trick, , gets around it:
This policy gradient is estimated from samples by REINFORCE (Williams, 1992): sample a response, score it, and step toward making it more likely, scaled by its reward. is the gradient of the response's log-probability, which backpropagation computes as in training.
The estimate is right on average but very noisy: if every reward is positive, every sample is pushed up, and only the relative sizes carry information. The fix subtracts a baseline , an estimate of the typical reward:
The average gradient is unchanged, since , but the noise falls: better-than-usual responses are pushed up and worse ones down. is the advantage.
Three actions with average rewards 5.0, 5.5 and 6.0 (plus noise): all positive, and close together relative to their size. Every line is one action’s probability over 400 steps; several independent runs are overlaid.
The simplest RL problem: three actions with fixed average rewards 5.0, 5.5 and 6.0, all positive. Run REINFORCE without a baseline, then with one. Before you do, predict which version converges to the best action faster and more reliably.
In words. A three-armed bandit (one state, three actions with noisy rewards around fixed means) trained with REINFORCE, with and without a baseline. The chart traces each action's probability over training, and several runs can be overlaid to compare their spread.
21.3 PPORun this section
At language-model scale, one large step can change the policy so much that its samples no longer describe it, and training collapses. Proximal Policy Optimization (PPO, Schulman et al., 2017) limits each update. It takes several gradient steps on each sampled batch, comparing each token's new probability with the sampling policy's, , in a clipped objective:
With typically 0.2, inside this is the advantage-weighted policy gradient; once a good action is 20% more likely (or a bad one 20% less), the clip removes the incentive to go further on this batch. The policy moves in many small, safe steps.
For language models, PPO adds a value model, a copy of the language model with a scalar output that predicts the expected reward from each partial response and supplies a per-token baseline, and a KL penalty toward the starting model (section 21.5). That is four models in memory at once (policy, reference, reward, value), the main reason simpler methods were sought.
21.4 Reward Models From PreferencesRun this section
A chat response's reward has no formula, so RLHF, as used for InstructGPT (Ouyang et al., 2022), learns it from people. They compare pairs of responses to a prompt , a winner and a loser , and a , usually a copy of the language model with a scalar output, is trained to score the winner higher:
This is the Bradley–Terry model of section 3.6 fitted by maximum likelihood: is the predicted probability of preferring , and only differences of reward are meaningful.
21.5 RLHF End to End
Then PPO trains toward responses the reward model scores highly, without drifting far from the instruction-tuned model it started from:
sets how strongly drift is penalized; in practice is added to the reward token by token. The penalty matters: the reward model only approximates human judgment, and a policy optimized against it without restraint finds outputs that score well and read badly, reward hacking. The result was striking: people preferred the 1.3B-parameter InstructGPT's answers to the 175B-parameter GPT-3's.
21.6 Direct Preference OptimizationRun this section
RLHF's reward model, sampling and RL loop are all hard to tune. Rafailov et al. (2023) showed that the RLHF optimum has a closed form, which eliminates the reward model, in three steps.
- The best policy has a closed form. For a fixed reward , section 21.5's objective is maximized by reweighting the reference model by the exponentiated reward:
where makes the probabilities sum to 1. Responses the reward likes become more likely, by a factor controls; responses the reference never produces stay unlikely. 2. So the reward can be read off the policy: . Any policy implicitly defines the reward for which it is optimal. 3. Substitute into Bradley–Terry. Preferences depend only on the difference of two rewards for the same prompt, so cancels, leaving only the policy and the reference model.
Maximum likelihood on the preferences, with standing in for , gives a loss on the pairs themselves:
Each log-ratio measures how much more likely the model has made a response than the reference did; the loss pushes the winner's up and the loser's down. ( plays its RLHF role, unrelated to Adam's and .) It is ordinary supervised training on a fixed dataset, which is why DPO and its variants are widely used alongside or instead of RLHF.
DPO recovers the closed-form optimal policy
A single prompt and a policy that can represent any distribution over five responses, so the only error left is from the finite sample of preferences. Real models share parameters across prompts and cannot reach the optimum exactly.
Exercises
Map these RL terms onto a language model: state, action, policy, episode.
A reward model scores the preferred answer 2.0 and the other 0.5. What is for this pair?
In PPO with and a positive advantage , what is the clipped objective's value for a token whose probability ratio is ?
Derive the optimal policy for a single prompt with two possible responses, by maximizing , where is the probability of response 1 and the reference gives it .
Explain reward hacking with an example, and say which part of the RLHF objective guards against it.
Core Academic References
- Sutton & Barto (2018): Reinforcement Learning: An Introduction (2nd ed.). MIT Press.
- Williams (1992): Simple Statistical Gradient-Following Algorithms for Connectionist Reinforcement Learning (REINFORCE). Machine Learning, 8: 229–256.
- Schulman et al. (2017): Proximal Policy Optimization Algorithms. arXiv:1707.06347
- Christiano et al. (2017): Deep Reinforcement Learning from Human Preferences. NeurIPS 2017. arXiv:1706.03741
- Ouyang et al. (2022): Training language models to follow instructions with human feedback (InstructGPT). NeurIPS 2022. arXiv:2203.02155
- Rafailov et al. (2023): Direct Preference Optimization: Your Language Model is Secretly a Reward Model. NeurIPS 2023. arXiv:2305.18290
- Gao, Schulman & Hilton (2023): Scaling Laws for Reward Model Overoptimization. ICML 2023. arXiv:2210.10760