DPO Derivation › Worked Example

Michael BrenndoerferJanuary 1, 20266 min read
Language AI HandbookAll chapters
Part 37 of 67Alignment and RLHF
Chapter 283DPO Derivation › Worked Example
Reading section 7 of 12 · Worked Example · 6 minSection 7 of 12 · 6 min

Book contents

Section 7 of 12 · 6 min read

DPO Derivation › Worked ExampleOne section · Sections in this chapter7 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's trace through the derivation with concrete numbers to build intuition. Suppose we have a prompt xx = "Write a haiku about autumn" and two responses:

  • ywy_w (preferred): "Crimson leaves descend / Dancing on October wind / Earth prepares to sleep"
  • yly_l (dispreferred): "Leaves fall down in fall / The weather gets cold outside / I like pumpkin spice"

The preferred response demonstrates poetic craft through concrete imagery ("crimson," "dancing"), sensory detail, and thematic resonance with the natural world. The dispreferred response is grammatically correct but prosaic, repeating "fall" clumsily and ending on an unrelated tangent about pumpkin spice. A human evaluator would clearly prefer the first response, and a well-trained reward model should assign it a higher score.

Assume our reference model πref\pi_{\text{ref}} assigns:

  • log⁡πref(yw∣x)=−45.2\log \pi_{\text{ref}}(y_w|x) = -45.2 (log probability of generating the preferred response)
  • log⁡πref(yl∣x)=−38.7\log \pi_{\text{ref}}(y_l|x) = -38.7 (log probability of generating the dispreferred response)

The reference model assigns higher probability to the dispreferred response because it's simpler and uses more common words. The word "fall" is far more common in training data than "crimson" or "descend," and the dispreferred response is shorter and uses everyday vocabulary throughout. This is exactly the distribution shift DPO is designed to fix: the reference model, trained to predict natural text, has no built-in preference for poetic quality over simplicity.

Now suppose our current policy πθ\pi_\theta assigns:

  • log⁡πθ(yw∣x)=−42.1\log \pi_\theta(y_w|x) = -42.1
  • log⁡πθ(yl∣x)=−39.5\log \pi_\theta(y_l|x) = -39.5

With β=0.1\beta = 0.1, the implicit rewards are:

r^θ(x,yw)=0.1×(−42.1−(−45.2))=0.1×3.1=0.31\begin{aligned} \hat{r}_\theta(x, y_w) &= 0.1 \times (-42.1 - (-45.2)) \\ &= 0.1 \times 3.1 \\ &= 0.31 \end{aligned} r^θ(x,yl)=0.1×(−39.5−(−38.7))=0.1×(−0.8)=−0.08\begin{aligned} \hat{r}_\theta(x, y_l) &= 0.1 \times (-39.5 - (-38.7)) \\ &= 0.1 \times (-0.8) \\ &= -0.08 \end{aligned}

The difference is:

r^θ(x,yw)−r^θ(x,yl)=0.31−(−0.08)=0.39\begin{aligned} \hat{r}_\theta(x, y_w) - \hat{r}_\theta(x, y_l) &= 0.31 - (-0.08) \\ &= 0.39 \end{aligned}

The loss contribution from this example:

−log⁡σ(0.39)=−log⁡(0.596)=0.517\begin{aligned} -\log \sigma(0.39) &= -\log(0.596) \\ &= 0.517 \end{aligned}

The model is doing okay on this example (probability of correct preference is about 60%), but there's room to improve. The gradient will push the model to further increase πθ(yw∣x)\pi_\theta(y_w|x) relative to the reference and decrease πθ(yl∣x)\pi_\theta(y_l|x) relative to the reference.

Let's also trace through what a "good" and a "bad" model state look like for this same example. Suppose after more training the policy reaches:

  • log⁡πθ(yw∣x)=−38.0\log \pi_\theta(y_w|x) = -38.0 (probability has increased from reference)
  • log⁡πθ(yl∣x)=−41.5\log \pi_\theta(y_l|x) = -41.5 (probability has decreased from reference)

Now the implicit rewards are:

r^θ(x,yw)=0.1×(−38.0−(−45.2))=0.1×7.2=0.72r^θ(x,yl)=0.1×(−41.5−(−38.7))=0.1×(−2.8)=−0.28\begin{aligned} \hat{r}_\theta(x, y_w) &= 0.1 \times (-38.0 - (-45.2)) = 0.1 \times 7.2 = 0.72 \\ \hat{r}_\theta(x, y_l) &= 0.1 \times (-41.5 - (-38.7)) = 0.1 \times (-2.8) = -0.28 \end{aligned}

The reward margin is now 0.72−(−0.28)=1.000.72 - (-0.28) = 1.00, and the preference probability is σ(1.00)≈0.73\sigma(1.00) \approx 0.73. The loss drops to −log⁡(0.73)≈0.31-\log(0.73) \approx 0.31. The model has learned to "see" the quality difference between these two responses, even though it never directly observed a reward signal during this training phase.

Out[5]:
Visualization
Grouped bar chart showing log probabilities for preferred and rejected responses under both the reference model and the current policy.
Log probabilities of the preferred and rejected responses under the reference and policy models. The policy shifts mass toward the preferred haiku compared to the reference.
Bar chart showing log-ratios of policy to reference, with a positive bar for the preferred response and a negative bar for the rejected response.
Log-ratio of policy to reference probabilities. A positive ratio for the preferred response and negative for the rejected indicates the model is moving in the right direction.
Bar chart showing implicit rewards for preferred and rejected responses, with an annotation showing a positive margin of 0.39.
Implicit rewards scaled by $\beta = 0.1$. The positive margin of 0.39 shows the model correctly assigns a higher implicit reward to the preferred response.