# Advanced Policy Gradients

## A Policy Iteration View for PG

We learned about the idea of policy iteration when discussing [Actor Critic Algorithms](actor-critic). Here, let's compare and contrast policy gradient (PG) and policy iteration (PI). Recall that both have a policy-improvement step, but they differ in how to update the policy: PG takes gradient of the objective w.r.t. policy parameters, then re-parametrizes the policy, while PI doesn't need parameters: it induces a new policy by simply taking the $$\text{argmax}$$ action for the advantage value. Notice how _gradient_ of the objective can be written as the $$\log p$$_-_&#x77;eighted advantage under the current policy.

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqPfoLj_uH6X_wLIIV%2Flec9_1.png?alt=media\&token=bf408426-264b-4e01-b330-d8d8c9919443)

Using PG, we can define a new value called policy _improvement_ as the difference between the objective values under the current and the next gradient-updated policies, I'm omitting the derivations below, but you can read the derivation in the [lecture slides](http://rail.eecs.berkeley.edu/deeprlcourse/static/slides/lec-9.pdf) that shows this improvement value equals the expected advantage of the _old_ policy, expectation taken over the _new_ policy. My own way to interpret this is that, if you move the old objective $$J(\theta)$$ to the right hand side of the second equation below, we can see that the new objective comes from a new policy that increases/adds to the old objective value by re-adjusting the old policy's action choices based on its advantage values, these marginal values are added to the old objective sort of in a piece-wise fashion. So now if we look from a value-based perspective, the new gradient-updated policy can also be seen as induced from an $$\text{argmax}$$ procedure.

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqPuoI64AzRL8L_Xn4%2Flec9_2.png?alt=media\&token=adce09da-f523-41b5-9be2-62f99f6144b4)

Hence, we've established the equivalency between policy iteration and policy gradient, but there's one issue that prevents us from using this derived expected value to optimize our policy: the advantage is measured under the old policy \pi, but the expectation is taken over the new policy $$\pi'$$. Because the new policy is unknown, this prevents us from sampling batches under it to estimate the above expectation. Although, we do have the importance sampling trick that allows us to still use samples from the old/current policy, and add a distribution-ratio term to account for the policy mismatch between the old and the new:

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqQ1XSQ8z25PiIawnl%2Flec9_3.png?alt=media\&token=e3f04b34-d60d-45e6-82c8-a465ee429824)

Note there's a difference between marginal probability ($$p_{\theta'}(s)$$'s) ratio and policy distribution ratio ($$\pi_{\theta'}(a)$$). It might feel intuitive but actually requires some rigorous proof on why marginal probabilities of _one_ state under two different policies are close _if_  the two policy distributions are close. You can check out the famous (or infamous?) TRPO paper that gives a proof on bounding the probability mismatch with delta, a bound originally for distribution mismatch:

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqQcLf44XeVHWWNth-%2Flec9_4.png?alt=media\&token=9dc2c0ba-aa41-4ce0-8e6e-9c1e7f16ed7d)

The neat thing about this bound is that it applies to both a deterministic policy and a general policy distribution, only the notion of "closeness" between policies is slightly different:

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqQkNqCnj5-ZeYAlAs%2Flec9_5.png?alt=media\&token=173a3186-6fe9-45a4-9260-80cc1755aec3)

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqQlFgHKj658j9ANNW%2Flec9_6.png?alt=media\&token=230f5b6f-698b-44d9-a243-845160b35d43)

Now that we have a bound on state marginal probabilities, we can expand, lower-bound the objective value (more math omitted here), and thus increase the objective by maximizing its lower bound:

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqQsB33P7VaEE1YTqZ%2Flec9_7.png?alt=media\&token=1ce66d39-2fb8-475a-9348-6cc3d6d8b38b)

At this point, we can write policy gradient as a **constrained optimization problem**, so that you are essentially finding a new policy that's close enough to the current/old one so that the distribution mismatch can be ignored, but also increases the objective value:

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqR5Tlq_aQabvEBEKH%2Flec9_8.png?alt=media\&token=006b34a0-7797-442b-828d-35b6a3504e9d)

In above we bound the distribution mismatch by a constant delta, but we can also obtain a more convenient bound for state marginals using **KL Divergence** between policies, an easier-to-approximate value, and solve this optimization problem with a different constraint instead:

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqRG39_93M471eq0cY%2Flec9_9.png?alt=media\&token=44d1f175-f7b6-4287-bcb7-ea22ad38e152)

### How to optimize the objective?

#### 1. Dual Gradient Descent

As shown in the new objective above, dual gradient descent incorporates the constraint _into_ the objective by a weight $$\lambda$$, and alternatively update \lambda to make sure the constraint term is not violated too much.

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqRfXJF0bnO1q_z0Ea%2Flec9_10.png?alt=media\&token=6ef20152-743c-448b-886f-7ddbcd510590)

#### &#x20;2. Taylor expansion (on the objective)

Using first-order approximation, i.e. linearization, with Taylor expansion allows us to optimize a locally linear objective instead:

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqS2u2EDLj4RHOBLu_%2Flec9_11.png?alt=media\&token=8e0613e7-23f0-4116-b97c-27fa85f5d1d9)



The nice thing about evaluating the above A-bar value _at_ current parameter $$\theta$$ is that the importance weight is gone, so we get a normal PG:\


![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqS574GryQhVLkcfwH%2Flec9_12.png?alt=media\&token=49cf3e6d-188a-4e61-865a-767852bb3da7)

#### 3. Natural Policy Gradient

Taylor expansion on the constraint Here by introducing F, the _Fisher Information Matrix_, we can directly update the policy parameters.\


![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MSqN7pUQkghbwTm0aVq%2F-MSqSF8b-NRjBtU-pDWT%2Flec9_13.png?alt=media\&token=b87a6968-f3cc-46bc-8fcb-7a18cbfe73d7)

#### 4. Trust Region Policy Optimization (PPO)

Avoids natural policy gradient's Fisher matrix calculation with advanced numerical methods to be better adapted to deep learning.

#### 5. Proximal Policy Optimization (TRPO)

Uses the IS objective directly by adding regularization in gradient updates to stay close to old policy

All included screenshots credit to Lecture 9 [(slides)](http://rail.eecs.berkeley.edu/deeprlcourse/static/slides/lec-9.pdf)
