Suppose the policy is a neural network with parameters that outputs a distribution over actions. The objective is the expected return . If you could compute you could do gradient ascent and be done. The difficulty is that is an expectation over trajectories, and the distribution over trajectories depends on in two ways: through the actions chosen, which is fine, and through which states are visited as a consequence of those actions, which is not fine, because the state visitation distribution is a complicated function of that runs through the unknown dynamics.
The policy gradient theorem says you can ignore the second dependence. The gradient of the expected return is an expectation, under the current policy's own state distribution, of the score weighted by the action value . Every piece of that can be sampled by running the policy. The intuition for why the state distribution's gradient disappears is that it is already accounted for: the value includes the downstream consequences of the action, so the effect of changing the policy on where you go later is priced into the weight on each action, not carried as a separate term.
The resulting update is easy to read. Increase the probability of actions that led to high value, decrease the probability of actions that led to low value, in proportion to how much better or worse they were than expected.
Setup
Let be differentiable in with wherever it matters. The objective is the expected discounted return from the start distribution,
Define the discounted state visitation distribution
The factor normalizes it to a probability distribution, since for each and . It weights states by how soon and how often the policy visits them.
The theorem
Theorem (Sutton, McAllester, Singh, Mansour 1999).
The constant is often absorbed into the step size and the result written as a proportionality; I keep it so that the two proofs below agree exactly.
First proof: unrolling the Bellman equation
This is the original proof. It differentiates the value function's recursive definition and unrolls.
Step 1. Differentiate . Start from and apply the product rule:
For the second term, , and neither nor depends on , so
Step 2. Write it as a recursion. Let and let be the policy induced transition kernel. Then
This has the same shape as a Bellman equation with playing the role of the reward. Note that the gradient of the transition kernel never appears: the recursion was derived from the value function's structure, and the only place enters explicitly is through inside .
Step 3. Unroll. Substitute the recursion into itself times:
The tail term vanishes as : is bounded (rewards are bounded and the score is assumed bounded), and . So
Step 4. Average over the start state. Taking on both sides,
Step 5. The log derivative trick. Since wherever ,
Substituting into Step 4 gives the theorem.
The moment to notice is Step 2. The state distribution depends on , and yet never had to be computed, because the derivative was taken of the value function's recursive definition rather than of the trajectory distribution. The unrolling then reconstructs the state distribution from the policy induced kernel without ever differentiating it.
Second proof: the likelihood ratio on trajectories
The same result from a different direction, which is the one most implementations are actually built on. Work with finite trajectories of length for clarity; the infinite horizon case follows by letting with the discount.
A trajectory has probability
and return . The objective is .
Step 1. Differentiate under the sum and apply the log derivative trick to the whole trajectory.
Step 2. The dynamics drop out of the log. Taking the log of turns the product into a sum, and only the policy factors depend on :
This is the second proof's version of the same miracle. The transition probabilities are inside the trajectory likelihood, but their gradient with respect to is zero, so they vanish from the score of the trajectory. You never need to know to compute the gradient. Hence
Step 3. Causality: an action cannot affect rewards that came before it. Expand and consider a single cross term with . Condition on everything up to and including ; the reward is then already determined, and
So every term pairing an action with an earlier reward has expectation zero and can be dropped:
where is the reward to go from time .
Step 4. Replace the return by its conditional expectation. Conditioning on , by definition, so
Finally, the sum over weighted by of an expectation over is times an expectation over , by the definition of the discounted visitation distribution, which recovers the theorem.
The two proofs establish the same identity by different routes, and each makes a different feature visible. The first shows that the gradient of the state distribution is never needed. The second shows that the gradient of the dynamics is never needed, and gives the trajectory form that is actually implemented.
REINFORCE
The trajectory form in Step 3 of the second proof is an expectation over trajectories generated by the policy, so it can be estimated by sampling. That estimator is REINFORCE (Williams 1992).
Algorithm. Repeat:
- Run the policy for one episode, collecting .
- For each , compute the reward to go .
- Update
Proposition. The REINFORCE update direction is an unbiased estimate of .
Proof. The update is a single sample of the random variable inside the expectation in Step 3, and Step 3 established that the expectation of that random variable is .
That is the whole justification, and it is worth appreciating how little it requires: a differentiable policy, the ability to run it, and nothing about the environment. The cost is variance. is a sum over the entire remaining trajectory and fluctuates with every future action and transition, so the estimator is unbiased but extremely noisy, and REINFORCE in its raw form needs many episodes per useful step.
Two remarks on the algorithm as written. In practice the factor in front of the score is usually dropped, which makes the estimator biased toward the undiscounted objective but reduces variance for later time steps; this is one of the places theory and practice quietly part ways. And the update is a gradient ascent, since is to be maximized.
Baselines
The most important variance reduction costs nothing in bias.
Lemma. For any function of the state alone,
Proof. The baseline does not depend on , so it factors out of the expectation:
This is the same computation as the causality step, and it means can be replaced by in the theorem, or by in REINFORCE, without changing the expectation.
Why it reduces variance. The gradient estimator at a state is times a scalar weight. If the weight is large and positive for every action, as it is when all returns are positive, every sampled action gets pushed up, and the useful signal, that some actions are better than others, is a small difference between large numbers. Subtracting a baseline centers the weights so that better than average actions are pushed up and worse than average actions are pushed down. Formally, the variance of is minimized over constant at a weighted average of , and the state value is close to that optimum and has a clear meaning.
With the weight becomes the advantage,
how much better action is than the policy's average behavior in . The advantage form of the theorem,
is the one every modern method starts from.
Actor critic
The baseline is not known, so estimate it. An actor critic method keeps two function approximators: the actor , updated by the policy gradient, and the critic , updated by Temporal difference learning. The advantage is then estimated by bootstrapping,
which is exactly the TD error of the critic. This replaces the Monte Carlo return with a one step bootstrapped estimate, trading the high variance of REINFORCE for the bias of an imperfect critic. Generalized advantage estimation (Schulman et al. 2016) interpolates between the two with a parameter , in the same way TD() interpolates between TD(0) and Monte Carlo.
The trust region and proximal methods, TRPO and PPO, keep the same gradient and constrain how far each update moves the policy, since the theorem is a statement about the local gradient and a large step can leave the region where it is informative. PPO is what I used for the verifiable rewards project in the lab, and its clipped objective is a direct descendant of the advantage form above.
Where this breaks
- The theorem is exact, but every estimator of it is noisy, and variance rather than bias is the practical limit. Baselines, bootstrapped critics and large batches are all responses to the same problem.
- The discounted state distribution is what the theory asks for, and almost no implementation samples from it. Implementations average over the states actually visited, which is the undiscounted distribution, and drop the weight. The gradient being followed is therefore not quite the gradient of . In practice this is fine; in theory it is a known gap (Nota and Thomas 2020).
- The score function must be well behaved. Policies that put probability near zero on actions produce huge scores, which is one reason entropy regularization and clipping are common.
- Nothing here guarantees convergence to a global optimum. Gradient ascent on a nonconvex finds a stationary point, and with function approximation there are results on convergence to local optima under step size conditions (Sutton et al. 1999; Konda and Tsitsiklis 2000) but not more.
References
- Williams. Simple Statistical Gradient-Following Algorithms for Connectionist Reinforcement Learning. Machine Learning, 1992.
- Sutton, McAllester, Singh, Mansour. Policy Gradient Methods for Reinforcement Learning with Function Approximation. NeurIPS, 1999.
- Konda, Tsitsiklis. Actor-Critic Algorithms. NeurIPS, 2000.
- Schulman, Levine, Moritz, Jordan, Abbeel. Trust Region Policy Optimization. ICML, 2015.
- Schulman, Moritz, Levine, Jordan, Abbeel. High-Dimensional Continuous Control Using Generalized Advantage Estimation. ICLR, 2016.
- Schulman, Wolski, Dhariwal, Radford, Klimov. Proximal Policy Optimization Algorithms. arXiv, 2017.
- Nota, Thomas. Is the Policy Gradient a Gradient? AAMAS, 2020.
- Sutton, Barto. Reinforcement Learning: An Introduction, 2nd ed. MIT Press, 2018, chapter 13.