DPO Derivation › The DPO Loss Function
Language AI HandbookAll chapters 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 over response is:
where:
- : probability that response is preferred over
- : the sigmoid function mapping values to
- : reward for the winning response
- : 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:
where:
- : the sigmoid function
- : the temperature parameter
- : the optimal policy
- : the reference policy
- : the partition function terms which appear with opposite signs
Notice that the 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 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 and not on the response will cancel in the subtraction. The partition function is exactly such a term, depending on through the sum over all possible responses but not on any particular response or . This is why the DPO derivation only works cleanly with pairwise comparison preference models and would need modification for other preference structures.
This leaves:
where:
- : the sigmoid function
- : temperature parameter
- : the optimal policy
- : the reference policy
We can simplify this using logarithm properties:
where:
- : the sigmoid function
- : the KL penalty coefficient
- : 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 to match this optimal policy, we maximize the log-likelihood of the observed preferences:
where:
- : the DPO loss function
- : the policy network being trained (parameterized by )
- : the reference policy
- : the dataset of preference pairs
- : expectation over the dataset
- : the KL penalty coefficient
- : 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 :
where:
- : the implicit reward assigned by the current model
- : the KL penalty coefficient scaling the reward
- : the policy probability
- : the reference policy probability
- : 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 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:
where:
- : the DPO loss function
- : the implicit reward function
- : the margin between implicit rewards for winning and losing responses
- : the sigmoid function
- : 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.