All courses

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

▶ Watch this lesson Start the whole course

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.

Why throw away a batch after one step?

REINFORCE samples responses, takes **one** gradient step, then discards them, even though generating responses is the slow part. Reusing the batch for many steps is tempting. But after step one the policy is no longer the one that produced the samples, and repeated steps keep pushing the same tokens until their probabilities run away. Also, one end-of-response reward credits every token equally. PPO fixes both problems.

A leash per token and a per-prefix baseline

**Leash:** for each sampled token, compare its probability now with its probability when it was sampled: the ratio $\pi_\theta/\pi_{old}$. Once that ratio leaves $[1-\epsilon, 1+\epsilon]$ (often $\epsilon=0.2$) in the direction the update wants, the token stops giving gradient. Each step reuses the same batch, but no token keeps being rewarded for drifting further from $\pi_{old}$. That makes several steps on one batch safe. **Baseline:** a learned value model $V_\phi$ predicts the expected final reward from each prefix. Advantage = reward − that prediction.

The clipped surrogate and the advantage

PPO maximizes a *surrogate* objective: a stand-in whose gradient equals the policy gradient at the sampling policy. $$J(\theta)=\tfrac{1}{|y|}\sum_{t}\min\big(\rho_t\hat A_t,\ \mathrm{clip}(\rho_t,1{-}\epsilon,1{+}\epsilon)\,\hat A_t\big)$$ Here $|y|$ is the number of response tokens, $s_t=(x,y_{<t})$ is the prefix, $\rho_t=\pi_\theta(y_t\mid s_t)/\pi_{old}(y_t\mid s_t)$, and $\hat A_t=r(x,y)-V_\phi(s_t)$. At $\theta=\theta_{old}$ (the parameters of $\pi_{old}$), $\nabla\rho_t=\nabla\log\pi_\theta(y_t\mid s_t)$, so the first step is exactly the baselined policy gradient. $V_\phi$ is often a separate copy of an LM with a scalar output head. It is trained to minimize $(V_\phi(s_t)-r)^2$.

Watching the clip switch off (ε = 0.2)

Take $\pi_{old}=0.30$ and $\hat A=+2$. - $\pi_\theta=0.33$: $\rho=1.1$, $\min(2.2,2.2)=2.2$. Gradient still pushes up. - $\pi_\theta=0.39$: $\rho=1.3$, $\min(2.6,\,1.2\cdot2)=2.4$. This value is constant in θ, so the gradient is zero. - $\pi_\theta=0.21$: $\rho=0.7$, $\min(1.4,\,0.8\cdot2)=1.4$. Unclipped, so the gradient pulls it back up. The min keeps the pessimistic value. Clipping removes the incentive to go further, never the incentive to undo a mistake.

Advantages and clipped surrogate in numpy

One reward ($r=1$) is spread over 3 tokens using value predictions. Several steps later, we check which tokens still get gradient. The first token's probability has grown by a factor of 1.3, beyond $1+\epsilon$, so it is frozen. The others still learn. A PPO update repeats this over the same batch for a few passes.
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

PPO maximizes $\min(\rho\hat A,\ \mathrm{clip}(\rho)\hat A)$. A token stops pulling once its ratio $\pi_\theta/\pi_{old}$ has passed $1\pm\epsilon$ in the favored direction, so it has no incentive to drift further. That is why one batch of sampled responses can safely drive several gradient steps. Per-token advantages $\hat A_t=r-V_\phi(s_t)$ come from a learned value model that predicts the expected reward from each prefix.

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.