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 that 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 returns 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.
You can create a PG agent with default actor and critic representations based on the observation and action specifications from the environment. To do so, perform the following steps.
Create observation specifications for your environment. If you already have an
environment interface object, you can obtain these specifications using getObservationInfo
.
Create action specifications for your environment. If you already have an
environment interface object, you can obtain these specifications using getActionInfo
.
If needed, specify the number of neurons in each learnable layer or whether to use
an LSTM layer. To do so, create an agent initialization option object using rlAgentInitializationOptions
.
If needed, specify agent options using an rlPGAgentOptions
object.
Create the agent using an rlPGAgent
object.
Alternatively, you can create actor and critic representations and use these representations to create your agent. In this case, ensure that the input and output dimensions of the actor and critic representations match the corresponding action and observation specifications of the environment.
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.
For more information on creating actors and critics for function approximation, see Create Policy and Value Function Representations.
PG agents use the REINFORCE (Monte Carlo policy gradient) algorithm either with or
without a baseline. To configure the training algorithm, specify options using an
rlPGAgentOptions
object.
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 ST. The episode experience consists of the sequence
Here, St is a state observation, At+1 is an action taken from that state, St+1 is the next state, and Rt+1 is the reward received for moving from St to St+1.
For each state in the episode sequence, that is, for t = 1, 2, …, T-1, calculate the return Gt, which is the discounted future reward.
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.
Update the actor parameters by applying the gradients.
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 you specify 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 the actor policy μ(S). The episode experience consists of the sequence
For t = 1, 2, …, T:
Calculate the return Gt, which is the discounted future reward.
Compute the advantage function δt using the baseline value function estimate from the critic.
Accumulate the gradients for the critic network.
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.
Update the critic parameters θ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 θμ.
Repeat steps 3 through 8 for each training episode until training is complete.
For simplicity, the 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 you specify using rlRepresentationOptions
.
[1] Williams, Ronald J. “Simple Statistical Gradient-Following Algorithms for Connectionist Reinforcement Learning.” Machine Learning 8, no. 3–4 (May 1992): 229–56. https://doi.org/10.1007/BF00992696.