Week 10 · Policy gradients and REINFORCE
Estimate a policy gradient and reduce its variance with a baseline.
← Week 9 · Subject contents · Week 11 →
Estimate a policy gradient and reduce its variance with a baseline.
Work through the problem
For a Bernoulli policy p=σ(θ), the log-probability derivative is a−p. In a one-step task, REINFORCE estimates ∂E[R]/∂θ with (R−b)(a−p). A constant baseline does not change the expectation because E[a−p]=0.
Let action 0 pay 1 and action 1 pay 3. At p=0.5, the true gradient is 2p(1−p)=0.5. Taking b=2 gives the same gradient on either action in this special case. Train with sampled actions and compare initial estimator variance with and without the baseline.
Run the Python lab
import numpy as np
rng = np.random.default_rng(17)
a = rng.binomial(1,.5,12000)
r = 1+2*a
for b in [0,2]:
g = (r-b)*(a-.5)
print(b,"mean",round(g.mean(),3),"variance",round(g.var(),3))
theta = 0.
for _ in range(600):
p = 1/(1+np.exp(-theta))
a = rng.binomial(1,p)
theta += .05*(1+2*a-2)*(a-p)
p = 1/(1+np.exp(-theta))
print("Learned probability of action 1:",round(p,3))
assert p>.9
Output from the lab
0 mean 0.484 variance 1.0
2 mean 0.5 variance 0.0
Learned probability of action 1: 0.982
Practice
- Can a baseline depend directly on the chosen action here?
Check your reasoning
- A general action-dependent baseline can bias the simple estimator. A state-dependent baseline is standard.