liveBook · Manning

DPO loss

As explained in Reinforcement Learning from Human Feedback

The DPO loss is a differentiable preference-training objective that increases the relative likelihood of chosen completions over rejected completions relative to a reference policy.

The DPO loss trains a language-model policy directly from preference pairs. It increases the chosen completion’s relative likelihood over the rejected completion while constraining movement from a frozen reference policy through β, avoiding an explicit reward-model training stage. section 8.1.1

Equation 8.2
\[\label{eq:dpo_core} \mathcal{L}_{\text{DPO}}(\pi_\theta; \pi_{\text{ref}}) = -\mathbb{E}_{(x, y_c, y_r) \sim \mathcal{D}}\left[ \log \sigma\left( \beta \log \frac{\pi_{\theta}(y_c \mid x)}{\pi_{\text{ref}}(y_c \mid x)} - \beta \log \frac{\pi_{\theta}(y_r \mid x)}{\pi_{\text{ref}}(y_r \mid x)} \right) \right].\]

The objective applies a negative log-sigmoid to the difference between chosen and rejected log-likelihood shifts relative to the reference model. The loss falls when the chosen shift exceeds the rejected shift. section 8.1.1

Operationally, calculate the loss as a sequence-level preference comparison:

  • Run policy and reference forward passes on the chosen and rejected completions to obtain sequence log-probabilities. section 8.3
  • Subtract rejected log-probabilities from chosen log-probabilities for each model, then subtract the reference ratio from the policy ratio. section 8.3
  • Apply negative log-sigmoid to β times that difference; β controls the preference pull and effective KL constraint. section 8.1.1 section 8.3
  • Detach implicit chosen and rejected rewards for logging, and cache reference log-probabilities when reducing peak memory matters. section 8.3

Preference-pair behavior

For one prompt, compare a winning completion with a losing completion. If policy training raises the winner’s log-ratio over the reference more than the loser’s, the margin grows and the DPO loss decreases. A DPO+NLL variant additionally penalizes low absolute likelihood on the winning completion. section 8.1.1 section 15.3.1

How Reinforcement Learning from Human Feedback covers it

How deep it goes
Derives the objective, interprets its terms, gives implementation steps, and discusses memory caching and gradient behavior.
Examples
Uses chosen–rejected completion pairs, sequence log-probability calculations, implementation pseudocode, and a DPO+NLL modification.
What it assumes
Assumes preference pairs, sequence log-probabilities, a trainable policy, and a frozen reference model.
What you can do after
Apply it in standard language-model training stacks using policy and frozen-reference forward-pass log-probabilities.
How it connects
Connects Bradley–Terry preference modeling, RLHF, KL divergence, reference policies, implicit rewards, and policy-gradient optimization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Reinforcement Learning from Human Feedback. The section links point to the parts of the book it draws on.