Index Ai Academy
Phase 6 · Modern LLM engineering · Post-training

RLHF

Reward models and reinforcement learning from human feedback.

About 40 minutes · Free account required

Open this lesson Create a free account

Why it matters

ChatGPT and GPT-3 started from the same kind of pretrained model. What made ChatGPT feel so different was a training step called reinforcement learning from human feedback, or RLHF. It trains the model to write the answers people prefer. Its parts now show up all over post-training: a learned score, a model pushed to raise that score, and a fine for drifting too far from where it started. Its main weakness is a model that learns to game a flawed score. That problem is one of the central puzzles in AI safety.

What you'll be able to do

  • Train a reward model on preference pairs with the Bradley-Terry loss
  • Use a reward model to pick the best of several sampled answers
  • Explain the KL-regularized objective that RLHF optimizes, and what β controls
  • Recognize reward hacking and why RLHF needs a leash on the policy

Inside this lesson

  • 13 short sections
  • 4 code examples you can edit and run
  • a practice exercise that checks your code
  • a 5-question quiz
  • 6 review cards for spaced repetition

What it covers

  1. The recipe
  2. Step 2: a reward model
  3. Worked example: the loss for four pairs
  4. Training a reward model on real preferences
  5. Step 3: reinforcement learning, on a leash
  6. Worked example: two answers, four leashes
  7. Watching reward hacking happen
  8. How the RL step actually runs

A taste of the code

import math

def bt_loss(r_chosen, r_rejected):
    gap = r_chosen - r_rejected
    chance = 1 / (1 + math.exp(-gap))     # sigmoid: the chance the chosen answer wins
    return -math.log(chance)

for chosen, rejected in [(3.0, 1.0), (1.0, 1.0), (1.0, 3.0), (13.0, 11.0)]:
    print(chosen, rejected, round(bt_loss(chosen, rejected), 3))
In the lesson you can change this and run it in your browser.

Try a quiz question

Why does RLHF penalize the KL divergence from the SFT model?

  1. The reward model is imperfect; without a leash the policy drifts toward answers that exploit its mistakes
  2. KL makes training faster
  3. To keep the policy's vocabulary unchanged
  4. Because the reward model only works on short answers

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson