Actor–critic and PPO
Be able to explain the advantage, the clipping and why PPO is used in RLHF.
Prerequisites
- FPolicy gradient and REINFORCErequired
Intuition
Actor–critic combines the two families:
- The actor — the policy , the one that acts.
- The critic — the value function , the one that judges how good the situation is.
The critic is used as a baseline, so that the actor is told not just «that went well» but «that went better than expected». That is the advantage:
The difference is large in practice. «You got 100 points» says little if the situation always gives 100. «You got 20 more than expected» is a usable signal.
PPO's contribution is to solve a different problem: how large a step is allowed? Too large a policy step can destroy a working policy, and unlike supervised learning it does not come back — the next round of data is collected by the broken policy.
PPO's answer is to clip away the incentive to move too far. No hard ceiling, no cluster of KL computations — just a min with a clipped alternative.
Formal
GAE (generalized advantage estimation) interpolates between low bias and low variance:
gives pure TD (low variance, high bias), gives Monte Carlo (high variance, low bias). Typically .
PPO's clipped objective. With the ratio :
The asymmetry is the whole point, and it is often missed. With :
| The clipped term | min picks | The effect | ||
|---|---|---|---|---|
| +1 | 1.5 | 1.2 | 1.2 | the upside is capped — stop increasing the probability |
| +1 | 0.5 | 0.8 | 0.5 | no capping — free to recover |
| −1 | 1.5 | −1.2 | −1.5 | no capping — free to be punished in full |
| −1 | 0.5 | −0.8 | −0.8 | the downside is capped — stop decreasing the probability |
In other words: min only clips in the direction that would make the update larger. Moving back towards the old policy is always allowed.
The full loss has three terms:
The entropy bonus counteracts the policy collapsing to determinism too early.
Why PPO in RLHF? Four reasons, in order:
- Robust to hyperparameters — important when every run costs GPU hours and you cannot search broadly.
- Several epochs per data collection — the ratio makes it safe to reuse data, which is decisive when the «environment» is expensive human or modelled feedback.
- No second-order machinery — TRPO needs conjugate gradients and Fisher matrix products; PPO is a few lines.
- The KL penalty against the reference model fits in naturally as an extra term — you want the model to improve without drifting away from the language model it started from.
DPO and other direct methods have in recent years taken over much of the RLHF work precisely because they avoid the whole RL loop — but PPO is still the reference implementation to understand first.
Code
import torch
def gae(rewards, values, dones, gamma=0.99, lam=0.95):
"""values has length T+1 (the last one is V(s_T))."""
T = len(rewards)
A = torch.zeros(T)
last = 0.0
for t in reversed(range(T)):
non_terminal = 1.0 - dones[t]
delta = rewards[t] + gamma * values[t + 1] * non_terminal - values[t]
last = delta + gamma * lam * non_terminal * last
A[t] = last
return A, A + values[:T] # (advantages, value targets)
def ppo_update(policy, critic, opt, batch, epochs=4, eps=0.2, c1=0.5, c2=0.01):
s, a, old_logp, A, V_target = batch
A = (A - A.mean()) / (A.std() + 1e-8)
for _ in range(epochs): # several epochs on the SAME data — what PPO enables
d = policy(s)
logp = d.log_prob(a)
ratio = torch.exp(logp - old_logp)
unclipped = ratio * A
clipped = torch.clamp(ratio, 1 - eps, 1 + eps) * A
pol_loss = -torch.min(unclipped, clipped).mean()
v_loss = ((critic(s).squeeze(-1) - V_target) ** 2).mean()
entropy = d.entropy().mean()
loss = pol_loss + c1 * v_loss - c2 * entropy
opt.zero_grad(); loss.backward()
torch.nn.utils.clip_grad_norm_(
list(policy.parameters()) + list(critic.parameters()), 0.5)
opt.step()
if (logp - old_logp).pow(2).mean() > 0.03: # approximate KL — break if we have drifted too far
break
Three diagnostic quantities to log, and what they mean:
| Quantity | A healthy level | What a deviation means |
|---|---|---|
| approximate KL per update | 0.005–0.02 | too high: too many epochs or too high an lr |
| the share of clipped samples | 0.1–0.3 | near 0: the clipping does nothing; near 1: the steps are too large |
| the policy's entropy | falls slowly | a steep fall: collapse, raise c2 |
The combination «a low clip share and a high KL» almost always means the value function is broken rather than the policy.
Mastery means
- Explains actor–critic and advantage estimation
- Derives and interprets PPO's clipped objective
- Knows why PPO was chosen for RLHF
Sign in to do the exercises and build your mastery up.
Sources
- arXiv — Proximal Policy Optimization Algorithms — arXiv (open access; licence per article)
- arXiv — High-Dimensional Continuous Control Using Generalized Advantage Estimation — arXiv (open access; licence per article)
- OpenAI Spinning Up in Deep RL (MIT) — MIT