5.2.9 · D2Deep & Advanced RL

Visual walkthrough — Proximal Policy Optimization (PPO)

2,287 words10 min readBack to topic

Step 1 — What is a policy, and what does "make an action more likely" mean?

WHAT. A policy is the brain of our agent. Given a situation (a state, written ), it outputs a probability for each possible move (an action, written ). We write this probability as — read "pi of given ". The little (the Greek letter theta) is just the collection of knobs (neural-network weights) that shape those probabilities.

WHY. We cannot improve what we cannot measure. The whole of PPO is about nudging these probabilities: raising them for moves that turned out well, lowering them for moves that turned out badly. So the object we push around is a number between 0 and 1 attached to each action.

PICTURE. Two bars for the same state: the old policy (violet) and a slightly-changed new policy (magenta). Notice one action got taller — the new policy makes it more likely.

Figure — Proximal Policy Optimization (PPO)

Step 2 — The advantage : was this action better or worse than average?

WHAT. After taking an action we get a number (read "A-hat at time "), the advantage. It answers one question: compared to what my agent usually does in this state, was this particular action better or worse?

  • better than average (a good action, we want more of it).
  • worse than average (a bad action, we want less of it).
  • → exactly average (no opinion).

WHY. If we simply rewarded high-reward actions, a state where every action is great would push all of them up equally — no learning signal. Subtracting the average (that is what "advantage" does) leaves only the relative judgement, which is exactly the direction to nudge. The hat means "estimated" — we don't know the true value, we approximate it (via GAE).

PICTURE. A horizontal zero-line. Green arrow up for , red arrow down for . The sign is the only thing that will steer the whole objective.

Figure — Proximal Policy Optimization (PPO)

Step 3 — The ratio : how far has the new policy moved?

WHAT. We collected our experience with the old policy , but we are now tuning a new policy . To compare them on the same sampled action we form the probability ratio:

WHY a ratio and not a difference? We are reusing old data — a trick called Importance Sampling. Importance sampling says: to estimate an average under the new policy using samples from the old one, reweight each sample by exactly this ratio. So is forced on us by the maths of reuse, not chosen for taste. And a ratio has a clean reading:

  • → policies agree, no change on this action.
  • → new policy makes this action more likely.
  • → new policy makes it less likely.
  • → new policy would never take it (degenerate — see Step 8).

PICTURE. A number line for anchored at . Left of 1 = "less likely", right of 1 = "more likely". We will spend the whole derivation drawing a fence on this line.

Figure — Proximal Policy Optimization (PPO)

Step 4 — Why we cannot just maximize

WHAT. Suppose we hand to an optimizer with no restrictions. For a good action () the optimizer discovers it can make the objective bigger forever by pushing .

WHY that is a disaster. The ratio is only a trustworthy estimate of the true objective near the old policy (importance sampling degrades as the two distributions separate). Far away, is a fantasy. Worse, itself is noisy — the optimizer will happily march to infinity on a wrong advantage estimate, and the policy collapses.

PICTURE. The straight line for a good action shoots off the top of the frame — an unbounded ramp with no brakes. This is the runaway we must stop.

Figure — Proximal Policy Optimization (PPO)

Step 5 — The clip: build a fence around

WHAT. The function takes and squashes it into a band :

Here (the Greek epsilon) is a small width, typically , giving the band .

WHY. Inside the band, clip does nothing — we behave like a normal policy gradient. Outside the band, clip flattens the value to a constant, and a constant has zero slope. Zero slope means zero gradient, means the optimizer gets no reward for pushing further. That flat shelf is our trust region, enforced with nothing but a first-order operation (the whole point versus Trust Region Policy Optimization (TRPO), which needed a costly KL Divergence constraint).

PICTURE. A staircase: rising diagonally through the band, then flat shelves on both sides. The flat parts are where "the fence" is.

Figure — Proximal Policy Optimization (PPO)

Step 6 — Combine with min: the good-action side ()

WHAT. The clipped objective takes the minimum of the unclipped and clipped terms:

For a good action, . Multiplying both terms by a positive number keeps their ordering, so we plot the objective against :

  • For : both terms agree, the objective rises with — improvement flows.
  • For : the clipped term becomes the flat , which is now the smaller one, so min picks it → flat shelf, gradient = 0.

WHY min and not just clip? With plain clip we'd cap both directions symmetrically. The min is deliberately pessimistic: it lets the objective get worse freely but refuses to let it look better than the honest capped value. For a good action that means: reward improvement up to the fence, then stop.

PICTURE. The green curve: a rising ramp that hits a ceiling at and goes flat. Past the fence, no incentive to push.

Figure — Proximal Policy Optimization (PPO)

Step 7 — The bad-action side () and the mistake-correction gift

WHAT. Now . Multiplying by a negative flips everything, so the shape is a mirror image. Trace it left-to-right along :

  • We want down (make a bad action less likely).
  • If (already suppressed a lot): the clipped term is the smaller (more negative) one, min picks it → flat shelf, gradient = 0. We stop over-suppressing.
  • If (we accidentally made a bad action more likely!): the unclipped is more negative than the clipped one, so min keeps the unclipped term → gradient flows, pushing back down.

WHY this is the clever bit. Even though is outside the trust region, the min still lets us fully punish a move in the wrong direction. You are always allowed to undo damage — the fence only stops you from over-committing to something that already worked, never from correcting a mistake.

PICTURE. The magenta curve: flat shelf on the left (over-suppression frozen), then a steep downward slope on the right (mistake correction always active).

Figure — Proximal Policy Optimization (PPO)

Step 8 — Every case on one map (including degenerate inputs)

WHAT. Combine both signs and cover the corners no one shows:

region of active branch gradient? meaning
unclipped flows correct: action got too rare, push up
in band either (equal) flows normal policy gradient
clipped 0 good enough — stop
clipped 0 already suppressed enough — stop
in band either (equal) flows normal policy gradient
unclipped flows mistake! push back down
anywhere 0 no advantage, no signal
new policy killed the action unclipped () flat there degenerate — see below

WHY cover and ?

  • : both terms are , the whole objective is flat — the action is exactly average, nothing to learn. Correct behaviour, not a bug.
  • : the new policy assigns probability zero to a sampled action. Then in the underlying gradient; in practice PPO clips ratios and uses stable log-probs so this never sends the update to infinity. The clip's flat shelves are exactly what keep such extremes harmless.
  • at the very first inner epoch: new = old, so everywhere and — plain policy gradient. Clipping only "wakes up" once repeated SGD epochs move away from .

PICTURE. The full two-curve diagram: green (good action) and magenta (bad action) plotted together over , with the flat shelves and live slopes labelled, and the vertical fences at .

Figure — Proximal Policy Optimization (PPO)

The one-picture summary

Below: the entire PPO objective as one shape. Read it as a decision map — pick your sign (colour), find your (horizontal), and the slope tells you instantly whether the update flows or freezes. This one figure is the derivation.

Figure — Proximal Policy Optimization (PPO)
Recall Feynman retelling of the whole walkthrough

A policy is a set of dials that say how likely each move is. After playing, each move gets a report card: the advantage — a plus if it beat the average, a minus if it lagged. We reuse our old playthroughs by measuring the ratio : how much more (or less) the new dials favour a move versus the old dials. We'd love to just crank good moves way up — but that ratio is only honest near where we started, so cranking blindly wrecks the policy. So we build a fence at and : inside it we learn normally; outside it the objective goes flat, so the optimizer gets no reward for straying. The magic is the min: it flattens things only when straying would look falsely good, but keeps full punishment when we've made a bad move more likely — meaning we can always undo a mistake, but never over-commit to a lucky one. That single fenced, one-way shape is PPO.

Active recall