advanced34 min studyLesson 68 in Engineering

Preference optimization and alignment

Derive a pairwise preference objective relative to a reference policy and evaluate what preference labels can establish.

On this page

What you’ll understand

  • Distinguish supervised demonstrations, reward modeling, and direct preference optimization.
  • Calculate a DPO pair loss from policy-to-reference probability ratios.
  • Identify preference-data bias and evaluate behavior beyond training-pair agreement.

A pretrained next-token objective does not fully specify how an assistant should respond to a user's instruction. Preference training adds judgments about desirable responses, such as usefulness, clarity, or adherence to a policy. Those judgments are data with a collection process and a population, not an objective definition of all good behavior. The engineering task is to state what was preferred, optimize a controlled objective, and test whether the resulting policy behaves well on held-out interactions and important failure cases.

Different supervision stages answer different questions

Supervised fine-tuning learns from example responses to prompts. A preference dataset instead presents alternatives and records which response a labeler favors under a rubric. One RLHF approach fits a reward model to those comparisons, then optimizes a policy while penalizing departure from a reference. InstructGPT provides an influential example of demonstrations followed by human preference feedback. Its empirical improvements are evidence for a particular training and evaluation setup, not proof that every preference-trained policy is truthful or universally aligned.

Write the annotation instructions and record ambiguous or disputed comparisons. Preferences can reflect conflicting judgments about style, length, facts, or values. If longer answers are often preferred, a model may learn verbosity instead of stronger reasoning. Collect representative prompts, separate related prompts across splits, and inspect annotator disagreement. Label model-selected or rule-selected pairs with their provenance rather than calling them independent human judgments.

max⁡π  Ex∼D, y∼π(⋅∣x)[r(x,y)]−β ExDKL ⁣(π(⋅∣x)∥πref(⋅∣x))\max_{\pi}\;\mathbb{E}_{x\sim D,\,y\sim\pi(\cdot\mid x)}[r(x,y)]-\beta\,\mathbb{E}_x D_{\mathrm{KL}}\!\left(\pi(\cdot\mid x)\|\pi_{\mathrm{ref}}(\cdot\mid x)\right)
Prompts x follow dataset distribution D, and responses follow the policy. This schematic objective trades expected reward against departure from a reference policy, with β positive. Reward remains an estimated or specified preference signal rather than an oracle for truth.↔ Scroll wide equations horizontally.

DPO compares relative sequence likelihoods

Direct Preference Optimization derives a pairwise classification objective from a regularized preference formulation. For each prompt, compare the policy's likelihood ratio to a fixed reference for the preferred response and for the rejected response. The loss encourages a higher relative score for the preferred response. This can avoid fitting and then sampling against a separate reward model during the DPO update, but it still requires appropriate preference data, optimization, and evaluation. It is not merely supervised training on the preferred text alone.

Use sums of token log probabilities for sequence scores, with consistent tokenization, prompt masking, and end-token treatment. Padding must not contribute to the score. Standard DPO uses sequence log likelihoods; replacing them with length-normalized scores changes the objective and should be named explicitly. Beta appears both in the derivation's regularization tradeoff and in the scaled pair logit. Its practical effect depends on optimization and data, so do not infer a universal update size from its numerical value alone.

ℓDPO=−log⁡σ ⁣(β[log⁡πθ(yw∣x)πref(yw∣x)−log⁡πθ(yl∣x)πref(yl∣x)])\ell_{\mathrm{DPO}}=-\log\sigma\!\left(\beta\left[\log\frac{\pi_\theta(y_w\mid x)}{\pi_{\mathrm{ref}}(y_w\mid x)}-\log\frac{\pi_\theta(y_l\mid x)}{\pi_{\mathrm{ref}}(y_l\mid x)}\right]\right)
yw is the preferred response and yl the rejected response for the same prompt. Positive sequence probabilities and a fixed reference are assumed; compute their logarithms stably rather than multiplying many tiny token probabilities.↔ Scroll wide equations horizontally.

Evaluate the policy, including its tradeoffs

Held-out pair accuracy measures whether the model orders similar response pairs as desired. It does not fully test interactive task success, truthfulness, robustness, or the appropriateness of refusal. Evaluate factual and procedural correctness with independent criteria, and compare responses with length and formatting controlled where relevant. Include distribution shifts, conflicting instructions, and cases requiring uncertainty. A policy can improve preference scores by sounding confident while becoming less honest about missing evidence.

Track capability regressions as well as preferred behavior. Evaluate the base or supervised reference under the same prompts and decoding settings, report important slices, and document data filtering. Preserve the training recipe, reference checkpoint, preference dataset identity, and scoring convention. Human feedback and automated judges can both carry systematic bias. Treat alignment as measured behavioral requirements with stated limits, rather than a binary property bestowed by one loss.

Calculate one preference-pair loss

For one prompt, the reference assigns probability 0.2 to the preferred response and 0.4 to the rejected response. The current policy assigns 0.4 and 0.2 respectively. Use β=0.5 and natural logarithms; other responses hold the remaining probability mass.

  1. The preferred policy-to-reference ratio is 0.4/0.2 = 2. The rejected ratio is 0.2/0.4 = 0.5. Their log-ratio difference is ln(2) − ln(0.5) = ln(4).

  2. Multiply by β=0.5 to get ln(2). Applying sigmoid gives 2/(1+2) = 2/3 as the pairwise model probability under this loss.

  3. The loss is −ln(2/3), approximately 0.4055. If policy and reference were identical, both ratios would be one and the loss would be ln(2), approximately 0.6931.

The result

This policy already favors the preferred response relative to its reference, yielding a lower pair loss. One pair says nothing about overall factual accuracy or the preference dataset's representativeness.

Explore the model

Interactive lab

Interactive lab · network

Inspect a logistic comparison

The lab computes σ(wx−0.5) from one input and one weight. Its sigmoid shape is the same mathematical link used in a pairwise loss, but the fixed bias and controls do not calculate DPO sequence ratios, train a policy, or represent a full language model.

Inspect a logistic comparisonOne inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Current readings: Product · wx: 1.2; Logit · wx − 0.5: 0.7; Sigmoid output: 0.6682. All parameters are adjustable with the labeled controls below.One input. One weight. One sigmoid.x1.20× w1.20+ b0.70σ0.668output0.668w = 1.00b = −0.50σ(z) = 1 / (1 + exp(−z))
Computation graphSignal trace
Product · wx
1.2
Logit · wx − 0.5
0.7
Sigmoid output
0.6682
t = 0 s

One inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA smooth pairwise probability can encode a preference comparison without making that comparison objectively correct.

Check your understanding

What is the strongest justified conclusion from lower held-out DPO pair loss alone?

Try it yourself

Policy and reference give identical probabilities to both responses in a pair. What are the DPO pair logit, implied pair probability, and loss for any positive β?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Each policy-to-reference ratio is one, so begin with its logarithm.

Compare with the solution

Both log ratios are zero, so the scaled difference is zero for any positive β. Sigmoid(0)=0.5 and the loss is −ln(0.5)=ln(2), approximately 0.6931. The current policy has not yet shifted the pair's relative preference away from the reference under this objective.

Take it with you

  • Preference labels encode a rubric and population, not an infallible reward oracle.
  • DPO compares reference-relative sequence likelihoods for preferred and rejected responses.
  • Measure correctness, robustness, and regressions independently of preference-pair fit.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.