Policy Gradient

Policy Gradient is one of the foundational concepts in reinforcement learning and is widely used, particularly when the problem can be made differentiable. This article draws primarily on the lecture slides of Hung-yi Lee at National Taiwan University.
Download link:1636956663-PPO (v3)

1 Policy of Actor

1.1 Basic Concepts

Reinforcement learning is a way to train a network using observations of the environment, the actions it takes, and the feedback it receives. It typically involves the following concepts:

  • Policy Denoted by \pi, it is a deep neural network with parameters \theta
  • Actor Denoted by \alpha, it represents the actions we can control. For example, in the game above, at each step we can choose one of three actions: left, right, or fire. This is what Policy Gradient learns
  • Environment The environment is something we cannot change; we can only observe it Observation
  • Reward Denoted by r, it is the feedback we receive from the environment after taking an action. In the game example above, it is the score we receive at that step.
  • State Denoted by s, it is the state we are currently in
  • Trajectory Denoted by \tau, it is a sequence of actions a and states s.
  • Episode One complete round of the game, from start to finish

In the example above, a complete trajectory is \tau=\left\{s_{1}, a_{1}, s_{2}, a_{2}, \cdots, s_{T}, a_{T}\right\}. Its probability is given below, since the next state depends only on the previous state and the action taken:

\begin{array}{l}p_{\theta}(\tau) \\=p\left(s_{1}\right) p_{\theta}\left(a_{1} \mid s_{1}\right) p\left(s_{2} \mid s_{1}, a_{1}\right) p_{\theta}\left(a_{2} \mid s_{2}\right) p\left(s_{3} \mid s_{2}, a_{2}\right) \cdots \\=p\left(s_{1}\right) \prod_{t=1}^{T} p_{\theta}\left(a_{t} \mid s_{t}\right) p\left(s_{t+1} \mid s_{t}, a_{t}\right)\end{array}\tag{1}

Clearly, p\left(s_{t+1} \mid s_{t}, a_{t}\right) is determined entirely by the game environment (the environment dynamics), so we cannot control it. What we can control is the policy term, p_{\theta}\left(a_{t} \mid s_{t}\right), which depends on our network parameters \theta.

1.2 Expected Reward

Given a trajectory \tau, we can calculate the total reward for a game: R(\tau)=\sum_{t=1}^{T} r_{t}. The goal of learning is to find the optimal actor that maximizes R(\tau).

Specifically, our objective is to maximize the expected reward over all possible trajectories \tau:

\bar{R}_{\theta}=\sum_{\tau} R(\tau) p_{\theta}(\tau)=E_{\tau \sim p_{\theta}(\tau)}[R(\tau)]\tag{2}

2 Policy Gradient

2.1 Derivation

To optimize \bar{R}_{\theta} above, we need to compute its gradient, \nabla \bar{R}_{\theta}, as follows:

\begin{aligned}\nabla \bar{R}_{\theta} &=\sum_{\tau} R(\tau) \nabla p_{\theta}(\tau)=\sum_{\tau} R(\tau) p_{\theta}(\tau) \frac{\nabla p_{\theta}(\tau)}{p_{\theta}(\tau)} \\&=\sum_{\tau} R(\tau) p_{\theta}(\tau) \nabla \log p_{\theta}(\tau) \\&=E_{\tau \sim p_{\theta}(\tau)}\left[R(\tau) \nabla \log p_{\theta}(\tau)\right] \approx \frac{1}{N} \sum_{n=1}^{N} R\left(\tau^{n}\right) \nabla \log p_{\theta}\left(\tau^{n}\right) \\&=\frac{1}{N} \sum_{n=1}^{N} \sum_{t=1}^{T_{n}} R\left(\tau^{n}\right) \nabla \log p_{\theta}\left(a_{t}^{n} \mid s_{t}^{n}\right)\end{aligned}\tag{3}
Here is how the derivation works:

The second equality uses the following identity:

\nabla f(x)= f(x) \nabla \log f(x)\tag{4}
The approximation sign indicates that we may not be able to enumerate every \tau to compute the expectation. Instead, we can draw N samples and use them to estimate it.

The final equality follows from equation (1), which gives us:

p_{\theta}(\tau) = p\left(s_{1}\right) \prod_{t=1}^{T} p_{\theta}\left(a_{t} \mid s_{t}\right) p\left(s_{t+1} \mid s_{t}, a_{t}\right)\tag{5}
As discussed earlier, p\left(s_{t+1} \mid s_{t}, a_{t}\right) is determined by the environment. We cannot control it, and it does not contribute to the gradient with respect to the policy parameters. So we only need to differentiate the policy term p_{\theta}\left(a_{t} \mid s_{t}\right), giving:

\nabla \log p_{\theta}\left(\tau^{n}\right)=\nabla \log \left( p\left(s_{1}\right) \prod_{t=1}^{T_n} p_{\theta}\left(a_{t} \mid s_{t}\right) p\left(s_{t+1} \mid s_{t}, a_{t}\right) \right)=\sum_{t=1}^{T_{n}} \nabla \log p_{\theta}\left(a_{t}^{n} \mid s_{t}^{n}\right)\tag{6}
Here’s how to interpret the final expression, \frac{1}{N} \sum_{n=1}^{N} \sum_{t=1}^{T_{n}} R\left(\tau^{n}\right) \nabla \log p_{\theta}\left(a_{t}^{n} \mid s_{t}^{n}\right): if taking action a_t^n in state s_t^n leads to a positive return R\left(\tau^{n}\right), increase the probability p_{\theta}\left(a_{t}^{n} \mid s_{t}^{n}\right); if the return is negative, decrease that probability.

2.2 Implementation

There are many ways to solve the optimization problem above. The most common is steepest descent, referred to here as Policy Gradient or Policy Optimization.

A typical workflow is as follows:

Draw a batch of samples using the box on the left, then feed those samples into the box on the right to compute the gradient and update the model. Repeat the process. Note that each batch of samples can be used for only one iteration.

2.3 A Supervised Learning Perspective

In supervised learning, we typically use cross-entropy as the loss function for classification problems. Cross-entropy is defined as follows:

Here, p(x) is the label indicating the correct class, and q(x) is the probability predicted by the network.

H(p, q)=-\sum_{x} p(x) \log q(x)\tag{7}

In Policy Gradient, we use the Reward as the label to define the loss function, as shown below:

L(\theta)=-\frac{1}{N} \sum_{\tau} R(\tau) \log \pi_{\theta}(\tau)\tag{8}


In the figure above, the top formula shows the cross-entropy loss for the original classification problem, where p(x)=1 for the correct class.

The bottom formula shows the Policy Gradient loss, where R replaces p(x).

References

[1] http://speech.ee.ntu.edu.tw/~tlkagk/courses_MLDS18.html
[2] https://zhuanlan.zhihu.com/p/107906954
[3] https://blog.csdn.net/cindy_1102/article/details/87905272
[4] https://blog.csdn.net/qq_36829091/article/details/83213707

Add a Comment

Your email address will not be published. Required fields are marked *