All labs Animated labs The chapter Course home

Lab 10Alignment: reward, penalty and margin

Preference tuning teaches a model which of two answers people prefer. Three small calculations carry the whole idea: a loss built from comparisons, a penalty that stops the model gaming its judge, and a margin that DPO reads straight off the model. Change the numbers and watch each one move. They come from alignment.py, so they match the chapter.

1. Turning a comparison into a loss

A reward model gives each answer a score. The Bradley-Terry model says the chance it agrees with the person is σ(rw − rl), where w is the answer they preferred and l the one they rejected.

2. Why the penalty is not an afterthought

The policy is optimised against the reward model, and left alone it finds text the reward model loves and people do not. The objective is the reward minus β times the KL divergence from the model it started as. Set β and see which candidate the optimiser picks.

β:

honest answer (reward 4.00, KL 1.00) reward hack. The ringed point is where the two lines cross.

What to look for

  • Set β to 0. The hack wins, because 5.00 beats 4.00 and the reward model is the only judge.
  • Raise β. The hack's objective falls much faster than the honest answer's, because its KL is twelve times larger. They cross at β = 0.0909. The chapter's table only tries 0.05 and 0.10, so it shows the order flipped by 0.10.
  • Push β to 0.5. The honest answer wins easily, but too large a β has its own cost: the policy stays so close to the reference that preference training changes nothing.
  • Raise the hack's reward to 8 and its KL to 12. A bigger reward needs a bigger penalty to be held back.

3. Direct preference optimisation

DPO drops the reward model. A policy trained under the penalised objective already carries a reward in its own log-ratio against the reference: r̂(y) = β(log πθ(y) − log πref(y)). Slide the policy away from the reference and watch the margin and the loss.

With β = 0.1 the three named stages are the chapter's table. At the start the policy is the reference, so the margin is 0 and the loss is log 2 = 0.6931, the same number the undecided Bradley-Terry pair gave. Moving past 1 over-corrects.

Where this fits

This is Adaptation and Alignment. Run alignment.py to print the same tables as text.