Bring the mental model from Cross-Entropy; this page will reuse it instead of restarting from zero.
Direct Preference Optimization
DPO turns pairwise preferences into binary cross-entropy on reference-relative log odds, using the KL-regularized RLHF optimum to make the policy itself an implicit reward model.
01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
Suppose a prompt x has two candidate completions:
- yw: the preferred completion
- yℓ: the rejected completion
RLHF usually trains a reward model rϕ(x,y), then runs an RL algorithm to make the policy choose higher-reward outputs while staying close to a reference model.
DPO asks a sharper question: can the policy itself act as the reward model?
Under the KL-regularized RLHF objective, the answer is yes, but only up to a prompt-only constant. If a policy has moved away from the reference model, that movement gives a convenient representative of an implicit reward class:
The exact reward can differ from this representative by a term that depends only on x. Only reward differences matter for preference pairs, so that prompt-only term cancels, and DPO compares how much the current policy has changed the winner-vs-loser odds relative to the reference policy.
The core idea is:
A preferred response should beat a rejected response by more than the reference model already made it beat it.
That "more than the reference" phrase is the whole mechanism. DPO is not just "make the winner more likely." It is binary cross-entropy on reference-relative log odds.
Copy-only prompts — each action copies a page-grounded prompt to your clipboard. Nothing is sent by this site.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
The source-level representative is the beta-scaled policy/reference log-ratio:
Setup and shapes
For a prompt x, πθ(y∣x) is the full sequence probability of completion y. In an autoregressive language model,
A preference datum is (x,yw,yℓ). All compared completions must have positive probability under the reference policy for the log ratio to be finite.
Define the policy and reference log-odds:
and
The reference-relative margin is
DPO predicts the preference probability as
For one hard winner/loser label, the DPO loss is
Let sθ=σ(βmθ). The browser demo below uses a soft target p∗ so the finite target log-odds is visible:
Ordinary hard-label DPO is the edge case p∗→1, where an isolated two-response target diverges.
From KL-regularized RLHF to DPO
For a fixed prompt x, write Kx(π) for the distribution-level KL from a candidate policy to the reference policy at that prompt. The direction is π(⋅∣x) to πref(⋅∣x):
Also write the expected reward as
The KL-regularized RLHF objective is to maximize
Its optimizer has the reweighted form
Rearrange it:
The partition term Z(x) depends on the prompt, not on which completion won. Bradley-Terry preferences use reward differences, so the prompt-only term cancels between yw and yℓ.
Bradley-Terry likelihood
Bradley-Terry models a preference as
Substituting the implicit reward gives the DPO probability. With the margin mθ=aθ−aref from above:
Maximum likelihood on preference labels gives the DPO loss. This is why DPO is a supervised objective even though it comes from a KL-regularized RLHF derivation.
What beta does
Here β follows the DPO paper's convention: it is the coefficient on the KL penalty in the KL-regularized RLHF objective.
- Larger β means a stronger reference anchor in the underlying RLHF objective.
- Smaller β means a given preference probability requires a larger policy/reference log-ratio gap.
- In the DPO loss, β also scales the sigmoid margin, so it affects optimization dynamics. Do not read "larger β" as simply "trust preferences more."
For a soft preference probability p∗, matching p∗ requires
Limits of the mechanism
DPO removes the explicit reward-model training stage and the RL loop. It does not remove the assumptions behind preference learning.
It still relies on a pairwise preference model such as Bradley-Terry. It only identifies rewards up to prompt-dependent constants. It needs a usable reference policy. And in an isolated two-response hard-label setting, the logistic loss can keep increasing the winner-over-loser log-ratio rather than settling at a finite target.
Copy-only prompts — each action copies a page-grounded prompt to your clipboard. Nothing is sent by this site.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
This finite-action witness verifies the derivation directly in a clean realizable toy setting. A synthetic reward defines the KL-regularized target policy. DPO sees full-support, exact soft Bradley-Terry preference probabilities and recovers the same unconstrained categorical policy through reference-relative log-ratio cross-entropy. Sampled hard labels, missing pairs, or a restricted neural parameterization would not make the equality exact in finite data.
import numpy as np
def softmax(z):
z = z - z.max()
e = np.exp(z)
return e / e.sum()
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
def kl(p, q):
return float(np.sum(p * (np.log(p) - np.log(q))))
# One prompt, three possible completions.
# Shapes: ref, reward, logits, pi are all (K,).
ref = np.array([0.15, 0.70, 0.15], dtype=float)
reward = np.array([1.0, 0.2, -0.5], dtype=float)
beta = 0.5
log_ref = np.log(ref)
# KL-regularized RLHF optimum:
# pi*(y|x) is proportional to pi_ref(y|x) exp(r(x,y)/beta).
pi_star = ref * np.exp(reward / beta)
pi_star = pi_star / pi_star.sum()
# DPO sees pairwise preferences. We use exact Bradley-Terry
# probabilities instead of sampled hard labels so the equality is visible.
pairs = [(0, 1), (0, 2), (1, 2)]
# Initialize policy at the reference.
logits = log_ref.copy()
lr = 0.2
for _ in range(3000):
grad = np.zeros_like(logits)
for i, j in pairs:
rel_log_odds = (logits[i] - logits[j]) - (log_ref[i] - log_ref[j])
pred = sigmoid(beta * rel_log_odds)
target = sigmoid(reward[i] - reward[j])
# Binary cross-entropy gradient for the soft preference target.
g = beta * (pred - target)
grad[i] += g
grad[j] -= g
logits -= lr * grad / len(pairs)
logits -= logits.mean() # logits are identifiable only up to a constant
pi = softmax(logits)
print("reference policy: ", np.round(ref, 3))
print("KL-reg optimum: ", np.round(pi_star, 3))
print("DPO learned policy: ", np.round(pi, 3))
print("KL(pi || ref): ", round(kl(pi, ref), 4))
assert np.allclose(pi, pi_star, atol=1e-6)
for i, j in pairs:
implicit_gap = beta * (
(np.log(pi[i]) - np.log(ref[i]))
- (np.log(pi[j]) - np.log(ref[j]))
)
reward_gap = reward[i] - reward[j]
assert abs(implicit_gap - reward_gap) < 1e-6
for i, j in pairs:
dpo_pref = sigmoid(beta * (
(np.log(pi[i]) - np.log(ref[i]))
- (np.log(pi[j]) - np.log(ref[j]))
))
bt_pref = sigmoid(reward[i] - reward[j])
assert abs(dpo_pref - bt_pref) < 1e-6
Copy-only prompts — each action copies a page-grounded prompt to your clipboard. Nothing is sent by this site.
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Live Concept Demo
Explore Direct Preference Optimization
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Choose what to inspect in Direct Preference Optimization. This shared fallback is an observation guide, not evidence of learning.
Use the demo as a ratio machine. It collapses the world to two completions, yw and yℓ, so its KL is the binary KL over this toy pair, not the full language-model KL over all completions. Change the reference winner probability, the soft target preference, and β. Watch how the required policy log-odds moves relative to the reference, how the DPO probability changes, and when the two-action KL from the reference spikes.
Copy-only prompts — each action copies a page-grounded prompt to your clipboard. Nothing is sent by this site.
Concept: Direct Preference Optimization
What is the smallest example that makes Direct Preference Optimization click without losing the math?
Object contextAlignment
concept:alignment/dpoDirect Preference Optimization
What is the smallest example that makes Direct Preference Optimization click without losing the math?
Start with the prediction checkpoint, then compare the reveal to the mental model.
Take this moveStudy modes
Keep the object fixed; change the lens.Route back through the notebook
Carry the same object through intuition, math, code, and demo.
DPO turns pairwise preferences into binary cross-entropy on reference-relative log odds, using the KL-regularized RLHF optimum to make the policy itself an implicit reward model.
The next edge should feel earned: use the demo prediction here before following Kahneman-Tversky Optimization.
After The First Pass
Turn the concept into an inspected object.
The lower panels are one second act: keep the object fixed, inspect it visually, check source boundaries, practice transfer, then attach the research question.Mechanism Storyboard
See the idea move before the page explains it
DPO turns pairwise preferences into binary cross-entropy on reference-relative log odds, using the KL-regularized RLHF optimum to make the policy itself an implicit reward model.

Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change Direct Preference Optimization should make visible.
Visual Inquiry
Make the image answer a mathematical question
DPO turns pairwise preferences into binary cross-entropy on reference-relative log odds, using the KL-regularized RLHF optimum to make the policy itself an implicit reward model.
Which visible object should carry the first intuition?
Pick the cue that should make Direct Preference Optimization easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
What is the smallest example that makes Direct Preference Optimization click without losing the math?
concept:alignment/dposources: rafailov-2023-dpo
Open the closest source note before trusting the local explanation.
1 selected-object source shown first; 1 reference total.
Audit the claim boundary, then ask from the same selected object.
Grounds DPO as a closed-form preference objective derived from KL-regularized RLHF.
Rafailov et al. derive the KL-regularized optimum pi_r proportional to pi_ref exp(r/beta), rearrange it as r=beta log(pi_r/pi_ref)+beta log Z(x), then substitute into Bradley-Terry pairwi...
Checks pairwise Bradley-Terry DPO derivation plus finite-action/two-completion intuition only; not finite-sample convergence, noisy hard labels, neural realizability, ranking variants, em...
Claim Review
DPO turns pairwise preferences into binary cross-entropy on reference-relative log odds, using the KL-regularized RLHF optimum to make the policy itself an implicit reward model.
What is the smallest example that makes Direct Preference Optimization click without losing the math?
concept:alignment/dposources: rafailov-2023-dpo
Treat every claim as provisional until source support and a local witness agree.
1 structured claim check on this concept.
Run the prediction or practice transfer before asking for a grounded review.
Publisher-side editorial review is not independent replication. Claims without it still need exact source-support review. 1 reference and 3 local witnesses are available for inspection.
Rafailov et al. derive the KL-regularized optimum pi_r proportional to pi_ref exp(r/beta), rearrange it as r=beta log(pi_r/pi_ref)+beta log Z(x), then substitute into Bradley-Terry pairwise preferences so Z(...
Checks pairwise Bradley-Terry DPO derivation plus finite-action/two-completion intuition only; not finite-sample convergence, noisy hard labels, neural realizability, ranking variants, empirical superiority...
Rafailov et al. derive pi_r proportional to pi_ref exp(r/beta), rearrange r=beta log(pi_r/pi_ref)+beta log Z(x), then cancel Z(x) in Bradley-Terry reward differences to obtain DPO BCE/logistic loss on beta-scaled policy-vs-reference winner-loser log-odds.
Reviewer: codex+oracle; reviewed 2026-05-07Practice notebook
Use the idea, then test it somewhere new
DPO turns pairwise preferences into binary cross-entropy on reference-relative log odds, using the KL-regularized RLHF optimum to make the policy itself an implicit reward model.
What is the smallest example that makes Direct Preference Optimization click without losing the math?
concept:alignment/dposources: rafailov-2023-dpo
Use one state from Direct Preference Optimization to explain what changes, why it changes, and which assumption the explanation needs.
No learner move yet; no learning state is inferred.
Write first, use only the help you need, then try a new case without it.
Use one state from Direct Preference Optimization to explain what changes, why it changes, and which assumption the explanation needs.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Write an attempt before asking the companion.
0 of 3 progressive hints opened.
This draft and any AI response do not establish mastery; a later unassisted case can.
- ObjectConceptDirect Preference Optimization
- PredictBefore revealDirect Preference Optimization prediction
- WitnessCompare codeDirect Preference Optimization code witness 1
- RoomAsk groundedChecking local snapshot
Research Room
Attach the question to an exact object
Pick the concept, equation, source, code witness, claim, misconception, or demo state before asking for help. The handoff stays grounded to that object.Open the draft below to save one note and next action in this browser.
Direct Preference Optimization
What is the smallest example that makes Direct Preference Optimization click without losing the math?
These are fixed, deterministic perspectives derived from the selected object. They do not represent people, community contributions, or independent review.
Source ids rafailov-2023-dpo must support the exact object, not just the surrounding topic.
Treat this as a mechanism object: connect the definition to one equation, code witness, or demo before broadening the discussion.
Ask the learner to perturb one representation, then check whether the same invariant survives in math, code, and demo.
The learner can state the mechanism in their own words
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays locally in this browser for concept:alignment/dpo.
- Source ids to inspect: rafailov-2023-dpo
- Definition, prerequisite, and contrast concept links
- The equation or code witness that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - Direct Preference Optimization Object key: concept:alignment/dpo Context: Alignment Anchor id: concept/concept-notebook/alignment/dpo Open question: What is the smallest example that makes Direct Preference Optimization click without losing the math? Evidence to inspect: - Source ids to inspect: rafailov-2023-dpo - Definition, prerequisite, and contrast concept links - The equation or code witness that makes the concept operational - One demo state that shows the invariant instead of a slogan Deterministic role lenses for this object: - Boundary: fixed perspectives, not people, community contributions, or independent review - Source-checking summary: Treat this as a mechanism object: connect the definition to one equation, code witness, or demo before broadening the discussion. - Proposed experiment: Ask the learner to perturb one representation, then check whether the same invariant survives in math, code, and demo. - Teach/transfer move: Turn the mechanism into one sentence that predicts a neighboring concept. - Assumptions: - Source ids rafailov-2023-dpo must support the exact object, not just the surrounding topic. - The stable content-object key lets local drafts, prompts, and route memory attach without changing the source page. - The concept explanation is local atlas prose until checked against its math, code, and source support. - Prerequisite gaps should become a repair route, not a reason to leave the object vague. - Role-lens requests: - Learner: ask for "Ask what would make "Direct Preference Optimization" feel predictable rather than familiar." | assumption: Source ids rafailov-2023-dpo must support the exact object, not just the surrounding topic. | next action: The learner can state the mechanism in their own words - Researcher: ask for "Source ids to inspect: rafailov-2023-dpo" | assumption: The stable content-object key lets local drafts, prompts, and route memory attach without changing the source page. | next action: The learner can name the prerequisite that would repair confusion - Experimenter: ask for "Choose one variable or condition to perturb before asking for an explanation." | assumption: The concept explanation is local atlas prose until checked against its math, code, and source support. | next action: The learner can predict how the mechanism changes under one perturbation - Professor: ask for "Find the smallest transferable rule a learner could reuse without the AI." | assumption: Prerequisite gaps should become a repair route, not a reason to leave the object vague. | next action: Teach or transfer: Turn the mechanism into one sentence that predicts a neighboring concept. What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action. Current deterministic role lens for this object: - Role lens: Learner - Evidence request: Ask what would make "Direct Preference Optimization" feel predictable rather than familiar. - Assumption to keep visible: Source ids rafailov-2023-dpo must support the exact object, not just the surrounding topic. - Proposed experiment: Ask the learner to perturb one representation, then check whether the same invariant survives in math, code, and demo. - Next action: The learner can state the mechanism in their own words
concept/concept-notebook/alignment/dpo
concept:alignment/dpo