Topic 2 of 6 · 5 min read
Direct preference optimization with pairs
Goal: Build a valid chosen and rejected pair for a stated behaviour and predict what a DPO run on such pairs will reinforce.
DPO trains directly on pairs of a chosen and a rejected answer, so the model learns whatever difference separates them, intended or not. It skips the separate reward model and the PPO loop of RLHF, which makes it much simpler to run.
Check yourself · 1 of 3
What does DPO train on?
Why it matters
The DPO paper (2023) showed that the RLHF objective can be rewritten as a simple classification-style loss on pairs. The policy itself plays the part of the reward model. In practice it is one training job, with a policy and a frozen reference copy, using the same trainer as SFT. Libraries such as Hugging Face TRL ship it as DPOTrainer, and later variants (IPO, KTO, SimPO, ORPO) change the loss or drop the reference.
What a training row looks like
Each row has a prompt, a chosen answer and a rejected answer:
{
"prompt": "Summarise this refund policy in two sentences for a customer.",
"chosen": "You can return items within 30 days for a full refund. Refunds go back to the original payment method within 5 business days.",
"rejected": "Our refund policy is comprehensive and designed to serve you. Please read the full policy for details."
}
The loss raises the chosen answer's probability relative to the reference and lowers the rejected one. A hyperparameter, beta, controls how far the policy may move from the reference. Smaller beta lets it move further.
How to build good pairs
- Pick one behaviour. "Concise and specific" is a behaviour; "better" is not.
- Make the pair differ mainly in that behaviour. Same topic, same facts, similar length. Otherwise the model learns the accident.
- Use on-policy samples when you can. Sample both answers from your SFT model, then label them. Pairs written by another model or by hand can sit far from what your model would say, and the gain is smaller.
- Check the pair in both directions. If you swapped chosen and rejected, would the data teach the opposite? Then it is a real contrast.
Before and after
Weak pair: chosen is a 300-word careful answer, rejected is a 20-word wrong one. DPO will learn "longer is better" as well as "correct is better", and the length habit is easier to learn.
Fixed pair: two answers of about 60 words, one with the right figure and one with the wrong figure.
What goes wrong
- A length, tone or formatting difference between chosen and rejected is learned as the target.
- Both answers are bad. DPO only learns which is relatively better, and can lower the probability of both while still widening the gap.
- No SFT first. The reference model should already produce answers of the right kind.
Examples
Spotting an accidental signal
A support team builds 8,000 pairs. In the chosen answers, 92% start with "Sure!" and in the rejected ones only 11% do. After DPO, the model opens nearly every reply with "Sure!", even for refusals. Fix: strip or balance the opener in both sides, then rebuild the pairs.
Minimal TRL run
from trl import DPOConfig, DPOTrainer
args = DPOConfig(output_dir="out", beta=0.1, learning_rate=5e-7)
trainer = DPOTrainer(model=model, args=args,
train_dataset=pairs, processing_class=tokenizer)
trainer.train()
The learning rate is far smaller than in SFT, because the loss is sensitive. Check the current TRL docs for argument names.
Key points
- DPO optimises a loss on chosen and rejected pairs against a frozen reference model, with no separate reward model or RL loop.
- Whatever systematically differs between chosen and rejected is what the model learns, including accidental differences like length.
- Beta controls how far the policy may move from the reference; lower values allow bigger moves.
- Pairs sampled from your own SFT model usually teach more than pairs written elsewhere.
- Start from a good SFT model, since DPO reorders behaviours the model can already produce.
Common mistakes
- Letting chosen answers be longer, warmer or more formatted than rejected ones, then wondering why the model became wordy.
- Running DPO on a base model that was never instruction-tuned.
- Using pairs from a stronger model's outputs and assuming they match what your model would produce.
- Reusing SFT learning rates, which usually overshoots on a preference loss.
Check yourself · 2 of 3
In your DPO pairs, every chosen answer is about twice as long as its rejected answer. What will the tuned model most likely do?
Check yourself · 3 of 3
You want a model to give shorter, specific answers. Which pair is the best training row?
Practice items, not counted towards your mastery.
Start here
- Training language models to follow instructions with human feedback (InstructGPT) (opens in a new tab)PaperFree
OpenAI's original RLHF recipe: demonstrations, ranked outputs, a reward model and PPO, with results on a 1.3B model.
- Direct Preference Optimization: Your Language Model is Secretly a Reward Model (opens in a new tab)PaperFree
The DPO paper: the loss, the role of the reference model and beta, and why no reward model is needed.
- RLHF Book (Nathan Lambert) (opens in a new tab)BookFree
A free, practical walk through reward models, PPO, DPO, rejection sampling and RL with verifiable rewards.
More for this topic
- TRL documentation (Hugging Face) (opens in a new tab)DocsFree
Working trainers for SFT, reward modelling, DPO, PPO and GRPO, with dataset formats.