Özer Cem KELAHMETKELAHMET
AI Simulations · Learning methods

PPO's clipped objective step by step: probability ratios, clipping and the approximate KL

Proximal Policy Optimisation reuses one batch of experience for several epochs of gradient ascent without letting the policy run away. The trick is a single min and a clip. Press Run, watch which actions stop pushing the policy, then switch clipping off and see what PPO protects you from.

Download Markdown

Interactive simulation. It needs JavaScript; the text below explains what it shows.

What you are looking at

The old policy has acted and produced a batch of sixteen actions. Each has an advantage A, the normalised generalised advantage estimate (GAE) that CleanRL computes: positive means the action turned out better than the critic expected, negative means worse. The advantages average to zero, as they do after per-minibatch normalisation.

To keep the picture readable the policy can move along a single direction, a step θ. The column dlogp says how much a unit step raises or lowers the log-probability of each action, so the new log-probability is the old one plus θ × dlogp. Real PPO moves millions of weights, but every one of them sees exactly the per-action terms shown here.

The chart has dlogp along the bottom and each action's objective term divided by |A| up the side. Actions with a positive advantage sit on the upper curve, the others on the lower one. As θ grows the upper curve flattens at 1 + ε: past that point making the action more likely earns nothing more. The bars show each action's contribution to the objective, and the small chart under the controls tracks the objective epoch by epoch.

The algorithm

  1. Ratio. For every action, r = π_new(a|s) / π_old(a|s), computed as the exponential of the difference of log-probabilities. At θ = 0 every ratio is 1.
  2. Clip. clip(r, 1 − ε, 1 + ε) keeps the ratio within ε of 1. Spinning Up and CleanRL both default to ε = 0.2.
  3. Pessimistic minimum. The term for each action is min(r·A, clip(r)·A). For a good action (positive A) the gain is capped at (1 + ε)·A; for a bad one (negative A) the reward for making it rarer is capped at (1 − ε)·A. But if a step makes a bad action more likely, the unclipped and worse term is kept: PPO never ignores damage.
  4. Gradient. When the clipped term is the minimum it is a constant, so that action's gradient is zero. Only the other actions push θ.
  5. Repeat. PPO does several epochs over the same batch (CleanRL: 4 epochs of 4 minibatches). Each epoch the ratios move further from 1, more terms clip, and the push fades.
  6. Monitor. CleanRL logs clipfrac, the share of ratios outside 1 ± ε, and approx_kl, the mean of (r − 1) − log r, Schulman's k3 estimator of the KL divergence between old and new policy. An optional target_kl stops the epochs early.

Worked example

With ε = 0.2 and a learning rate of 0.5, the first gradient is 0.0966, so θ moves to 0.048. By the fourth step θ = 0.200 and action 4 (dlogp 1.15, A = 0.94) has a ratio of 1.259: clipped, its gradient is gone. After that θ cycles between about 0.20 and 0.23: one-dimensional gradient ascent cannot sit exactly on the kink where action 4 starts to clip, so it steps over it and back. At step 50 θ = 0.211, the objective is 0.0171, 12.5% of the ratios lie outside 0.8 to 1.2, one action has zero gradient and the approximate KL is 0.0102. Notice that the largest ratio is 1.275, above 1.2: action 11 has a negative advantage and is getting more likely, so its unclipped penalty is still counted.

Things to try

  • Press Run with the defaults and watch θ stop climbing near 0.21 while the approximate KL stays between 0.009 and 0.013.
  • Set clipping to 0 and press Run. With the plain surrogate r·A the objective keeps rising: after 10 steps θ = 0.553 and the KL is 0.073, after 20 steps θ = 1.52, the largest ratio is 5.78 and the KL is 0.66. The simulation stops θ at 3; a real policy would collapse.
  • Halve ε to 0.1: θ never gets past 0.13 and the KL stays below 0.004. Raise it to 0.3: θ climbs to about 0.29 to 0.33 and the KL to about 0.02. ε is a trust region in disguise.
  • Raise the learning rate to 2 with clipping on: θ jumps back and forth between about 0 and 0.32. The clip limits how far the policy goes, not how noisily it gets there.
  • Edit an advantage from positive to negative and watch its dot jump from the upper curve to the lower one.

How it works in real systems

PPO replaced TRPO's constrained second-order update with this first-order clipped objective; Spinning Up notes that it is much simpler to implement and empirically performs at least as well. Most of its reliability comes from implementation details around the objective: advantage normalisation, a clipped value loss, learning-rate annealing, gradient clipping and early stopping on KL, which CleanRL documents one by one.

In RLHF, PPO fine-tunes a language model against a reward model; each token is an action and a KL penalty to the reference model is added to the reward. GRPO keeps the same clipped ratio but replaces the learned value baseline with the average reward of a group of answers, as in the GRPO simulation. For the value-based side of reinforcement learning see Q-learning in a grid world, and for preference methods that skip reinforcement learning altogether see SFT and DPO.

Sources

Last updated Nov 24, 2026
Type to search.
    ↑↓ navigate↵ open