The policy gradient (PG) algorithm is a model-free, online, on-policy reinforcement learning method. A PG agent is a policy-based reinforcement learning agent which directly computes an optimal policy that maximizes the long-term reward.

For more information on the different types of reinforcement learning agents, see Reinforcement Learning Agents.

PG agents can be trained in environments with the following observation and action spaces.

Observation Space | Action Space |
---|---|

Discrete or continuous | Discrete or continuous |

During training, a PG agent:

Estimates probabilities of taking each action in the action space and randomly selects actions based on the probability distribution.

Completes a full training episode using the current policy before learning from the experience and updating the policy parameters.

PG agents represent the policy using an actor function approximator
*μ*(*S*). The actor takes observation
*S* and outputs the probabilities of taking each action in the action
space when in state *S*.

To reduce the variance during gradient estimation, PG agents can use a baseline value
function, which is estimated using a critic function approximator,
*V*(*S*). The critic computes the value function for a
given observation state.

For more information on creating actors and critics for function approximation, see Create Policy and Value Function Representations.

To create a PG agent:

Create an actor representation using an

`rlStochasticActorRepresentation`

object.If you are using a baseline function, create a critic using an

`rlValueRepresentation`

object.Specify agent options using the

`rlPGAgentOptions`

object.Create the agent using an

`rlPGAgent`

object.

PG agents use the REINFORCE (Monte-Carlo policy gradient) algorithm either with or
without a baseline. To configure the training algorithm, specify options using
`rlPGAgentOptions`

.

Initialize the actor

*μ*(*S*) with random parameter values*θ*._{μ}For each training episode, generate the episode experience by following actor policy

*μ*(*S*). To select an action, the actor generates probabilities for each action in the action space, then the agent randomly selects an action based on the probability distribution. The agent takes actions until it reaches the terminal state,*S*. The episode experience consists of the sequence:_{T}$${S}_{0},{A}_{0},{R}_{1},{S}_{1},\dots ,{S}_{T-1},{A}_{T-1},{R}_{T},{S}_{T}$$

Here,

*S*is a state observation,_{t}*A*is an action taken from that state,_{t+1}*S*is the next state, and_{t+1}*R*is the reward received for moving from_{t+1}*S*to_{t}*S*._{t+1}For each state in the episode sequence; that is, for

*t*= 1, 2, …,*T*-1, calculate the return*G*, which is the discounted future reward._{t}$${G}_{t}={\displaystyle \sum _{k=t}^{T}{\gamma}^{k-t}{R}_{k}}$$

Accumulate the gradients for the actor network by following the policy gradient to maximize the expected discounted reward. If the

`EntropyLossWeight`

option is greater than zero, then additional gradients are accumulated to minimize the entropy loss function.$$d{\theta}_{\mu}={\displaystyle \sum _{t=1}^{T-1}{G}_{t}{\nabla}_{{\theta}_{\mu}}\mathrm{ln}\mu \left({S}_{t}|{\theta}_{\mu}\right)}$$

Update the actor parameters by applying the gradients.

$${\theta}_{\mu}={\theta}_{\mu}+\alpha d{\theta}_{\mu}$$

Here,

*α*is the learning rate of the actor. Specify the learning rate when you create the actor representation by setting the`LearnRate`

option in the`rlRepresentationOptions`

object. For simplicity, this step shows a gradient update using basic stochastic gradient descent. The actual gradient update method depends on the optimizer specified using`rlRepresentationOptions`

.Repeat steps 2 through 5 for each training episode until training is complete.

Initialize the actor

*μ*(*S*) with random parameter values*θ*._{μ}Initialize the critic

*V*(*S*) with random parameter values*θ*._{Q}For each training episode, generate the episode experience by following actor policy

*μ*(*S*). The episode experience consists of the sequence:$${S}_{0},{A}_{0},{R}_{1},{S}_{1},\dots ,{S}_{T-1},{A}_{T-1},{R}_{T},{S}_{T}$$

For

*t*= 1, 2, …,*T*:Calculate the return

*G*, which is the discounted future reward._{t}$${G}_{t}={\displaystyle \sum _{k=t}^{T}{\gamma}^{k-t}{R}_{k}}$$

Compute the advantage function

*δ*using the baseline value function estimate from the critic._{t}$${\delta}_{t}={G}_{t}-V\left({S}_{t}|{\theta}_{V}\right)$$

Accumulate the gradients for the critic network.

$$d{\theta}_{V}={\displaystyle \sum _{t=1}^{T-1}{\delta}_{t}{\nabla}_{{\theta}_{V}}V\left({S}_{t}|{\theta}_{V}\right)}$$

Accumulate the gradients for the actor network. If the

`EntropyLossWeight`

option is greater than zero, then additional gradients are accumulated to minimize the entropy loss function.$$d{\theta}_{\mu}={\displaystyle \sum _{t=1}^{T-1}{\delta}_{t}{\nabla}_{{\theta}_{\mu}}\mathrm{ln}\mu \left({S}_{t}|{\theta}_{\mu}\right)}$$

Update the critic parameters

*θ*._{V}$${\theta}_{V}={\theta}_{V}+\beta d{\theta}_{V}$$

Here,

*β*is the learning rate of the critic. Specify the learning rate when you create the critic representation by setting the`LearnRate`

option in the`rlRepresentationOptions`

object.Update the actor parameters

*θ*._{μ}$${\theta}_{\mu}={\theta}_{\mu}+\alpha d{\theta}_{\mu}$$

Repeat steps 3 through 8 for each training episode until training is complete.

For simplicity, this actor and critic updates in this algorithm show a gradient update
using basic stochastic gradient descent. The actual gradient update method depends on the
optimizer specified using `rlRepresentationOptions`

.

[1] R. J. Williams, “Simple
statistical gradient-following algorithms for connectionist reinforcement learning,"
*Machine Learning*, vol. 8, issue 3-4, pp. 229-256,
1992.