class_docstr: MPO learner.
class_docstr_tok: ["MPO","learner","."]
code: def _step(self) -> types.Nest:
      # Update target network.
      online_policy_variables = self._policy_network.variables
      target_policy_variables = self._target_policy_network.variables
      online_critic_variables = (
          *self._observation_network.variables,
          *self._critic_network.variables,
      )
      target_critic_variables = (
          *self._target_observation_network.variables,
          *self._target_critic_network.variables,
      )
  
      # Make online policy -> target policy network update ops.
      if (
          tf.math.mod(
              self._num_steps, self._target_policy_update_period
          )
          == 0
      ):
          for src, dest in zip(
              online_policy_variables, target_policy_variables
          ):
              dest.assign(src)
      # Make online critic -> target critic network update ops.
      if (
          tf.math.mod(
              self._num_steps, self._target_critic_update_period
          )
          == 0
      ):
          for src, dest in zip(
              online_critic_variables, target_critic_variables
          ):
              dest.assign(src)
  
      # Increment number of learner steps for periodic update bookkeeping.
      self._num_steps.assign_add(1)
  
      # Get next batch of data.
      inputs = next(self._iterator)
  
      # Get data from replay (dropping extras if any). Note there is no
      # extra data here because we do not insert any into Reverb.
      transitions: types.Transition = inputs.data
  
      # Cast the additional discount to match the environment discount dtype.
      discount = tf.cast(
          self._discount, dtype=transitions.discount.dtype
      )
  
      with tf.GradientTape(persistent=True) as tape:
          # Maybe transform the observation before feeding into policy and critic.
          # Transforming the observations this way at the start of the learning
          # step effectively means that the policy and critic share observation
          # network weights.
          o_tm1 = self._observation_network(transitions.observation)
          # This stop_gradient prevents gradients to propagate into the target
          # observation network. In addition, since the online policy network is
          # evaluated at o_t, this also means the policy loss does not influence
          # the observation network training.
          o_t = tf.stop_gradient(
              self._target_observation_network(
                  transitions.next_observation
              )
          )
  
          # Get action distributions from policy networks.
          online_action_distribution = self._policy_network(o_t)
          target_action_distribution = self._target_policy_network(o_t)
  
          # Get sampled actions to evaluate policy; of size [N, B, ...].
          sampled_actions = target_action_distribution.sample(
              self._num_samples
          )
          tiled_o_t = tf2_utils.tile_tensor(
              o_t, self._num_samples
          )  # [N, B, ...]
  
          # Compute the target critic's Q-value of the sampled actions in state o_t.
          sampled_q_t = self._target_critic_network(
              # Merge batch dimensions; to shape [N*B, ...].
              snt.merge_leading_dims(tiled_o_t, num_dims=2),
              snt.merge_leading_dims(sampled_actions, num_dims=2),
          )
  
          # Reshape Q-value samples back to original batch dimensions and average
          # them to compute the TD-learning bootstrap target.
          sampled_q_t = tf.reshape(
              sampled_q_t, (self._num_samples, -1)
          )  # [N, B]
          q_t = tf.reduce_mean(sampled_q_t, axis=0)  # [B]
  
          # Compute online critic value of a_tm1 in state o_tm1.
          q_tm1 = self._critic_network(
              o_tm1, transitions.action
          )  # [B, 1]
          q_tm1 = tf.squeeze(
              q_tm1, axis=-1
          )  # [B]; necessary for trfl.td_learning.
  
          # Critic loss.
          critic_loss = trfl.td_learning(
              q_tm1,
              transitions.reward,
              discount * transitions.discount,
              q_t,
          ).loss
          critic_loss = tf.reduce_mean(critic_loss)
  
          # Actor learning.
          policy_loss, policy_stats = self._policy_loss_module(
              online_action_distribution=online_action_distribution,
              target_action_distribution=target_action_distribution,
              actions=sampled_actions,
              q_values=sampled_q_t,
          )
  
      # For clarity, explicitly define which variables are trained by which loss.
      critic_trainable_variables = (
          # In this agent, the critic loss trains the observation network.
          self._observation_network.trainable_variables
          + self._critic_network.trainable_variables
      )
      policy_trainable_variables = (
          self._policy_network.trainable_variables
      )
      # The following are the MPO dual variables, stored in the loss module.
      dual_trainable_variables = (
          self._policy_loss_module.trainable_variables
      )
  
      # Compute gradients.
      critic_gradients = tape.gradient(
          critic_loss, critic_trainable_variables
      )
      policy_gradients, dual_gradients = tape.gradient(
          policy_loss,
          (policy_trainable_variables, dual_trainable_variables),
      )
  
      # Delete the tape manually because of the persistent=True flag.
      del tape
  
      # Maybe clip gradients.
      if self._clipping:
          policy_gradients = tuple(
              tf.clip_by_global_norm(policy_gradients, 40.0)[0]
          )
          critic_gradients = tuple(
              tf.clip_by_global_norm(critic_gradients, 40.0)[0]
          )
  
      # Apply gradients.
      self._critic_optimizer.apply(
          critic_gradients, critic_trainable_variables
      )
      self._policy_optimizer.apply(
          policy_gradients, policy_trainable_variables
      )
      self._dual_optimizer.apply(
          dual_gradients, dual_trainable_variables
      )
  
      # Losses to track.
      fetches = {
          "critic_loss": critic_loss,
          "policy_loss": policy_loss,
      }
      fetches.update(policy_stats)  # Log MPO stats.
  
      return fetches
  
file_path: acme/acme/agents/tf/mpo/learning.py
imports: import time
  from typing import List, Optional
  import acme
  from acme import types
  from acme.tf import losses
  from acme.tf import savers
  from acme.tf import utils
  from acme.utils import counting
  from acme.utils import loggers
  import numpy
  import sonnet
  import tensorflow
  import trfl
label_desc: Use image node_id 2 for calling the MPOLearner obj's underlying member method code with example usage: obj._step() and returns: fetches