Value-based methods like Q-learning learn how good actions are and then act greedily. Policy-gradient methods take a more direct route: they parameterize the policy itself and adjust its parameters to increase expected return by gradient ascent. This turns out to be essential when actions are continuous, when we want genuinely stochastic policies, or when we later optimize language models with RL. This lesson develops the policy gradient from scratch, exposes its crippling variance problem, fixes it with baselines and advantages, and arrives at Proximal Policy Optimization (PPO), the workhorse behind much of modern applied RL, including RLHF.
A policy you can differentiate
Instead of a table, let the policy be a differentiable function with parameters θ (for example, a neural network). It outputs a probability distribution over actions:
πθ(a∣s).
Ask the tutor
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
The objective is the expected return of trajectories τ=(s0,a0,s1,a1,…) generated by this policy:
J(θ)=Eτ∼πθ[R(τ)],R(τ)=∑t=0Tγtrt+1.
We want to climb J. The difficulty is that the distribution we sample from depends on the very parameters we are differentiating, the environment's dynamics sit between θ and the reward, and we cannot differentiate through them. The policy gradient theorem is the elegant way around this.
The policy-gradient theorem and REINFORCE
Using the "log-derivative trick" (∇θpθ=pθ∇θlogpθ), the gradient of the objective takes a form that requires no gradient of the environment at all:
∇θJ(θ)=Eτ∼πθ[∑t=0T∇θlogπθ(at.
This is the REINFORCE estimator. Read it as a rule with a very intuitive shape:
1Roll out the current policySample one or more full trajectories by acting in the environment.
2Score each action's log-probability gradientCompute grad log pi(a_t | s_t), the direction in parameter space that makes that action more likely.
3Weight by the returnMultiply each action's gradient by how much total reward the trajectory earned.
4Average and stepTake the mean over samples and nudge theta in that direction (gradient ascent).
The one-line intuition
Policy gradients make actions that led to high return more likely, and
actions that led to low return less likely, each weighted by how good the
outcome was. That is the entire idea; everything after this is about making
the estimate less noisy.
The high-variance problem
REINFORCE is unbiased but extremely noisy. Three forces conspire against it:
Whole-trajectory credit. Every action in a trajectory is scaled by the same total return R(τ), even actions taken long after the reward was determined. Good actions get blamed for later bad luck and vice versa.
Absolute-magnitude weighting. If all returns are large and positive (say between 990 and 1000), every action's probability is pushed up; the tiny differences that actually distinguish good from bad drown in the common offset.
Monte Carlo sampling. Returns depend on many random transitions and action samples, so the estimate swings wildly from batch to batch.
The practical symptom is training that is slow, unstable, and hungry for enormous numbers of samples. Two ideas tame it.
Fix 1: causality (reward-to-go)
An action cannot affect rewards that already happened. So replace the full-trajectory return with the reward-to-go, only the rewards that came after the action:
This is still unbiased and strictly lower variance, because we stop crediting each action with rewards it could not have caused.
Fix 2: baselines and the advantage function
We can subtract any function of the state b(s) (a baseline) from the weight without changing the gradient's expectation, because E[∇θlogπθ(a∣s)b(s)]=0. The variance-minimizing choice of baseline is (close to) the state value V(s). Using reward-to-go with V(s) as the baseline yields the advantage function:
Aπ(s,a)=Qπ(s,a)−Vπ(s).
The advantage answers a sharper question: how much better than average is this action in this state? Positive advantage means "better than the policy's typical behavior here, do it more"; negative means "worse, do it less." Substituting it gives the modern actor–critic gradient:
∇θJ(θ)=E[∑t∇θlogπθ(at∣st)Aπ(st,at)].
Here a learned value estimate V(s) (the critic) supplies the baseline while the policy (the actor) is updated, hence "actor–critic." Centering the signal around zero is exactly what removes the "everything gets pushed up" pathology.
PPO: taking bigger steps without falling over
Even with advantages, plain policy gradients are fragile: one overly large update can push the policy into a bad region from which its own (now-bad) data cannot recover. We would like to reuse each batch of data for several gradient steps for efficiency, but naively doing so moves the policy far from the one that collected the data, and the gradient estimate becomes invalid.
PPO's answer is to optimize a surrogate objective built on the probability ratio between the new and old policies:
rt(θ)=πθold(at∣st)πθ(at∣st).
A ratio above 1 means the new policy makes that action more likely than the data-collecting policy did. The clipped surrogate objective is:
LCLIP(θ)=Et[min(rt(θ)A^t,clip(rt(θ),.
The clip range ϵ is small (commonly 0.1 or 0.2). Here is why the min-of-clipped construction stabilizes training:
1When advantage is positiveWe want to raise the action's probability, so the ratio grows above 1. Clipping caps the reward of the objective at ratio = 1 + epsilon, so once the policy has moved 'enough' there is no further incentive to push harder.
2When advantage is negativeWe want to lower the probability, so the ratio drops below 1. Clipping floors it at 1 - epsilon, again removing the incentive for an enormous corrective step.
3The min() keeps it pessimisticBy taking the minimum of the clipped and unclipped terms, PPO never lets a large ratio inflate the objective; it only ever discards improvement that would come from moving too far.
4ResultYou can safely run several epochs of minibatch updates on the same rollout data, because the objective refuses to reward straying far from theta_old.
Intuition
For a curious beginner
Clipping is a leash. The policy is free to improve, but the moment it tries to sprint too far from where it gathered its evidence, the leash goes taut and it stops getting rewarded for running. This keeps each update inside a region the collected data can still speak to.
Engineering
How it is actually used
PPO replaced TRPO's expensive second-order trust-region constraint with a cheap first-order clip you can implement in a few lines and run with ordinary Adam. Standard practice: estimate advantages with GAE, normalize them per batch, add an entropy bonus to keep exploring, run K=3-10 epochs over minibatches, and share or separate actor/critic networks depending on the task.
Mathematical
The underlying mechanism
The unclipped surrogate rt(θ)A^t is a first-order approximation of the expected advantage under the new policy (importance sampling from θold). It is trustworthy only while πθ≈πθold. TRPO enforces this with a KL constraint; PPO approximates the same trust region by clipping rt to [1−ϵ,1+ϵ] and taking the pessimistic min, which removes the gradient signal outside the trusted band.
For a curious beginner
Clipping is a leash. The policy is free to improve, but the moment it tries to sprint too far from where it gathered its evidence, the leash goes taut and it stops getting rewarded for running. This keeps each update inside a region the collected data can still speak to.
How it is actually used
PPO replaced TRPO's expensive second-order trust-region constraint with a cheap first-order clip you can implement in a few lines and run with ordinary Adam. Standard practice: estimate advantages with GAE, normalize them per batch, add an entropy bonus to keep exploring, run K=3-10 epochs over minibatches, and share or separate actor/critic networks depending on the task.
The underlying mechanism
The unclipped surrogate rt(θ)A^t is a first-order approximation of the expected advantage under the new policy (importance sampling from θold). It is trustworthy only while πθ≈πθold. TRPO enforces this with a KL constraint; PPO approximates the same trust region by clipping rt to [1−ϵ,1+ϵ] and taking the pessimistic min, which removes the gradient signal outside the trusted band.
A tiny numeric feel for clipping
Suppose an action has positive advantage A^t=+2, the clip parameter is ϵ=0.2, and after some updates the ratio has grown to rt(θ)=1.5 (the new policy makes this action 50% more likely than the old one). The unclipped term is 1.5×2=3.0, but the clipped term is clip(1.5,0.8,1.2)×2=1.2×2=2.4. PPO takes the minimum, 2.4, so the gradient contribution behaves as if the ratio were pinned at 1.2, the policy gets no extra credit for having moved past the trust band, and the incentive to move even further vanishes.
Common mistakes
Forgetting the baseline. Without subtracting V(s) (or normalizing
advantages), variance stays enormous and training crawls. - Crediting
actions with past rewards. Use reward-to-go; an action cannot influence
rewards that already occurred. - Reusing data for too many epochs. PPO
tolerates several epochs, but push it too far and the ratio drifts outside the
clip range for most samples, so gradients vanish and learning stalls. -
Dropping the min. Clipping the ratio alone is not enough; without the
pessimistic min, negative-advantage samples can still produce destabilizing
updates. - Confusing the clip with a hard constraint. PPO does not
forbid large policy changes; it merely stops rewarding them. A single sample
can still move the policy, hence the entropy bonus and modest learning rates.
Further reading
Schulman et al., "Proximal Policy Optimization Algorithms" (2017), the PPO paper and the clipped objective: https://arxiv.org/abs/1707.06347
Sutton & Barto, Reinforcement Learning: An Introduction (2nd ed.), Chapter 13, policy-gradient methods and the policy-gradient theorem: http://incompleteideas.net/book/the-book.html
Schulman et al., "High-Dimensional Continuous Control Using Generalized Advantage Estimation" (GAE), the advantage estimator PPO usually pairs with: https://arxiv.org/abs/1506.02438