PPO Algorithm › Worked Example: Tracing a Single PPO Update
Language AI HandbookAll chapters PPO Algorithm › Worked Example: Tracing a Single PPO UpdateOne section · Sections in this chapter7 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Before implementing PPO in code, let's trace through one complete update step numerically. This example uses tiny numbers to keep the arithmetic tractable, but the procedure is identical for million-parameter networks.
Suppose we have collected a short trajectory with three timesteps. The states, actions, rewards, and value estimates are:
| Timestep | State | Action | Reward | ||
|---|---|---|---|---|---|
| 0 | 1.0 | 0.8 | -0.693 | ||
| 1 | 0.0 | 0.5 | -1.386 | ||
| 2 | 2.0 | 1.2 | -0.405 |
The episode terminates after timestep 2, so . We use and .
Step 1: Compute TD errors. Working forward through the trajectory:
Step 2: Compute GAE advantages. Working backwards through the trajectory, initializing :
The advantage at is largest because the trajectory begins with a low value prediction (0.8) but transitions to states with higher-than-expected cumulative reward.
Step 3: Normalize advantages. Standard PPO normalizes advantages to have zero mean and unit variance. The mean is . The variance is approximately , giving standard deviation . Normalizing:
Step 4: Compute probability ratios. After one gradient step, suppose the new policy has log-probabilities: , , .
Step 5: Apply clipping with . The clip bounds are .
For : , (positive advantage). The unclipped term is . The clipped ratio is , so the clipped term is also . Both terms equal, so . Note that exactly: the ratio is right at the boundary.
For : , (small positive). Similarly .
For : , (negative advantage). The unclipped term is . The clipped ratio clips 0.75 to 0.8, giving clipped term . The (more negative wins). So .
Here, , so clipping activates and the clipped objective is flatter (less negative) than the unclipped one. This means the optimizer does not receive a gradient signal pushing below 0.8 further, preventing excessive suppression of action .
Step 6: Average the objective. .
This positive value indicates the gradient step improved the objective. On subsequent passes through the same data (multiple epochs), the ratios will update further, and clipping will activate when the policy has changed enough.