LLMs
RLHF (Reinforcement Learning from Human Feedback)
Distinguish demonstrations, preference comparisons, reward models, and policy updates. Compare SFT, PPO, and DPO visuals.
Jump to a section
Why this matters
Reinforcement learning from human feedback uses human preferences to shape model behavior. A common pipeline first trains on demonstrations, then fits a reward model to comparisons, and finally optimizes a policy against that reward with a constraint on how far it moves.
Keep the roles separate: SFT imitates demonstrations, a reward model scores outputs, and RL changes the policy using that signal. DPO offers a different preference-learning objective; it is not an extra stage of the same pipeline.
Build the mental model
You can't directly backprop "be more helpful." So you build a proxy: show humans pairs of model outputs for the same prompt, ask which is better, and fit a reward model to those preferences. Then treat the LLM as a policy and use RL to produce outputs the reward model scores highly, but tether it to the original SFT model with a KL penalty so it doesn't drift into degenerate, reward-hacking text.
First, learn from demonstrations
Choose one update to see probability move toward the demonstrated outcome. Open the visualization in Studio to change the target.
The visual loads as you reach this section.
The KL term is the safety leash: high reward with low KL means "better, but still recognizably the same model."
Work through the math
Reward model. Given human preference data where response is preferred over for prompt , fit a scalar reward with the Bradley-Terry loss:
i.e. push the preferred response's reward above the rejected one's.
Policy optimization. Optimize the policy to maximize reward while staying near the reference (SFT) policy :
PPO implements this with a clipped surrogate objective that limits how far the policy moves per update (stability). controls the leash: too small and the model reward-hacks; too large and it barely changes. DPO (Direct Preference Optimization) is the popular alternative: it derives a closed-form loss directly on the preference data that implicitly optimizes the same KL-regularized objective, skipping the separate reward model and RL loop entirely.
Key takeaway: Humans can't write perfect answers, but they can reliably rank pairs, so RLHF learns a scalar reward from comparisons (Bradley-Terry) and optimizes the policy against it, with a KL penalty as the leash that prevents reward hacking.
| PPO-based RLHF | DPO | |
|---|---|---|
| Reward model | Trained explicitly, used online | Implicit in the loss |
| Optimization loop | Online RL, sampled rollouts | Supervised-style, closed form |
| Complexity | 4 models in memory (policy, ref, RM, value) | 2 models (policy, reference) |
| Stability | Sensitive hyperparameters | Simpler to train, no rollouts |
| Flexibility | Can optimize non-differentiable rewards | Limited to preference datasets |
Read the implementation
The reward-model loss and the KL-shaped reward: the two pieces unique to RLHF:
1import torch
2import torch.nn.functional as F
3from torch import Tensor
4
5
6def reward_model_loss(r_chosen: Tensor, r_rejected: Tensor) -> Tensor:
7 # Bradley-Terry: preferred reward should exceed rejected reward
8 return -F.logsigmoid(r_chosen - r_rejected).mean()
9
10
11def kl_shaped_reward(
12 reward: Tensor, logp_policy: Tensor, logp_ref: Tensor, beta: float = 0.1
13) -> Tensor:
14 # per-token reward used by PPO: task reward minus KL drift from the SFT model
15 kl = logp_policy - logp_ref # sample estimate of KL
16 return reward - beta * kl
17
18
19r_chosen, r_rejected = torch.tensor([2.1]), torch.tensor([0.4])
20assert reward_model_loss(r_chosen, r_rejected) < reward_model_loss(r_rejected, r_chosen)Questions and trade-offs
- Conceptual: What are the three stages of RLHF? (SFT on demonstrations → reward model from human preference comparisons → RL (PPO) optimizing the policy against the reward model.)
- Implementation: Why train a reward model from comparisons instead of absolute scores? (Humans are far more consistent ranking A vs B than assigning calibrated numeric scores; Bradley-Terry turns comparisons into a trainable reward.)
- Applied: What is the KL penalty for? (It keeps the optimized policy close to the SFT model, preventing drift into degenerate, reward-hacking outputs.)
- Systems-level: What is reward hacking and how does the setup mitigate it? (The policy exploits flaws in the imperfect reward model for high score / low quality; the KL leash and capping training steps limit it.)
- Failure modes: How does DPO differ from PPO-based RLHF? (DPO optimizes a closed-form preference loss directly on the data (no separate reward model or RL loop) implicitly solving the same KL-regularized objective.)
Check your understanding
From memory: name the three RLHF stages, write the Bradley-Terry reward loss, write the KL-penalized objective and say what β controls. Then state one way DPO simplifies the pipeline. Check against Stages 1–3.
Oliver Perrin
Machine Learning Engineer · Founder, LiminalML
LiminalML brings together concept explanations, mathematical examples, and working visuals. Find more writing by Oliver on the LiminalML Substack.
Keep exploring the idea.
Ask a follow-up in a guided session, work through the self-check, or open the visual in Studio to build on it.
Related concepts
LLMs
LoRA and PEFT
Follow a frozen weight path and a trainable low-rank correction. Understand parameter savings, scaling, and merging.
Deep Learning
Transformer Architecture
Follow attention, feed-forward layers, normalization, and residual connections through a Transformer block.
Optimization
Gradient Descent (SGD, Momentum, Adam)
Follow updates across a loss surface, then connect learning rate, momentum, and Adam to the update equations.