๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #282

RLHF

RLHF (Reinforcement Learning from Human Feedback) is the technique most responsible for turning capable pretrained-and-fine-tuned LLMs into genuinely well-behaved assistants โ€” using human preference comparisons to further train the model beyond what SFT's direct imitation alone can achieve.

The Three-Stage Process

  1. Start from an SFT model โ€” the already fine-tuned model from Supervised Fine-Tuning serves as the starting point.
  2. Train a reward model: humans are shown multiple model-generated responses to the same prompt and asked to rank them by preference. A separate neural network (the reward model) is trained to predict these human preference rankings.
  3. Fine-tune the LLM via reinforcement learning: using the reward model as an automated feedback signal (standing in for actual human raters at scale), the LLM's own generation policy is further trained to produce outputs the reward model scores highly, typically via the PPO algorithm.

Reward Model Training โ€” The Preference Loss

\[ L_{\text{reward}} = -\log\sigma\big(r(x, y_{\text{chosen}}) - r(x, y_{\text{rejected}})\big) \]

\(r(x,y)\) is the reward model's scalar score for response \(y\) given prompt \(x\). This is the Bradley-Terry preference model โ€” the loss pushes the reward model to score the human-preferred response (\(y_{\text{chosen}}\)) higher than the rejected one, using exactly the sigmoid/logistic structure from Sigmoid Function.

The RL Fine-Tuning Objective โ€” With a Crucial Safeguard

\[ \text{maximize } \mathbb{E}\big[r(x,y)\big] - \beta \cdot D_{KL}(\pi_{\text{RL}} \parallel \pi_{\text{SFT}}) \]

The model is trained to maximize reward โ€” but with a KL divergence penalty (see KL Divergence) discouraging it from drifting too far from the original SFT model's behavior. Without this penalty, the model could learn to exploit weaknesses in the reward model (a failure mode called "reward hacking") โ€” producing outputs that score artificially highly according to the reward model without actually being genuinely better responses.

Diagram

SFT Model Reward Model (from human prefs) RL Fine-Tuned Model

RLHF's three stages, each building on the previous one's output.

Common Mistakes

  • Forgetting the KL penalty's role โ€” without it, RL fine-tuning can drift into "reward hacking," producing outputs that game the reward model's specific weaknesses rather than genuinely improving response quality.
  • Assuming RLHF trains directly on raw human ratings โ€” it trains a separate reward model first, which then stands in for scalable, automated feedback during the RL stage, since collecting fresh human feedback for every single RL training step would be far too slow and expensive.

Interview Relevance

Q: "Why does RLHF fine-tuning include a KL divergence penalty against the original SFT model?" Without it, the RL optimization process can learn to exploit specific weaknesses or blind spots in the reward model โ€” producing outputs that score artificially high according to the reward model without actually being better responses, a failure mode called reward hacking. The KL penalty constrains the model to stay reasonably close to its original SFT behavior, limiting how far this exploitation can drift.

Practice Question

Why is a separate reward model trained, rather than directly using live human ratings as the reinforcement learning signal at every training step?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

RLHF โ€“ FAQs

Quick answers about learning RLHF in Deep Learning.

This free note from CodingNow 2.0 explains RLHF in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including RLHF, is 100% free with no signup required.
With focused practice, most students grasp RLHF in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now