Simple Algorithm
Deep Learning · Representation · 24 min

Fine-Tuning & Aligning Large Models

Instruction tuning, policy gradients from REINFORCE to PPO, and learning from preferences.

Labelers preferred the answers of a 1.3-billion-parameter InstructGPT model to those of the 175-billion-parameter GPT-3, a model with the same architecture and over 100 times as many parameters. The difference was post-training: about 13,000 written demonstrations, a reward model learned from human rankings, and a reinforcement learning stage. For the 175-billion-parameter version, the SFT and RL stages together took under 2% of the compute used to pretrain GPT-3 (Ouyang et al., 2022). This lesson derives the machinery: supervised fine-tuning, policy gradients from REINFORCE to PPO, and rewards learned from preferences.

What you’ll learn

  • Write the supervised fine-tuning loss for chat data, with a chat template and a loss mask on the prompt, and explain what it changes in a base model.
  • Derive the policy gradient ∇θE[r]=E[r ∇θlog⁡πθ(y∣x)]\nabla_{\boldsymbol\theta}\E[r]=\E\big[r\,\nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(y\mid x)\big] (REINFORCE), and prove that subtracting a baseline keeps it unbiased.
  • Explain PPO's clipped surrogate objective and the KL penalty to a reference model, and how each limits how far the policy moves.
  • Describe the RLHF pipeline (demonstrations, a Bradley–Terry reward model, KL-regularized RL), direct preference optimization as an RL-free alternative, and reinforcement learning with verifiable rewards.

From base model to assistant

Pretraining by next-token prediction produces a base model: a distribution over continuations of web-like documents. Give it "Write a haiku about rain." and it may write a haiku, or it may add three more writing prompts, as if the request were one line of a worksheet. Both are plausible continuations of a document. An assistant should treat the text as a request from a user and answer it.

Post-training reshapes the base model's distribution toward helpful, honest and harmless responses, using orders of magnitude less data and compute than pretraining. The recipe that made this standard, InstructGPT, had three stages:

StageDataObjectiveIn InstructGPT
  1. Supervised fine-tuning (SFT)
prompts with responses written by labelersmaximize the likelihood of the responseabout 13,000 prompts, 16 epochs
  1. Reward model
labelers' rankings of 4 to 9 sampled responses per prompta Bradley–Terry loss on every pair from each ranking33,000 prompts; one 6-billion-parameter reward model for all policy sizes
  1. Reinforcement learning
prompts only; the reward model scores each sampled responsePPO on the reward minus a KL penalty toward the SFT model31,000 prompts, 256,000 episodes, β=0.02\beta=0.02
From Ouyang et al. (2022), whose labelers were a team of about 40 contractors. Later pipelines change every row: pairs instead of rankings, direct preference optimization instead of PPO, labels from AI models, and automatic checkers instead of reward models.
Think first A base model has read millions of haiku. If it can already write one, what does supervised fine-tuning on demonstrations teach it?

Mostly which of its behaviors to produce when a prompt arrives: the format, persona and conventions of an assistant, and when to stop. Demonstrations select from the base model's repertoire far more than they add to it. How far this "superficial" view holds, and where it breaks, is the subject of Instruction Tuning & Its Data.

Supervised fine-tuning on demonstrations

A conversation is a list of messages with roles. Before training, a chat template renders it into one token sequence, with special tokens that mark where each role's turn begins and ends:

text
<|system|>You are a helpful assistant.<|end|>
<|user|>Write a haiku about rain.<|end|>
<|assistant|>Soft drops on the roof,
the gutter hums its one note,
the street smells of stone.<|end|>

A schematic template. The role markers and the end-of-turn token are extra tokens added to the vocabulary, and every model family defines its own. A model served with a template other than the one it was tuned on often responds worse.

Let xx be everything before the assistant's turn (system prompt, earlier turns, markers) and y=(y1,…,y∣y∣)y=(y_1,\dots,y_{\abs y}) the assistant's tokens, ending with the end-of-turn token. SFT is maximum likelihood on the responses alone:

LSFT(θ)=−∑(x,y)∈D ∑t=1∣y∣log⁡πθ(yt∣x, y<t)\loss_{\text{SFT}}(\boldsymbol\theta)=-\sum_{(x,y)\in\data}\ \sum_{t=1}^{\abs y}\log\pi_{\boldsymbol\theta}\big(y_t\mid x,\,y_{<t}\big)
The same cross-entropy as pretraining, with the prompt's tokens masked out: the model still reads them (they are in the context) but is not trained to predict them. Including the end-of-turn token teaches the model when to stop. In multi-turn data every assistant turn is a target, and user turns are masked.
python
def sft_loss(model, ids, is_response):
    """ids: (B, T) chat-formatted token ids; is_response: (B, T) True on assistant tokens."""
    labels = ids.masked_fill(~is_response, -100)   # -100 = "ignore" for cross_entropy
    logits = model(ids)[:, :-1]                     # position t predicts token t + 1
    return F.cross_entropy(logits.reshape(-1, logits.size(-1)),
                           labels[:, 1:].reshape(-1), ignore_index=-100)

Loss masking in PyTorch, for a model that returns logits of shape (B, T, |V|). The loss is averaged over the response tokens of the whole batch, so long responses weigh more than short ones; Instruction Tuning & Its Data discusses that choice, packing several examples into one sequence, and when keeping some loss on the prompt helps.

Check your understanding

InstructGPT's SFT models overfit on validation loss after one epoch, yet the authors trained for 16 epochs. Why can that be the right call?

  1. Validation loss is meaningless for language models
  2. More epochs always reduce validation loss eventually
  3. The goal is responses people prefer, and the reward-model score and human ratings kept improving with more epochs
  4. The extra epochs were needed to learn the chat template
Show the answer

C. The goal is responses people prefer, and the reward-model score and human ratings kept improving with more epochs

Ouyang et al. selected the SFT model by reward-model score, which, like human preference ratings, kept improving despite the rising validation loss. Likelihood of held-out demonstrations measures how well the model imitates one labeler's exact wording; post-training is judged by the quality of what the model itself generates.

By default SFT updates every weight. Parameter-efficient methods train a small fraction instead, such as low-rank adapters (Fine-Tuning & Transfer, Low-Rank Adaptation in Depth), and any fine-tuning can erode skills learned earlier (Catastrophic Forgetting & Continual Learning). SFT has a deeper limit: it can only imitate. To make responses better than the demonstrations, the model must be scored on its own samples.

A language model is a policy

In reinforcement-learning terms, a prompt xx is a state, a whole response yy is an action, and the model is a policy πθ(y∣x)=∏tπθ(yt∣x,y<t)\pi_{\boldsymbol\theta}(y\mid x)=\prod_t\pi_{\boldsymbol\theta}(y_t\mid x,y_{<t}). A reward r(x,y)r(x,y) scores complete responses: a reward model's estimate of human preference, or a program that checks the answer. Post-training by reinforcement learning maximizes the expected reward

J(θ)=Ex∼D Ey∼πθ(⋅∣x)[r(x,y)].J(\boldsymbol\theta)=\E_{x\sim\data}\,\E_{y\sim\pi_{\boldsymbol\theta}(\cdot\mid x)}\big[r(x,y)\big].

Two things block ordinary backpropagation: yy is sampled, a discrete choice, and rr need not be differentiable at all; a unit test passes or fails.

DerivationThe policy gradient by the log-derivative trick
  1. ∇θJ=∇θ∑yπθ(y∣x) r(x,y)=∑yr(x,y) ∇θπθ(y∣x)\nabla_{\boldsymbol\theta}J=\nabla_{\boldsymbol\theta}\sum_y\pi_{\boldsymbol\theta}(y\mid x)\,r(x,y)=\sum_y r(x,y)\,\nabla_{\boldsymbol\theta}\pi_{\boldsymbol\theta}(y\mid x)

    For one prompt, write the expected reward as a sum over responses. Only the probabilities depend on θ\boldsymbol\theta:

  2. ∇θJ=∑yπθ(y∣x) r(x,y) ∇θlog⁡πθ(y∣x)=Ey∼πθ[r(x,y) ∇θlog⁡πθ(y∣x)]\nabla_{\boldsymbol\theta}J=\sum_y\pi_{\boldsymbol\theta}(y\mid x)\,r(x,y)\,\nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(y\mid x)=\E_{y\sim\pi_{\boldsymbol\theta}}\big[r(x,y)\,\nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(y\mid x)\big]

    Use ∇π=π ∇log⁡π\nabla\pi=\pi\,\nabla\log\pi to turn the sum back into an expectation under the policy, which samples can estimate:

  3. ∇θlog⁡πθ(y∣x)=∑t=1∣y∣∇θlog⁡πθ(yt∣x,y<t)\nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(y\mid x)=\sum_{t=1}^{\abs y}\nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(y_t\mid x,y_{<t})

    The log-probability of a response is a sum over its tokens, so its gradient is exactly what backpropagation computes for the SFT loss of that sampled response:

  4. g^=1k∑i=1kr(x,yi) ∇θlog⁡πθ(yi∣x),yi∼πθ(⋅∣x)\hat{\mathbf g}=\frac1k\sum_{i=1}^k r(x,y_i)\,\nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(y_i\mid x),\qquad y_i\sim\pi_{\boldsymbol\theta}(\cdot\mid x)

    REINFORCE (Williams, 1992) averages over kk sampled responses. The estimate is unbiased but noisy: when all rewards are positive, every sample is pushed up, the good ones only slightly more.

  5. Ey∼πθ[b(x) ∇θlog⁡πθ(y∣x)]=b(x)∑y∇θπθ(y∣x)=b(x) ∇θ1=0\E_{y\sim\pi_{\boldsymbol\theta}}\big[b(x)\,\nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(y\mid x)\big]=b(x)\sum_y\nabla_{\boldsymbol\theta}\pi_{\boldsymbol\theta}(y\mid x)=b(x)\,\nabla_{\boldsymbol\theta}1=0

    A baseline b(x)b(x) that does not depend on the sampled response can be subtracted for free, because the score function has mean zero:

  6. g^=1k∑i=1k(r(x,yi)−b(x)) ∇θlog⁡πθ(yi∣x)\hat{\mathbf g}=\frac1k\sum_{i=1}^k\big(r(x,y_i)-b(x)\big)\,\nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(y_i\mid x)

    So weighting each sample by the advantage Ai=r(x,yi)−b(x)A_i=r(x,y_i)-b(x) leaves the expected gradient unchanged. With b(x)≈E[r∣x]b(x)\approx\E[r\mid x], above-average responses are reinforced and below-average ones suppressed, and the variance falls.

Predict before you look

A toy policy picks one of three responses A, B, C with softmax probabilities 0.50.5, 0.30.3, 0.20.2. It samples B, which earns reward 11; the baseline is 0.60.6. With learning rate 11, by how much does one REINFORCE step raise B's logit? (For a softmax, ∂log⁡πB/∂zj=1[j=B]−πj\partial\log\pi_B/\partial z_j=\mathbb 1[j=B]-\pi_j.)

Show the answer

0.28

The step is (r−b)(eB−π)=0.4×(−0.5, 0.7, −0.2)=(−0.20, 0.28, −0.08)(r-b)(\mathbf e_B-\boldsymbol\pi)=0.4\times(-0.5,\ 0.7,\ -0.2)=(-0.20,\ 0.28,\ -0.08), which moves the probabilities to about (0.41, 0.40, 0.19)(0.41,\ 0.40,\ 0.19). Without the baseline the step would be 0.70.7. Had B earned reward 00, the baseline would make its logit fall by 0.6×0.7=0.420.6\times0.7=0.42, whereas without a baseline nothing would happen: the baseline is what lets bad samples push probability away.

PPO: small steps and a KL leash

REINFORCE uses each sample for one gradient step and discards it, which is wasteful when generating a response costs far more than a gradient step on it. To take several steps on one batch, each token's term is reweighted by the ratio ρt(θ)=πθ(yt∣st)/πold(yt∣st)\rho_t(\boldsymbol\theta)=\pi_{\boldsymbol\theta}(y_t\mid s_t)/\pi_{\text{old}}(y_t\mid s_t) between the current policy and the one that sampled it, with st=(x,y<t)s_t=(x,y_{<t}). Maximized without limit, ρtA^t\rho_t\hat A_t drives the policy far from πold\pi_{\text{old}}, where the samples no longer represent it and the advantage estimates are stale, and training can collapse. PPO (Schulman et al., 2017) clips the ratio:

JCLIP(θ)=Et[min⁡(ρt(θ) A^t, clip⁡(ρt(θ), 1−ϵ, 1+ϵ) A^t)]J^{\text{CLIP}}(\boldsymbol\theta)=\E_t\Big[\min\Big(\rho_t(\boldsymbol\theta)\,\hat A_t,\ \operatorname{clip}\big(\rho_t(\boldsymbol\theta),\,1-\epsilon,\,1+\epsilon\big)\,\hat A_t\Big)\Big]
Maximized over θ\boldsymbol\theta for several gradient steps per batch, with ϵ\epsilon around 0.20.2 (InstructGPT's value). The expectation runs over the tokens of a batch sampled from πold\pi_{\text{old}}.
AdvantageRatioTerm kept by the minGradient on ρt\rho_t
A^t>0\hat A_t>0ρt<1+ϵ\rho_t<1+\epsilonρtA^t\rho_t\hat A_traises the token's probability
A^t>0\hat A_t>0ρt≥1+ϵ\rho_t\ge1+\epsilon(1+ϵ)A^t(1+\epsilon)\hat A_t, a constantnone: no reward for going further
A^t<0\hat A_t<0ρt>1−ϵ\rho_t>1-\epsilonρtA^t\rho_t\hat A_tlowers the token's probability
A^t<0\hat A_t<0ρt≤1−ϵ\rho_t\le1-\epsilon(1−ϵ)A^t(1-\epsilon)\hat A_t, a constantnone: no reward for going further
The minimum keeps the worse of the two terms, a pessimistic estimate: a move that looks good stops paying once the ratio leaves [1−ϵ,1+ϵ][1-\epsilon,1+\epsilon], while a move that looks bad always counts (with A^t>0\hat A_t>0 and ρt<1−ϵ\rho_t<1-\epsilon, say, the unclipped term is kept and its gradient pulls the ratio back up).
Check your understanding

With ϵ=0.2\epsilon=0.2, a token has advantage A^t=+2\hat A_t=+2 and ratio ρt=1.3\rho_t=1.3. What does this token contribute to the gradient of JCLIPJ^{\text{CLIP}}?

  1. A push to raise ρt\rho_t further, with weight 2
  2. A push back toward ρt=1\rho_t=1
  3. Nothing: the clipped term 1.2×2=2.41.2\times2=2.4 is the smaller one and does not depend on θ\boldsymbol\theta
  4. It is removed from the batch and resampled
Show the answer

C. Nothing: the clipped term 1.2×2=2.41.2\times2=2.4 is the smaller one and does not depend on θ\boldsymbol\theta

min⁡(1.3×2, 1.2×2)=2.4\min(1.3\times2,\ 1.2\times2)=2.4, the clipped term, which is constant in θ\boldsymbol\theta. PPO does not actively pull the ratio back; it simply stops rewarding further movement until the next batch is sampled from the updated policy.

For a language model each token is an action, and PPO estimates per-token advantages with a learned value network, a critic that predicts the reward still to come from each prefix, combined by generalized advantage estimation (Schulman et al., 2016). InstructGPT initialized its value network from the reward model. With the frozen reference model, that puts four large networks in play: policy, reference, reward and value. The Reinforcement Learning course derives PPO from trust-region methods and analyzes advantage estimation. Clipping limits each round of updates; a second constraint limits the whole run:

max⁡θ Ex∼D, y∼πθ(⋅∣x)[rϕ(x,y)]−β Ex∼D[DKL(πθ(⋅∣x) ∥ πref(⋅∣x))]\max_{\boldsymbol\theta}\ \E_{x\sim\data,\ y\sim\pi_{\boldsymbol\theta}(\cdot\mid x)}\big[r_\phi(x,y)\big]-\beta\,\E_{x\sim\data}\Big[\KL\big(\pi_{\boldsymbol\theta}(\cdot\mid x)\,\big\|\,\pi_{\text{ref}}(\cdot\mid x)\big)\Big]
The KL penalty, with πref\pi_{\text{ref}} the SFT model. In practice each token's reward gets −βlog⁡πθ(yt∣st)πref(yt∣st)-\beta\log\frac{\pi_{\boldsymbol\theta}(y_t\mid s_t)}{\pi_{\text{ref}}(y_t\mid s_t)}; summed over a response these terms give −βlog⁡πθ(y∣x)πref(y∣x)-\beta\log\frac{\pi_{\boldsymbol\theta}(y\mid x)}{\pi_{\text{ref}}(y\mid x)}, whose expectation is the KL term. The maximizer is π∗(y∣x)∝πref(y∣x) erϕ(x,y)/β\pi^*(y\mid x)\propto\pi_{\text{ref}}(y\mid x)\,e^{r_\phi(x,y)/\beta}, the reference model tilted toward high reward; the Artificial Intelligence course proves it.

Why a leash? The reward model learned from responses near the SFT model's and is accurate only there. Optimized without limit, the policy drifts toward responses that the reward model overrates, reward hacking. In a synthetic setup where a fixed "gold" reward model stands in for people, the gold score first rises, then falls as the KL grows (Gao, Schulman & Hilton, 2023). The coefficient β\beta sets the exchange rate between reward and drift; InstructGPT used β=0.02\beta=0.02.

Learning from preferences

People compare two responses more reliably than they score one, and more cheaply than they write one. The Bradley–Terry model turns comparisons into a reward: response ywy_w is preferred to yly_l with probability σ\sigma of the reward difference, and the reward model rϕr_\phi is fitted by maximum likelihood.

P(yw≻yl∣x)=σ(rϕ(x,yw)−rϕ(x,yl)),LRM(ϕ)=−E(x,yw,yl)[log⁡σ(rϕ(x,yw)−rϕ(x,yl))]P(y_w\succ y_l\mid x)=\sigma\big(r_\phi(x,y_w)-r_\phi(x,y_l)\big),\qquad\loss_{\text{RM}}(\phi)=-\E_{(x,y_w,y_l)}\Big[\log\sigma\big(r_\phi(x,y_w)-r_\phi(x,y_l)\big)\Big]
The reward model is usually the SFT model with its unembedding replaced by a scalar head. InstructGPT's labelers ranked K=4K=4 to 99 responses at a time, giving (K2)\binom K2 pairs per prompt. Shuffled into one dataset, the pairs made the reward model overfit within a single epoch, since each response appeared in K−1K-1 updates; training on all pairs of a prompt as one batch element fixed it.
Think first Only differences of rewards for the same prompt enter this loss. What does that say about a reward model's raw scores?

They are defined only up to a constant per prompt. A score of 3.2 means nothing alone, and scores for different prompts are not on a common scale, which is one more reason RL subtracts a baseline per prompt. InstructGPT shifted its reward model so that labelers' demonstrations scored 0 on average. The targets are noisy, too: training labelers agreed with each other on 72.6±1.5%72.6\pm1.5\% of comparisons.

InstructGPT's final objective (they called it PPO-ptx) adds a third term, the log-likelihood of pretraining text, to limit regressions on tasks the preference data never covered:

max⁡θ Ex, y∼πθ(⋅∣x)[rϕ(x,y)−βlog⁡πθ(y∣x)πSFT(y∣x)]+γ Ex∼Dpretrain[log⁡πθ(x)]\max_{\boldsymbol\theta}\ \E_{x,\ y\sim\pi_{\boldsymbol\theta}(\cdot\mid x)}\Big[r_\phi(x,y)-\beta\log\frac{\pi_{\boldsymbol\theta}(y\mid x)}{\pi_{\text{SFT}}(y\mid x)}\Big]+\gamma\,\E_{x\sim\data_{\text{pretrain}}}\big[\log\pi_{\boldsymbol\theta}(x)\big]

The results set the pattern for later assistants. The 1.3-billion-parameter InstructGPT was preferred to the 175-billion-parameter GPT-3; the 175-billion-parameter InstructGPT was preferred to GPT-3 85±3%85\pm3\% of the time, and 71±4%71\pm4\% of the time when GPT-3 was given a few-shot prompt; and on closed-domain tasks such as summarization it made up information about half as often (21% against 41%). Held-out labelers who wrote none of the training data preferred it at about the same rate.

Direct preference optimization. The maximizer π∗∝πref er/β\pi^*\propto\pi_{\text{ref}}\,e^{r/\beta} can be inverted: r(x,y)=βlog⁡π∗(y∣x)πref(y∣x)+βlog⁡Z(x)r(x,y)=\beta\log\frac{\pi^*(y\mid x)}{\pi_{\text{ref}}(y\mid x)}+\beta\log Z(x). Substituted into the Bradley–Terry likelihood, the normalizer Z(x)Z(x) cancels and a loss on the policy alone remains (Rafailov et al., 2023):

LDPO(θ)=−E(x,yw,yl)[log⁡σ(βlog⁡πθ(yw∣x)πref(yw∣x)−βlog⁡πθ(yl∣x)πref(yl∣x))].\loss_{\text{DPO}}(\boldsymbol\theta)=-\E_{(x,y_w,y_l)}\Big[\log\sigma\Big(\beta\log\frac{\pi_{\boldsymbol\theta}(y_w\mid x)}{\pi_{\text{ref}}(y_w\mid x)}-\beta\log\frac{\pi_{\boldsymbol\theta}(y_l\mid x)}{\pi_{\text{ref}}(y_l\mid x)}\Big)\Big].

No reward model is trained and nothing is sampled during training: DPO is a classification loss on fixed preference pairs. The Artificial Intelligence course derives it and its failure modes. Open recipes often combine the families: Tülu 3 applies SFT, then DPO, then reinforcement learning with verifiable rewards (Lambert et al., 2024).

Verifiable rewards, and knowing whether it worked

For some tasks the reward needs no model. A math problem has a reference answer; a program has unit tests. Reinforcement learning with verifiable rewards (RLVR, a name from Lambert et al., 2024) gives reward 1 when the extracted answer matches or the tests pass, and 0 otherwise. There is no learned reward model to exploit, though a weak checker can still be gamed: code that special-cases the visible tests passes them without solving the task.

Trained this way at scale, models learn to write long chains of thought before answering: reasoning models. DeepSeek-AI (2025) applied large-scale RL with rule-based rewards for correctness and format directly to a base model, deliberately avoiding neural reward models because they were susceptible to reward hacking. Pass@1 accuracy on the AIME 2024 competition problems rose from 15.6% to over 70% during training, and the average response length grew steadily, with more passages that re-check or revise earlier steps.

How post-training is evaluatedWhat it measuresMain pitfall
Benchmarks with reference answersknowledge, math, code, instruction-following constraintstest items leaking into training data (contamination); saturation
Human pairwise comparisons on held-out promptswhich of two models' responses people prefer (win rate); InstructGPT's main metricslow and costly; raters reward style, length and confidence
An LLM as the judgethe same comparisons, cheaply and at scaleposition, length and self-preference biases (Learning from AI Feedback)
Regression and safety suiteswhat got worse: the "alignment tax", harmful or evasive answersonly the failures someone thought to test
InstructGPT lost accuracy on SQuAD, DROP, HellaSwag and French-to-English translation during RLHF, an alignment tax that the pretraining term of PPO-ptx greatly reduced without lowering labelers' preference.

Key takeaways

  • Post-training turns a base model, which continues documents, into an assistant with little data and compute: InstructGPT used about 13,000 demonstrations, 33,000 ranked prompts and 31,000 RL prompts, and the SFT and RL stages of its 175-billion-parameter model took under 2% of GPT-3's pretraining compute.
  • SFT is maximum likelihood on the responses alone: a chat template marks the roles, and the loss masks the prompt but keeps the end-of-turn token.
  • The policy gradient is ∇E[r]=E[r ∇log⁡πθ(y∣x)]\nabla\E[r]=\E\big[r\,\nabla\log\pi_{\boldsymbol\theta}(y\mid x)\big] (REINFORCE). A baseline that does not depend on the sample leaves it unbiased and cuts its variance, weighting each sample by its advantage A=r−bA=r-b.
  • PPO's clipped ratio keeps each round of updates near the sampling policy; a KL penalty toward the reference model keeps the whole run where the reward model can be trusted.
  • Reward models fit Bradley–Terry to comparisons; DPO optimizes the same KL-regularized objective directly from preference pairs; verifiable rewards replace the reward model wherever a program can check the answer.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A training conversation has a system prompt, a user turn and one assistant turn. Under standard SFT, which tokens carry the loss?

  1. Every token, so the model learns the whole conversation
  2. The assistant's tokens, including its end-of-turn token
  3. The user's tokens, since they say what is wanted
  4. Only the last token of the assistant's turn
Show the answer

B. The assistant's tokens, including its end-of-turn token

The model reads the whole sequence as context but is trained only to produce the assistant's response, and the end-of-turn token in that response is how it learns to stop. Masking the rest keeps the model from spending capacity on predicting users' and system prompts.

Question 2

In REINFORCE you subtract from each sampled response's reward a baseline b(x)b(x): the average reward of responses to that prompt, estimated independently of the sample. What happens to the gradient estimate?

  1. Its expectation is unchanged and its variance usually falls
  2. It becomes biased toward average responses
  3. It no longer needs sampled responses
  4. Its expectation is scaled by 1−b(x)1-b(x)
Show the answer

A. Its expectation is unchanged and its variance usually falls

E[b(x)∇log⁡πθ(y∣x)]=b(x)∇∑yπθ(y∣x)=0\E\big[b(x)\nabla\log\pi_{\boldsymbol\theta}(y\mid x)\big]=b(x)\nabla\sum_y\pi_{\boldsymbol\theta}(y\mid x)=0, so the mean is untouched. Centering the rewards stops the estimator from pushing up every sample when all rewards are positive, which reduces its variance.

Question 3

Why does PPO clip the probability ratio ρ=πθ/πold\rho=\pi_{\boldsymbol\theta}/\pi_{\text{old}} in its objective?

  1. To keep the probabilities between 0 and 1
  2. To bound the size of the reward
  3. To remove the incentive to move the policy far from the one that generated the samples while a batch is reused for several updates
  4. To make a KL penalty to the reference model unnecessary
Show the answer

C. To remove the incentive to move the policy far from the one that generated the samples while a batch is reused for several updates

Reusing samples requires importance ratios, which are trustworthy only near ρ=1\rho=1. Beyond 1±ϵ1\pm\epsilon in the direction the advantage favors, the clipped objective is flat, so there is no gradient pushing further; harmful moves are still counted, because the minimum keeps the worse of the two terms.

Question 4

Why does RLHF subtract β DKL(πθ ∥ πref)\beta\,\KL(\pi_{\boldsymbol\theta}\,\|\,\pi_{\text{ref}}) from the reward it maximizes?

  1. To make training faster
  2. Because PPO cannot work without it
  3. To make the policy deterministic
  4. Because the reward model is reliable only near the responses it was trained on, and without a leash the policy drifts to outputs it overrates
Show the answer

D. Because the reward model is reliable only near the responses it was trained on, and without a leash the policy drifts to outputs it overrates

A learned reward is a proxy. Optimized hard, a policy finds responses the reward model scores highly but people do not: reward hacking. Staying close to the SFT model keeps the policy where the reward model was trained, and preserves fluency and diversity.

Question 5

A reward model trained with the Bradley–Terry loss scores several responses to one prompt. You add the same constant cc to all of their scores. How does the loss change?

  1. It grows in proportion to cc
  2. It does not change: the loss depends only on differences of rewards for the same prompt
  3. It changes sign
  4. It is minimized at c=0c=0
Show the answer

B. It does not change: the loss depends only on differences of rewards for the same prompt

σ((rw+c)−(rl+c))=σ(rw−rl)\sigma\big((r_w+c)-(r_l+c)\big)=\sigma(r_w-r_l). Comparisons identify a reward only up to a constant per prompt, which is why InstructGPT shifted its reward model so that the labelers' demonstrations averaged 0 before RL.

Go deeper

Further reading

  • L. Ouyang et al., "Training language models to follow instructions with human feedback," NeurIPS (2022).
  • R. J. Williams, "Simple statistical gradient-following algorithms for connectionist reinforcement learning," Machine Learning 8, 229–256 (1992).
  • J. Schulman, F. Wolski, P. Dhariwal, A. Radford & O. Klimov, "Proximal policy optimization algorithms," arXiv:1707.06347 (2017).
  • P. F. Christiano, J. Leike, T. B. Brown, M. Martic, S. Legg & D. Amodei, "Deep reinforcement learning from human preferences," NeurIPS (2017).
  • R. Rafailov, A. Sharma, E. Mitchell, S. Ermon, C. D. Manning & C. Finn, "Direct preference optimization: Your language model is secretly a reward model," NeurIPS (2023).