#
(Optional) the Policy Gradient Theorem

In this optional section where we’re going to study how we differentiate the objective function that we will use to approximate the policy gradient.

Let’s first recap our different formulas:

1. The Objective function

2. The probability of a trajectory (given that action comes fromπ θ\pi_\theta π θ​):

So we have: ∇θ J(θ)=∇θ∑τ P(τ;θ)R(τ)\nabla_\theta J(\theta) = \nabla_\theta \sum_{\tau}P(\tau;\theta)R(\tau)∇θ​J(θ)=∇θ​∑τ​P(τ;θ)R(τ)

We can rewrite the gradient of the sum as the sum of the gradient: =∑τ∇θ(P(τ;θ)R(τ))=∑τ∇θ P(τ;θ)R(τ) = \sum_{\tau} \nabla_\theta (P(\tau;\theta)R(\tau)) = \sum_{\tau} \nabla_\theta P(\tau;\theta)R(\tau) =∑τ​∇θ​(P(τ;θ)R(τ))=∑τ​∇θ​P(τ;θ)R(τ) asR(τ)R(\tau)R(τ) is not dependent onθ\theta θ

We then multiply every term in the sum byP(τ;θ)P(τ;θ)\frac{P(\tau;\theta)}{P(\tau;\theta)}P(τ;θ)P(τ;θ)​(which is possible since it’s = 1) =∑τ P(τ;θ)P(τ;θ)∇θ P(τ;θ)R(τ) = \sum_{\tau} \frac{P(\tau;\theta)}{P(\tau;\theta)}\nabla_\theta P(\tau;\theta)R(\tau) =∑τ​P(τ;θ)P(τ;θ)​∇θ​P(τ;θ)R(τ)

We can simplify further this sinceP(τ;θ)P(τ;θ)∇θ P(τ;θ)=P(τ;θ)∇θ P(τ;θ)P(τ;θ) \frac{P(\tau;\theta)}{P(\tau;\theta)}\nabla_\theta P(\tau;\theta) = P(\tau;\theta)\frac{\nabla_\theta P(\tau;\theta)}{P(\tau;\theta)} P(τ;θ)P(τ;θ)​∇θ​P(τ;θ)=P(τ;θ)P(τ;θ)∇θ​P(τ;θ)​.

Thus we can rewrite the sum as P(τ;θ)∇θ P(τ;θ)P(τ;θ)=∑τ P(τ;θ)∇θ P(τ;θ)P(τ;θ)R(τ) P(\tau;\theta)\frac{\nabla_\theta P(\tau;\theta)}{P(\tau;\theta)}= \sum_{\tau} P(\tau;\theta) \frac{\nabla_\theta P(\tau;\theta)}{P(\tau;\theta)}R(\tau) P(τ;θ)P(τ;θ)∇θ​P(τ;θ)​=∑τ​P(τ;θ)P(τ;θ)∇θ​P(τ;θ)​R(τ)

We can then use the derivative log trick (also called likelihood ratio trick or REINFORCE trick), a simple rule in calculus that implies that∇x l o g f(x)=∇x f(x)f(x) \nabla_x log f(x) = \frac{\nabla_x f(x)}{f(x)} ∇x​l o g f(x)=f(x)∇x​f(x)​

So given we have∇θ P(τ;θ)P(τ;θ)\frac{\nabla_\theta P(\tau;\theta)}{P(\tau;\theta)} P(τ;θ)∇θ​P(τ;θ)​ we transform it as∇θ l o g P(τ∣θ)\nabla_\theta log P(\tau|\theta) ∇θ​l o g P(τ∣θ)

So this is our likelihood policy gradient: ∇θ J(θ)=∑τ P(τ;θ)∇θ l o g P(τ;θ)R(τ) \nabla_\theta J(\theta) = \sum_{\tau} P(\tau;\theta) \nabla_\theta log P(\tau;\theta) R(\tau) ∇θ​J(θ)=∑τ​P(τ;θ)∇θ​l o g P(τ;θ)R(τ)

Thanks for this new formula, we can estimate the gradient using trajectory samples (we can approximate the likelihood ratio policy gradient with sample-based estimate if you prefer). ∇θ J(θ)=1 m∑i=1 m∇θ l o g P(τ(i);θ)R(τ(i))\nabla_\theta J(\theta) = \frac{1}{m} \sum^{m}_{i=1} \nabla_\theta log P(\tau^{(i)};\theta)R(\tau^{(i)})∇θ​J(θ)=m 1​∑i=1 m​∇θ​l o g P(τ(i);θ)R(τ(i)) where eachτ(i)\tau^{(i)}τ(i) is a sampled trajectory.

But we still have some mathematics work to do there: we need to simplify∇θ l o g P(τ∣θ) \nabla_\theta log P(\tau|\theta) ∇θ​l o g P(τ∣θ)

We know that: ∇θ l o g P(τ(i);θ)=∇θ l o g[μ(s 0)∏t=0 H P(s t+1(i)∣s t(i),a t(i))π θ(a t(i)∣s t(i))]\nabla_\theta log P(\tau^{(i)};\theta)= \nabla_\theta log[ \mu(s_0) \prod_{t=0}^{H} P(s_{t+1}^{(i)}|s_{t}^{(i)}, a_{t}^{(i)}) \pi_\theta(a_{t}^{(i)}|s_{t}^{(i)})]∇θ​l o g P(τ(i);θ)=∇θ​l o g[μ(s 0​)∏t=0 H​P(s t+1(i)​∣s t(i)​,a t(i)​)π θ​(a t(i)​∣s t(i)​)]

Whereμ(s 0)\mu(s_0)μ(s 0​) is the initial state distribution andP(s t+1(i)∣s t(i),a t(i)) P(s_{t+1}^{(i)}|s_{t}^{(i)}, a_{t}^{(i)}) P(s t+1(i)​∣s t(i)​,a t(i)​) is the state transition dynamics of the MDP.

We know that the log of a product is equal to the sum of the logs: ∇θ l o g P(τ(i);θ)=∇θ[l o g μ(s 0)+∑t=0 H l o g P(s t+1(i)∣s t(i)a t(i))+∑t=0 H l o g π θ(a t(i)∣s t(i))]\nabla_\theta log P(\tau^{(i)};\theta)= \nabla_\theta \left[log \mu(s_0) + \sum\limits_{t=0}^{H}log P(s_{t+1}^{(i)}|s_{t}^{(i)} a_{t}^{(i)}) + \sum\limits_{t=0}^{H}log \pi_\theta(a_{t}^{(i)}|s_{t}^{(i)})\right] ∇θ​l o g P(τ(i);θ)=∇θ​[l o gμ(s 0​)+t=0∑H​l o g P(s t+1(i)​∣s t(i)​a t(i)​)+t=0∑H​l o g π θ​(a t(i)​∣s t(i)​)]

We also know that the gradient of the sum is equal to the sum of gradient: ∇θ l o g P(τ(i);θ)=∇θ l o g μ(s 0)+∇θ∑t=0 H l o g P(s t+1(i)∣s t(i)a t(i))+∇θ∑t=0 H l o g π θ(a t(i)∣s t(i)) \nabla_\theta log P(\tau^{(i)};\theta)=\nabla_\theta log\mu(s_0) + \nabla_\theta \sum\limits_{t=0}^{H} log P(s_{t+1}^{(i)}|s_{t}^{(i)} a_{t}^{(i)}) + \nabla_\theta \sum\limits_{t=0}^{H} log \pi_\theta(a_{t}^{(i)}|s_{t}^{(i)}) ∇θ​l o g P(τ(i);θ)=∇θ​l o gμ(s 0​)+∇θ​t=0∑H​l o g P(s t+1(i)​∣s t(i)​a t(i)​)+∇θ​t=0∑H​l o g π θ​(a t(i)​∣s t(i)​)

Since neither initial state distribution or state transition dynamics of the MDP are dependent ofθ\theta θ, the derivate of both terms are 0. So we can remove them:

Since:∇θ∑t=0 H l o g P(s t+1(i)∣s t(i)a t(i))=0\nabla_\theta \sum_{t=0}^{H} log P(s_{t+1}^{(i)}|s_{t}^{(i)} a_{t}^{(i)}) = 0 ∇θ​∑t=0 H​l o g P(s t+1(i)​∣s t(i)​a t(i)​)=0 and∇θ μ(s 0)=0 \nabla_\theta \mu(s_0) = 0∇θ​μ(s 0​)=0 ∇θ l o g P(τ(i);θ)=∇θ∑t=0 H l o g π θ(a t(i)∣s t(i))\nabla_\theta log P(\tau^{(i)};\theta) = \nabla_\theta \sum_{t=0}^{H} log \pi_\theta(a_{t}^{(i)}|s_{t}^{(i)})∇θ​l o g P(τ(i);θ)=∇θ​∑t=0 H​l o g π θ​(a t(i)​∣s t(i)​)

We can rewrite the gradient of the sum as the sum of gradients: ∇θ l o g P(τ(i);θ)=∑t=0 H∇θ l o g π θ(a t(i)∣s t(i)) \nabla_\theta log P(\tau^{(i)};\theta)= \sum_{t=0}^{H} \nabla_\theta log \pi_\theta(a_{t}^{(i)}|s_{t}^{(i)}) ∇θ​l o g P(τ(i);θ)=∑t=0 H​∇θ​l o g π θ​(a t(i)​∣s t(i)​)

So, the final formula for estimating the policy gradient is: ∇θ J(θ)=g^=1 m∑i=1 m∑t=0 H∇θ log⁡π θ(a t(i)∣s t(i))R(τ(i)) \nabla_{\theta} J(\theta) = \hat{g} = \frac{1}{m} \sum^{m}_{i=1} \sum^{H}_{t=0} \nabla_\theta \log \pi_\theta(a^{(i)}_{t} | s_{t}^{(i)})R(\tau^{(i)}) ∇θ​J(θ)=g^​=m 1​∑i=1 m​∑t=0 H​∇θ​log π θ​(a t(i)​∣s t(i)​)R(τ(i))

< > Update on GitHub