Lesson 31.2 · 2 of 5 · 7 min read · Reviewed 24 Nov 2026
DPO step by step
By the end, you can
- Walk through one DPO update from log-probabilities to loss.
- Explain what the reference model and beta do.
Lesson 29.4 said DPO learns directly from chosen and rejected pairs, with no separate reward modelReward model: A model trained to score responses the way human raters would, used to guide reinforcement learning.. Here is what actually happens to one pair during training.
The two models
- The policy is the model you are training. It starts as a copy of your supervised fine-tunedFine-tuning: Continuing to train a model on your own examples to change its behaviour or specialise it. model (29.2).
- The reference is a frozen copy of that same starting model. It never changes.
For each answer, both models give a log-probability: how likely they think that exact answer is, given the prompt. A higher (less negative) number means more likely.
One update, step by step
Take one pair. Suppose the numbers are:
| Policy log-prob | Reference log-prob | Gain (policy minus reference) | |
|---|---|---|---|
| Chosen | -40 | -42 | +2 |
| Rejected | -50 | -49 | -1 |
- Gain for each answer. The policy has raised the chosen answer by 2 and lowered the rejected one by 1, compared with where it started.
- Margin. Chosen gain minus rejected gain: 2 - (-1) = 3.
- Scale by beta. With beta = 0.1, the scaled margin is 0.3.
- Loss. Put the scaled margin through a sigmoid and take minus the log:
loss = -log(sigmoid(beta * margin))
= -log(sigmoid(0.3))
= -log(0.574)
= about 0.55
- Update. Gradient descent nudges the policy to push the margin higher: chosen up, rejected down.
At the very start the policy equals the reference, so the margin is 0 and the loss is -log(0.5), about 0.69, for every pair. As training works, the loss falls below that.
What beta does
Beta controls how hard the method pushes and how far the policy may drift from the reference. A low beta (say 0.05) needs big margins before the loss drops, and lets the model move further. A high beta (say 0.5) reaches a low loss with small margins, so the model stays closer to where it began. Values around 0.1 are a common starting point, but tune it on your evalEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria..
Why the reference matters
Without the reference, the easiest way to “prefer” the chosen answer is to make the rejected one wildly unlikely, which can damage the model’s general writing. Measuring gains relative to a frozen copy keeps the change focused on the difference between the pair.
In practice
Libraries such as TRL provide a DPODPO: Direct preference optimization: training directly on pairs of preferred and rejected responses, without a separate reward model. trainer: you give it a dataset with prompt, chosen and rejected fields, a model and a beta, and it runs the loop above. Runs are usually short, often one to three passes over the data, and LoRALoRA: Low-rank adaptation: fine-tuning a small set of added weights instead of the whole model. (29.3) works well.
Common mistakes
- Starting from a base model. Run supervised fine-tuningSupervised fine-tuning (SFT): Fine-tuning on pairs of inputs and ideal outputs. first; DPO refines behaviour, it does not teach the format.
- Watching only the loss. A falling loss can still mean the model drifts into longer or odder answers. Read samples and run evals (29.5).
- Mismatched templates. Format the pairs with the same chat template the model uses (29.8), or the log-probs measure the wrong text.
- Too many passes. DPO can overfit quickly on a small dataset; track held-out loss and stop early.
Key points
- DPO compares how much the model being trained has raised the chosen answer, relative to a frozen reference copy, against the rejected one.
- The loss is small when the chosen answer gained more than the rejected one, and large when it is the other way round.
- Beta sets how strongly the model is pulled away from the reference: higher beta means a sharper push and less drift allowed.
- No reward model and no sampling during training: just pairs, two models and an ordinary training loop.
Compute the DPO loss by hand
Follow one DPO pair from log-probabilities to loss, and see what beta does.
You will have: A dpo_loss() function and a table of margins and losses for three pairs at two values of beta.
Steps
- Compute each gain: policy log-prob minus reference log-prob.
- Compute the margin: chosen gain minus rejected gain.
- Return -log(sigmoid(beta x margin)).
- Run it and compare the losses at beta 0.1 and 0.5.
Done when
- The start pair has loss 0.693 at both betas.
- The lesson example gives margin +3 and loss 0.554 at beta 0.1.
- The wrong-way pair has a loss above 0.693.
Open in the Playground No API key needed.
Starter code (Python)
# Compute the DPO loss by hand for three pairs, at two values of beta.
import math
def sigmoid(x):
return 1 / (1 + math.exp(-x))
def dpo_loss(pol_c, ref_c, pol_r, ref_r, beta):
# TODO: gain = policy log-prob minus reference log-prob, for chosen and for rejected
# TODO: margin = chosen gain minus rejected gain
# TODO: loss = -log(sigmoid(beta * margin))
margin = 0.0
return 0.0, margin
pairs = {
"start (policy = reference)": (-42, -42, -49, -49),
"lesson example": (-40, -42, -50, -49),
"going the wrong way": (-44, -42, -47, -49),
}
for beta in (0.1, 0.5):
for name, lp in pairs.items():
loss, margin = dpo_loss(*lp, beta)
print(f"beta {beta}: {name:<27} margin {margin:+.0f}, loss {loss:.3f}")
Show a solution
# Compute the DPO loss by hand for three pairs, at two values of beta.
import math
def sigmoid(x):
return 1 / (1 + math.exp(-x))
def dpo_loss(pol_c, ref_c, pol_r, ref_r, beta):
chosen_gain = pol_c - ref_c
rejected_gain = pol_r - ref_r
margin = chosen_gain - rejected_gain
return -math.log(sigmoid(beta * margin)), margin
pairs = {
"start (policy = reference)": (-42, -42, -49, -49),
"lesson example": (-40, -42, -50, -49),
"going the wrong way": (-44, -42, -47, -49),
}
for beta in (0.1, 0.5):
for name, lp in pairs.items():
loss, margin = dpo_loss(*lp, beta)
print(f"beta {beta}: {name:<27} margin {margin:+.0f}, loss {loss:.3f}")