Post · 4 min read· pp. 92–94

Unbiased Estimates in Policy Gradient

Introduction

In reinforcement learning policy gradient methods, the goal is usually to maximize E[Rt]\mathbb{E}[R_t], the expected value of return RR at time step tt, where Rt=∑k=0∞γkrt+kR_t = \sum_{k=0}^{\infty} \gamma^k r_{t+k}.

We have parameterized the policy π(a∣s;θ)\pi(a|s; \theta) and update the parameter θ\theta so that E[Rt]\mathbb{E}[R_t] is maximized.

Standard REINFORCE algorithm updates policy in the direction ∇θRtlog⁡π(at∣st;θ)\nabla_{\theta} R_t \log \pi (a_t|s_t; \theta). That is to say, we have a loss function of $ -\log R_t \pi (a_t|s_t; \theta)$ and we are minimizing the loss.

REINFORCE

But in order to mamixize E[Rt]\mathbb{E}[R_t], why do we have to calculate the the derivatives for Rtlog⁡π(at∣st;θ)R_t \log \pi (a_t|s_t; \theta)? It turns out that it is because ∇θRtlog⁡π(at∣st;θ)\nabla_{\theta} R_t \log \pi (a_t|s_t; \theta) is an unbiased estimate of ∇θE[Rt]\nabla_{\theta} \mathbb{E}[R_t], i.e., E[Rt∇θlog⁡π(at∣st;θ)]=∇θE[Rt]\mathbb{E}[R_t \nabla_{\theta} \log \pi (a_t|s_t; \theta)] = \nabla_{\theta} \mathbb{E}[R_t].

Proof

We use the definition of expected value, because random variable RtR_t is dependent on ata_t and it follows distribution π(at∣st;θ)\pi (a_t|s_t; \theta).

∇θE[Rt]=∇θ∫Rtπ(at∣st;θ)d(at)\nabla_{\theta} \mathbb{E}[R_t] = \nabla_{\theta} \int R_t \pi (a_t|s_t; \theta) d(a_t)

Then we apply a special case of Leibniz Integral Rule, where

ddx∫abf(x,t)dt=∫ab∂∂xf(x,t)dt\frac{d}{dx} \int_{a}^{b} f(x,t) dt = \int_{a}^{b} \frac{\partial}{\partial x} f(x,t) dt

We then have

∇θ∫Rtπ(at∣st;θ)d(at)=∫∇θ[Rtπ(at∣st;θ)]d(at)\nabla_{\theta} \int R_t \pi (a_t|s_t; \theta) d(a_t) = \int \nabla_{\theta} [ R_t \pi (a_t|s_t; \theta) ] d(a_t)

Because RtR_t is dependent on ata_t but not dependent on θ\theta (Is it?). We have

∫∇θ[Rtπ(at∣st;θ)]d(at)=∫Rt∇θπ(at∣st;θ)d(at)\int \nabla_{\theta} [ R_t \pi (a_t|s_t; \theta) ] d(a_t) = \int R_t \nabla_{\theta} \pi (a_t|s_t; \theta) d(a_t)

We apply an identity trick here

∫Rt∇θπ(at∣st;θ)d(at)=∫Rtπ(at∣st;θ)∇θπ(at∣st;θ)π(at∣st;θ)d(at)=∫Rtπ(at∣st;θ)∇θlog⁡π(at∣st;θ)d(at)\begin{aligned} \int R_t \nabla_{\theta} \pi (a_t|s_t; \theta) d(a_t) &= \int R_t \pi (a_t|s_t; \theta) \frac{\nabla_{\theta} \pi (a_t|s_t; \theta)}{\pi (a_t|s_t; \theta)} d(a_t) \\ &= \int R_t \pi (a_t|s_t; \theta) \nabla_{\theta} \log \pi (a_t|s_t; \theta) d(a_t) \end{aligned}

Because the random variable Rt∇θlog⁡π(at∣st;θ)R_t \nabla_{\theta} \log \pi (a_t|s_t; \theta) also follows distribution π(at∣st;θ)\pi (a_t|s_t; \theta), we have

∫Rtπ(at∣st;θ)∇θlog⁡π(at∣st;θ)d(at)=E[Rt∇θlog⁡π(at∣st;θ)]\int R_t \pi (a_t|s_t; \theta) \nabla_{\theta} \log \pi (a_t|s_t; \theta) d(a_t) = \mathbb{E}[R_t \nabla_{\theta} \log \pi (a_t|s_t; \theta)]

Therefore,

∇θE[Rt]=E[Rt∇θlog⁡π(at∣st;θ)]\nabla_{\theta} \mathbb{E}[R_t] = \mathbb{E}[R_t \nabla_{\theta} \log \pi (a_t|s_t; \theta)]

This concludes the proof.

Generalization

More generally it is not hard to see that,

∇θE[f(z)]=E[f(z)∇θlog⁡p(z;θ)]\nabla_{\theta} \mathbb{E}[f(z)] = \mathbb{E}[f(z) \nabla_{\theta} \log p (z; \theta)]

Extension

We also use REINFORCE with baseline to reduce the variance of the estimate of ∇θE[Rt]\nabla_{\theta} \mathbb{E}[R_t] while keeping the estimate unbiased.

REINFORCE with Baseline

In stead of using ∇θRtlog⁡π(at∣st;θ)\nabla_{\theta} R_t \log \pi (a_t|s_t; \theta) as an unbiased estimate of ∇θE[Rt]\nabla_{\theta} \mathbb{E}[R_t], we use ∇θ(Rt−v(st))log⁡π(at∣st;θ)\nabla_{\theta} (R_t - v(s_t)) \log \pi (a_t|s_t; \theta) as long as v(st)v(s_t) does not dependent on aa.

To see why E[(Rt−v(st))∇θlog⁡π(at∣st;θ)]=∇θE[Rt]\mathbb{E}[(R_t - v(s_t)) \nabla_{\theta} \log \pi (a_t|s_t; \theta)] = \nabla_{\theta} \mathbb{E}[R_t], we have

∇θE[Rt]=∇θ∫Rtπ(at∣st;θ)d(at)=∇θ∫Rtπ(at∣st;θ)d(at)−∇θ∫v(st)π(at∣st;θ)d(at)\nabla_{\theta} \mathbb{E}[R_t] = \nabla_{\theta} \int R_t \pi (a_t|s_t; \theta) d(a_t) = \nabla_{\theta} \int R_t \pi (a_t|s_t; \theta) d(a_t) - \nabla_{\theta} \int v(s_t) \pi (a_t|s_t; \theta) d(a_t)

Where ∇θ∫v(st)π(at∣st;θ)d(at)=0\nabla_{\theta} \int v(s_t) \pi (a_t|s_t; \theta) d(a_t) = 0.

Because vv use different parameters other than θ\theta and it is not dependent on aa,

∇θ∫v(st)π(at∣st;θ)d(at)=v(st)∇θ∫π(at∣st;θ)d(at)=v(st)∇θ1=v(st)×0=0\nabla_{\theta} \int v(s_t) \pi (a_t|s_t; \theta) d(a_t) = v(s_t) \nabla_{\theta} \int \pi (a_t|s_t; \theta) d(a_t) = v(s_t) \nabla_{\theta} 1 = v(s_t) \times 0 = 0

Quick Proof to Leibniz Integral Rule

ddx∫abf(x,t)dt=ddx∫ab∫x∂f(x,t)∂xdxdt=ddx∫x∫ab∂f(x,t)∂xdtdx=ddx∫x(∫ab∂f(x,t)∂xdt)dx=∫ab∂f(x,t)∂xdt\begin{aligned} \frac{d}{dx} \int_{a}^{b} f(x,t) dt &= \frac{d}{dx} \int_{a}^{b} \int_{\mathbf{x}} \frac{\partial f(x,t)}{\partial x} dx dt\\ &= \frac{d}{dx} \int_{\mathbf{x}} \int_{a}^{b} \frac{\partial f(x,t)}{\partial x} dt dx\\ &= \frac{d}{dx} \int_{\mathbf{x}} (\int_{a}^{b} \frac{\partial f(x,t)}{\partial x} dt) dx\\ &= \int_{a}^{b} \frac{\partial f(x,t)}{\partial x} dt\\ \end{aligned}

Thanks to the guidance from my friend Guotu Li on this.

Final Remarks

There is a statement or an assumption we made during the proof, that is RtR_t is not dependent on θ\theta. It is because in this case RtR_t is a scala value instead of an output from the policy neural network.