Skip to main content
Actor-critic methods take one gradient step per batch of experience and then throw the batch away, because after the step the data no longer comes from the current policy. Collecting experience is the expensive part of RL, so you would like to take several steps on each batch. Taking them naively is unsafe: the policy gradient is only accurate near the policy that collected the data, and a few large steps can move the policy into a region where it performs much worse and from which the new data it collects cannot recover. Proximal policy optimization (PPO) (Schulman et al., 2017) makes several steps safe by limiting how far each batch can move the policy.

Surrogate objective and trust regions

Suppose a batch was collected with the policy πθold\pi_{\theta_{old}}. For a new policy πθ\pi_\theta, importance sampling rewrites the expected advantage-weighted objective with samples from the old policy: L(θ)=Et[rt(θ) A^t],rt(θ)=πθ(at∣st)πθold(at∣st).L(\theta) = \mathbb E_t[ r_t(\theta)\, \hat A_t ], \qquad r_t(\theta) = \frac{\pi_\theta(a_t \mid s_t)}{\pi_{\theta_{old}}(a_t \mid s_t)}. Here A^t\hat A_t is an advantage estimate computed with the old policy, usually by generalized advantage estimation. At θ=θold\theta = \theta_{old} the gradient of LL equals the policy gradient. Away from θold\theta_{old}, LL is only a local approximation, because the states in the batch were visited by the old policy, and maximizing it without limit overshoots. Trust region policy optimization (TRPO) (Schulman et al., 2015) maximizes LL subject to a bound on the average KL divergence between the old and the new policy, Et[KL(πθold(⋅∣st) ∥ πθ(⋅∣st))]≤δ\mathbb E_t[ \mathrm{KL}\big(\pi_{\theta_{old}}(\cdot \mid s_t) \,\|\, \pi_\theta(\cdot \mid s_t)\big) ] \le \delta. TRPO comes with a monotonic improvement guarantee for an idealized version of the problem, but each update needs a second-order solve (conjugate gradient with Fisher-vector products and a line search), which is complex to implement and does not combine easily with networks that share parameters between actor and critic.

The clipped objective

PPO keeps the idea of a trust region and replaces the constraint by a change in the objective: LCLIP(θ)=Et[min⁡(rt(θ) A^t, clip⁡(rt(θ),1−ϵ,1+ϵ) A^t)],L^{CLIP}(\theta) = \mathbb E_t[ \min\Big( r_t(\theta)\, \hat A_t,\ \operatorname{clip}\big(r_t(\theta), 1 - \epsilon, 1 + \epsilon\big)\, \hat A_t \Big) ], with ϵ\epsilon around 0.2. The minimum makes LCLIPL^{CLIP} a pessimistic bound on LL. Its effect depends on the sign of the advantage: Once a good action has become 1+ϵ1 + \epsilon times more likely, or a bad action 1−ϵ1 - \epsilon times as likely, its sample stops contributing. On the other side of the window the unclipped term stays active, so a step that made a good action less likely is still corrected. Clipping does not bound the size of a step; it decides, before each step, which samples still pull. The minimal examples show both effects on a policy with one parameter. The PPO paper also proposes a variant with a KL penalty instead of clipping, Et[rtA^t−β KL(πθold ∥ πθ)]\mathbb E_t[ r_t \hat A_t - \beta\, \mathrm{KL}\big(\pi_{\theta_{old}} \,\|\, \pi_\theta\big) ], where β\beta is doubled or halved after each update depending on whether the measured KL was above or below a target. In the paper’s experiments the clipped objective performed better, and it is the version most implementations use.

The full loss

With a shared network, or with separate actor and critic networks trained together, PPO maximizes L(θ)=Et[LtCLIP(θ)−c1(v^θ(st)−G^t)2+c2 H[πθ(⋅∣st)]].L(\theta) = \mathbb E_t[ L^{CLIP}_t(\theta) - c_1 \big( \hat v_\theta(s_t) - \hat G_t \big)^2 + c_2\, \mathcal H\big[\pi_\theta(\cdot \mid s_t)\big] ]. The second term trains the critic toward return targets G^t\hat G_t, usually A^t+v^θold(st)\hat A_t + \hat v_{\theta_{old}}(s_t). The third term is an entropy bonus that keeps the policy from becoming deterministic too early. The coefficients c1c_1 and c2c_2 weight the three parts.

The algorithm

Typical values are a few thousand samples per iteration, KK between 3 and 10 epochs and minibatches of 64 to 4,096 samples. The KL divergence between the old and the new policy is often monitored, and the epochs for an iteration stop early if it grows past a threshold.

PPO for language models

PPO is the algorithm of the original RLHF pipeline for language models (Ouyang et al., 2022). The state is the prompt together with the tokens generated so far, an action is the next token, and an episode is one complete response. A reward model trained on human preference comparisons scores the finished response. Two changes adapt PPO to this setting:
  • A frozen copy of the model before RL, the reference model, stays loaded. A penalty on the per-token KL divergence between the policy and the reference is subtracted from the reward, which keeps the policy from drifting into text that fools the reward model.
  • The critic is a separate value model, usually initialized from the reward model and as large as the policy.
PPO actor-critic loop: query q enters the Policy Model, which generates output o evaluated by a frozen Reference Model (KL penalty), a frozen Reward Model, and a trainable Value Model; combined reward r and value estimate v feed into GAE to produce advantage A; dashed arcs show actor and critic gradient paths back to their respective models PPO for language-model fine-tuning. The policy generates a response oo to the query qq. The frozen reference and reward models give the KL penalty and the reward, which combine into the per-token reward rr. The value model gives vv, and GAE turns rr and vv into advantages AA for the policy update. The value model is trained toward return targets computed from the same rewards. Keeping four large models in memory, two of them trained, is the main cost of this setup. GRPO removes the value model.

Further reading

Key references: (Schulman et al., 2017; Schulman et al., 2015; Ouyang et al., 2022)

References

  • Ouyang, L., Wu, J., Jiang, X., Almeida, D., Wainwright, C., et al. (2022). Training language models to follow instructions with human feedback. arXiv [cs.CL]. Link
  • Schulman, J., Wolski, F., Dhariwal, P., Radford, A., Klimov, O. (2017). Proximal Policy Optimization Algorithms. arXiv [cs.LG]. Link
  • Schulman, J., Levine, S., Moritz, P., Jordan, M., Abbeel, P. (2015). Trust Region Policy Optimization. arXiv [cs.LG]. Link

Found an error on this page? Report an issue.