Reinforcement Learning for Deep Learning
Beyond Supervised Learning
Supervised learning requires a “right answer” for every training example. But what if the right answer is subjective (“Is this response helpful?”), non-differentiable (“Does this code compile?”), or only available after a long sequence of actions (“Did we win the chess game?”)? This is where reinforcement learning enters the deep learning toolkit. RL provides a framework for:- Learning from human preferences (RLHF) — this is how ChatGPT, Claude, and other AI assistants are aligned with human values
- Optimizing non-differentiable objectives — BLEU score, compilation success, user engagement
- Aligning AI systems with human values — teaching models to be helpful, harmless, and honest
- Training agents that interact with environments — robotics, game playing, autonomous systems
RL Fundamentals for Deep Learning
The RL Framework
- State : Current situation
- Action : What the agent does
- Reward : Feedback signal
- Policy : Probability of taking action in state
- Value : Expected cumulative reward from state
Policy Gradient
The fundamental theorem for learning policies:REINFORCE Algorithm
REINFORCE is the simplest policy gradient method, and understanding it is essential before tackling PPO or RLHF. The core idea: sample actions from the policy, observe the reward, and increase the probability of actions that led to high reward while decreasing those that led to low reward. Think of it like training a dog — you cannot tell it what to do in advance, but you can reward good behavior after the fact and the dog gradually learns to repeat those behaviors.Proximal Policy Optimization (PPO)
PPO is the workhorse of modern RL, used in RLHF and many robotics applications. Its genius is in the name: “proximal” means it prevents the policy from changing too much in a single update. Without this constraint, RL training is notoriously unstable — one bad update can catastrophically degrade the policy. PPO clips the update ratio so the new policy never strays too far from the old one, providing a “trust region” for safe optimization.The PPO Objective
whereReinforcement Learning from Human Feedback (RLHF)
RLHF is how models like ChatGPT are aligned with human preferences. The three-stage pipeline below might seem complex, but each stage addresses a fundamental limitation: SFT gives the model basic instruction-following ability, the reward model captures nuanced human preferences that are hard to express as rules, and PPO fine-tuning optimizes the model against those preferences while preventing it from drifting too far from its original capabilities.The RLHF Pipeline
Reward Model
RLHF Training Loop
Direct Preference Optimization (DPO)
DPO is one of the most elegant simplifications in recent ML research. The RLHF pipeline requires training three separate models (SFT model, reward model, policy model) and running PPO, which is notoriously fiddly to tune. DPO showed that you can derive a closed-form loss that directly optimizes the same objective as RLHF, but using only supervised learning on preference pairs. No reward model. No PPO. No RL instability. The trade-off: DPO is offline (it learns from a fixed dataset of preferences) while PPO is online (it generates new responses and gets fresh rewards). For simple, single-turn tasks, DPO often matches or beats PPO. For complex, multi-turn scenarios or when you need to iterate on the reward, PPO’s online nature gives it an edge.DPO Objective
Other RL Objectives
REINFORCE with Baseline for Text
Reward-Weighted Regression
Best Practices
Exercises
Exercise 1: Implement GRPO
Exercise 1: Implement GRPO
Implement Group Relative Policy Optimization:
Exercise 2: Reward Model Ensemble
Exercise 2: Reward Model Ensemble
Train an ensemble of reward models for more robust preferences:
Exercise 3: Implement IPO
Exercise 3: Implement IPO
Implement Identity Preference Optimization:
What’s Next?
Neural Architecture Search
Automatically discover optimal architectures
Interpretability
Understand what your models learn
Interview Deep-Dive
Compare PPO-based RLHF with DPO. What are the trade-offs for a production alignment pipeline?
Compare PPO-based RLHF with DPO. What are the trade-offs for a production alignment pipeline?
Strong Answer:PPO-based RLHF is a three-stage pipeline: SFT, reward model training, then PPO optimization against the reward model with a KL penalty. DPO collapses the last two stages by directly optimizing the policy on preference pairs, treating the LM itself as an implicit reward model.PPO advantages: it can optimize any reward signal — pairwise preferences, rule-based rewards (toxicity filters), length penalties, or tool execution feedback. It supports online data collection during training for continuous improvement.DPO advantages: dramatically simpler (no reward model, no value function, no PPO hyperparameter tuning), more stable (no reward hacking), and roughly 2x cheaper compute.DPO limitations: only trains on offline preference data and assumes preference data came from a similar policy.For production: start with DPO for simplicity. Switch to PPO only if you need online reward signals or external tool feedback.Follow-up: How do you detect reward hacking during RLHF training?Monitor three signals: (1) reward score should plateau, not climb indefinitely; (2) periodic human evaluation — if reward rises but human ratings stall, that is reward hacking; (3) output length and repetition — hacking often produces verbose, formulaic responses. Fixes include increasing KL penalty, retraining the reward model with adversarial examples, or switching to DPO.
Explain the KL divergence penalty in RLHF. Why is it necessary?
Explain the KL divergence penalty in RLHF. Why is it necessary?
Strong Answer:The KL penalty constrains how far the RLHF policy drifts from the SFT reference. The objective: maximize E[R(x, y)] - beta * KL(pi_RL || pi_SFT).Without it, the policy discovers shortcuts that score high with the imperfect reward model — extreme verbosity, sycophantic agreement, repetition — while losing coherent generation ability. The reward model is an imperfect proxy, and an unconstrained optimizer exploits every imperfection.Beta too low: rapid divergence, degenerate high-reward outputs, mode collapse. Beta too high: barely any change from SFT, wasting the entire RLHF compute budget. Optimal beta is found by sweeping values and measuring human evaluation.A key subtlety: KL is computed per-token, so the model can concentrate changes on specific tokens (like adding safety refusals) while keeping most output close to reference. This is actually desirable for alignment.Follow-up: How does reference policy choice affect the result?Using the raw pretrained model gives more freedom but risks losing SFT improvements. Using the SFT model (standard) preserves quality but limits deviation. Some teams use a mid-SFT checkpoint as reference for a balance between the two. The reference effectively defines the “center of gravity” that the KL penalty pulls toward.
A PM asks why you cannot just fine-tune on good examples instead of doing RLHF. How do you explain the value?
A PM asks why you cannot just fine-tune on good examples instead of doing RLHF. How do you explain the value?
Strong Answer:Sometimes SFT is enough. But RLHF provides three specific capabilities SFT cannot.First, comparative judgments are far easier and cheaper to collect than demonstrations. “Which response is better?” is simpler than “write the perfect response.” You get 10x more data at the same cost with higher quality.Second, SFT suffers from mode averaging — two valid but different training responses to the same prompt get averaged into a bland compromise. RLHF learns to commit to one coherent style because the reward model can distinguish coherence from wishy-washy averaging.Third, SFT optimizes token-by-token and cannot directly optimize holistic response properties like helpfulness, consistency, or self-contradiction. RLHF assigns a single reward to the full response, enabling sequence-level optimization.For practical applications like customer service bots or structured extraction, SFT alone is sufficient and dramatically simpler. RLHF shines when you need nuanced alignment that demonstrations cannot capture.Follow-up: How does Constitutional AI reduce the annotation burden?Constitutional AI replaces human preference labeling with AI-generated feedback. Define principles, have the model critique its own response pairs against those principles, and train the reward model on AI-generated preferences. This leverages the insight that large models are better judges than generators, dramatically reducing annotator costs while producing well-aligned models.