← All posts

Model development

Post 16 of 26

Learning from preferences: reward models and DPO

Posts in this series · 26

After supervised fine-tuning, a model answers in the right format, but it only imitates its examples. Many qualities we want, helpfulness, tone, honesty about uncertainty, are hard to demonstrate and easy to judge: given two answers, people can usually say which is better. Preference learning turns those judgements into a training signal. This post needs only the SFT post and basic probability.

From comparisons to a reward

The data is a set of triples: a prompt xx, a preferred response ywy_w and a rejected one yly_l. InstructGPT showed labelers 4 to 9 responses at a time and had them rank them; each ranking yields many pairs (Ouyang et al., 2022, §3.5). Judgements are noisy: InstructGPT’s trained labelers agreed with each other 72.6% of the time (§3.4). No model can be more consistent with the labels than the labels are with themselves.

The standard way to turn pairs into a number is the Bradley–Terry model: each response has a hidden score, a reward r(x,y)r(x, y), and

P(yw≻yl∣x)=σ(r(x,yw)−r(x,yl)),σ(z)=11+e−z.P(y_w \succ y_l \mid x) = \sigma\big(r(x, y_w) - r(x, y_l)\big), \qquad \sigma(z) = \frac{1}{1 + e^{-z}}.

Only the difference of rewards matters. A difference of 0 gives a coin flip; a difference of 1 gives σ(1)≈0.73\sigma(1) \approx 0.73; a difference of 3 gives 0.95. Read backwards: if response A is preferred to B in 3 of 4 comparisons, the best-fitting reward gap is ln⁡3≈1.10\ln 3 \approx 1.10.

A reward model is a language model with its output layer replaced by a single number, trained on the pairs by maximizing that probability, that is, minimizing −log⁡σ(r(x,yw)−r(x,yl))-\log \sigma(r(x, y_w) - r(x, y_l)) (InstructGPT Eq. 1; DPO Eq. 2). It is logistic regression on the difference of two scores.

The objective: more reward, but stay close

Given a reward model, the obvious move is to make the policy produce high-reward responses. Pushed too far, that fails: the reward model is only accurate near the kinds of responses it was trained on, and a policy that wanders far from them finds responses the reward model scores highly for the wrong reasons. So the standard objective adds a penalty for moving away from a reference policy πref\pi_{\text{ref}}, usually the SFT model:

max⁡π Ey∼π(⋅∣x)[r(x,y)]−β KL(π(⋅∣x) ∥ πref(⋅∣x)).\max_{\pi}\ \mathbb{E}_{y \sim \pi(\cdot \mid x)}\big[r(x, y)\big] - \beta\, \mathrm{KL}\big(\pi(\cdot \mid x)\,\|\,\pi_{\text{ref}}(\cdot \mid x)\big).

This objective has an exact solution (DPO, §4, Eq. 4):

π∗(y∣x)=1Z(x) πref(y∣x) exp⁡ ⁣(r(x,y)/β),\pi^*(y \mid x) = \frac{1}{Z(x)}\,\pi_{\text{ref}}(y \mid x)\,\exp\!\big(r(x, y)/\beta\big),

where Z(x)Z(x) is whatever makes the probabilities sum to 1. The optimal policy reweights the reference policy by exponentiated reward.

Work it on three possible responses with reference probabilities (0.5,0.3,0.2)(0.5, 0.3, 0.2) and rewards (0,1,2)(0, 1, 2):

β\beta is a dial between “trust the reward model” and “stay where you were”.

DPO: optimizing preferences directly

The usual way to reach π∗\pi^* is reinforcement learning against the reward model, the subject of the next post. Direct Preference Optimization (DPO) takes a shortcut (Rafailov et al., 2023, §4). Rearranging the optimal policy gives the reward in terms of the policy:

r(x,y)=βlog⁡π∗(y∣x)πref(y∣x)+βlog⁡Z(x).r(x, y) = \beta \log \frac{\pi^*(y \mid x)}{\pi_{\text{ref}}(y \mid x)} + \beta \log Z(x).

In the toy example, log⁡(π∗/πref)\log(\pi^*/\pi_{\text{ref}}) is (−1.03,−0.03,0.97)(-1.03, -0.03, 0.97): the rewards (0,1,2)(0, 1, 2) shifted by a constant. Substituting this into Bradley–Terry, the constant βlog⁡Z(x)\beta \log Z(x) appears in both rewards and cancels in their difference. What remains is a loss on the policy alone (Eq. 7):

LDPO=−log⁡σ ⁣(βlog⁡πθ(yw∣x)πref(yw∣x)−βlog⁡πθ(yl∣x)πref(yl∣x)).\mathcal{L}_{\text{DPO}} = -\log \sigma\!\left(\beta \log \frac{\pi_\theta(y_w \mid x)}{\pi_{\text{ref}}(y_w \mid x)} - \beta \log \frac{\pi_\theta(y_l \mid x)}{\pi_{\text{ref}}(y_l \mid x)}\right).

Each log⁡πθ(y∣x)\log \pi_\theta(y \mid x) is the sum of the log-probabilities of the response’s tokens under the model, the same quantity as the SFT loss with the sign flipped. So DPO needs no reward model and no sampling during training: for each pair, run the policy and the frozen reference on both responses and compute one logistic loss. The quantity βlog⁡(πθ/πref)\beta \log(\pi_\theta/\pi_{\text{ref}}) acts as an implicit reward.

The gradient shows what DPO does. On the three-response toy with β=0.1\beta = 0.1, preferring response 3 over response 1, the first gradient on the logits is (+0.05,0,−0.05)(+0.05, 0, -0.05): push the preferred response up and the rejected one down, by β σ(−margin)\beta\,\sigma(-\text{margin}). The weight σ(−margin)\sigma(-\text{margin}) is large when the implicit reward orders the pair wrongly and small when it already orders it right, so well-learned pairs stop contributing. After four steps the probabilities move from (0.5,0.3,0.2)(0.5, 0.3, 0.2) to (0.43,0.32,0.26)(0.43, 0.32, 0.26), and the response not in the pair moved too, through the softmax.

Try it: compute a DPO loss. At the start of training πθ=πref\pi_\theta = \pi_{\text{ref}}, so both log-ratios are 0. What is the loss? Later, the chosen response’s log-ratio is +0.2 and the rejected one’s −0.1, with β=0.1\beta = 0.1. And if they are −0.5 and −1.5?

Answer. At the start the margin is 0 and the loss is −log⁡σ(0)=ln⁡2≈-\log \sigma(0) = \ln 2 \approx 0.693. Then the margin is 0.1×(0.2−(−0.1))=0.030.1 \times (0.2 - (-0.1)) = 0.03 and the loss 0.678. In the third case the margin is 0.1×(−0.5+1.5)=0.10.1 \times (-0.5 + 1.5) = 0.1 and the loss 0.644, lower still, although both responses became less likely than under the reference. DPO only cares about the difference, so it can lower the probability of the chosen response as long as the rejected one falls faster. This is a known behaviour of DPO and one reason practitioners monitor the chosen response’s log-probability during training.

What practice has found

Everyday picture: a wine tasting where judges only say which of two glasses they prefer. A reward model learns a score for each wine from those choices; DPO skips the score and adjusts the cellar directly, buying more of what won and less of what lost, but never straying too far from the cellar it started with.

What to remember

Sources

Further reading