DPO Derivation › DPO as Classification

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

Book contents

Section 6 of 12 · 5 min read

DPO Derivation › DPO as ClassificationOne section · Sections in this chapter6 / 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

The DPO loss has a revealing interpretation: it's essentially binary classification with a specific parameterization. Consider a standard binary cross-entropy loss for classifying which of two responses is preferred:

LBCE=−E[log⁡σ(f(x,yw,yl))]\mathcal{L}_{\text{BCE}} = -\mathbb{E} \left[ \log \sigma(f(x, y_w, y_l)) \right]

where:

  • LBCE\mathcal{L}_{\text{BCE}}: Binary Cross-Entropy loss
  • f(x,yw,yl)f(x, y_w, y_l): a scoring function showing preference strength
  • σ\sigma: the sigmoid function
  • E\mathbb{E}: expectation over the dataset

In this context, ff is some function that should output a positive value when ywy_w is truly preferred. In DPO, this function is:

f(x,yw,yl)=β[log⁡πθ(yw∣x)πref(yw∣x)−log⁡πθ(yl∣x)πref(yl∣x)]f(x, y_w, y_l) = \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]

where:

  • f(x,yw,yl)f(x, y_w, y_l): the logit fed into the sigmoid classifier
  • β\beta: the KL penalty coefficient
  • πθ\pi_\theta: the policy network
  • πref\pi_{\text{ref}}: the reference policy

The model is learning to classify preference pairs by adjusting its output probabilities. When the loss is low, the model assigns higher relative probability to the preferred response.

This classification view explains why DPO is so stable compared to RLHF. Instead of learning a separate reward model and then using policy gradients with their high variance, DPO directly optimizes the likelihood of preference labels. The gradients flow directly through the model's log probabilities, just like in standard language model training.

The gradient of the DPO loss with respect to θ\theta has a particularly intuitive form. For a single example:

∇θLDPO=−βσ(−r^θ(x,yw)+r^θ(x,yl))[∇θlog⁡πθ(yw∣x)−∇θlog⁡πθ(yl∣x)]\nabla_\theta \mathcal{L}_{\text{DPO}} = -\beta \sigma(-\hat{r}_\theta(x, y_w) + \hat{r}_\theta(x, y_l)) \left[ \nabla_\theta \log \pi_\theta(y_w|x) - \nabla_\theta \log \pi_\theta(y_l|x) \right]

where:

  • ∇θ\nabla_\theta: gradient with respect to model parameters θ\theta
  • LDPO\mathcal{L}_{\text{DPO}}: the DPO loss
  • σ\sigma: the sigmoid function
  • r^θ\hat{r}_\theta: the implicit reward function
  • σ(… )\sigma(\dots): the weighting term derived from the sigmoid derivative (probability of the wrong preference)
  • ∇θlog⁡…\nabla_\theta \log \dots: the geometric gradient direction
  • β\beta: the KL penalty coefficient

This gradient expression reveals the geometry of DPO optimization. The term ∇θlog⁡πθ(yw∣x)−∇θlog⁡πθ(yl∣x)\nabla_\theta \log \pi_\theta(y_w|x) - \nabla_\theta \log \pi_\theta(y_l|x) points in the direction that increases the probability of the winning response while decreasing the probability of the losing response. This is the natural direction to push given our objective.

The term σ(−r^θ(x,yw)+r^θ(x,yl))\sigma(-\hat{r}_\theta(x, y_w) + \hat{r}_\theta(x, y_l)) is the probability that the model currently assigns to the wrong preference. This acts as an implicit weighting:

  • When the model is confident and correct (low wrong-preference probability), the gradient is small
  • When the model is confident but wrong, the gradient is large
  • When the model is uncertain, the gradient is moderate

This automatic weighting helps the model focus on examples it's getting wrong, similar to how hard negative mining works in contrastive learning.

Out[4]:
Visualization
Line plot showing gradient weight decreasing sigmoidally from 1 to 0 as reward difference increases from -4 to 4.
The DPO gradient weighting function (probability of the wrong preference) across implicit reward differences. Large weights occur when the model incorrectly favors the rejected response (negative difference), triggering strong updates. Small weights occur when the model correctly favors the chosen response (positive difference), stabilizing training.

Connecting the Gradient to Language Model Training

It is instructive to compare the DPO gradient to what happens during ordinary language model pre-training. In standard cross-entropy training on tokens, the gradient for a single token at position tt increases the log-probability of the correct next token wtw_t and the magnitude is governed by the current probability of that token: if the model already assigns high probability, the gradient is small. DPO follows the same pattern but at the sequence level: it increases log-probability for the entire chosen sequence ywy_w and decreases it for the rejected sequence yly_l, weighted by how wrong the model currently is.

This parallel makes DPO feel like a natural extension of language modeling rather than a fundamentally different training paradigm. You can implement DPO in virtually any training framework that supports computing sequence log-probabilities, because the loss is just a scalar function of those log-probabilities. The practical implementation details, including how to handle variable-length sequences, padding masks, and batched reference model inference, are covered in the next chapter on DPO Implementation.