Language models, retrieval, prompting, and language adaptation · Instruction, preference, and parameter-efficient adaptation · LNG065 · Training method
Direct Preference Optimization (DPO)
Trains a policy directly from preferred/rejected response pairs relative to a reference policy, avoiding an explicit separately optimized reward model in the standard formulation.
In CodeSplash
No method yet, but the reference's example runs here as a notebook.
Do it in CodeSplash Open the reference's example as a Python or R notebook.
- Availability
- Runs as a notebook
- Code example
- Complete calculation
- Who ran it
- CodeSplash's own run
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Trains a policy directly from preferred/rejected response pairs relative to a reference policy, avoiding an explicit separately optimized reward model in the standard formulation. It is a preference-training objective, not a model architecture.
Question / objective
Can preference pairs improve a policy through a direct reference-relative objective?
Architecture / computational structure
A trainable policy and frozen reference score chosen/rejected sequences; the scalar DPO objective updates the policy or its adapters. Reference weights remain fixed; beta and length/masking conventions are hyperparameters.
Assumptions
Pair labels may be inconsistent or biased, and offline preference data may not cover current policy behavior.
Formula / computational definition
L=-log sigma(beta[(log pi_theta(y_w|x)-log pi_ref(y_w|x))-(log pi_theta(y_l|x)-log pi_ref(y_l|x))]). Beta controls the scale of the reference-relative preference margin.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Beta, reference policy, preference dataset, learning rate, response masking, length treatment, and adapter versus full-weight updates.
Fitting, fine-tuning, and adaptation
Prepare paired responses for each prompt and compute properly masked sequence log probabilities. Update full weights or LoRA while freezing the reference. Tune beta and learning rate on held-out pairs and independent response quality; monitor divergence, length inflation, and loss of base-task competence.
How to use and calibrate
Use held-out pair accuracy, independent response comparisons, reference divergence, task retention, response length, and reward-hacking checks. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.
Example application
Improve answer preferences without separately fitting a reward model.
Interpretation / pitfalls
Watch out
Sequence length and token-probability aggregation affect optimization; preference gain can coexist with factuality loss. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.
Python and R code examples
Full implementation route: TRL documents DPO training; inspect the installed version's dataset format and configuration. R uses reticulate. The native paired example computes exactly the scalar DPO loss for one preference pair without claiming to fine-tune a model.
Example scope: Exact scalar DPO loss for one chosen/rejected pair of sequence log probabilities.
Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.
Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Complete calculation — Exact scalar DPO loss for one chosen/rejected pair of sequence log probabilities.
Shared setup for this chapter
import math
def softmax(xs):
m = max(xs)
es = [math.exp(x-m) for x in xs]
return [e/sum(es) for e in es]
def dot(a, b):
return sum(x*y for x, y in zip(a, b))
def matvec(matrix, vector):
return [dot(row, vector) for row in matrix]
def meanvec(rows):
return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]
def sigmoid(x):
return 1/(1+math.exp(-x))
def nll(probabilities):
return -sum(math.log(p) for p in probabilities)
log_pi_w,log_pi_l,log_ref_w,log_ref_l = -2.0,-3.0,-2.5,-2.7
beta = 0.2
margin = beta*((log_pi_w-log_ref_w)-(log_pi_l-log_ref_l))
print(-math.log(sigmoid(margin)))
Complete calculation — Exact scalar DPO loss for one chosen/rejected pair of sequence log probabilities.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
log_pi_w <- -2; log_pi_l <- -3; log_ref_w <- -2.5; log_ref_l <- -2.7
beta <- 0.2
margin <- beta*((log_pi_w-log_ref_w)-(log_pi_l-log_ref_l))
print(-log(plogis(margin)))
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG065.