RLHF: Reward Model + PPO

SFT teaches the model to follow instructions. But it doesn't teach the model which response is BETTER. Two grammatically correct, factually accurate answers can differ enormously in helpfulness. RLHF is how you encode human judgment into the model's behavior. It's what makes Claude helpful and GPT polite.

Build 1.5 hr Python (with numpy)

Prerequisites: Phase 10, Lesson 06 (Instruction Tuning / SFT)

#Learning Objectives

  • Build a reward model that scores response quality from human preference pairs (chosen vs rejected)
  • Implement the PPO training loop that optimizes a language model policy against the reward model with a KL penalty
  • Explain why RLHF requires three models (SFT, reward, policy) and how the KL constraint prevents reward hacking
  • Evaluate the effect of RLHF by comparing response quality before and after preference optimization
Full Access lesson

Keep going. The rest of this lesson is one step away.

Unlock all 503 lessons, video, code, quizzes and every future update. Pay in your own currency with Paystack.

The text and code of this lesson are based on AI Engineering from Scratch by Rohit Ghumare, MIT licensed.