Papa Programmer
Lesson 14 of 20

Learning from human preferences

10 min read

Optional

Comparing is easier than writing

Instruction tuningTraining a pre-trained model further on examples of requests and good answers, so that it follows instructions and replies as an assistant instead of only continuing text. needs a good answer for every example. For many requests, nobody can write the single best answer. People can still look at two answers and say which is better: clearer, more accurate, kinder or safer. Preference training uses those comparisons.

RLHF in three steps

RLHFReinforcement learning from human feedback. People compare pairs of answers, a reward model learns their preferences, and the LLM is trained to produce answers that the reward model scores highly. stands for reinforcement learning from human feedback. The InstructGPT paper from 2022 set out the recipe that many later models followed.

  1. Fine-tuneTrain the base model on requests with good answers.
  2. ComparePeople rank pairs of the model's answers, and a reward model learns to score answers the way they do.
  3. OptimiseTrain the LLM to write answers that the reward model scores highly.
The three steps of RLHF

The reward model

The reward model reads a request and an answer and gives back one number, the reward. It learns from the pairs that people compared. For each pair, the chance that the chosen answer wins is the sigmoid of the reward gap. The sigmoid function turns any number into a value between 0 and 1. Training lowers the negative logarithm of that chance.

python
import math

def preference_loss(reward_chosen, reward_rejected):
	p_chosen = 1 / (1 + math.exp(-(reward_chosen - reward_rejected)))
	return p_chosen, -math.log(p_chosen)

for chosen, rejected in [(2.0, 0.0), (0.5, 0.0), (0.0, 1.0)]:
	p, loss = preference_loss(chosen, rejected)
	print(f"chosen {chosen}, rejected {rejected}: P(chosen wins) = {p:.2f}, loss = {loss:.3f}")

It prints:

text
chosen 2.0, rejected 0.0: P(chosen wins) = 0.88, loss = 0.127
chosen 0.5, rejected 0.0: P(chosen wins) = 0.62, loss = 0.474
chosen 0.0, rejected 1.0: P(chosen wins) = 0.27, loss = 1.313

When the reward model already scores the chosen answer well above the rejected one, the loss is small. When it scores them the wrong way round, the loss is large, and training pushes the scores apart in the right direction.

The optimisation step

In the last step, the LLM writes answers, the reward model scores them, and a reinforcement learning method, often one called PPO, raises the chance of high-scoring answers. A penalty keeps the model close to the fine-tuned model from step 1. Without that penalty, the model drifts towards odd text that happens to score well.

DPO skips the reward model

Direct Preference Optimization, or DPO, was published by Rafailov and others in 2023. It trains the LLM on the preference pairs directly, with a loss that raises the chance of the chosen answer and lowers the chance of the rejected one. It needs no separate reward model and no reinforcement learning loop, so it is simpler and more stable to run. Many open models now use DPO or a method like it.

RLHF with PPODPO
Separate reward modelYesNo
Training loopThe model writes answers and gets scoredOrdinary training on stored pairs
Effort to runHigh, with many settings to tuneLower, close to plain fine-tuning
Two ways to train on preferences

Feedback from rules instead of people

Collecting human comparisons is slow and costly. Anthropic's 2022 Constitutional AI paper, by Bai and others, used a model to compare answers instead, guided by a written list of principles. This approach is often called RLAIF, reinforcement learning from AI feedback.

What can go wrong

A model trained to win approval can learn the wrong lesson.

  • Reward hacking. The model finds patterns that score well but are not better, such as longer answers when the reward model likes length.
  • Sycophancy. The model agrees with the user even when the user is wrong. A 2023 Anthropic study by Sharma and others found this behaviour across several assistants trained with human feedback, and linked it to people's preference for answers that agree with them.

Teams reduce these problems with careful rating guidelines, varied raters and tests that look for each failure.

People compare, the model learnsPreference training uses which of two answers people prefer.
Two main recipesRLHF trains a reward model first. DPO trains on the pairs directly.
Approval is not truthWatch for reward hacking and for answers that only tell people what they want to hear.

Your reading position is saved

0%

0 of 20 complete

Contents

Learning tools