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.

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

Open in the decoder →

Term by term

  1. (x,yw,yl)∼D(x, y_w, y_l) \sim \mathcal{D}

    A prompt xx with two answers where people preferred ywy_w (the "winner") over yly_l (the "loser"), written yw≻yly_w \succ y_l. The expectation averages over the preference dataset.

  2. πθ(y∣x)\pi_\theta(y \mid x)

    The policy being trained: the language model's probability of the whole answer yy given the prompt, the product of its token probabilities. Here π\pi is a distribution, not 3.14.

  3. πref\pi_{\mathrm{ref}}

    The reference model, frozen: usually the supervised fine-tuned model that training starts from. The subscript is a label.

  4. βlog⁡πθ(y∣x)πref(y∣x)\beta \log \frac{\pi_\theta(y \mid x)}{\pi_{\mathrm{ref}}(y \mid x)}

    The "implicit reward" of an answer: how much more likely the trained model makes it than the reference does, on a log scale. β\beta (often 0.1) sets how far the model may drift from the reference.

  5. −log⁡σ(…)-\log \sigma(\ldots)

    σ\sigma 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 β=0.1\beta = 0.1: at the start πθ=πref\pi_\theta = \pi_{\mathrm{ref}}, both log-ratios are 0, and the loss is −log⁡σ(0)=log⁡2≈0.693-\log \sigma(0) = \log 2 \approx 0.693. After some training, say the winner's log-ratio is +2+2 and the loser's −1-1: the argument is 0.1×3=0.30.1 \times 3 = 0.3, σ(0.3)≈0.574\sigma(0.3) \approx 0.574, loss ≈0.554\approx 0.554.

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

x∼pdatax \sim p_{\text{data}}Distributed as (tilde)D={(xi,yi)}i=1N\mathcal{D} = \{(\mathbf{x}_i, y_i)\}_{i=1}^{N}Dataset (𝒟)xi,  Wq,  pθ,  dmodelx_i,\; W_q,\; p_\theta,\; d_{\text{model}}Subscript (index or label)Σ⪰0\Sigma \succeq 0Succeeds / PSD orderπθ(a∣s)\pi_\theta(a \mid s)Pipθ(x)p_\theta(x)Thetap(y∣x)p(y \mid x)Given (conditional bar)p(x),  q(z)p(\mathbf{x}), \; q(\mathbf{z})Probability p(x)β1,β2\beta_1, \beta_2Betalog⁡pθ(x)\log p_\theta(x)Logarithmab\frac{a}{b}Fractionσ(z)\sigma(z)Sigma (lowercase)

Other equations