Skip to content

Reinforcement learning in one sitting: policy, reward, baseline, advantage

The four ideas you need before the training lessons, with one runnable 25-line example: a policy that learns to pick the rewarded action, why subtracting a baseline helps, and what a group-relative advantage is.

20 minยทVerified 2026-111 animated diagram+50 XP
Share

Reasoning training uses reinforcement learning (RL). You do not need prior RL knowledge. Four ideas, then one tiny program.

#The four ideas

Term Meaning In a language model
Policy A probability distribution over actions The model's probabilities over the next token (or whole answers)
Reward A number saying how good an outcome was 1 if the final answer is correct, else 0
Baseline What you would normally expect, subtracted from the reward The average reward of a group of attempts
Advantage Reward minus baseline: better or worse than usual? Positive for attempts better than average, negative for worse

The training idea: make actions that led to above-average rewards more likely, and actions that led to below-average rewards less likely. That is all of policy gradients.

#A tiny example you can run

A policy with three actions, where action 2 is the only one that earns a reward. We sample a group of 8 attempts, compute each one's advantage against the group average, and nudge the probabilities:

python
import random, math
random.seed(0)

logits = [0.0, 0.0, 0.0]            # the policy's parameters: all actions equally likely

def probs(l):
    m = max(l); e = [math.exp(x - m) for x in l]; t = sum(e)
    return [v / t for v in e]

lr = 0.5
for step in range(200):
    p = probs(logits)
    acts = random.choices(range(3), weights=p, k=8)        # a group of 8 attempts
    rewards = [1.0 if a == 2 else 0.0 for a in acts]       # reward only for action 2
    base = sum(rewards) / len(rewards)                     # baseline = group average
    for a, r in zip(acts, rewards):
        adv = r - base                                     # advantage
        for j in range(3):                                 # gradient of log-probability of action a
            grad = (1.0 if j == a else 0.0) - p[j]
            logits[j] += lr * adv * grad / len(acts)

print([round(x, 3) for x in probs(logits)])

Output: [0.003, 0.003, 0.994]. The policy started at one third each and learned to pick action 2 about 99% of the time.

Policy gradients: reinforce what beat the group average
Animated
Action 0
33%
Action 1
33%
Action 2 (rewarded)
33%

Group of 8 attempts (green = rewarded, advantage > 0)

-0.25-0.25+0.75+0.75-0.25-0.25-0.25-0.25

Group average (baseline): 0.25 ยท each attempt shows its advantage = reward โˆ’ baseline

Round 1

The policy starts uniform: each action has probability one third. A group of 8 attempts is sampled.

The same 25-line program from the lesson (seeded so this animation always replays the same run). The probability bars are the policy; each square is one attempt.

#Reading the loop

  1. Sample a group from the current policy.
  2. Score each attempt with the reward.
  3. Subtract the group average to get advantages.
  4. Update: for each attempt, raise the probability of its action in proportion to its advantage (and lower it if the advantage is negative). The expression (1 if j == a else 0) - p[j] is the gradient of the log-probability of action a with respect to the logits.

This is the shape of every RL-for-LLMs method. In a real model the "action" is a whole generated answer, the log-probability is the sum over its tokens, and the update flows into millions of weights through backpropagation instead of three numbers.

#Why the baseline matters

Suppose almost every attempt earns reward 1. Without a baseline, each of them pushes its actions up with the same strength, and the signal is mostly noise. With the group average subtracted, an attempt is only reinforced if it is better than its siblings. That reduces the variance of the updates, so training is steadier.

#Where it can go wrong

  • All attempts equal. All right or all wrong gives zero advantage everywhere, so nothing is learned. Problems that are too easy or too hard waste compute.
  • Reward hacking. The model finds a way to earn reward without solving the task (an answer format that fools the checker). The verifier has to be honest.
  • Collapse. The policy becomes too narrow, too fast, and stops exploring. Real methods limit how far the policy may move per step (clipping and a KL penalty).
  • Noisy rewards. A flawed verifier teaches the wrong behaviour confidently.

The training lessons return to each of these.

#Exercise

  1. In a group of 8 attempts, 2 are correct. What is the baseline, and what is the advantage of a correct attempt and of an incorrect one?
  2. What happens to the update if 8 of 8 are correct?
  3. In the program above, change lr to 5.0 and run it a few times. What do you see, and why?

Answers: 1. The baseline is 2/8 = 0.25; a correct attempt has advantage 0.75 and an incorrect one โˆ’0.25. 2. The baseline is 1, every advantage is 0, and the policy does not change. 3. Large steps make the policy jump to one action quickly and may lock onto the wrong one early, or oscillate; a smaller learning rate is steadier, which is why training recipes also limit the update size.

Next: setting up and running the experiments.

Quick quiz

+10 XP per correct answer, +25 XP for a perfect score

  1. 1. In this course, what is a policy?

  2. 2. What does a baseline do in policy-gradient training?

  3. 3. What is the advantage of an attempt in a group of 8?

  4. 4. If all 8 attempts in a group get the same reward, what is every advantage?

  5. 5. Why does reinforcement learning need a checkable reward?

Found this useful?

Send it to someone who is learning the same thing.

Share this lesson