Reward models from human comparisons
Replace an LM's unembedding with a single scalar output and train it with the Bradley-Terry loss −log σ(r(x,y_w) − r(x,y_l)) on pairs of preferred (y_w) and rejected (y_l) responses. Explain what the resulting reward does and does not capture.
Builds on: Bradley-Terry model for pairwise preferences, The decoder-only transformer, end to end
What the animation says
No answer key
In pretraining, every position had a correct next token to check against. An assistant's reply has no answer key like that. Ask people to score a reply out of ten, and their numbers wander. But show them two replies, and they reliably pick the better one. So we collect many such picks. We want a function that scores any prompt and reply, even replies nobody has rated.
A one-number head
Start with a pretrained transformer. It already reads text well. Feed it the prompt followed by the response, as one sequence. Normally the last layer turns each position into a logit for every word in the vocabulary. We swap that for a head that outputs one number: the reward. Similar replies get similar scores, so it can rate replies nobody has seen. Under the causal mask, which position's hidden state should feed that head? Tap it.
Loss on one pair
Training uses pairs: a chosen reply and a rejected one, for the same prompt. The model scores them: one point two for chosen, zero point two for rejected. The gap, delta, is one. Sigmoid of one is about zero point seven three. The loss is minus the log of that: about zero point three one. Its gradient on the gap is minus zero point two seven: a gentle nudge apart. Now suppose the order were wrong, delta minus one. How hard is the push?
Push where it's wrong
With the wrong order, sigmoid of minus one is about zero point two seven. The loss jumps to one point three one, and the push to zero point seven three. Almost three times stronger. Plot loss against the gap. On the left, wrong order, it's steep. On the right, it flattens out. So pairs already ranked by a wide margin contribute almost nothing. In code, this line computes the loss without overflow. Now add five to both scores of a pair. What happens to its loss?
What the score means
Unchanged. Shifting both scores by the same amount leaves the gap, and the loss, untouched. So the head's bias learns nothing. Scores only compare replies to the same prompt. A two on one prompt and a three on another tells you nothing about which reply is better. The model also inherits raters' tastes and blind spots, like a liking for longer answers. And trust it only on replies that resemble its training pairs.
A transformer with a one-number head
Reward head and Bradley-Terry loss
Loss and push on one pair
Reward-model loss in numpy
import numpy as np
rng = np.random.default_rng(0)
B, T, d = 4, 6, 8
H_w = rng.normal(size=(B, T, d)) # final-norm states, chosen responses
H_l = rng.normal(size=(B, T, d)) # final-norm states, rejected responses
w = rng.normal(size=d) * 0.1 # reward head: replaces W_U [d, V]
r_w = H_w[:, -1] @ w # [B] score read at last token
r_l = H_l[:, -1] @ w
delta = r_w - r_l
loss = np.mean(np.logaddexp(0, -delta)) # mean of -log sigmoid(delta)
print(loss, (r_w > r_l).mean()) # loss, pairwise accuracy
What the reward does and doesn't mean
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses