PPO: clipped updates and a learned value baseline
Write PPO's surrogate using the probability ratio π_θ/π_old and clip it so one batch cannot push the policy too far. Explain how a learned value model, which predicts the expected reward from each prefix, supplies per-token advantages (reward minus that baseline). Explain why clipping allows several gradient steps per batch of sampled responses.
Builds on: Treating the LLM as an RL policy
What the animation says
One batch, one step?
Generating responses is the slow part of training a language model with reinforcement learning. Yet REINFORCE samples a batch, takes one gradient step, and throws the batch away. Why not reuse it? After one step, the policy is no longer the one that made those samples. And a second problem: one reward at the end credits every token equally. So if we keep stepping on the same batch with no limit, what happens to a token the update likes?
A leash per token
It runs away. PPO puts a leash on every sampled token. The leash is a ratio: probability now over probability when sampled. Epsilon point two allows point eight to one point two. Old probability point three, advantage plus two. At point three three, ratio one point one: the gradient still pushes up. At point three nine, ratio one point three: the term is capped at two point four. Flat, so zero gradient. Now it falls to point two one instead, ratio point seven. What does the gradient do?
The min keeps the pessimist
It pulls back up. The term is the smaller of two: ratio times advantage, and clipped ratio times advantage. Point seven times two is one point four; point eight times two is one point six. The min keeps one point four, the unclipped term, so gradient flows. Clipping removes the reason to go further, never the reason to undo a mistake. With a negative advantage the picture mirrors: the update wants the token less likely. Advantage minus two: tap the zone where this token stops getting gradient.
A baseline for every prefix
The leash fixed reuse. Now credit: one reward at the end, reward one. A value model, often a language model copy with a one-number head, predicts the final reward from each prefix. Here it predicts point four, point four, point eight. Advantage is reward minus prediction: point six, point six, point two. The value model learns by shrinking the squared gap between its prediction and the real reward. PPO averages the clipped terms over the response's tokens. On the first step, that gives exactly the baselined policy gradient.
Which token is frozen?
Now the same story in numpy: three tokens, after a few steps on one batch. These lines build advantages from the value predictions, and the ratio from old and new log-probabilities. Ratios: one point three, point nine, and about one point zero two. This line takes the min of the plain and clipped terms; the next marks which tokens still get gradient. Tap the token that no longer gets gradient.
A leash per token and a per-prefix baseline
The clipped surrogate and the advantage
Watching the clip switch off (ε = 0.2)
Advantages and clipped surrogate in numpy
import numpy as np
eps, r = 0.2, 1.0 # end-of-response reward
V = np.array([0.4, 0.4, 0.8]) # value model on prefixes s_1..s_3
adv = r - V # per-token advantages
logp_old = np.log([0.30, 0.50, 0.90]) # at sampling time
logp_new = np.log([0.39, 0.45, 0.92]) # after a few steps
rho = np.exp(logp_new - logp_old)
clipped = np.clip(rho, 1 - eps, 1 + eps) * adv
surr = np.minimum(rho * adv, clipped)
active = rho * adv <= clipped # unclipped term chosen -> gradient flows
print(adv, rho.round(2), surr.round(3), active)
# [0.6 0.6 0.2] [1.3 0.9 1.02] [0.72 0.54 0.204] [False True True]
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses