Skip to content
E
Educatum

Reinforcement Learning with Human Feedback (RLHF)

By Educatum AI teamLast updated: Original walkthrough ↗
Key Intuition

Reinforcement Learning with Human Feedback (RLHF) aligns a language model with human preferences by training a Bradley-Terry reward model on pairwise human rankings and then optimizing the LLM policy to maximize predicted reward. Because discrete token sampling is non-differentiable, practical RLHF uses policy-gradient reinforcement learning such as PPO with a KL-divergence penalty to prevent drifting too far from the base SFT model.

Reference Diagram & Materials
Reinforcement Learning with Human Feedback (RLHF) matrix calculation diagram

The Logical Steps of the RLHF Pipeline

(In practice, RLHF starts from a Supervised Fine-Tuned (SFT) base LLM and proceeds through preference collection, reward modeling, and RL policy optimization.)


Step 1: Human Preference Evaluation

  1. Given an input prompt, the LLM generates multiple candidate completions (using stochastic decoding such as temperature/top-pp sampling or beam search).
    • Example completions for "doc is": (doc is, him) and (doc is, them).
  2. A human annotator compares the candidate completions and selects a winner (ywy_w) over a loser (yly_l) based on alignment criteria (e.g., helpfulness, neutrality, safety).
    • Here, (doc is, them) is chosen as the winner (ywy_w) because it is less biased.

Step 2: Train the Reward Model (RM)

The Reward Model takes (prompt, completion) embeddings, processes them through transformer/linear layers with pooling, and outputs a scalar reward score rϕ(x,y)r_\phi(x, y).

  1. Score the loser (yly_l): e.g., rl=3r_l = 3.
  2. Score the winner (ywy_w): e.g., rw=5r_w = 5.
  3. Compute the reward gap: Δr=rw−rl=5−3=2\Delta r = r_w - r_l = 5 - 3 = 2
  4. Compute Bradley-Terry pairwise preference loss and its gradient:
    • Map the reward gap through the sigmoid function to estimate the probability that ywy_w is preferred over yly_l: p=σ(Δr)=11+e−2≈0.88p = \sigma(\Delta r) = \frac{1}{1 + e^{-2}} \approx 0.88
    • The pairwise binary cross-entropy (Bradley-Terry) loss with target 11 is: LRM=−log⁡σ(rw−rl)=−log⁡(0.88)≈0.13\mathcal{L}_{\text{RM}} = -\log \sigma(r_w - r_l) = -\log(0.88) \approx 0.13
    • Its gradient with respect to the reward gap Δr\Delta r is: ∂LRM∂Δr=σ(Δr)−1≈0.88−1=−0.12\frac{\partial \mathcal{L}_{\text{RM}}}{\partial \Delta r} = \sigma(\Delta r) - 1 \approx 0.88 - 1 = -0.12
    • (Note: σ(Δr)−1\sigma(\Delta r) - 1 is the loss gradient, not the loss itself, which is always non-negative.) Backpropagating this negative gradient increases rwr_w and decreases rlr_l.

Step 3: Score New LLM Rollouts with the Reward Model

  1. Provide a prompt (e.g., "CEO is") to the LLM policy πθ\pi_\theta.
  2. Generate a completion: In the simplified hand-calculation diagram, the model chooses the highest-probability next token via greedy decoding (argmax) to get "CEO is him". (In real RLHF rollouts, stochastic sampling is used so the policy explores diverse responses.)
  3. Score the completion with the trained Reward Model from Step 2:
    • Suppose the RM outputs R=rϕ(x,y)=3R = r_\phi(x, y) = 3.

Step 4: Update the LLM Policy

  1. Conceptual objective in the hand-calculation diagram:
    • Maximizing reward RR is written conceptually as minimizing L=−R\mathcal{L} = -R (so L=−3\mathcal{L} = -3 and ∂L∂R=−1\frac{\partial \mathcal{L}}{\partial R} = -1).
  2. How real RLHF propagates gradients across discrete tokens:
    • Because selecting discrete tokens (argmax or sampling) is non-differentiable, gradients cannot flow backward directly from RR through the generated text tokens as in standard supervised backprop.
    • Instead, RLHF uses policy-gradient reinforcement learning (most commonly PPO or GRPO), weighting token log-probabilities log⁡πθ(yt∣x,y<t)\log \pi_\theta(y_t \mid x, y_{<t}) by an advantage estimate while adding a KL-divergence penalty against the reference SFT model πref\pi_{\text{ref}}: max⁡θ Ey∼πθ(⋅∣x) ⁣[rϕ(x,y)−β DKL ⁣(πθ(⋅∣x) ∥ πref(⋅∣x))]\max_{\theta}\ \mathbb{E}_{y \sim \pi_\theta(\cdot|x)}\!\left[r_\phi(x, y) - \beta \, D_{\text{KL}}\!\left(\pi_\theta(\cdot|x) \,\|\, \pi_{\text{ref}}(\cdot|x)\right)\right]
    • The KL penalty prevents reward hacking—ensuring the model stays fluent and faithful to language modeling rather than exploiting quirks in the learned Reward Model. (Alternative offline methods like DPO bypass explicit RL and reward models altogether.)

Iterative Alignment Cycle

  • Repeating preference collection, RM updates, and policy optimization iteratively improves alignment and helps the LLM generalize preferred behaviors to unseen prompts.

RLHF step-by-step diagram

More in GenAI by Hand

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.