Visual walkthrough — Direct Preference Optimization (DPO)
Step 0 — The three characters, drawn as dice
Before any equation, let's fix what the words mean using the simplest possible picture: a language model answering a prompt is just rolling a loaded die over possible responses.
Look at the figure: two dice for the same prompt "Write a haiku". Each face is one possible response; the height of the bar is how probable that response is. The reference die (teal) is our starting point; the policy die (orange) is what we sculpt. Training = reshaping the orange bars so good answers rise and bad answers fall — while not letting orange wander too far from teal.
Step 1 — What we are trying to maximize (the RLHF goal)
We want the die to roll high-scoring answers on average, but we don't want it to mutate into a weird die that games the judge. That tension is the whole objective:
Term by term:
- — "choose the die-shape that makes the whole thing as big as possible".
- — the average reward, our carrot.
- — the KL divergence (see KL Divergence), a number measuring how different the orange die is from the teal die. Zero if identical, growing as they diverge.
- — read "beta", a knob (a positive number) setting how hard we yank on that leash.
The figure shows a tug-of-war: the orange arrow (reward) pulls the die toward high-scoring faces; the teal spring (KL leash) pulls it back toward the reference shape. is the stiffness of the spring.
Step 2 — The best die has a known shape
Here is a fact from optimization (the same trick used across PPO - Proximal Policy Optimization and entropy-regularized RL): the objective in Step 1 has an exact best answer. The optimal die is:
Term by term:
- — start from the frozen teal die's probability.
- — the exponential function . Why this tool? Because it turns a score (which can be negative) into a positive multiplier, and it's the exact function that pops out when you solve the KL-regularized maximization. Big reward ⇒ multiplier much bigger than 1 ⇒ bar shoots up. Negative reward ⇒ multiplier below 1 ⇒ bar shrinks.
- — divides the reward. Small ⇒ huge tilt (reward dominates). Large ⇒ gentle tilt (stay near teal). This is the same leash from Step 1, now living in the exponent.
- — the partition function, defined below.
In the figure: teal bars get multiplied by an orange exponential curve (tall where reward is high), giving raw tilted bars; then squeezes them so they re-sum to 1. The red box around marks it as "the thing we can't afford".
Step 3 — Flip it: read the reward off the die
We have "reward ⇒ best die". We now run it backwards: given the die, what reward produced it? Why? Because our real goal is to train the die directly; if the reward hides inside the die's shape, maybe we never need a separate reward model.
Take the natural logarithm of both sides. Why the log? Log turns products into sums and undoes the exponential — it's the exact inverse tool for an expression built from and multiplication. Applying it:
Now isolate (multiply through by and rearrange):
Term by term:
- — the log-ratio. Positive if the trained die made more likely than the reference did; negative if less; zero if unchanged. This ratio is the heart of DPO.
- — converts that policy shift into reward units.
- — the leftover villain. Crucially it depends only on , not on . Every response to the same prompt carries the same term. Remember this — it's the pin we pull in Step 5.
The figure reads left-to-right: the reward of a response equals (its log-ratio, drawn as an up/down arrow between orange and teal bars) a constant grey slab labelled "same for all ".
Step 4 — How humans express preference (Bradley–Terry)
Humans don't hand us numeric rewards; they hand us comparisons: "answer is better than answer ". ( = winner, = loser.) We need a rule linking two hidden rewards to the probability a human picks the winner.
Why sigmoid and not something else? Because we're converting a reward gap (any real number) into a probability (must live in 0–1), and the sigmoid is the natural, smooth, monotonic bridge — the bigger the reward margin, the more sure the human is to pick the winner.
The figure plots with three dots marked: at (fifty-fifty), at a positive margin (winner likely), at a negative margin (we're wrong — winner unlikely). The horizontal axis is the reward margin .
Step 5 — The villain cancels (the punchline)
Now substitute the Step-3 reward into the Step-4 margin. Write and :
Group the terms:
The figure stacks two grey slabs (identical height) and shows them cancelling with a red "✕", leaving only the two log-ratio arrows behind.
Step 6 — Assemble the loss
Replace the ideal die with our actual trainable die , feed the surviving margin into Bradley–Terry, and ask the model to make the observed preference likely. Maximizing that likelihood is the same as minimizing its negative :
Term by term:
- — small when the winner is favoured (inside , ), large when we're wrong.
- — the margin: how much more our die lifts the winner than the loser (relative to reference).
- — average this over every preference triple in the dataset.
The figure is the loss curve vs the margin: flat-and-low on the right (winner already preferred, little to learn), steep-and-high on the left (model wrong, big gradient). The dot at margin sits at height .
Step 7 — Edge & degenerate cases (never get surprised)
The one-picture summary
One flow, left to right: RLHF objective (carrot + leash) → closed-form optimal die (with the red villain) → flip to read reward off the die → Bradley–Terry difference cancels → one sigmoid classification loss on the log-ratios. The whole three-model, RL-heavy pipeline compresses into a single supervised loss.
Recall Feynman retelling — say it in plain words
A language model is a loaded die over answers. RLHF wants to reshape the die toward high-scoring answers without letting it stray far from its sane starting shape — carrot plus leash. Solving that exactly gives a best-die formula, but it hides an impossible-to-compute normalizer that sums over every sentence in existence. So we run the formula backwards: the reward of any answer equals times how much the trained die lifted that answer relative to the frozen die, plus that same slab. Humans only ever tell us "this answer beats that one", and Bradley–Terry turns that into a probability via the sigmoid, which needs the difference of two rewards for the same prompt. Both answers carry the identical slab, so subtracting kills it stone dead. What remains is a plain "make the winner's log-ratio exceed the loser's" classification loss. The die secretly is the reward model. No RL, no reward network, no partition function — as long as you always compare in pairs.