Policy Gradient Methods › The Policy Gradient Theorem
Language AI HandbookAll chapters Policy Gradient Methods › The Policy Gradient TheoremOne section · Sections in this chapter4 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Here is the core challenge stated precisely: we need to compute so we can update our parameters via gradient ascent. But the expectation involves sampling from the policy itself, which depends on . This creates a circular dependency that seems difficult to resolve. The gradient depends on how changing affects which sequences get sampled, but sampling is a discrete, non-differentiable operation. We cannot simply backpropagate through the sampling process the way we would through a continuous neural network layer.
The policy gradient theorem provides an elegant solution to this problem. Rather than trying to differentiate through sampling directly, it rewrites the gradient in a form that we can estimate using samples from the current policy. The key insight is that we can express the gradient of an expectation in terms of an expectation of gradients that we can compute.
Deriving the Policy Gradient
Let's derive the policy gradient step by step. Starting with our objective written as an explicit sum:
We take the gradient with respect to . Since is produced by a frozen reward model and does not depend on , only the policy probability is differentiated:
This is still a sum over all sequences, which we cannot compute directly. We need to transform it into an expectation. The trick is to multiply and divide by , introducing a factor of 1 without changing the value:
Now we apply the log-derivative trick (also called the likelihood ratio trick or REINFORCE trick). This technique appears throughout machine learning and statistics whenever we need to compute gradients of expectations. The core identity is:
This follows directly from the chain rule of differentiation. Recall that , which rearranges to . The logarithm converts a multiplicative relationship into an additive one, which proves extremely useful when we deal with sequence probabilities that are products of many terms.
Substituting back:
A sum over all possible sequences weighted by their probabilities is exactly an expectation under the policy:
where:
- : gradient of the objective function with respect to parameters
- : gradient of the log-probability of the sequence, called the "score function"
- : the reward, acting as a scalar weight on the gradient
This is the policy gradient theorem for our setting. It tells us something remarkable: to compute the gradient of expected reward, we can sample sequences from our policy, compute the gradient of their log-probabilities, and multiply by their rewards. The score function points in the direction in parameter space that would increase the probability of sequence . Multiplying by the reward scales this direction: high rewards give large positive contributions, low rewards give small or negative contributions.
The gradient of expected reward can be written as an expectation:
where:
- : gradient of the objective function
- : expectation over sequences sampled from the current policy
- : gradient of the log-probability (the score function)
- : reward of the sampled sequence
This turns an intractable sum over all possible sequences into a tractable Monte Carlo estimate using samples from the policy.
The transformation from an intractable sum to a Monte Carlo estimate is the key practical insight. We cannot enumerate all possible sequences; for even modest vocabulary sizes and sequence lengths, this would involve more sequences than atoms in the universe. But we can sample sequences from our policy, and the policy gradient theorem tells us that averaging over these samples gives us an unbiased estimate of the true gradient.
The unbiasedness guarantee is important. It means that if we sample enough sequences, our gradient estimate will converge to the true gradient. We might not get the exact gradient from a single sample, but in expectation across many samples, we get the right answer. This is what allows reinforcement learning to work despite the discrete, non-differentiable nature of sampling.
