Seyed Masoud Hosseini · Overview · Study log · Weekly summaries · Ideas · Search · Transcript · RSS feed

Deep Reinforcement Learning · Lecture 89 of 99 · 23:39

Lecture 21: RL with Sequence Models & Language Models, Part 2

CS 285: Lecture 21, RL with Sequence Models & Language Models, Part 2 on YouTube

Study guide

What this lecture covers

Having covered RL under partial observability, the lecture turns to the opposite direction: using RL to train better language models. It sets up the single-step formulation used by systems like ChatGPT, where a prompt is the state, a full completion is the action, and treats getting the reward function right as the central design problem, since ground-truth rewards for open-ended text rarely exist.

After watching, you can explain how a language generation task is cast as a one-step RL (bandit) problem, why importance-weighted policy gradient estimators are preferred over plain REINFORCE for language models, how a reward model is trained from human preference comparisons rather than raw scores, and how KL penalties guard against reward over-optimization.

Key ideas

  • Language models as next-token predictors: a transformer models p(x_t | x_1...x_{t-1}) and generates text by repeatedly sampling the next token, making the process inherently non-Markovian in the token history.
  • RL objective for language models: supervised training matches the training data's distribution, while RL training optimizes a language model to maximize a reward function, useful for satisfying preferences, using tools, or holding goal-directed dialogue.
  • Single-step formulation: the prompt is the state, the full completion (a variable-length sequence of tokens) is the action, and generating an entire response counts as one RL time step even though it spans many language modeling time steps, making this a bandit problem.
  • Importance-weighted policy gradients: because sampling from and evaluating a language model is expensive, most methods generate a batch of completions once, then take several importance-weighted gradient steps against that fixed batch before resampling, rather than resampling for every gradient step as plain REINFORCE would.
  • Reward models from preferences: rather than asking humans to assign raw numerical scores (hard for subjective quality), reward models are trained on pairwise preference labels, since people can more reliably say which of two completions they prefer.
  • Bradley-Terry-style preference model: the probability that completion A1 is preferred over A2 is modeled as the exponential of A1's reward divided by the sum of both exponentials, the same mathematical form used in maximum entropy inverse RL, and the reward model is trained by maximizing the likelihood of observed human preferences.
  • KL penalty against over-optimization: because repeatedly optimizing against a fixed, imperfect reward model risks the policy exploiting it (distributional shift, sometimes called over-optimization), a penalty subtracting the KL divergence from the original supervised policy is added to the reward.

Walkthrough

Language models as sequence predictors, and why use RL (0:00)

The lecture recaps how transformer language models generate text token by token, then explains that supervised training only teaches a model to match its training distribution. RL is introduced as the tool for going beyond that: satisfying human preferences, learning to use tools such as databases or calculators, and holding effective dialogues, all framed as needing a reward function rather than direct imitation of training text.

Casting language generation as a one-step MDP (4:06)

Using "What is the capital of France?" as a running example, the prompt becomes the state, and the completion (for example, the tokens for "Paris" and an end-of-sequence marker) becomes the action. The policy's probability of an action is the product of per-token probabilities from the transformer. A key clarification is that there are two distinct notions of time step: the many language-generation steps inside a completion, and the single RL time step that corresponds to producing an entire completion, making this a bandit problem for now.

Policy gradient estimators for language models (8:09)

The policy gradient for this objective reduces to the same gradients computed during standard cross-entropy training, summed across the completion's tokens. A plain REINFORCE estimator needs fresh samples from the current policy for every gradient step, which is costly since sampling and scoring completions is slow. Instead, most methods sample a batch of completions, fix an old policy pi_bar to describe how they were generated, and then run several importance-weighted gradient steps (in mini-batches) on that fixed batch before generating new samples, a PPO-style loop.

Building a reward model from human preferences (12:11)

Because a language model can produce open-ended answers with no fixed ground truth, the reward function itself must be learned, typically as a neural network. Asking humans to assign a raw numeric score to each completion is unreliable for subjective quality, so the lecture proposes collecting pairwise preferences instead ("I prefer this completion") and modeling the preference probability as the exponential of one reward divided by the sum of both completions' exponentiated rewards, the same functional form used in maximum entropy inverse RL. The reward model is then trained by maximizing the likelihood of the observed human preference labels, and this generalizes to k-way comparisons by decomposing them into pairwise terms.

The full RLHF-style loop and over-optimization (17:13)

The lecture assembles the pieces into the method behind InstructGPT and ChatGPT: start from a supervised fine-tuned policy, sample several completions per prompt, collect human preference labels, train the reward model on those labels, then run RL (with nested loops of sampling and importance-weighted updates) against the reward model, repeating as needed. Because human labeling is slow and expensive, most preference data comes from an early round rather than a continuously updated loop, which effectively makes this a model-based, sometimes offline model-based, RL method with the reward model playing the role of the learned model. The main risk is that the policy overfits and exploits weaknesses in the fixed reward model. The lecture's fix is a KL-divergence penalty, implemented by subtracting the current policy's log probability and adding the supervised policy's log probability to the reward, which keeps the optimized policy close to the original supervised model.

Before you watch

  • Review the policy gradient and importance sampling material from earlier in the course, since this lecture reuses those estimators directly.
  • Recall the maximum entropy inverse RL formulation from the IRL lecture, since the preference model here uses the same mathematical form.

Check your understanding

  1. Why is generating a full language model response treated as a single RL time step even though it involves many token-by-token predictions?
  2. Why do most RL methods for language models use importance-weighted updates from a fixed batch of samples rather than resampling for every gradient step?
  3. Why are pairwise human preferences generally easier to collect reliably than raw numerical reward scores?
  4. What problem does the KL-divergence penalty address, and how is it incorporated into the reward?

Vocabulary

next-token predictor (noun)
A model that generates text by repeatedly guessing the next word or symbol.
A language model is a next-token predictor.
completion (noun)
The text a language model generates in response to a prompt.
The full completion is treated as one RL action.
bandit problem (noun)
A decision problem with a single action step and an immediate reward, no ongoing sequence.
Generating one response is framed as a bandit problem.
importance-weighted (adjective)
Adjusted using weights that correct for samples coming from a different distribution.
Importance-weighted policy gradients reuse a fixed batch of samples.
REINFORCE (noun)
A basic policy gradient algorithm that updates a policy using sampled rewards.
Plain REINFORCE needs fresh samples for every gradient step.
pairwise (adjective)
Involving comparisons made between two items at a time.
Pairwise preferences are easier for humans to give reliably.
cross-entropy (noun)
A common loss function that measures the difference between predicted and true probability distributions.
The gradient reduces to the same cross-entropy training gradients.
over-optimization (noun)
Pushing too hard against an imperfect measure until it breaks down.
Repeated optimization against the reward model risks over-optimization.
reward hacking (phrase)
Finding a way to score well on a reward measure without really achieving the intended goal.
A KL penalty helps prevent reward hacking.
non-Markovian (adjective)
Depending on more than just the current state, such as on the past history.
Token generation is inherently non-Markovian in the token history.
transformer (noun)
A neural network architecture widely used for processing sequences like text.
A transformer models the probability of the next token.
subjective (adjective)
Based on personal opinion rather than an objective, measurable fact.
Raw numeric scores are unreliable for subjective quality.
decompose (verb)
To break something into simpler separate parts.
K-way comparisons are decomposed into pairwise terms.
k-way (adjective)
Involving k different options being compared at once.
The preference model generalizes to k-way comparisons.
exploit (verb)
To take advantage of a weakness for unintended gain.
The policy can learn to exploit weaknesses in the reward model.
nested (adjective)
Contained inside another structure of the same kind.
The RLHF loop has nested loops of sampling and updates.
offline (adjective)
Using previously collected data rather than fresh live interaction.
Using an early round of preference data makes this an offline method.
fine-tuned (adjective)
Further trained from an existing model to improve it on a task.
RL starts from a supervised fine-tuned policy.
guard against (phrasal verb)
To take precautions to prevent something bad from happening.
A KL penalty guards against reward over-optimization.
mini-batch (noun)
A small subset of data used for one training update.
Importance-weighted updates run in mini-batches on a fixed sample.
resample (verb)
To draw new samples again from a distribution.
The method avoids having to resample for every gradient step.
cast (verb)
To frame or present a problem in a particular form.
Language generation is cast as a one-step RL problem.
variable-length (adjective)
Not having a fixed size; able to be different lengths.
The completion is a variable-length sequence of tokens.
exponentiated (adjective)
Raised as a power of e, following exponential growth.
The preference model uses the exponentiated reward of each completion.
open-ended (adjective)
Not having a single fixed correct answer.
Language models can produce open-ended answers with no fixed ground truth.

← Lecture 21: RL with Sequence Models & Language Models, Part 1 · Lecture 21: RL with Sequence Models & Language Models, Part 3 →