Skip to content

5.10 RL for reasoning

ready≈ 14 hoursNeeds: 5.9
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Policy-gradient bandit, RL loop animation)
  4. 4 Do
  5. 5 Check

Policy gradients. SFT copies demonstrations and DPO learns from comparisons. Reinforcement learning needs only a score for what the model itself produced. A policy πθ samples a response, a reward scores it, and the gradient of the expected reward is E[r ∇ log π(y)]: raise the log-probability of each sample in proportion to its reward (Williams 1992, REINFORCE). Every sample gives an unbiased but noisy gradient estimate.

Baselines. Subtracting any constant b from r leaves that expectation unchanged, because the score ∇ log π has mean zero. With b near the average reward, the estimate’s variance drops sharply: below-average samples are pushed down instead of up a little (Spinning Up).

Verifiable rewards. For maths and code the reward can be a checker rather than a learned model: parse the final answer and compare it with the ground truth. Nothing to fool, and no reward model in memory. DeepSeek-R1-Zero learned long chains of reasoning from rewards of this kind alone (DeepSeek-AI 2025, section 2.2).

GRPO. PPO estimates the baseline with a learned value network and clips the probability ratio πθ/πold to 1 ± ε, so that several gradient steps can reuse one batch of samples (Schulman et al. 2017). GRPO drops the value network: it samples a group of responses per prompt and uses the group’s mean reward as the baseline, divided by the group’s standard deviation (Shao et al. 2024, section 4.1). A group whose replies all score the same contributes nothing, so prompts must be neither too easy nor too hard. Dividing by the standard deviation and averaging per sequence both introduce biases that Dr. GRPO removes (Liu et al. 2025). CS336 Assignment 5 implements these losses (assignment5-alignment).

The policy gradient and why a baseline is freestep 1 of 7
  1. J(θ)=Ey∼πθ[r(y)]=∑yπθ(y) r(y)J(\theta) = \mathbb{E}_{y \sim \pi_\theta}[r(y)] = \sum_y \pi_\theta(y)\, r(y)

Policy-gradient bandit

REINFORCE basics

Static view on small screens. Open this page on a laptop to interact.

25%arm A25%arm B25%arm C25%arm D
π = softmax(θ), θ = 0: every arm starts at 25%. θ = [0.00, 0.00, 0.00, 0.00] after 0 pulls

REINFORCE raises the log-probability of each arm it pulls in proportion to the reward. If every reward is positive, every pull is reinforced, and only the differences between arms steer the policy, so learning is slow and noisy. Subtracting the average reward does not change the expected update, but it makes below-average pulls push their arm down. Raise the offset, then compare 40 learners: without the baseline, many of them commit to the wrong arm.

A four-armed bandit whose arms pay noisy rewards with hidden means. Bars show the policy’s probability for each arm; buttons pull once or 50 times, or reset. Checkboxes turn the running-mean baseline on or off and reveal the true means; sliders set the learning rate and a constant added to every reward. The readout explains the last update; a line shows the policy’s expected reward over time. A further button runs 40 learners for 300 pulls with and without the baseline and plots where each ends. Keyboard: Tab to the buttons, checkboxes and sliders.

★ RL loop animation

How models learn from verifiable rewards (GRPO)

Static view on small screens. Open this page on a laptop to interact.

group size G divide by
  1. Sample G responses
  2. Verify each answer
  3. Group-normalise rewards
  4. Clipped policy-gradient update

Prompt 1 of 3: What is 17 + 25? (ground truth 42)

responserewardπsampled
<think>17 + 25 = 42</think> <answer>42</answer>correct ✓ 8%
<think>17 + 20 = 37, 37 + 5 = 42</think> <answer>42</answer>correct ✓ 4%
<think>17 + 25 = 32</think> <answer>32</answer>wrong ✗ 25%
The answer is 42.format ✗ 30%
<answer>43</answer>wrong ✗ 23%
<answer>42</answer> <answer>42</answer>format ✗ 10%
iteration 0; expected accuracy over the three prompts: 12%

This is reinforcement learning with a verifiable reward: no reward model, just a checker that parses the <answer> tag and compares it with the ground truth. GRPO samples a group of responses, uses the group's mean reward as the baseline (dividing by its spread if you choose), and raises the probability of the above-average responses. The clip removes the incentive to push a response's probability ratio past 1 ± ε: beyond that, its gradient is zero. It is a soft limit, not a hard one: the other responses' updates still move it, so with a high learning rate or several updates per batch the ratios can end well outside the band. A response the model never samples can never be rewarded: the third prompt's correct answers start at 5%.

GRPO on three arithmetic prompts, each with six candidate responses (some correct, some wrong, some badly formatted) and a tabular policy that starts from an SFT model’s probabilities. Each iteration cycles through four stages: sample a group of G responses, verify each against the ground truth, compute group-normalised advantages, and apply clipped policy-gradient updates. A table shows each response’s reward, probability, how often it was sampled and its advantage. Controls set G (2, 4 or 8), whether advantages are divided by the group’s standard deviation, the clip ε, the learning rate and updates per batch; buttons step, play, run 60 iterations or reset. A line shows expected accuracy over iterations. Keyboard: Tab to the buttons and sliders; the step button advances one stage.
RL for reasoning stubs/m5_10_rl.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m5_10_rl.py and run mlc check 5.10. The tests are identical; import the results on the progress page.

Hints · reinforce_bandit 0/3 revealed
Hints · group_normalized_advantages 0/3 revealed
Hints · policy_gradient_loss 0/3 revealed

Then run GRPO on your model. Open notebooks/5.10-grpo.ipynb on Kaggle with a GPU and paste in your 4.6, 5.8 and 5.10 files. It pretrains your GPT, fine-tunes it to answer in <answer> tags, checks a PyTorch GRPO loss against your functions, then trains with groups of sampled replies scored by your math_reward and compares greedy accuracy before and after.

1. Subtracting a baseline b from the reward in REINFORCE…
2. In GRPO, a group in which every sampled reply is correct…
3. What does the clip in the GRPO/PPO loss prevent?
4. Why can RL for maths skip the reward model that RLHF needs?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.