← All algorithms

Reinforcement Learning

RL: Policy Gradient Agent

2023-12-30

Value-based agents select actions indirectly using the action-value function q(s,a). When the state or action space is very large or continuous, evaluating every combination is impractical. Policy-based agents instead select actions directly according to a policy π(a|s), making them applicable to a wider range of problems.

In order to compare with policy-based Agent, let's review the value-based Agent. We learned that, when MDP was known, it was possible to determine the policy by repeating the policy through policy iteration and repeating the improvement and repetition of the policy.

If the MDP is unknown, a value-based agent improves the value function q(s, a) using Monte Carlo control or Temporal Difference control (SARSA or Q-learning) with a learning rate. In other words, it gradually improves what it has previously learned.

\(q(s,a)=q(s,a)+\alpha(G_t-q(s,a))\)

Here, using the relationship \(q(s, a)=E[G_t|S_t=s, A_t=a]\) (i.e., the expected value of Gt at given St=s, At=a), the current profit Gt represent the target return as the objective function for improving q(s, a).

In the case of a policy-based Agent, it is not possible to directly set the objective function for the policy function π(a|s). The reason is that to improve the policy function, a loss function must be defined, but there is no such thing as an expected policy function to calculate the error. How can we handle this? Answer is by modeling directly policy function and choose the action based on the policy function. (Forget the q(s,a)update, we don't need it any more). Now question is how to improve the policy function as the episodic learning progresses. The answer is following:

(a) Introduce a parameter θ into the policy function, making the policy as a function of θ.

\(\pi _\theta(a|s)=f(s,a,\theta)\)

For example, \(\pi _\theta(a|s)=\dfrac {1}{1+exp(-a\theta)}\) at any state.

(b) Now the state value function at any state, \(\hat {s}\) become the policy objective function J(θ) to be maximize w.r.t θ,

\(J(\theta)=v_{\pi_\theta}(\hat {s})=\sum _a^{\ }\pi _\theta(a|\hat {s})q(\hat{s},a)\)

and the objective can be expanded to

\(J(\theta)=\sum _a^{\ }\pi _\theta(a|\hat {s})P(s|\hat {s},a)v_{\pi _\theta}(s) \ \propto \ \sum _s^{\ }d(s)v_{\pi \theta}(s)=\sum _s^{\ }d(s)\sum _a^{\ }\pi _\theta(a|s)q(s,a) \)

where d(s) is the probabilistic distribution of state (s) from the environment, and independent from the action.

\(J(\theta) \propto \sum _a^{\ }\pi _\theta(a|s)q(s,a)\)

(c) Update the parameter θ in a way to maximize J

\(\theta'=\theta+\alpha \nabla_\theta J(\theta)\)

Here, ∇θJ(θ) is the Jacobian matrix of the policy action function J

\(\nabla_\theta J(\theta)=[\partial J/\partial \theta_1, \partial J/\partial \theta_2, \partial J/\partial \theta_3, \cdots , \partial J/\partial \theta_n]\)

Using the nature of the derivative of the log function, the Jacobian can be rewritten to

\(\nabla_\theta J(\theta)= \nabla_\theta \sum _a^{\ }\pi _\theta (a|s)q(s,a)=\sum _a^{\ }\pi_\theta (a|s) \nabla _\theta \log (\pi _\theta (a|s))q(s,a)\)

\(=E\left[ \nabla _\theta \log (\pi _\theta (a|s))q(s,a)\right]\)

Derivative of the log function

Since \(\dfrac{d(\log (y))}{dx}=\dfrac{1}{y}\dfrac{dy}{dx}\)

\(\dfrac{dy}{dx}=y\cdot d(\log (y))\)

In other words, ∇θJ(θ) is the weighted sum of the expected return where the weight is the derivative of the log(policy function) at state (s). This is called "Policy Gradient Theorem".

Using the derivate of the log, the ∇θJ(θ) can be expressed as

\(\nabla_\theta J(\theta)=E\begin{bmatrix}\dfrac {\nabla_\theta \pi_\theta (a|s_0)}{\pi_\theta (a|s)} q(s,a)\end{bmatrix}\).

and plugging into the parameter θ, we get the final equation.

\(\theta'=\theta+\alpha\dfrac{\nabla_\theta\pi _\theta(a|s_0)}{\pi _\theta(a|s)}q(s,a)\)

(1) 1 step MDP algorithm

If we use the immediate reward R(s,a) as the q(s,a), it becomes "1 step MDP algorithm."

\(\nabla_\theta J(\theta) = E\begin{bmatrix}\dfrac{\nabla_\theta\pi _\theta(a|s)}{\pi_\theta(a|s)}R(s,a)\end{bmatrix}\)​

(2) Reinforce algorithm

if we use the Episodic return Gt as the q(s,a) in the same way we did in the Monte Carlo Control, it becomes "Reinforce algorithm."

\(G_t=R_{t+1}+\gamma R_{t+2}+\gamma ^2R_{t+3}+\dots =\sum _{k=0}^{\infty} \gamma ^kR_{t+k+1}\)

\(\nabla_\theta J(\theta) = E\begin{bmatrix}\dfrac{\nabla_\theta\pi _\theta(a|s)}{\pi_\theta(a|s)}G_t\end{bmatrix}\)

​


In this blog, we demonstrate the Reinforce algorithm by using an one dimensional Grid World problem and a two dimensional Grid World problem.

(1) One dimensional Grid World problem

Below is showing 1x11 Grid World problem to solve. Each episode always starts at s5 and ends at s0 or s10. The immediate rewards only occur at s0 with -1 and s10 with +1. At each state, there are only two actions to take, left (a=1) or right (a=2)

​

(step 1) Expected return, Gt

\(G_t=R_{t+1}+\gamma R_{t+2}+\gamma ^2R_{t+3}+\dots =\sum _{k=0}^{\infty}\gamma ^kR_{t+k+1}\)

(step 2) Policy function, πθ(a|s)

The policy function which is the probability of the action 1 or 2 at each state is modeled as a Sigmoid function with the parameter aθ(s). For each state si (i = 0,1,2,3,4,5,6,7,8,9,10), there exist only two actions aj (j = 1,2 for left, right)

At the state si, the policy function for the action a1:

\(\pi _\theta(a_1|s_i)=\text{Sigmoid}(a_1\theta(s_i,a_1))=\dfrac{1}{1+\exp (-a_1\theta_i)}\)

At the state si, the policy function for the action a2:

\(\pi _\theta(a_2|s_i)=1-\pi _\theta(a_1|s_i)=\dfrac{\exp (-a_1\theta_i)}{1+\exp (-a_1\theta_i)}=\dfrac{1}{\exp (a_1\theta_i)+1}\)​

Therefore, we only need total 11 parameters of θ which means one per state from s0 to s10, because they are not a function of the action chosen.

(step 3) Policy Gradient, ∇θJ(θ)

By using the nature of the derivative of the Sigmoid and the chain rule, the policy gradient, ∇θJ(θ) at the state si are derived as follows:

\(\nabla_\theta\log (\pi _\theta(a_1|s_i))=\dfrac{1}{\pi _\theta(a_1|s_i)}\nabla_\theta(\pi _\theta(a_1|s_i))\)

\(=\dfrac{1}{\pi _\theta(a_1|s_i)}\pi _\theta(a_1|s_i)(1-\pi _\theta(a_1|s_i))a_1=a_1(1-\pi _\theta(a_1|s_i))\)

\(\nabla_\theta\log (\pi _\theta(a_2|s_i))=\dfrac{1}{\pi _\theta(a_2|s_i)}\nabla_\theta(\pi _\theta(a_2|s_i))\)

\(=\dfrac{1}{\pi _\theta(a_2|s_i)}\pi _\theta(a_2|s_i)(\pi _\theta(a_2|s_i)-1)a_1=a_1(\pi _\theta(a_2|s_i)-1)=-a_1\pi _\theta(a_1|s_i)\)

Derivative of the Sigmoid

Let \(z=a\theta\)

For \(f(\theta)=\dfrac{1}{1+\exp (-z)}\),

\(\dfrac{df}{d\theta}=\dfrac{df}{dz}\dfrac{dz}{d\theta}\)

\(\dfrac{df}{dz}=\dfrac{\exp (-z)}{(1+\exp (-z))^2}=\dfrac{1+\exp (-z)-1}{(1+\exp (-z))^2}=f(z)(1-f(z))\)

\(\dfrac{dz}{d\theta}=a\)

​\(\dfrac{df}{d\theta} = =af(z)(1-f(z))\)

For \(g(\theta)=\dfrac{1}{1+\exp (z)}\)

\(\dfrac{dg}{d\theta}=\dfrac{dg}{dz}\dfrac{dz}{d\theta}\)

\(\dfrac{dg}{dz}=\dfrac{-\exp (z)}{(1+\exp (z))^2}=\dfrac{1-\exp (z)-1}{(1+\exp (z))^2}=g(z)(g(z)-1)\)

\(\dfrac{dz}{d\theta}=a\)

\(\dfrac{dg}{d\theta} = ag(z)(g(z)-1)\)

​

(step 4) Update the parameter θ

Update the parameter θ to calculate the new probability of the actions (i.e. policy function πθ(a|s))

\(\theta^{t+1}(s_i,a_1)=\theta^t(s_i,a_1)+\alpha a_1(1-\pi _\theta(a_1|s_i))G_t\)

\(\theta^{t+1}(s_i,a_2)=\theta^t(s_i,a_2)+\alpha a_1(-\pi _\theta(a_1|s_i))G_t\)

(step 5) Continue to the steps toward the end of the episode and repeat step (1)~(4) to the new episode.

For the implementation in Excel, we need to setup the worksheet as follows.

The "πθ(a0|s)" is the corresponding probability for the action=1 at the state, s, which borrowed from the policy function, and the "action" is decided by IF (rand() < πθ(a0|s), 1, 2).

Following are the detail steps.

(step 1) Initialize all θ(si) = 0 (where the state index, i=0,10)

(step 2) Compute the policy function and the policy gradient by using the equation (step 2) and (step 3).

\(\pi _\theta(a_1|s_i)=\dfrac{1}{1+\exp (-a_1\theta_i)}\)

\(\pi _\theta(a_2|s_i)=1-\pi _\theta(a_1|s_i)\)

\(\nabla_\theta\log (\pi _\theta(a_1|s_i))=a_1(1-\pi _\theta(a_1|s_i))\)

\(\nabla_\theta\log (\pi _\theta(a_2|s_i))=-a_1\pi _\theta(a_1|s_i)\)

(step 3) Complete the episode starting at s0 using the current policy function πθ(aj|si) from (step 2)

(step 3.1) Generate a random number x by using the RAND() function to be used to decide the action from the policy function.

(step 3.2) Decide the next state, s', per the action, a

​

(step 3.3) Calculate the expected return, Gt

If the next state s' = the terminal state (i.e. s0 or s10), \(G_t=0\)

otherwise, \(G_t=R_{t+1}+\gamma G_{t+1}\)

(step 4) After the completion of the episode, revise the parameter θ(s,a) through the steps from the start of the episode to the end of the episode.

\(\theta^{t+1}(s_i,a_1)=\theta^t(s_i,a_1)+\alpha a_1(1-\pi _\theta(a_1|s_i))G_t\)

\(\theta^{t+1}(s_i,a_2)=\theta^t(s_i,a_2)+\alpha a_1(-\pi _\theta(a_1|s_i))G_t\)

(step 5) Repeat steps (2)~(4) to the next episode until the policy function get converged.

With the discount rate = 0.9, and the learning rate =0.1, the policy function of action 1 (left) at each state is shown below as a function of the Episode and step. The results agree with the logical guess which states that the left action should be taken less than 0.5 probability and closer to the state 10 the state is, less probability of the left action to take to get the net reward.

​

(2) Two dimensional 3x3 Grid World problem​

Episode always starts at the state s0 and ends at s8. The immediate reward at every state is -1 (so it is actually penalty) except the terminal state whose reward is zero as shown below:

​

(step 1) Expected return, Gt

\(G_t=R_{t+1}+\gamma R_{t+2}+\gamma ^2R_{t+3}+\dots =\sum _{k=0}^{\infty}\gamma ^kR_{t+k+1}\)

(step 2) Policy function, πθ(a|s)

The policy function, πθ(a|s) is a function of the parameter θ. The probability of each action follows the Softmax function because there are 4 choices of the action (East, West, North, and South).

For each state si (where i = 0,1,2,3,4,5,6,7) and the action aj는(where j = 1,2,3,4 representing (E,W,N,S)), the policy function becomes

\(\pi _\theta(a_j|s_i)=\dfrac{\exp (a_j\theta(s_i,a_j))}{\sum _k^{\ }\exp (a_k\theta(s_i,a_k))}\)

The parameter θ(s, a) is the function of the state, s and action, a. Since they are independent each other, we can assign one θ per state and action, so total 32 independent parameters for the 8 states (s0~s7) and the 4 actions (a1~a4) to handle the parameter function.

(step 3) Policy Gradient, ∇θJ(θ)

​The policy gradient came from the derivative of the Softmax function

​\(\nabla_\theta log(\pi_\theta(a_j|s_i)) = a_j(1-\pi_\theta(a_j|s_i))\)

The derivative of the Softmax function

The policy function πθ(ai|s) for the action, ai at any state, s becomes

\(\pi _\theta(a_i|s)=\dfrac{\exp (a_i\theta_i)}{\sum _{k=1}^4\exp (a_k\theta_k)}\)

Let \(z_i \equiv a_i \theta_i \), take a \(\log\) both sides to use \(\log(a/b) = \log(a)-\log(b)\).

\(J = \log (\pi _{\theta}(a_i|s))=\log \begin{pmatrix}\dfrac{\exp (z_i)}{\sum _k^{\ }\exp (z_k)}\end{pmatrix}=z_i-\log \left(\sum _k^{\ }\exp (z_k)\right)\)

Taking a derivative w.r.t. θm and using the Chain rule and the derivative of the log function,

\(\dfrac{dJ}{d\theta_m}=\sum _j^{\ }(\dfrac{dJ}{dz_j})(\dfrac{dz_j}{d\theta_m})\)

\(\dfrac {dJ}{dz_j} = \dfrac{d(z_i-\log (\sum _k^{\ }\exp (z_k)))}{dz_j}=\delta_{ij}-\pi _\theta(a_j|s)\)

\(\dfrac{dz_j}{d\theta_m}=a_m\delta_{jm}\)

where the \(\delta_{ij}\) and \(\delta_{jm}\) are the Dirac delta function such that \(\delta_{ij}\) = 1 if i=j, or 0,

the final result becomes

\(\dfrac{dJ}{d\theta_m} = \nabla_{\theta m}\log (\pi _\theta(a_i|s)) = \sum _j^{\ } \left[\delta_{ij}-\pi _\theta(a_j|s)\right] a_m\delta_{jm}\)

or \(\nabla_{\theta m}\log (\pi _\theta(a_i|s))=\left[1(i=j)-\pi _\theta(a_j|s)\right]a_j(j=m)\)

The equation means the derivative of the policy gradient only exists at the corresponding action (i=j=m) with the value

\(\nabla_{\theta i}\log (\pi _\theta(a_i|s))=a_i(1-\pi _\theta(a_i|s))\)

otherwise

\(\nabla_{\theta i}\log (\pi _\theta(a_i|s))=0\)

(step 4) Update the parameter θ

The parameter θ to revise the policy function πθ(a|s) get updated using the

\(\theta^{t+1}(s_i,a_j)=\theta^t(s_i,a_j)+\alpha a_j(1-\pi _\theta(a_j|s_i))G_t\)

(step 5) Continue to the steps toward the end of the episode and repeat step (1)~(4) to the new episode.

To implement the 3x3 Grid World in Excel, detail steps to follow are given below: 

(step 1) Initialize all θ(si,aj) = 0 (where the state index, i=1,8 and the action index, j=1,4)

(step 2) Compute the policy function and the policy gradient by using the equation (step 2) and (step 3).

\(\pi _\theta(a_j|s_i)=\dfrac{\exp (a_j\theta(s_i,a_j))}{\sum _k^{\ }\exp (a_k\theta(s_i,a_k))}\)

\(\nabla_\theta log(\pi_\theta(a_j|s_i)) = a_j(1-\pi_\theta(a_j|s_i))\)

(step 3) Complete the episode starting at s0 using the current policy function πθ(aj|si) from (step 2)

(step 3.1) Generate a random number x by using the RAND() function to be used to decide the action from the policy function.

(step 3.2) Decide the next state, s', per the action, a

​

(step 3.3) Calculate the expected return, Gt

If the next state s' = the terminal state (i.e. s8), \(G_t=0\)

otherwise, \(G_t=R_{t+1}+\gamma G_{t+1}\)

(step 4) After the completion of the episode, revise the parameter θ(s,a) through the steps from the start of the episode to the end of the episode.

\(\theta^{t+1}(s_i,a_j)=\theta^{t}(s_i,a_j)+\alpha a_j(1-\pi _\theta(a_j|s_i))G_t\)

(step 5) Repeat steps (2)~(4) to the next episode until the policy function get converged.

With the discount rate γ=1, and the learning rate α =0.01, the change of the parameter θ(s0,aj) and the policy function πθ(aj|s0) are shown below over 80 episodic learning.

The change of the parameter θ(s0,aj) for all 4 actions as a function of the episode

The change of the policy function πθ(aj|s0) for all 4 actions as a function of the episode

After the policy function πθ(a|s) gets converged, the probability is tabulated as a function of the state and the action. The table tells that which action at each state is the best action to choose to maximize the return.