DPO Derivation › The DPO Loss Function

Michael BrenndoerferJanuary 1, 20265 min read
Language AI HandbookAll chapters
Part 37 of 67Alignment and RLHF
Chapter 283DPO Derivation › The DPO Loss Function
Reading section 5 of 12 · The DPO Loss Function · 5 minSection 5 of 12 · 5 min

Book contents

Section 5 of 12 · 5 min read

DPO Derivation › The DPO Loss FunctionOne section · Sections in this chapter5 / 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

Now we can derive the DPO loss by substituting our reward reparameterization into the Bradley-Terry preference model. Recall from the chapter on the Bradley-Terry Model that the probability of preferring response ywy_w over response yly_l is:

p(yw≻yl∣x)=σ(r(x,yw)−r(x,yl))p(y_w \succ y_l | x) = \sigma\left( r(x, y_w) - r(x, y_l) \right)

where:

  • p(yw≻yl∣x)p(y_w \succ y_l | x): probability that response ywy_w is preferred over yly_l
  • σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}: the sigmoid function mapping values to (0,1)(0, 1)
  • r(x,yw)r(x, y_w): reward for the winning response
  • r(x,yl)r(x, y_l): reward for the losing response

The Bradley-Terry model is elegant in its simplicity: the probability of preferring one response over another depends only on the difference in their rewards, passed through a sigmoid function. This means responses with much higher reward are strongly preferred, while responses with similar rewards have preference probabilities close to 0.5.

Substituting our reparameterized reward:

p(yw≻yl∣x)=σ([βlog⁡π∗(yw∣x)πref(yw∣x)+βlog⁡Z(x)]−[βlog⁡π∗(yl∣x)πref(yl∣x)+βlog⁡Z(x)])p(y_w \succ y_l | x) = \sigma\left( \left[ \beta \log \frac{\pi^*(y_w|x)}{\pi_{\text{ref}}(y_w|x)} + \beta \log Z(x) \right] - \left[ \beta \log \frac{\pi^*(y_l|x)}{\pi_{\text{ref}}(y_l|x)} + \beta \log Z(x) \right] \right)

where:

  • σ\sigma: the sigmoid function
  • β\beta: the temperature parameter
  • π∗\pi^*: the optimal policy
  • πref\pi_{\text{ref}}: the reference policy
  • Z(x)Z(x): the partition function terms which appear with opposite signs

Notice that the βlog⁡Z(x)\beta \log Z(x) terms cancel! This cancellation is not a coincidence; it's a direct consequence of using reward differences in the Bradley-Terry model. The partition function contributes equally to both rewards, so when we subtract them, it disappears. This cancellation is needed for the practical success of DPO because computing Z(x)Z(x) would require summing over all possible responses, which is computationally intractable for language models with exponentially large output spaces.

The key insight is that this cancellation is guaranteed by the structure of the Bradley-Terry model. Because preferences are defined as comparisons between pairs of responses (rather than absolute scores), any term that depends only on the prompt xx and not on the response yy will cancel in the subtraction. The partition function is exactly such a term, depending on xx through the sum over all possible responses but not on any particular response ywy_w or yly_l. This is why the DPO derivation only works cleanly with pairwise comparison preference models and would need modification for other preference structures.

This leaves:

p(yw≻yl∣x)=σ(βlog⁡π∗(yw∣x)πref(yw∣x)−βlog⁡π∗(yl∣x)πref(yl∣x))p(y_w \succ y_l | x) = \sigma\left( \beta \log \frac{\pi^*(y_w|x)}{\pi_{\text{ref}}(y_w|x)} - \beta \log \frac{\pi^*(y_l|x)}{\pi_{\text{ref}}(y_l|x)} \right)

where:

We can simplify this using logarithm properties:

p(yw≻yl∣x)=σ(β[log⁡π∗(yw∣x)πref(yw∣x)−log⁡π∗(yl∣x)πref(yl∣x)])p(y_w \succ y_l | x) = \sigma\left( \beta \left[ \log \frac{\pi^*(y_w|x)}{\pi_{\text{ref}}(y_w|x)} - \log \frac{\pi^*(y_l|x)}{\pi_{\text{ref}}(y_l|x)} \right] \right)

where:

  • σ\sigma: the sigmoid function
  • β\beta: the KL penalty coefficient
  • log⁡π∗(y∣x)πref(y∣x)\log \frac{\pi^*(y|x)}{\pi_{\text{ref}}(y|x)}: the log-likelihood ratio of the optimal policy vs reference

This expression is remarkably clean. The probability of the correct preference depends entirely on the difference between two log ratios: how much the optimal policy prefers the winning response (relative to the reference) versus how much it prefers the losing response. When this difference is large and positive, the sigmoid outputs a value close to 1, showing strong confidence in the correct preference.

This is the probability that the optimal policy assigns to the correct preference. To train a policy πθ\pi_\theta to match this optimal policy, we maximize the log-likelihood of the observed preferences:

LDPO(πθ;πref)=−E(x,yw,yl)∼D[log⁡σ(β[log⁡πθ(yw∣x)πref(yw∣x)−log⁡πθ(yl∣x)πref(yl∣x)])]\mathcal{L}_{\text{DPO}}(\pi_\theta; \pi_{\text{ref}}) = -\mathbb{E}_{(x, y_w, y_l) \sim \mathcal{D}} \left[ \log \sigma\left( \beta \left[ \log \frac{\pi_\theta(y_w|x)}{\pi_{\text{ref}}(y_w|x)} - \log \frac{\pi_\theta(y_l|x)}{\pi_{\text{ref}}(y_l|x)} \right] \right) \right]

where:

  • LDPO\mathcal{L}_{\text{DPO}}: the DPO loss function
  • πθ\pi_\theta: the policy network being trained (parameterized by θ\theta)
  • πref\pi_{\text{ref}}: the reference policy
  • D\mathcal{D}: the dataset of preference pairs (x,yw,yl)(x, y_w, y_l)
  • E\mathbb{E}: expectation over the dataset
  • β\beta: the KL penalty coefficient
  • σ\sigma: the sigmoid function

The negative sign appears because we're minimizing a loss rather than maximizing likelihood.

To make the notation more compact, define the log-ratio for a response yy:

r^θ(x,y)=βlog⁡πθ(y∣x)πref(y∣x)\hat{r}_\theta(x, y) = \beta \log \frac{\pi_\theta(y|x)}{\pi_{\text{ref}}(y|x)}

where:

  • r^θ(x,y)\hat{r}_\theta(x, y): the implicit reward assigned by the current model πθ\pi_\theta
  • β\beta: the KL penalty coefficient scaling the reward
  • πθ(y∣x)\pi_\theta(y|x): the policy probability
  • πref(y∣x)\pi_{\text{ref}}(y|x): the reference policy probability
  • log⁡πθ(y∣x)πref(y∣x)\log \frac{\pi_\theta(y|x)}{\pi_{\text{ref}}(y|x)}: the log-ratio of the policy probability to the reference probability

This is sometimes called the "implicit reward" because it's what the reward would be if πθ\pi_\theta were optimal. The terminology is fitting: we never explicitly compute or learn a reward function, yet the policy implicitly defines one through its log-probability ratios with the reference model.

The DPO loss becomes:

LDPO=−E(x,yw,yl)∼D[log⁡σ(r^θ(x,yw)−r^θ(x,yl))]\mathcal{L}_{\text{DPO}} = -\mathbb{E}_{(x, y_w, y_l) \sim \mathcal{D}} \left[ \log \sigma\left( \hat{r}_\theta(x, y_w) - \hat{r}_\theta(x, y_l) \right) \right]

where:

  • LDPO\mathcal{L}_{\text{DPO}}: the DPO loss function
  • r^θ(x,y)\hat{r}_\theta(x, y): the implicit reward function
  • r^θ(x,yw)−r^θ(x,yl)\hat{r}_\theta(x, y_w) - \hat{r}_\theta(x, y_l): the margin between implicit rewards for winning and losing responses
  • σ\sigma: the sigmoid function
  • E\mathbb{E}: expectation over the dataset

This is remarkably simple. The loss encourages the model to increase its log probability (relative to the reference) on preferred responses and decrease it on dispreferred responses.