Equations · Rafailov et al., “Direct Preference Optimization: Your Language Model is Secretly a Reward Model” (2023)
The DPO loss (Direct Preference Optimization), explained
DPO fine-tunes a language model on human preferences without training a separate reward model or running RL. For each prompt it has a preferred and a rejected answer, and it raises the model's probability of the preferred one relative to a frozen reference model, more than it raises the rejected one.
Term by term
A prompt with two answers where people preferred (the "winner") over (the "loser"), written . The expectation averages over the preference dataset.
Distributed as (tilde) · Dataset (𝒟) · Subscript (index or label) · Succeeds / PSD order
The policy being trained: the language model's probability of the whole answer given the prompt, the product of its token probabilities. Here is a distribution, not 3.14.
The reference model, frozen: usually the supervised fine-tuned model that training starts from. The subscript is a label.
is the sigmoid. This is logistic loss on the gap between the two implicit rewards: small when the winner's reward is well above the loser's.
A small worked example
With : at the start , both log-ratios are 0, and the loss is . After some training, say the winner's log-ratio is and the loser's : the argument is , , loss .
In code
import torch.nn.functional as F
def dpo_loss(pi_w, pi_l, ref_w, ref_l, beta=0.1):
# each: summed token log-probs of the full answer, shape (B,)
logits = beta * ((pi_w - ref_w) - (pi_l - ref_l))
return -F.logsigmoid(logits).mean()Every symbol in it
Other equations
- Scaled dot-product attention
- Softmax
- Cross-entropy loss
- Adam update
- Layer normalization
- RMSNorm
- The ELBO
- KL divergence
- Backpropagation through one layer
- LoRA
- GELU
- Dropout
- Batch normalization
- Rotary position embedding (RoPE)
- Multi-head attention
- PPO clipped objective
- Policy gradient (REINFORCE)
- Bellman equation
- Q-learning update
- InfoNCE contrastive loss
- Diffusion forward process
- DDPM simplified loss
- VAE reparameterization trick
- GAN minimax objective
- AdamW
- Cosine learning-rate schedule
- Perplexity
- Logistic regression
- Normal equation (linear regression)
- Singular value decomposition (SVD)
- Principal component analysis (PCA)