Skip to content
AgenticAIWiki
  1. Learn
  2. Part 5
  3. 29. Fine-tuning
  4. 29.4

Lesson 29.4 · 4 of 9 · 5 min read · Reviewed 24 Nov 2026

Preference tuning (DPO and RLHF-style)

By the end, you can

  • Explain preference tuning.
  • Compare DPO with RLHF-style methods.

Why preferences

Some qualities are easier to judge than to write down: tone, helpfulness, concision. People can compare two answers and pick the better one even when they couldn’t write the perfect answer themselves.

{ "prompt": "Explain our refund policy to an angry customer.",
  "chosen": "I'm sorry about the trouble. Here's what we can do ...",
  "rejected": "Per policy section 4.2, refunds are ..." }

Two families of method

RLHF-style DPO
Steps 1. Train a reward model on preferences. 2. Optimise the model with RL against it One step: train directly on chosen vs rejected pairs
Complexity Higher: more models, more tuning Lower
Flexibility Reward model can score new outputs during training Limited to the pairs you collected

Chapter 5 covers how RLHF and Constitutional AIConstitutional AI: Anthropic's training approach in which a model critiques and revises outputs against a written set of principles. shaped assistant models like Claude.

Typical pipeline for an open model

base -> SFT on good examples (29.2) -> preference tuning on pairs -> eval (29.5)

Data quality

  • Write rating guidelines with examples of good and bad.
  • Use several raters on a sample and check they agree.
  • Include pairs that test safety and honesty, not only style, or the model may learn to please rather than to be right.

Key points

  • Preference tuning teaches a model which of two answers people prefer, not just one 'right' answer.
  • RLHF trains a reward model on preferences, then optimises the model against it with reinforcement learning.
  • DPO skips the separate reward model and learns directly from preferred vs rejected pairs, which is simpler to run.
  • Preference data needs clear rating guidelines; vague guidelines teach vague behaviour.
Hands-on labRuns in the Playground

Compute a DPO loss by hand

See how DPO turns a preferred and a rejected answer into a training signal, using log-probabilities from the policy and a frozen reference model.

You will have: A function that computes the DPO loss for preference pairs, with a printout showing which pairs the model already gets right.

Steps

  1. For each pair you have log-probabilities of the chosen and rejected answers under the policy and the reference model.
  2. Compute the margin: (policy chosen minus reference chosen) minus (policy rejected minus reference rejected).
  3. Loss = minus log sigmoid(beta x margin), with beta = 0.1.
  4. Print each pair's margin and loss, and the mean loss.
  5. Check that a pair the policy already prefers has a lower loss than one it gets wrong.

Done when

  • A positive margin gives a loss below log 2 (about 0.693); a negative margin gives a loss above it.
  • The mean loss prints with three decimals.
  • Raising beta makes the losses more extreme in both directions.

Open in the Playground No API key needed.

Starter code (Python)
import math

# (policy_chosen, policy_rejected, ref_chosen, ref_rejected) log-probabilities. Illustrative.
PAIRS = {
    "apology tone": (-12.0, -20.0, -14.0, -18.0),
    "concise answer": (-30.0, -26.0, -29.0, -27.0),
    "same as reference": (-15.0, -15.0, -15.0, -15.0),
}
BETA = 0.1

def dpo_loss(pc, pr, rc, rr, beta=BETA):
    # TODO: margin = (pc - rc) - (pr - rr); loss = -log(sigmoid(beta * margin))
    return 0.0

for name, p in PAIRS.items():
    print(f"{name:18} loss={dpo_loss(*p):.3f}")
Show a solution
import math

# (policy_chosen, policy_rejected, ref_chosen, ref_rejected) log-probabilities. Illustrative.
PAIRS = {
    "apology tone": (-12.0, -20.0, -14.0, -18.0),
    "concise answer": (-30.0, -26.0, -29.0, -27.0),
    "same as reference": (-15.0, -15.0, -15.0, -15.0),
}
BETA = 0.1

def dpo_loss(pc, pr, rc, rr, beta=BETA):
    margin = (pc - rc) - (pr - rr)
    return -math.log(1 / (1 + math.exp(-beta * margin))), margin

losses = []
for name, p in PAIRS.items():
    loss, margin = dpo_loss(*p)
    losses.append(loss)
    side = "prefers chosen" if margin > 0 else "prefers rejected" if margin < 0 else "no preference"
    print(f"{name:18} margin={margin:+.1f} loss={loss:.3f}  ({side})")
print(f"mean loss = {sum(losses) / len(losses):.3f}")
Quick checkWhat does DPO need for each training example?
How sure are you?
Pick an answer and how sure you are