The Direct Preference Optimization (DPO) algorithm uses a loss function that enables language models to be aligned with human preferences without requiring a separate reward model. Here's the formula for the DPO loss function:
The core DPO loss function can be represented as:
LDPO(πθ;πref)=−E
Where:
- πθ is the policy model being trained (the language model)
- πref is the reference model (frozen, usually the SFT model)
- represents a data point with prompt , chosen (winning) response , and rejected (losing) response
Mathematical Derivation
The DPO formula is derived from two key components:
-
The Bradley-Terry model of preferences, which converts rewards into probabilities[3][5]. This model states that the probability of choosing one response over another is proportional to the exponential of their respective rewards:
P(yw≻yl
The key innovation in DPO is that it reparameterizes the reward function in terms of the policy, eliminating the need for a separate reward model[1]. Through mathematical manipulation, the researchers found that an optimal reward function can be expressed as:
r(x,y)=βlogπ
Where Z(x) is a normalizing term. When this reward function is plugged into the Bradley-Terry model, the Z(x) terms cancel out, resulting in the DPO loss function above[3].
Intuitive Understanding
The DPO loss function encourages the model to:
- Increase the probability of generating responses that humans prefer (yw)
- Decrease the probability of generating responses that humans don't prefer (yl)
- Stay reasonably close to the original reference model through the KL divergence penalty controlled by β[1][5]
This approach makes training more stable and straightforward than traditional RLHF methods, as it removes the need for repeated sampling and constant adjustments during training[1].