PPO: Proximal Policy Optimization Algorithms
Clipping the probability ratio lets a policy train on each batch of experience for several epochs.
A plain policy gradient takes one step per batch, because further steps on the same data can push the policy somewhere much worse. PPO limits how much reward the objective gives for changing any single action's probability, so many minibatch steps on one batch stay close to the policy that collected it.
Explaining the paperProximal Policy Optimization AlgorithmsTRPO kept each update small with a KL constraint and a second-order solver. PPO gets similar behavior from a min and a clip, a change of a few lines to a vanilla policy gradient, and OpenAI later used it to fine-tune InstructGPT from human feedback.
Data the agent pays for
A supervised model trains on a dataset that already exists. A reinforcement-learning agent has to produce its own data by acting: it runs its current behavior in an environment (a physics simulator, an Atari game), records what happened, and learns from that record. Every batch costs simulator time or real-world interaction, and only a reasonably good policy produces useful batches. Algorithms are therefore compared by sample efficiency, how much the policy improves per timestep of experience.
Standard policy-gradient methods collect a batch, take one gradient step, and throw the batch away. PPO, published by Schulman, Wolski, Dhariwal, Radford and Klimov at OpenAI in July 2017, changes the objective so that the same batch supports several epochs of minibatch updates. It comes out of a line of work on one question, how far a policy may move in one update: conservative policy iteration (2002) posed it, and TRPO (2015) answered it with a KL-divergence constraint solved by second-order optimization. PPO keeps the goal and replaces the machinery with a clipped ratio that ordinary gradient ascent can optimize.
The page builds it in order: the policy gradient and the advantage that weights it; why a batch is normally used once; the trust region TRPO uses to prevent that; the clipped objective; the KL-penalty alternative the paper tested; and the full algorithm with its hyperparameters and results.
The policy gradient
The trained object is a policy , a neural network with parameters that takes the current state (joint angles and velocities, or a stack of game frames) and outputs a probability distribution over actions . In the paper's MuJoCo experiments it is a two-hidden-layer MLP with 64 tanh units per layer whose output is the mean of a Gaussian over torques, with learned standard deviations. The agent samples its action from that distribution, receives a reward, and moves to a new state. The return is the discounted sum of rewards from a point onward, and training adjusts to raise the expected return.
The return depends on only through which actions get sampled, and the environment in between is not differentiable, so there is no computation graph from to the return to backpropagate through. The score-function identity gets around this. Since , the gradient of an expectation over sampled actions becomes an expectation of times the outcome, which a batch of samples can estimate. The paper writes the estimator as
For each action taken in the batch, is the direction in parameter space that makes that action more likely in that state. It is scaled by , an estimate of how much better the action was than usual, and is the empirical average over the batch. This is REINFORCE (Williams, 1992) with a baseline. The update is gradient ascent: , because the aim is to maximize return. Autodiff frameworks get by differentiating an objective whose gradient is (1):
The paper calls a "loss", following autodiff habit, but it is maximized; code that expects a loss to minimize is handed .
The weight estimates the advantage:
is the expected return from state when following the policy, and is the expected return when taking action first and following the policy after. The difference measures how much better than average the action was in that state. Weighting by the raw return would also give a correct gradient on average, but a noisier one. If every action in some state earns a return near 100, raw returns push up every action taken there, including mediocre ones, and only the small differences between them carry information about which action to prefer. Subtracting a baseline that depends only on the state leaves the expected gradient unchanged, because the baseline term sums to zero over the policy's actions:
The expectation is unchanged and the variance drops, since each sample's weight is now centered near zero. Figure 1 runs this on a one-dimensional action. The amber curve is the return for each action, which the agent cannot see; the teal bell is the policy. Each step samples a few actions, compares each return to the baseline, and pushes the policy toward the actions above it. Press Play and watch the bell move toward the peak of the amber curve:
Estimating the advantage
The agent never observes ; it has one trajectory of rewards and a learned value network . PPO uses generalized advantage estimation (GAE), built from the one-step residual
compares what the value network predicted at step with the reward received plus the discounted prediction one step later. A positive means the step went better than predicted. The discount (0.99 in every experiment in the paper) makes rewards further ahead count for less. GAE sums the residuals from to the end of the collected segment of steps, with weights that shrink by per step:
sets how far the estimate looks ahead. At only counts: low variance, but the estimate inherits every error in . At the sum telescopes to the discounted return actually collected (bootstrapped with at the end of the segment) minus . That estimate depends on only through the final bootstrap and has high variance. The paper uses , so a residual 11 steps later gets about half the weight of .
Figure 2 works an 8-step segment. The value network predicts 0.55 at step 0 and 0.60 at step 1, and the step-0 reward is 0, so . A reward of +1 arrives at step 5 that the network underpredicted. With , and step 0 barely registers as good. With , , the Monte Carlo value. The paper's gives 0.384. Drag and watch the bright bars, the weighted residuals that sum to , grow toward the step-5 reward:
Â0 = 0.384 · λ=0 (one step): 0.044 · λ=1 (Monte Carlo): 0.482
weight halves every 11.3 steps
GAE with is biased on purpose. The baseline argument above shows that subtracting the exact costs nothing in bias; bootstrapping from a learned inside the residuals introduces some wherever is wrong, and GAE accepts it for lower variance. None of what follows depends on how was computed.
Why a batch is used once
Equation (1) averages over actions sampled from the current policy. After one gradient step, has changed, and the batch now comes from a policy that no longer exists. A second step on the same batch computes the gradient of the new policy from actions the new policy would sample with different frequencies, so the estimate is biased, and each further step makes the mismatch larger. The paper puts it directly: multiple steps on with the same trajectory are "not well-justified" and "often [lead] to destructively large policy updates". In its experiments that setting scored similar to or worse than the no-clipping baseline of Section 6.1, the worst row in Table 1.
The standard correction for samples from the wrong distribution is importance sampling: reweight each sample by how much more or less likely the current policy is to take that action than the policy that collected it. The weight is the probability ratio
is the parameter vector frozen at collection time. Suppose the old policy chose some action with probability 0.30 and, after a few gradient steps, the new policy gives it 0.39. Then : the new policy takes that action 30% more often. Every ratio starts at 1 at the beginning of an update. Weighting the advantages by the ratio gives the surrogate objective
The superscript stands for conservative policy iteration (Kakade and Langford, 2002), where this objective was proposed. At its gradient equals (1), since and every there. The TRPO paper showed a related property: this surrogate matches the true expected return to first order around , so for small steps, improving it improves the policy.
For large steps nothing in (6) limits the optimizer. Take the action above with . Its term grows linearly in : 2.0 at the start, 2.6 at , 6.0 at , with the same slope throughout. Gradient ascent keeps raising the probability of the few actions with large positive advantages far past the range where the batch's estimate of their advantage is reliable. The paper: "Without a constraint, maximization of would lead to an excessively large policy update."
TRPO's trust region
TRPO makes "do not move far" an explicit constraint. It measures how different the new policy is from the old one with the KL divergence, a standard score of how different two probability distributions are (zero when they are identical, larger as they separate), averaged over the states in the batch:
The set of policies within the KL budget is the trust region: the neighborhood of where the surrogate is trusted as a stand-in for the return. The KL is written with the old policy first, , and the order matters because KL is not symmetric. For two Gaussians over one action with the same standard deviation , it reduces to
so TRPO's allows the mean to move by . In Figure 3 the amber curve is the old policy; drag the new policy's mean and watch the KL bar cross at the edge of the shaded band:
within the trust region, surrogate still trustworthy
TRPO's theory calls for a penalty rather than a constraint, that is, maximizing
because a version of this surrogate that uses the maximum KL over states, rather than the mean, is a lower bound on the policy's true performance. Raising a lower bound guarantees that the true return does not fall, which gives monotonic improvement. The practical algorithm gives up that guarantee twice: it replaces the maximum KL with the mean in (4), and it swaps the penalty for a hard constraint. It uses a constraint because, per the PPO paper, no single fixed works across different problems, "or even within a single problem, where the characteristics change over the course of learning".
TRPO solves (3)–(4) approximately with the conjugate gradient algorithm, using a linear approximation of the objective and a quadratic approximation of the constraint, followed by a line search. Conjugate gradient with a line search is a second-order method, and the PPO paper lists its costs: TRPO is "relatively complicated" and does not fit architectures with noise such as dropout, or with parameters shared between the policy and value function. PPO's goal is TRPO's data efficiency and reliability "while using only first-order optimization".
The clipped objective
The failure in (6) was that keeps growing as the ratio moves further from 1. PPO removes that growth outside a band around 1. It clips the ratio to and takes the minimum of the clipped and unclipped terms:
is a hyperparameter; the paper's example value is 0.2, which is also the best value in its sweep. The first term inside the min is . The clipped term, in the paper's words, "removes the incentive for moving outside of the interval". Which clip matters depends on the sign of the advantage.
Positive advantage. The gradient raises the action's probability, so grows. The term follows up to and then stays flat at . Past that point this sample's gradient is exactly zero. The lower clip never binds for a positive advantage.
Negative advantage. The gradient lowers the action's probability, so shrinks. Now the lower clip binds: the term is flat at for and follows everywhere above that. Written per sign, the term is
The min becomes a max in the second case because multiplying by a negative reverses the order.
With the numbers from before and : the action with at has an unclipped term of 2.60 and a clipped term of . The min is 2.40, the flat branch, so the objective no longer rewards raising that action's probability. Figure 4 plots one sample's term against . Switch the sign of the advantage, move past each edge of the band, and read in the line under the plot:
ε = 0.20 · L = 1.00 · dL/dr = +1 · inside [1−ε, 1+ε]: gradient active
Keeping each update near the old policy is what proximal refers to. There is no KL computation in (7), no Fisher matrix, no conjugate gradient and no line search: the objective is a min, a clip and one hyperparameter, and any autodiff framework differentiates it. Because it only changes the objective, it works unchanged with a network that shares layers between the policy and the value function, which TRPO's constraint made difficult.
The clip approximates the trust region and does not reproduce it. TRPO bounds the average KL of the whole update. The clip acts on each sample separately: once a sample's ratio leaves the band on the improving side, that sample stops contributing gradient, so the samples still pulling the parameters are those still inside the band or pushed out on the worsening side (next section). Nothing bounds how far the shared parameters move the other samples in the meantime, and the paper states no KL guarantee for .
Why the objective takes a minimum
Clipping alone would flatten a good action's term past , but it would also flatten it below , where the action has become less likely than before. The min restores the unclipped term there. The paper: "we only ignore the change in probability ratio when it would make the objective improve, and we include it when it makes the objective worse."
Take the bad-action case with numbers. The old policy chose an action with probability 0.30 and it had , but updates driven by other samples have raised it to 0.40, so . The unclipped term is ; the clipped term is . The min picks , the unclipped branch, so this sample's gradient is still per unit of ratio and it pulls back down. A clip without the min would have used , a flat branch with zero gradient, and left the mistake uncorrected. This is the violet zone in Figure 4.
Since the min never exceeds its unclipped argument, for every . The two are equal while every ratio is inside its band, and in particular, as the paper notes, to first order around . The paper calls a pessimistic lower bound on the unclipped objective. It bounds the surrogate, not the true return; the bound on the return belongs to TRPO's max-KL theory.
Figure 5 reconstructs the paper's Figure 2, which interpolates the parameters from to the result of one PPO update and evaluates the objectives along the way. Here the batch is synthetic. The unclipped keeps rising the further the policy moves. rises, peaks, and then falls, because past the peak the samples on the worse side of their band are still counted at full cost while the samples on the improving side no longer add anything. In the paper the peak sits at the updated policy, whose KL from the old one is about 0.02:
KL=0.021 · L^CPI=0.08 · L^CLIP peaks near KL=0.020, then falls
A small calculation connects to a KL. Take the one-action Gaussian from Figure 3 with and move its mean by 0.2. The KL is . The log-ratio for an action is , so an action one standard deviation above the old mean has , right at the edge for , and one two standard deviations above it has . In this toy, a clip of 0.2 starts to bind on typical good actions at about the KL the paper reports after one update.
The adaptive KL penalty
The paper also tests the direct alternative: keep TRPO's penalty form (5) and optimize it with SGD, adjusting so that each update lands near a target KL . Each policy update first runs several epochs of minibatch SGD on
and then measures and updates for the next policy update:
With , an update that reaches a KL below 0.0067 halves the penalty and one above 0.015 doubles it. The factors 1.5 and 2 were "chosen heuristically", and the paper reports the algorithm is not very sensitive to them or to the initial . Figure 6 runs the rule on a toy model in which the KL an update reaches is proportional to times a pull that drifts 25-fold over training. Compare adaptive against a fixed , and count the updates that land in the band:
β = 0.250 · d = 0.0180 · d > 1.5·d_targ → β doubles to 0.500
At the adaptive rule keeps 37 of the toy's 60 updates in the band against 8 for the fixed penalty, which misses because the pull drifts during training, the change "within a single problem" that the paper gives as the reason a fixed is not enough. On the real benchmark the penalty variants work but trail the clip: the best adaptive setting () scores 0.74 and the best fixed one () 0.72, against 0.82 for clipping at . The paper keeps the penalty as "an important baseline" and recommends the clip.
The full algorithm
A practical PPO objective adds two terms to the clip. When the policy and value function share a network, the shared layers also need the value network's error signal, so a value loss enters the objective. An entropy bonus rewards the policy for keeping its action distribution spread out, which keeps it exploring:
Expression (9) is maximized, "approximately", once per iteration. The value error is subtracted because it should be small; the entropy is added because it should be large. Code that minimizes a loss negates all of (9), which flips all three signs. The paper does not define ; the official code uses , the GAE advantage plus the old value prediction, which at is a mix of observed rewards and bootstrapped values. On MuJoCo the paper uses separate policy and value networks, so has no effect, and no entropy bonus. On Atari the networks share parameters, with and .
The loop (Algorithm 1): each iteration, actors run the frozen policy in parallel for timesteps each and compute advantages. The objective is then optimized on those samples for epochs (an epoch is one pass over all of them) with minibatches of size , using minibatch SGD or, "usually for better performance", Adam. Finally and the next batch is collected. The paper's three settings give different amounts of reuse:
- MuJoCo (Table 3): , , minibatch 64, Adam step size , , . With one environment, as in the official script, a batch of 2048 samples gives 32 minibatches per epoch and 320 Adam steps before it is discarded.
- Atari (Table 5): 8 actors, , , minibatch . A batch of 1024 samples gives 4 minibatches of 256 per epoch and 12 Adam steps. The step size and are both multiplied by a factor that is annealed linearly from 1 to 0.
- Roboschool humanoids (Table 4): 32 actors (128 for the flagrun tasks), , , minibatch 4096. The Adam step size is adjusted from the measured KL.
In Figure 7, drag to change how many epochs each collected batch supports:
one rollout → 10 epochs over the same data (the paper's MuJoCo setting)
The same iteration in code. The ratio is computed as the exponential of a difference of log-probabilities, which avoids dividing small probabilities, and the last lines show the ascent-as-negated-loss convention. The official implementation also standardizes the advantages in each batch to zero mean and unit variance before the epochs start, a detail the paper does not mention.
# one PPO iteration (Algorithm 1, actor-critic style)
data = []
for actor in range(N): # N actors, in parallel
traj = run(pi_old, T) # T timesteps with the frozen policy
adv = gae(traj, V, gamma, lam) # A_hat_t, eqs (11)-(12)
ret = adv + V(traj.s) # value target (official code)
data += zip(traj, adv, ret, logp(pi_old, traj)) # keep old log-probs
for epoch in range(K): # reuse the SAME data K times
for mb in minibatches(data, M): # M <= N*T
r = exp(logp(pi, mb) - mb.logp_old) # ratio r_t, eq (6)
unclip = r * mb.adv
clip = clamp(r, 1 - eps, 1 + eps) * mb.adv
L_clip = mean(min(unclip, clip)) # eq (7)
L_vf = mean((V(mb.s) - mb.ret) ** 2) # squared error
L = L_clip - c1 * L_vf + c2 * mean(ent(pi, mb)) # eq (9)
loss = -L # frameworks minimize, so negate
loss.backward(); adam.step()
pi_old = copy(pi) # then collect a fresh batchFigure 8 shows why the repeated epochs stay controlled. It optimizes one fixed minibatch of ten samples twice, once on and once on , and plots each sample's ratio in the strip below the curves. Scrub to epoch 10 and compare where the two rows of dots end up:
mean |r−1|: 0.00 clipped · 0.00 unclipped · ε = 0.2
In the toy each sample has its own ratio, so a sample that stops is never moved again. In a real network the samples share parameters, and a stopped sample's ratio can still drift because of the others. That residual drift is one reason the clip gives no hard bound on the KL.
Results
The first experiment compares the surrogate objectives on 7 MuJoCo tasks (HalfCheetah, Hopper, InvertedDoublePendulum, InvertedPendulum, Reacher, Swimmer, Walker2d), one million timesteps each, 3 seeds per task. Each run is scored by its average total reward over the last 100 episodes, rescaled per environment so that a random policy scores 0 and the best result scores 1, and the 21 runs are averaged into one number per setting. Select a bar to read its setting:
ε=0.2 → 0.82 · Clipping with ε=0.2. The winner, and the value the paper reuses for the MuJoCo algorithm comparison (Atari uses 0.1×α).
beats 0.1 (0.76) and 0.3 (0.70), and the paper reuses it for the later MuJoCo comparison. The value comes from this sweep in Section 6.1; it is not a row of Table 3. The for "no clipping or penalty" is the unbounded ratio from (6) in practice: repeated epochs on drive HalfCheetah to a score far worse than the random initial policy, enough to pull the 7-task average below zero.
Against other algorithms on the same tasks (TRPO, the cross-entropy method, vanilla policy gradient with an adaptive step size, A2C, and A2C with a trust region), PPO with "outperforms the previous methods on almost all the continuous control environments". On Roboschool it trains a 3D humanoid to run, to steer toward a target that moves every 200 timesteps or when reached, and, in the hardest task, to get up off the ground while being pelted by cubes.
Atari is closer. Over 49 games, with the winner of each game decided by the average of three runs, PPO wins 30 games on average reward over all of training, against 18 for ACER (actor-critic with experience replay, which also learns from stored off-policy samples and adds its own trust-region step) and 1 for A2C (the synchronous version of A3C). That metric rewards learning fast. On average reward over the last 100 episodes, which rewards final performance, ACER wins 28 games, PPO 19, A2C 1, with 1 tie. The paper's own summary is that PPO does significantly better than A2C in sample complexity and similarly to ACER "though it is much simpler".
That simplicity drove adoption: the change to a policy-gradient implementation is a few lines, it runs on Adam with a shared policy-value network, and it has one main hyperparameter, . The human-preference paper of Christiano et al. appeared five weeks before PPO and used A2C and TRPO. Later RLHF work used PPO, including InstructGPT (2022), which fine-tunes GPT-3 against a learned reward model with PPO plus a per-token KL penalty from the supervised model. That penalty does a job similar to the clip's, holding the fine-tuned model near a reference policy, here the supervised model rather than . Methods such as DPO later removed the RL loop from preference tuning altogether.
Questions you might still have
Why take many small clipped steps instead of one large gradient step?
The surrogate is only a reliable estimate of improvement near the policy that collected the batch. One large step can leave that neighborhood before anything checks it. Many small steps on the clipped objective each follow a gradient that is still valid, and each sample stops contributing once its ratio leaves the band, so later epochs change the policy less and less.
Does clipping keep the KL divergence below a fixed limit?
No. The clip removes the incentive to push any single sample’s ratio past 1 ± ε, but nothing in the objective measures or bounds the KL, and a shared network can still move samples whose own gradients are zero. The paper reports a KL of about 0.02 after one update on Hopper. Many later implementations add an explicit KL check that stops the epochs early; that is later practice, not part of the paper.
What does PPO inherit from TRPO?
The idea that an update should stay near the old policy so the surrogate remains trustworthy. It does not inherit the theorem: TRPO’s monotonic-improvement bound needs the maximum KL over states, and PPO replaces the constraint with a clip that a first-order optimizer can handle.
Is the clipped objective a lower bound on the true return?
No. It is a lower bound on the unclipped surrogate L^CPI. The lower bound on the return belongs to TRPO’s theory, through a penalty on the maximum KL.
Does PPO always run ten epochs per batch?
No. K = 10 is the MuJoCo setting (Table 3). The Atari runs use K = 3 and the Roboschool humanoid runs use K = 15. The paper’s general phrase is "several epochs".
Where does the common value-loss coefficient c1 = 0.5 come from?
Not from this paper. Its only stated c1 is 1, for Atari, where the policy and value function share a network. The MuJoCo runs use separate networks, so c1 has no effect there. The value 0.5 is a default in later libraries such as Stable-Baselines.
Is PPO on-policy?
Yes. Every batch comes from the current policy, and it is discarded after K epochs. The ratio corrects for the small drift that accumulates within those epochs, and the clip keeps that drift small. Off-policy methods such as soft actor-critic instead keep a replay buffer of old experience.
Did the 2017 human-preferences paper by Christiano et al. use PPO?
No. It appeared in June 2017, predates PPO by about five weeks and used A2C and TRPO. PPO became the RL stage of later RLHF work, including InstructGPT (2022), which adds a per-token KL penalty from the supervised model and initializes the value function from the reward model.
Footnotes & further reading
- The paper: Schulman, Wolski, Dhariwal, Radford, Klimov, Proximal Policy Optimization Algorithms (OpenAI, 2017; v1 20 July, v2 28 August). Official code: openai/baselines, ppo1. A clear secondary reference is OpenAI's Spinning Up page.
- The trust region it descends from: Schulman, Levine, Moritz, Jordan, Abbeel, Trust Region Policy Optimization (2015), with the max-KL lower bound that PPO emulates with a first-order method. See also the TRPO explainer.
- The advantage estimator: Schulman, Moritz, Levine, Jordan, Abbeel, High-Dimensional Continuous Control Using Generalized Advantage Estimation (GAE, 2015). The clean GAE weights are on rewards and on residuals.
- The surrogate's origin: Kakade and Langford, Approximately Optimal Approximate Reinforcement Learning (ICML 2002), the conservative policy iteration that names .
- The policy gradient itself: Williams, Simple Statistical Gradient-Following Algorithms (REINFORCE, 1992), and Sutton, McAllester, Singh, Mansour, Policy Gradient Methods for RL with Function Approximation (2000).
- PPO in RLHF: Ouyang et al., Training language models to follow instructions with human feedback (InstructGPT, 2022), whose RL stage is PPO with a per-token KL penalty from the SFT model. The earlier Deep RL from Human Preferences (Christiano et al., 2017) used A2C and TRPO.
How could this explainer be improved? Found an error, or something unclear? I read every message.