All courses

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

▶ Watch this lesson Start the whole course

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.

Scoring answers that have no answer key

Pretraining had a correct next token at every position. Assistant replies have no answer key, and people cannot reliably say "this reply is a 7.3". They *can* reliably pick the better of two replies. How do we turn many such picks into a function $r(x,y)$ that scores any new prompt–response pair, including responses nobody has ever rated?

A transformer with a one-number head

Start from a pretrained transformer, which already reads text well. Feed it the prompt and response as one sequence. At the last position, replace the unembedding (which gives $V$ logits) with a head that outputs **one number**. Then fit Bradley-Terry, but the scores come from the network instead of a table of free numbers. Similar responses get similar scores, so it generalizes to unseen responses.

Reward head and Bradley-Terry loss

Let $h_T\in\mathbb{R}^{1\times d}$ be the final-norm hidden state at the last token of $x\|y$ (prompt followed by response). Under the causal mask, only this position has seen everything. $$r(x,y)=h_T\,w+b,\quad w\in\mathbb{R}^{d\times 1}$$ $$\mathcal{L}_{RM}=-\operatorname{mean}_{\text{pairs}}\log\sigma\big(r(x,y_w)-r(x,y_l)\big)$$ Adding any $c(x)$ to every response of prompt $x$ changes no loss term. So $b$ is useless, and scores only compare responses **to the same prompt**.

Loss and push on one pair

Let $\Delta=r(x,y_w)-r(x,y_l)$. **Right order:** $r_w=1.2$, $r_l=0.2$, so $\Delta=1$. $\sigma(1)=0.731$, and the loss is $-\log 0.731=0.313$. Then $\partial\mathcal{L}/\partial\Delta=-(1-\sigma(\Delta))=-0.269$: a gentle nudge of $r_w$ up and $r_l$ down. **Wrong order:** $\Delta=-1$. $\sigma=0.269$, the loss is $1.313$, and the gradient is $-0.731$, almost 3× the push. Pairs already ranked by a wide margin contribute almost nothing.

Reward-model loss in numpy

Toy hidden states stand in for the transformer's final-norm output. Real batches pad sequences to equal length, so you would take each row's last *non-pad* token instead of `-1`. `logaddexp(0, -delta)` computes $\log(1+e^{-\Delta})=-\log\sigma(\Delta)$ without overflow. Backpropagation then carries the gradient into $w$ and every transformer weight.
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

A reward model is a transformer whose unembedding is swapped for a one-number head, trained so preferred responses outscore rejected ones. Its score predicts *which of two answers to the same prompt raters would pick*. It does not measure absolute quality, and it is not comparable across prompts. It inherits raters' tastes and blind spots (e.g. a liking for length). It is trustworthy only on responses resembling its training pairs.

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.