A pre-trained model only continues text. To make an assistant, people compare answers and a reward model learns what they prefer. That reward then steers the chat model.
AI · beginner · 10 min · 23 steps · free
What you'll learn
Tell pre-training, fine-tuning and RLHF apart
Train a tiny reward model from one human preference
Explain how the reward steers the chat model
rlhf.py
import math
# A pre-trained model just continues text. To make it helpful, people compare two answers,
# and a reward model learns to score answers the way they would.
answers = ["Paris is the capital of France.", "idk lol"]
features = [[1.0, 1.0], [0.0, 0.0]] # [polite, answers the question]
weights = [0.0, 0.0] # the reward model starts knowing nothing
lr = 0.5 # a person preferred the first answer
for epoch in range(3):
rewards = [round(weights[0] * f[0] + weights[1] * f[1], 2) for f in features]
agree = round(math.exp(rewards[0]) / (math.exp(rewards[0]) + math.exp(rewards[1])), 2)
weights = [round(w + lr * (1 - agree) * (features[0][i] - features[1][i]), 2) for i, w in enumerate(weights)]
print("epoch", epoch, "rewards", rewards, "agrees with the person:", agree)
best = answers[rewards.index(max(rewards))]
print("the chat model is then trained towards answers like:", best)
Step by step
This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
Two answers to "What is the capital of France?".
Two simple features per answer: is it polite, and does it answer? Real reward models are whole language models reading the text.
The reward model's weights start at 0: it has no opinion yet.
A person preferred the first answer. The reward model will learn to agree.
epoch is created with the value 0.
Score each answer: both get 0.
The chance the reward model agrees with the person is a softmax over the two rewards: 0.5, a coin toss.
Nudge the weights towards the features of the preferred answer, by how much it still disagreed (1 − 0.5): both weights become 0.25.
It prints epoch 0 rewards [0.0, 0.0] agrees with the person: 0.5.
epoch changes from 0 to 1.
Now the good answer scores 0.5 and the bad one 0.
Agreement 0.62.
weights now points to a different list: [0.44, 0.44].
It prints epoch 1 rewards [0.5, 0.0] agrees with the person: 0.62.
epoch changes from 1 to 2.
Rewards 0.88 and 0.
Agreement 0.71, and rising.
weights now points to a different list: [0.58, 0.58].
It prints epoch 2 rewards [0.88, 0.0] agrees with the person: 0.71.
Python moves to the next item of the loop: for epoch in range(3).
The answer with the highest reward.
The polite, correct answer wins. In RLHF, the chat model then writes many answers, the reward model scores them, and reinforcement learning pushes it towards the high scorers. That is how a text predictor becomes a helpful assistant.
Common mistakes
Thinking RLHF teaches new facts: it mostly shapes style and behaviour
Assuming the reward model is always right: models can learn to please it without being correct
Best practices
Judge assistants on correctness as well as tone
Key takeaways
Pre-training teaches language; fine-tuning on example chats teaches the assistant format.
People compare answers; a reward model learns to predict their preferences.
RLHF trains the chat model to get high rewards.
It shapes behaviour and tone; it does not guarantee correct facts.
Check yourself
What do people provide in RLHF?
Answer: Which of two answers they prefer. Comparisons are the human signal; the reward model learns from them.
A model is very polite but often wrong. Could RLHF have caused that?
Answer: Yes, if the reward favoured tone over correctness. The model learns whatever the reward model rewards.