AI-330 · Lesson 12 of 12 · 30 min

Reinforcement learning for language models, safety and a project

Lesson 9 introduced reinforcement learning from human feedback, the method that turned pre-trained language models into helpful assistants. Since then, reinforcement learning has become a central part of how large language models are trained: to follow instructions, to refuse harmful requests, and, increasingly, to reason through mathematics and code problems using rewards that can be checked automatically. This final lesson examines the RL objective used for language models and its closed-form solution, direct preference optimization, reward model over-optimization, group-relative policy optimization and RL with verifiable rewards, and the safety questions these raise. It ends with a project applying the course's methods to a real control problem.

The language model as a policy

For RL purposes, a language model is a policy \(\pi_\theta(y \mid x)\) that, given a prompt \(x\), generates a response \(y\) one token at a time. Each token is an action, the prompt and tokens so far form the state, and the episode ends with the response. Rewards usually arrive only at the end, from a reward model, a human rating, or an automatic check such as running unit tests or comparing a final answer with a known solution. The action space (a vocabulary of 50,000 to 200,000 tokens) is huge, but the pre-trained model already places most of its probability on sensible continuations, so RL refines an excellent starting policy rather than learning from scratch.

The KL-regularized objective

RLHF maximizes reward while penalizing divergence from a reference policy \(\pi_{\text{ref}}\), usually the supervised fine-tuned model:

\[\max_\pi \; \mathbb{E}_{x,\, y \sim \pi}\big[r(x, y)\big] - \beta\,\mathrm{KL}\big(\pi(\cdot \mid x)\,\|\,\pi_{\text{ref}}(\cdot \mid x)\big)\]

The KL term keeps the policy close to the fluent, broadly capable reference, and limits how far it can move to exploit errors in the reward model. This objective has a closed-form optimum, found by the same reasoning as in maximum entropy RL:

\[\pi^*(y \mid x) = \frac{1}{Z(x)}\,\pi_{\text{ref}}(y \mid x)\exp\!\left(\frac{r(x, y)}{\beta}\right)\]

The optimal policy reweights the reference by the exponential of the reward. Small \(\beta\) concentrates probability on the highest-reward responses; large \(\beta\) stays near the reference. In practice the optimum cannot be computed directly, because \(Z(x)\) sums over all possible responses, so it is approached with policy gradient methods such as PPO (Lesson 9), with the KL penalty included in the reward.

Reward model over-optimization

A learned reward model is only an approximation of human preferences, trained on limited comparisons. Optimizing it hard finds the responses it overrates: overly long answers, confident-sounding but wrong claims, flattery, or exploitation of quirks in its training data. Measured against true human judgement, quality first rises with optimization and then falls, a direct instance of Goodhart's law and reward hacking (Lesson 4). Studies of this effect found that the gap between proxy and true reward grows predictably with the KL divergence from the reference.

The closed-form KL-regularized optimum for decreasing \(\beta\), when 1% of responses exploit a flaw in the reward model.
import numpy as np

rng = np.random.default_rng(13)
n = 2000                                              # possible responses to one prompt
ref = rng.dirichlet(np.ones(n) * 0.5)                 # reference (pre-trained) policy probabilities
true_r = rng.normal(0, 1, n)                          # what humans actually prefer
flawed = rng.random(n) < 0.01                       # 1% of responses exploit a flaw in the reward model:
true_r[flawed] = rng.normal(-1.0, 0.5, flawed.sum())  # people dislike them,
proxy = true_r.copy(); proxy[flawed] += 4.0           # but the reward model scores them highly

for beta in (10.0, 1.0, 0.3, 0.1, 0.03):
    logits = np.log(ref) + proxy / beta               # optimum of E[r] - beta * KL(pi || ref): pi ~ ref * exp(r / beta)
    pi = np.exp(logits - logits.max()); pi /= pi.sum()
    kl = np.sum(pi * np.log(pi / ref))
    print(f"beta {beta:5.2f}: KL from reference {kl:5.2f} nats; proxy reward {pi @ proxy:+.2f}; true reward {pi @ true_r:+.2f}")
print(f"reference policy: proxy {ref @ proxy:+.2f}, true {ref @ true_r:+.2f}")
Output
beta 10.00: KL from reference  0.01 nats; proxy reward +0.14; true reward +0.07
beta  1.00: KL from reference  0.68 nats; proxy reward +1.33; true reward +0.77
beta  0.30: KL from reference  4.11 nats; proxy reward +3.20; true reward +0.03
beta  0.10: KL from reference  9.06 nats; proxy reward +3.99; true reward -0.00
beta  0.03: KL from reference 11.17 nats; proxy reward +4.17; true reward +0.17
reference policy: proxy +0.02, true -0.03
Plot of reward against KL divergence from the reference policy, from 0 to 11 nats. The proxy reward, measured by the reward model, rises steadily and levels off near 4. The true reward, measured by human preference, rises to a peak of about 0.85 near 1 nat of KL and then falls back to around zero as optimization concentrates on flawed responses.
Reward over-optimization: the proxy reward keeps rising while the true reward peaks early and then falls.

Moderate optimization (\(\beta = 1\)) raises true quality substantially. Pushing further multiplies the proxy reward but sends the true reward back to zero, as the policy concentrates on the flawed responses that the reward model overrates. Mitigations include choosing \(\beta\) and stopping points using held-out human evaluation, ensembles of reward models (optimizing a conservative combination), improving reward models with adversarially collected data, constraining length and style, and auditing samples during training.

Direct preference optimization

Rearranging the closed-form optimum expresses the reward in terms of the optimal policy: \(r(x, y) = \beta\log\frac{\pi^*(y \mid x)}{\pi_{\text{ref}}(y \mid x)} + \beta\log Z(x)\). Substituting this into the Bradley-Terry preference model used to train reward models (Lesson 9), the intractable \(Z(x)\) cancels, because preferences depend only on reward differences between two responses to the same prompt. The result is direct preference optimization (DPO), which trains the policy directly on preference pairs \((y_w, y_l)\) (preferred and dispreferred) with a simple classification loss:

\[\mathcal{L}_{\text{DPO}} = -\log\sigma\!\left(\beta\log\frac{\pi_\theta(y_w \mid x)}{\pi_{\text{ref}}(y_w \mid x)} - \beta\log\frac{\pi_\theta(y_l \mid x)}{\pi_{\text{ref}}(y_l \mid x)}\right)\]

DPO needs no separate reward model and no sampling during training, which makes it simpler and cheaper than PPO-based RLHF. It is an offline method (Lesson 10): it learns only from the fixed preference data set, so it cannot discover and correct the policy's own new failure modes the way online RL can, and it can over-fit to the data. Online and iterative variants, which generate new responses, collect fresh preferences and repeat, combine its simplicity with some of the benefits of on-policy learning.

Worked problem

A DPO loss

For a preference pair, the policy's log-probability ratios relative to the reference are +0.6 for the preferred response and −0.4 for the dispreferred one, and \(\beta = 0.1\). Compute the DPO loss. How does it compare with the loss at initialization, when the policy equals the reference?

Show solution
  1. Margin: \(0.1 \times (0.6 - (-0.4)) = 0.1\).
  2. Loss: \(-\log\sigma(0.1) = -\log(0.525) = 0.644\).
  3. At initialization both ratios are 0, so the loss is \(-\log\sigma(0) = \log 2 = 0.693\).

Answer: The loss falls from 0.693 to 0.644 as the policy raises the preferred response's probability relative to the reference and lowers the dispreferred one. Training continues to widen the margin, with \(\beta\) controlling how strongly deviation from the reference is rewarded.

RL with verifiable rewards and GRPO

For tasks with checkable answers, such as mathematics problems with known solutions, code with unit tests, or formal proofs, the reward can be computed exactly: 1 if the final answer is correct, 0 otherwise. RL with verifiable rewards avoids the reward model and much of its exploitability (though checkers have their own loopholes, such as tests that can be gamed). Trained this way, models learn to produce long chains of reasoning, check intermediate results, and backtrack from mistakes, behaviours that emerged without being explicitly demonstrated.

Group relative policy optimization (GRPO), introduced with the DeepSeekMath models, is a popular algorithm for this setting. For each prompt it samples a group of \(G\) responses, scores them, and uses each response's reward relative to its group as the advantage:

\[A_i = \frac{r_i - \mathrm{mean}(r_1, \ldots, r_G)}{\mathrm{std}(r_1, \ldots, r_G)}\]

The group mean replaces the learned value function of PPO as the baseline (Lesson 9), saving the memory and computation of a separate critic network the size of the policy, and the update otherwise follows PPO's clipped objective with a KL penalty. Prompts where every response is right or every response is wrong give zero advantage and no learning signal, so training data are often filtered toward problems of intermediate difficulty for the current model.

A toy GRPO run: a policy choosing among four solution strategies with different success rates learns from group-relative advantages of verified rewards.
import numpy as np

rng = np.random.default_rng(14)
# A toy "reasoning" policy chooses one of 4 strategies for each problem; each strategy solves a problem with
# a different probability, and the reward is 1 if the final answer is verified correct.
p_correct = np.array([0.2, 0.35, 0.5, 0.8])
theta = np.array([1.5, 1.0, 0.5, 0.0])                # initial preferences favour the weakest strategy
G, lr = 8, 0.5                                        # group size and learning rate
for step in range(201):
    pi = np.exp(theta - theta.max()); pi /= pi.sum()
    if step % 50 == 0:
        print(f"step {step:3d}: strategy probabilities {np.round(pi, 2)}; expected accuracy {pi @ p_correct:.2f}")
    grad = np.zeros(4)
    for _ in range(16):                               # 16 prompts per step
        acts = rng.choice(4, G, p=pi)
        rewards = (rng.random(G) < p_correct[acts]).astype(float)
        if rewards.std() == 0: continue               # all right or all wrong: no learning signal
        adv = (rewards - rewards.mean()) / rewards.std()   # group-relative advantage, no value network
        for a, A in zip(acts, adv):
            g = -pi.copy(); g[a] += 1                 # gradient of log pi(a) for a softmax policy
            grad += A * g / G
    theta += lr * grad / 16
Output
step   0: strategy probabilities [0.46 0.28 0.17 0.1 ]; expected accuracy 0.35
step  50: strategy probabilities [0.02 0.02 0.04 0.92]; expected accuracy 0.77
step 100: strategy probabilities [0.01 0.01 0.01 0.97]; expected accuracy 0.79
step 150: strategy probabilities [0.   0.01 0.01 0.98]; expected accuracy 0.79
step 200: strategy probabilities [0.   0.   0.01 0.99]; expected accuracy 0.79

Starting from a policy that prefers its weakest strategy, the group-relative updates shift probability to the strategy that succeeds most often, and expected accuracy rises from 0.35 to near the best achievable 0.80 within about 50 steps. No value network is needed: comparing responses within each group provides the baseline.

Process reward models offer a complement to outcome rewards: instead of scoring only the final answer, they score each reasoning step, giving denser feedback and helping identify where reasoning goes wrong, at the cost of needing step-level labels or automated step verification.

Safety and responsible use of RL

Reinforcement learning optimizes objectives with persistence and creativity, which is both its power and its risk. Issues to manage in any deployment include:

  • Specification: the reward may not capture what is wanted (reward hacking, Lesson 4); test what the trained policy actually does, not just its reward.
  • Distribution shift: policies can behave unpredictably in situations unlike training; keep fallback controllers and monitor inputs.
  • Safe exploration: in physical systems, constrain actions during learning (constrained MDPs, shields) and train in simulation first.
  • Goal misgeneralization: a policy can learn a goal that coincides with the intended one in training but differs elsewhere, such as following a visual cue that happened to mark the goal.
  • Oversight: for powerful systems, human review, interpretability tools and evaluations for dangerous capabilities complement reward-based training. Methods that use AI feedback guided by written principles, such as constitutional AI, scale oversight beyond what human labelling alone allows.

Capstone project: RL for data centre cooling control

The project applies the course to an industrial problem. A data centre wants an RL controller to adjust chilled-water set points, fan speeds and the number of running chillers and cooling towers every five minutes, minimizing energy while keeping server inlet temperatures within limits (DCE-330). Published work on data centre cooling has reported energy savings for cooling on the order of tens of percent with learned controllers operating under safety constraints and human supervision.

Step 1: Formulate the problem

State: outdoor temperature and humidity, IT load, current set points and equipment status, temperatures at key sensors, and recent trends (to make the state closer to Markov, Lesson 4). Actions: set point changes within small allowed steps, and equipment staging. Reward: negative energy use. Constraints: inlet temperatures below limits and minimum run times for equipment, handled as a constrained MDP with a Lagrangian rather than a fixed penalty. Discount: with decisions every 5 minutes and thermal effects over about an hour, a horizon of about 12 to 24 steps suggests \(\gamma \approx 0.95\).

Step 2: Start from data

Years of operating logs from the building management system are available. Use them to train a dynamics model (an ensemble, Lesson 10) predicting temperatures and power from state and action, and evaluate it on held-out periods, checking its accuracy over multi-step rollouts.

Step 3: Learn offline, conservatively

Train a policy offline with a conservative method (CQL or a model-based method with uncertainty penalties), so it does not rely on actions the logs never contain. Evaluate candidate policies with fitted Q evaluation and with the model, and compare with the existing rule-based controller.

Step 4: Deploy safely

Deploy in recommendation mode first, with operators approving each action; then in closed loop with a safety layer that verifies every proposed action against the model and hard limits, reverting to the existing controller if predictions and measurements diverge or any limit is approached. Use model predictive control (Lesson 10) as a planning layer, with the learned value function at the horizon.

Step 5: Improve online

With safety established, allow limited exploration within constraints, update the model and policy periodically, and monitor energy savings, constraint violations and operator overrides. Re-check for distribution shift when IT load patterns change, such as when AI training clusters are added.

Worked problem

Is the saving real?

Over a month, the RL controller's cooling energy is 1,450 MWh, compared with 1,700 MWh in the same month last year under the old controller. Last year's month had an average outdoor temperature 2 °C higher and an IT load 5% lower. How should the saving be assessed?

Show solution
  1. Raw saving: \((1{,}700 - 1{,}450)/1{,}700 = 14.7\%\), but conditions differed: cooler weather favours this year, higher IT load favours last year.
  2. A fair comparison normalizes for weather and load, using a regression model of cooling energy on outdoor conditions and IT load fitted to old-controller data, or alternates controllers on randomized days.

Answer: The raw 14.7% cannot be credited to the controller. A randomized alternation of controllers, or a weather- and load-normalized baseline, gives a defensible estimate, an application of the evaluation principles from Lessons 2 and 8.

The project draws on bandits and evaluation, MDP formulation and constraints, dynamic programming and TD learning, planning, deep and distributional value learning, policy optimization, model-based and offline RL, and safe deployment, which together form the practical toolkit of reinforcement learning.

Common mistakes

  • MistakeOptimizing a learned reward model as hard as possible.

    InsteadTune the KL penalty and stopping point with held-out human evaluation.

  • MistakeTreating DPO as equivalent to online RL.

    InsteadDPO learns offline from fixed preferences and cannot correct new failure modes without fresh data.

  • MistakeTraining GRPO on problems the model always or never solves.

    InsteadSelect prompts of intermediate difficulty, where groups have varied rewards.

  • MistakeDeploying an RL controller directly in closed loop.

    InsteadUse offline training, recommendation mode, safety layers and fallbacks.

  • MistakeComparing energy use with last year without normalization.

    InsteadNormalize for weather and load, or randomize controller assignment.

Summary and key terms

  • Language models are policies over tokens; RL refines a strong pre-trained reference.
  • The KL-regularized objective has the closed-form optimum \(\pi^* \propto \pi_{\text{ref}}e^{r/\beta}\).
  • Over-optimizing a learned reward model raises proxy reward while true quality falls.
  • DPO trains directly on preferences by substituting the closed-form reward into the Bradley-Terry loss.
  • RL with verifiable rewards and GRPO, which uses group-relative advantages without a critic, trains reasoning.
  • Safe deployment of RL combines specification testing, constraints, monitoring and staged rollout, as in the cooling control project.
Reference policy
The model from which the RL policy is regularized not to stray.
KL penalty
A term penalizing divergence from the reference policy.
Reward over-optimization
Exploiting a learned reward model's errors, lowering true quality.
Bradley-Terry model
A model in which preference probability is the sigmoid of the reward difference.
DPO
Direct preference optimization, training a policy on preference pairs without a reward model.
Verifiable reward
A reward computed by checking correctness automatically.
GRPO
Group relative policy optimization, using within-group normalized rewards as advantages.
Process reward model
A model scoring each reasoning step rather than only the outcome.
Goal misgeneralization
Learning a goal that matches the intended one in training but not elsewhere.
Safety layer
A component that checks and can override a learned controller's actions.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. What does a small \(\beta\) do in the KL-regularized objective?

    Show answer

    Concentrates probability on high-reward responses, allowing larger divergence.

  2. Write the optimal policy of the KL-regularized objective.

    Show answer

    \(\pi^* \propto \pi_{\text{ref}}\exp(r/\beta)\).

  3. Why does the partition function cancel in DPO?

    Show answer

    Preferences depend only on reward differences for the same prompt.

  4. DPO loss with margin 0?

    Show answer

    \(\log 2 = 0.693\).

  5. Group rewards (1, 0, 1, 0): advantages?

    Show answer

    (+1, −1, +1, −1).

  6. Why do all-correct groups give no GRPO signal?

    Show answer

    All advantages are zero.

  7. What replaces the critic in GRPO?

    Show answer

    The group mean reward as baseline.

  8. Name two mitigations for reward over-optimization.

    Show answer

    Tuned KL penalty, early stopping, reward model ensembles, adversarial data.

  9. Discount for a 5-minute step and a 1-hour horizon?

    Show answer

    About 0.92 (12 steps).

  10. Why deploy an RL controller in recommendation mode first?

    Show answer

    Operators can catch unsafe or poor actions before closed-loop control.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz