intuitiveai

RLHF

Teach a preference, not just an answer

Pretraining learns to predict text. Supervised fine-tuning (SFT) learns from demonstrations. Preference training adds a different signal: for this prompt, response A was preferred to B. None of these objectives guarantees truth or safety.

A common RLHF pipeline fits a reward model to comparisons, then optimizes a policy with a reference penalty. DPO instead trains directly on paired preferences and reference log probabilities. We will implement one small DPO update, not a full PPO pipeline.

Before starting: be comfortable with natural logs, cross-entropy and a derivative. Use Reference for the policy objective and the PPO/DPO derivation.

Initializing Interactive Playground...

Work one pair by hand

Let A be preferred. The reference q assigns [0.5, 0.5]; the policy p assigns [0.8, 0.2]. At beta=1, the DPO margin is ln(0.8/0.5) − ln(0.2/0.5) = ln 4 = 1.386294. Its pair loss is −ln sigmoid(margin) = 0.223144 nats, down from ln 2 at p=q.

Parameterize p(A)=sigmoid(theta). With this uniform reference, margin=beta × theta, and the loss derivative is −beta × sigmoid(−beta × theta). At theta=0 and learning rate 0.2, one gradient step gives theta=0.1 and p(A)=0.524979. The downloadable runner computes this update and checks the derivative numerically.

For a real language model, use sums of completion-token log probabilities, excluding prompt and padding tokens, for the original sequence-level DPO objective. Do not substitute probabilities, average lengths silently, or update the reference checkpoint.

A lower loss can hide a worse answer

Suppose labels prefer a verbose answer even when its arithmetic is wrong. DPO can fit those labels successfully while task accuracy falls. Keep the training preference and the independent correctness check separate.

In Implementation, repair the stable loss, then run the two-response experiment. Freeze settings before evaluating the transfer fixture. In Practice, explain the disagreement between label fit and correctness; a loss number alone is not an acceptance test.

Experiment notes

Record your experiment

Keep beta=1 and move the policy probability of A from 0.5 to 0.8. Predict the direction of pair loss and exact KL, then explain why neither measures independent correctness.

Feedback & sharing

Helpful?