Baselines & Variance Reduction
Optimal baselines, control variates and the mirage of action-dependent baselines.
A baseline never changes the expected policy gradient, so it can be chosen purely to cut variance. Which one cuts it most? Not the value function: in a two-action example the value baseline does 64 times worse than no baseline at all. This lesson takes the estimator's variance apart, derives the optimal baseline, and follows action-dependent baselines from their promising results to the discovery that the gains were a mirage.
What you’ll learn
- Decompose the variance of the policy-gradient estimator into state, action and trajectory terms, and say which of them a baseline can reduce.
- Derive the variance-optimal baseline and the exact excess variance of any other baseline.
- Explain why action-dependent baselines need a correction term, how Q-Prop and Stein control variates supply it, and what Tucker et al. found when they re-examined them.
- Use reward-to-go, discounting and common random numbers to reduce variance, and state what each costs.
Where the variance comes from
Write one sample of the gradient at a visited state as , where is the score, an unbiased but noisy estimate of such as the return, and a baseline. Three sources of randomness enter: which state is visited, which action is drawn there, and how the rest of the trajectory unfolds. Conditioning on and then on , the law of total variance splits the total variance (the trace of the covariance) along exactly these lines, as in Tucker et al. (2018):
Only depends on the baseline. A state baseline shrinks it but cannot remove it; an action-dependent one could remove it entirely; and no baseline touches , the noise of future rewards, or , the noise of which states happen to be visited. Other tools attack those.
| Tool | What it reduces | Cost |
|---|---|---|
| State baseline | part of | none in bias; a value function to fit |
| Action-dependent control variate | all of , in principle | a correction term that is easy to get wrong |
| Reward-to-go | noise from rewards earned before the action | none |
| Critic, -step and λ-returns, GAE | bias when the critic is wrong | |
| Discount as a knob | , by shortening the effective horizon | bias with respect to the undiscounted objective |
| Bigger batches; common random numbers | everything; noise in comparisons between policies | compute; control over the simulator's randomness |
On long-horizon locomotion tasks with discounted Monte Carlo returns, Tucker et al. found one term dominating the variance. Which, and what follows for action-dependent baselines?
- , so action-dependent baselines should help a lot
- , so even a perfect action-dependent baseline, which can only remove , would barely help
- , so only running more environments in parallel can help
- All three terms were about equal
Show the answer
B. , so even a perfect action-dependent baseline, which can only remove , would barely help
Over hundreds of steps the randomness of the rest of the trajectory swamps everything else, and baselines cannot touch it. With GAE's shorter effective horizon shrinks and an oracle action-dependent baseline could help, but the learned ones they tested did not reduce variance below a learned state baseline.
The optimal baseline
The mean does not depend on , because . Minimizing the variance therefore means minimizing the second moment, a quadratic in :
Complete the square around :
So averages the action values with weights , where uses alone. Actions with large scores, typically the rare ones, count for more.
Take two actions with and a logistic parameterization, so and . The weights are and , proportional to and :
The optimal baseline is the value of the opposite policy, and with it both actions give the same estimate, the exact gradient: zero variance.
Now let , and , so , and the gradient is . Without a baseline the samples are (probability 0.9) and (probability 0.1), with variance . With they are and :
Here the value baseline is 64 times worse than none: the rare action has a large score, and centering at gives it a large advantage, , so its rare samples are huge. Shift both values up by 10 and the picture flips: no baseline now has variance , while both baselines are unaffected. protects against offsets; also accounts for which samples the score amplifies.
Same two actions with , and . What is the variance-optimal baseline ?
Show the answer
2.6
, far from . With it every sample equals the exact gradient .
The history of this result is a lesson in itself. Weaver and Tao (2001) showed that for the GPOMDP estimator, in the limit where its discount-induced bias vanishes, the variance-minimizing constant reward baseline is the long-run average reward. Greensmith, Bartlett and Baxter (2004) treated baselines and critics as additive control variates, computed optimal baselines, and showed that the popular choices, the average reward as a baseline and even the true value function as a critic, can be suboptimal. Practice still uses a learned : it is easy to fit by regression, unbiased whatever its errors, and usually close enough. The identity above prices the gap at each state: .
Action-dependent baselines, and a mirage
A baseline that also depends on the action, , could remove entirely: take . But it is no longer free, because is not zero in general. The remedy is to add that expectation back, computed analytically or with low variance:
Q-Prop (Gu, Lillicrap, Ghahramani, Turner & Levine, 2017) uses a first-order Taylor expansion of an off-policy critic around the policy's mean action. For a Gaussian policy with mean , the correction term is then a deterministic-policy-gradient term, , so the method blends a likelihood-ratio gradient with a critic's pathwise gradient. Stein control variates (Liu et al., 2018) allow general nonlinear and compute the correction with the reparameterization trick, which they connect to Stein's identity. Wu et al. (2018) proposed action-dependent baselines that factorize over the dimensions of the action. All reported large gains in sample efficiency on MuJoCo tasks.
Tucker, Bhupatiraju, Gu, Turner, Ghahramani and Levine (2018) then measured the three variance terms on the same benchmarks. With discounted Monte Carlo returns dominated, so even an optimal action-dependent baseline could barely help. With GAE an oracle could help, but the learned state–action baselines reduced variance no more than learned state baselines. Reviewing the released code, they traced the reported gains to implementation choices that departed from the published methods:
- Q-Prop's code standardized the learning signal (subtracting its batch mean and dividing by its batch standard deviation) but left the correction term unnormalized, an adaptive and biased trade-off. Made unbiased, it did not outperform TRPO.
- In the Stein control variate experiments, the value function behind the GAE advantages was poorly fit, so even a plain state-dependent baseline would have cut the variance about as much as the action-dependent one: that, not action dependence, explains the very large reported reductions. The baseline was also fitted on the same batch used for the gradient, which biases the estimate. Unbiased state and state–action baselines then performed about the same.
- The code of a related method, LAX (Grathwohl et al., 2018), measured the variance of an estimator that neither compared method used, and a bug gave LAX a different advantage estimator; once corrected, it showed no improvement on the task examined.
An implementation subtracts a learned from the return but omits the term . What is the effect?
- None, since baselines never bias the gradient
- Lower variance and no bias, as long as fits well
- A bias of , which vanishes when ignores the action but not in general
- The estimate becomes the natural gradient
Show the answer
C. A bias of , which vanishes when ignores the action but not in general
The first term has mean , and . For a state baseline this is zero; for an action-dependent one it is generally not. A well-fit makes it worse, not better: subtracting the whole signal leaves almost nothing.
More ways to cut variance
Reward-to-go is a baseline. Replacing by subtracts the past rewards , which are fixed before is drawn. It costs nothing and usually helps, since past rewards are pure noise for the decision at step .
The discount is a dial. With an undiscounted or average-reward objective, computing returns with shortens the effective horizon to about steps, which cuts at the price of bias. Baxter and Bartlett (2001) made this precise for their GPOMDP estimator of the average-reward gradient: its discount trades bias against variance, and the right setting depends on the mixing time of the controlled chain, since the bias is small only once is long compared with it. GAE's is a second, finer dial of the same kind (Generalized Advantage Estimation).
Common random numbers. When comparing two policies, or estimating a gradient by finite differences, use the same random numbers for both. PEGASUS (Ng & Jordan, 2000) fixes the simulator's random seeds in advance, which turns each policy's estimated return into a deterministic function of its parameters: differences between nearby policies then reflect the policies, not the luck of the draw, and standard optimizers apply. It requires a simulator whose randomness you control, and it optimizes the average over a fixed set of scenarios, an estimate of the true objective. Mirrored sampling in evolution strategies, which evaluates and together, is a close relative (antithetic variates).
Bigger batches. Averaging independent estimates divides the variance by , the bluntest and most reliable tool, and the reason policy-gradient methods scale with parallel actors (Distributed Actor–Critic: A3C & IMPALA).
Key takeaways
- The variance of the policy-gradient estimator splits into state, action and trajectory terms; baselines act only on the action term, so they cannot remove the noise of future rewards or of which states are visited.
- The optimal constant baseline is and any other costs . With two actions, is the value of the opposite policy, and can even be worse than no baseline.
- Action-dependent baselines need the correction term ; on common benchmarks their reported gains were traced to biased implementations (Tucker et al., 2018).
- Reward-to-go is a free baseline; discounting and λ trade bias for less trajectory noise; common random numbers make comparisons between policies nearly noise-free.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
The variance-optimal constant baseline at a state averages the action values with which weights?
- The probabilities , so that
- The probabilities times the squared norms of the scores,
- The advantages
- Equal weights for every action
Show the answer
B. The probabilities times the squared norms of the scores,
Minimizing the quadratic in gives the -weighted average. Rare actions usually have large scores, so they count for more than in ; with two actions, is the value of the opposite policy.
Two actions, logistic parameterization, , and . What is the variance-optimal baseline ?
- 3
- 5
- 7
- 10
Show the answer
C. 7
, the value of the opposite policy, while . With both actions give the exact gradient .
What did Tucker et al. (2018) conclude about action-dependent baselines on standard continuous-control benchmarks?
- They reduce variance by orders of magnitude, as originally reported
- They are biased however they are implemented
- They are equivalent to following the natural gradient
- Learned state–action baselines reduced variance no more than state baselines, and the reported gains came from implementation choices that departed from the published, unbiased methods
Show the answer
D. Learned state–action baselines reduced variance no more than state baselines, and the reported gains came from implementation choices that departed from the published, unbiased methods
Their variance decomposition showed the action term was small next to the trajectory term, and their review of the released code found deviations, such as a normalization applied to only part of Q-Prop's estimator, that made the methods biased. With the deviations removed, the advantage disappeared.
Why does PEGASUS fix the simulator's random numbers before optimizing a policy?
- So that the estimated return becomes a deterministic function of the parameters, and differences between nearby policies are not swamped by sampling noise
- To remove the need for a simulator
- To make the learned policy deterministic
- To make the estimate exactly equal to the true expected return
Show the answer
A. So that the estimated return becomes a deterministic function of the parameters, and differences between nearby policies are not swamped by sampling noise
With common random numbers, two policies are scored on the same scenarios, so their difference reflects the policies rather than luck, and ordinary optimizers can work on a deterministic surrogate (gradient methods need it to be differentiable, which a discontinuous reward such as "1 inside the goal region" breaks). The price is that the surrogate averages over a finite set of scenarios: an estimate of the objective that becomes accurate only with enough of them.
Further reading
- L. Weaver & N. Tao, "The optimal reward baseline for gradient-based reinforcement learning," UAI (2001).
- E. Greensmith, P. L. Bartlett & J. Baxter, "Variance reduction techniques for gradient estimates in reinforcement learning," JMLR 5, 1471–1530 (2004).
- S. Gu, T. Lillicrap, Z. Ghahramani, R. E. Turner & S. Levine, "Q-Prop: Sample-efficient policy gradient with an off-policy critic," ICLR (2017).
- G. Tucker, S. Bhupatiraju, S. Gu, R. E. Turner, Z. Ghahramani & S. Levine, "The mirage of action-dependent baselines in reinforcement learning," ICML (2018).
- A. Y. Ng & M. I. Jordan, "PEGASUS: A policy search method for large MDPs and POMDPs," UAI (2000).