↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Learning the policy directly

DQN learns values and acts greedily on them. Policy-gradient methods learn the policy itself: a network that gives a probability to every action, nudged so that actions which turned out better than expected become more likely. That works for any action space and gives smooth, stochastic behaviour, but its updates are noisy and easy to overdo. Proximal Policy Optimization (PPO) is the most widely used answer to both problems, and the policy update inside RLHF.

This page trains PPO on Gymnasium's LunarLander-v3: eight numbers describe the lander (position, velocity, angle, leg contact), four actions fire nothing, the left, the main or the right engine, and a shaped reward pays for approaching the pad slowly and level, charges for fuel, and ends with −100 for a crash or +100 for coming to rest. An actor (the policy) and a critic (a value estimate ), each a 64 × 64 tanh MLP, were trained here on the CPU for 61 rollouts of 16 environments × 1,024 steps, about a million steps in all.

Credit along a trajectory

The policy gradient increases in proportion to an advantage : how much better taking was than the policy's average from . The critic supplies the baseline. The simplest estimate is one step's TD error,

the surprise between what happened next and what the critic expected. Generalised advantage estimation (GAE) adds up the surprises that follow, each shrunk by :

The Credit along the episode charts show one whole episode step by step, played with the policy and critic of a chosen rollout (outside the training data, but scored exactly as its steps are): the reward, the critic's against the actual discounted return, , and the advantage at four values of . With the advantage is alone: low variance, but only as right as the critic. With it is the whole return minus , the Monte Carlo estimate: unbiased but noisy. This is the same trade-off as the TD vs MC tab, made continuous; this run uses and .

The clipped objective

A rollout is expensive to collect, so PPO reuses it for several epochs of minibatch updates (4 epochs of 64 samples here). After the first step the policy being trained, , is no longer the one that collected the data, . The probability ratio corrects for that, and PPO limits how far it may move:

Taking the minimum means a sample stops contributing gradient once its ratio has moved past the band in the direction its advantage asks for: above for a good action, below for a bad one. Moves in the other direction are never clipped, so a mistake can always be undone. The clipped update scatter shows the first minibatch of each epoch of one recorded update (rollout 30) with the band shaded. In epoch 1 every ratio is exactly 1. Later epochs see the ratios spread, and the grey samples are the ones the clip has switched off. The histogram below it tracks a fixed sample of 1,024 states through the four epochs.

What the run did

The Return chart follows the training episodes and a greedy evaluation every five rollouts. Returns climb from about −190 at the start, when nearly every episode is a crash; by rollout 20 the greedy policy already scores above 200, the usual mark for solving LunarLander. But the How episodes end chart shows that from then until about rollout 50 most episodes end at the 1,000-step time-out: the lander hovers or rests on its legs while it keeps firing the engines, so it never counts as having come to rest. Only in the last ten or so rollouts does it learn to switch the engines off: in rollout 61, 52 of the 53 episodes that ended were landings, with a mean return of 273, and the final greedy policy averages 270 over ten seeded episodes.

The Policy entropy falls from 1.39 nats (uniform over four actions) to about 0.72 as the policy becomes more decisive, slowed by an entropy bonus of 0.01 in the loss. The Clip fraction and KL chart shows how far each update moved the policy: 1.5 to 10 % of samples clipped in each update's last epoch, and an approximate KL below 0.01.

Where it came from

PPO (Schulman et al., 2017) simplified TRPO, which enforced a hard limit on the KL divergence between successive policies with second-order optimisation; the clip gets most of the benefit with plain first-order steps. GAE (Schulman et al., 2015) came from the same group. Many of PPO's results depend on details beyond the objective (advantage normalisation, orthogonal initialisation, gradient clipping, the value loss), all of which this run uses with Stable-Baselines3's tuned LunarLander settings. In language models, the Post-training Explainer's RLHF tab uses this same update with a reward model in place of the environment, and GRPO drops the critic in favour of a group baseline.

Papers

  1. Proximal Policy Optimization Algorithms John Schulman et al. · arXiv 2017 Introduces the clipped surrogate objective and several epochs of minibatch updates per rollout, as on this page.
  2. High-Dimensional Continuous Control Using Generalized Advantage Estimation John Schulman et al. · ICLR 2015 Defines GAE(λ), the exponentially weighted sum of TD errors the page draws at four values of λ.
  3. Trust Region Policy Optimization John Schulman et al. · ICML 2015 PPO's predecessor: the same surrogate objective, kept close to the old policy by a KL constraint.
  4. Simple statistical gradient-following algorithms for connectionist reinforcement learning Ronald J. Williams · Machine Learning 1992 REINFORCE, the likelihood-ratio policy gradient that PPO's update builds on.
  5. Implementation Matters in Deep Policy Gradients: A Case Study on PPO and TRPO Logan Engstrom et al. · ICLR 2020 Shows how much of PPO's performance comes from code-level choices such as advantage normalisation and initialisation.
  6. What Matters In On-Policy Reinforcement Learning? A Large-Scale Empirical Study Marcin Andrychowicz et al. · arXiv 2020 Measures the effect of λ, the clip range, epochs and network choices across many runs.

See also