Skip to content
Lambdian

Proximal policy optimisation (PPO)

Stable, reusable policy updates with a clipped surrogate objective and generalised advantage estimation.

Animated explanation

The idea

Policy gradients have a nasty failure mode: one overly large update can wreck the policy, the next batch of data is then collected by the wrecked policy, and learning collapses. We want to take the biggest safe step.

PPO does this with a simple trick. It measures how much the new policy changes the probability of each sampled action (the ratio rtr_t) and clips the objective so that pushing rtr_t beyond 1±ε1\pm\varepsilon earns nothing. The policy can then be optimised for several epochs on the same batch without running away. Combined with GAE advantages, a value loss and an entropy bonus, PPO is robust, simple to implement and the default algorithm in much of modern RL, including the fine-tuning of language models from human feedback (RLHF).

Trust regions, simplified

TRPO enforced a hard limit on the KL divergence between old and new policies using second-order optimisation. PPO approximates the same "trust region" with first-order SGD and clipping, which is much simpler and usually works as well. Some variants use an adaptive KL penalty instead of clipping; monitoring the approximate KL each update is a good health check.

Implementation details that matter

Normalise advantages per mini-batch; clip the value function too (optionally); use GAE with λ ≈ 0.95 and γ ≈ 0.99; 3–10 epochs per batch; Adam with learning-rate annealing; orthogonal initialisation; observation normalisation. Many published differences between PPO implementations come from these details rather than the core objective.

The algorithm, step by step

  1. 1
    Collect T steps with πθold\pi_{\theta_\text{old}} (log-probabilities, values, rewards, done flags).
  2. 2
    Compute δ and GAE advantages backwards: A^t=δt+γλ(1−dt)A^t+1\hat A_t = \delta_t + \gamma\lambda(1-d_t)\hat A_{t+1}; value targets A^t+V(st)\hat A_t + V(s_t).
  3. 3
    For K epochs, over shuffled mini-batches: maximise LCLIP−c1LV+c2HL^{\text{CLIP}} - c_1L^V + c_2H.
  4. 4
    Set θold←θ\theta_\text{old} \leftarrow \theta; repeat.

The maths

Probability ratio

rt(θ)=exp⁡(log⁡πθ(at∣st)−log⁡πθold(at∣st))r_t(\theta) = \exp\big(\log\pi_\theta(a_t|s_t) - \log\pi_{\theta_\text{old}}(a_t|s_t)\big)

Clipped surrogate

LCLIP(θ)=Et[min⁡(rtA^t, clip⁡(rt,1−ε,1+ε)A^t)]L^{\text{CLIP}}(\theta) = \mathbb{E}_t\big[\min(r_t\hat A_t,\ \operatorname{clip}(r_t,1-\varepsilon,1+\varepsilon)\hat A_t)\big]

GAE recursion

A^t=δt+γλ(1−dt)A^t+1\hat A_t = \delta_t + \gamma\lambda(1-d_t)\hat A_{t+1}

Strengths and weaknesses

Strengths

  • Stable and robust across many tasks
  • Multiple epochs per batch: better sample use than vanilla PG
  • Simple first-order implementation; scales with parallel environments

Weaknesses

  • Still on-policy: data is discarded after each iteration
  • Many implementation details and hyperparameters
  • Less sample-efficient than off-policy methods like SAC

Reach for it when…

A strong default for policy optimisation: games, robotics in simulation, and RLHF for language models.

Check your understanding

Question 1

Q1A good action (Â > 0) already has ratio r = 1.3 with ε = 0.2. Does PPO's objective still push its probability up?

Question 2

Q2What does GAE's λ control?

Question 3

Q3Why can PPO reuse a batch for several epochs, unlike vanilla policy gradient?

Lambdian Lab

The pieces of PPO

Implement and test PPO's core computations.

  • gae(rewards, values, dones, gamma, lam): values has length T + 1 (the last one bootstraps); return (advantages, returns) with returns = advantages + values[:T].
  • clipped_objective(logp_new, logp_old, adv, eps): the mean of the clipped surrogate.
  • ppo_bandit(steps, epochs, eps, lr, seed): a 3-armed bandit with rewards [0.2, 1.0, 0.5] and a softmax policy over logits; each step samples 64 actions, advantages = reward − batch mean, then "epochs" gradient-ascent steps on the clipped objective (numerical gradient is fine). Return the final logits.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.