## 6.3 A2C Algorithm

Here we put the actor and critic together to form the Advantage Actor-Critic (A2C) algorithm, shown in Algorithm 6.1.

#### Algorithm 6.1 A2C algorithm

Each algorithm we have studied so far focused on learning one of two things: how to act (a policy) or how to evaluate actions (a critic). Actor-Critic algorithms learn both together. Aside from that, each element of the training loop should look familiar, since they have been part of the algorithms presented earlier in this book. Let’s go through Algorithm 6.1 step by step.

Lines 1–3: Set the values of important hyperparameters:

*β*,*α*,_{A}*α*._{C}*β*determines how much entropy regularization to apply (see below for more details).*α*and_{A}*α*are the learning rates used when optimizing each of the networks. They can be the same or different. These values vary depending on the RL problem we are trying to solve, and need to be determined empirically._{C}Line 4: Randomly initialize the parameters of both networks.

Line 6: Gather some data using the current policy network

*θ*. This algorithm shows episodic training, but this approach also applies to batch training._{A}Lines 8–10: For each (

*s*,_{t}*a*,_{t}*r*, ) experience in the episode, calculate , , and using the critic network._{t}Line 11: For each (

*s*,_{t}*a*,_{t}*r*, ) experience in the episode, optionally calculate the entropy of the current policy distribution_{t}*π*using the actor network. The role of entropy is discussed in detail in Box 6.2._{θA}Lines 13–14: Calculate the value loss. As with the DQN algorithms, we selected MSE

^{6}as the measure of distance between and . However, any other appropriate loss function, such as the Huber loss, could be used.

Lines 15–16: Calculate the policy loss. This has the same form as we saw in the REINFORCE algorithm with the addition of an optional entropy regularization term. Notice that we are minimizing the loss, but we want to maximize the policy gradient, hence the negative sign in front of log

*π*(_{θA}*a*|_{t}*s*) as in REINFORCE._{t}Lines 17–18: Update the critic parameters using the gradient of the value loss.

Lines 19–20: Update the actor parameters using the policy gradient.