Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Practising to please the judge, without losing yourself
Once a reward model can score answers, the language model can practise: write answers, get scores, and shift towards writing higher-scoring ones. This is reinforcement learning from human feedback. Left alone, the model might find weird answers that the judge likes but people would not, so a leash (the KL penalty) keeps it close to its original, sensible behaviour. PPO is the training method that takes careful, limited steps. The demo shows how the leash strength changes the model’s choices.
RLHF-style tuning lets an assistant learn organisation-specific preferences (concise, safety-first answers) beyond what demonstrations alone teach, but its reward model must be audited, because the policy will exploit any flaw.
- 1. RLHF optimises the model to maximise…
- 2. The KL penalty keeps the policy close to…
- 3. PPO takes ___ policy steps:
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Objective: maximise E_(y∼π)[r(x, y)] − β · KL(π(·|x) ‖ π_ref(·|x)). Its optimum is the tilted distribution π*(y|x) ∝ π_ref(y|x) · exp(r(x, y)/β): small β chases reward, large β stays near the reference.
- Per-sample reward: r(x, y) − β (log π(y|x) − log π_ref(y|x)).
- PPO: for sampled actions, ratio ρ = π_θ/π_old; maximise E[min(ρA, clip(ρ, 1 − ε, 1 + ε)A)] with ε ≈ 0.2 over a few epochs per batch. Advantages A come from a value model with generalised advantage estimation (GAE).
- Models in memory: policy, frozen reference, reward model and value model: four large models.
- KL control: fixed β or an adaptive controller targeting a KL budget (for example 6 nats). Moving a 50/50 choice to 90/10 already costs 0.37 nats.
- Practicalities: normalise rewards, penalise or truncate overly long outputs, monitor KL and reward together, and sample-check outputs for reward hacking (F7-6).
- 1. The RLHF optimum is…
- 2. PPO's clip range ε is typically…
- 3. Models held in memory by PPO RLHF:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.PPO RLHF in code
Exercise 1.
- Compute the KL-penalised reward of a sample.
- Compute the PPO clipped surrogate (gains capped at 1 + ε).
- Compute the closed-form KL-regularised optimum.
Practice.
- TRL (
PPOTrainer), OpenRLHF and veRL implement PPO for LLMs with reference and value models, generation and KL control. - Start from a good SFT model; use a held-out prompt set and track reward, KL, response length and sample quality.
- Tune β (or the KL target) first; too low leads to reward hacking, too high to no change.
- Consider DPO-style methods (F7-3) or GRPO (F7-5) to avoid the value model and online sampling cost.
from trl import PPOConfig, PPOTrainer
cfg = PPOConfig(learning_rate=1e-6, kl_coef=0.05, cliprange=0.2, num_ppo_epochs=4, batch_size=64)
trainer = PPOTrainer(args=cfg, processing_class=tok, model=policy, ref_model=ref,
reward_model=rm, value_model=value, train_dataset=prompts)
trainer.train()- 1. A library implementing PPO RLHF:
- 2. Tune first:
- 3. Alternatives that avoid a value model:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
The tilted optimum. Maximising E[r] − βKL(π ‖ π_ref) over distributions gives π* ∝ π_ref exp(r/β) (a Gibbs distribution). As β → 0 the policy concentrates on the reward model’s favourite response; as β → ∞ it stays at π_ref. The trade-off between reward and KL is a smooth curve, traced in exercise 2.
Why clip. Policy-gradient steps computed on samples from π_old are only valid near π_old. PPO’s clipped surrogate removes the incentive to move the probability ratio outside [1 − ε, 1 + ε] in one update, preventing destructive large steps. This approximates a trust region at low cost.
Proxy vs truth. The policy is optimised for the reward model, not for the true preference. Wherever the reward model errs, the policy moves towards its errors, so the reward model’s score of the final policy overstates its true quality. This gap grows with optimisation pressure (lower β), which is why the KL leash matters.
- 1. As β → 0, the optimal policy…
- 2. PPO's clipping approximates…
- 3. The reward model's score of the trained policy is usually…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How does the KL penalty trade reward against staying close to the reference model?
History.
- J. Schulman et al., “Proximal policy optimization algorithms” (2017).
- D. Ziegler et al., “Fine-tuning language models from human preferences” (2019).
- N. Stiennon et al., “Learning to summarize from human feedback” (NeurIPS 2020).
- L. Ouyang et al., “InstructGPT” (NeurIPS 2022).
Exercise 2.
- Simulated environment: 200 prompts, each with 8 candidate responses described by 16 features. The hidden true reward is linear plus an interaction. The reference (SFT) policy has random logits. The reward model is a linear Bradley–Terry model fitted to 300 noisy comparisons.
- Policy: the reference logits plus a learned linear adjustment, trained with PPO (clip 0.2, 4 epochs per batch of 64, 150 iterations) for β = 0, 0.05, 0.3 and 1.
- Metrics: expected true reward, expected reward-model (proxy) reward and KL to the reference, averaged over prompts.
| policy | true reward | proxy reward | KL to reference (nats) |
|---|---|---|---|
| reference | −0.046 | −0.055 | 0 |
| PPO, β = 0 | 1.135 | 1.257 | 1.635 |
| PPO, β = 0.05 | 1.128 | 1.247 | 1.588 |
| PPO, β = 0.3 | 1.055 | 1.160 | 1.283 |
| PPO, β = 1 | 0.541 | 0.589 | 0.305 |
| best possible (oracle) | 1.453 | — | — |
- RLHF works: even with a 300-comparison reward model, true reward rises far above the reference.
- β sets the trade-off: a stronger penalty keeps the policy much closer to the reference (KL 0.31 against 1.64) at the cost of reward.
- The proxy always flatters the policy: the reward model rates every trained policy above its true reward, and the gap grows as the KL grows.
- No over-optimisation yet: here true reward kept rising as β fell, because the policy class (a linear tilt) is too weak to exploit the reward model’s errors strongly. F7-6 shows what happens with more pressure.
- Seed 1: true reward 0.732 (β = 0) to 0.276 (β = 1), with KL 1.48 to 0.16.
RLHF in AI, robotics and aerospace
- Assistant alignment: encoding preferences for concise, safety-first and properly hedged answers.
- Robotics: PPO is the workhorse of simulated robot learning; RLHF adds human preference rewards for hard-to-specify behaviour.
- Design lesson: optimise against a learned judge only with a leash, and keep checking the true quality.
References
- J. Schulman, F. Wolski, P. Dhariwal, A. Radford and O. Klimov (2017). “Proximal policy optimization algorithms.” arXiv:1707.06347.
- N. Stiennon et al. (2020). “Learning to summarize from human feedback.” NeurIPS.
- L. Ouyang et al. (2022). “Training language models to follow instructions with human feedback.” NeurIPS.
- S. Huang et al. (2024). “The N+ implementation details of RLHF with PPO.” ICLR Blogposts.