class_docstr: DDPG learner.
  
  This is the learning component of a DDPG agent. IE it takes a dataset as input
  and implements update functionality to learn from this dataset.
class_docstr_tok: ["DDPG","learner",".","This","is","the","learning","component","of","a","DDPG","agent",".","IE","it","takes","a","dataset","as","input","and","implements","update","functionality","to","learn","from","this","dataset","."]
code: def _step(self):
      # Update target network.
      online_variables = (
          *self._observation_network.variables,
          *self._critic_network.variables,
          *self._policy_network.variables,
      )
      target_variables = (
          *self._target_observation_network.variables,
          *self._target_critic_network.variables,
          *self._target_policy_network.variables,
      )
  
      # Make online -> target network update ops.
      if tf.math.mod(self._num_steps, self._target_update_period) == 0:
          for src, dest in zip(online_variables, target_variables):
              dest.assign(src)
      self._num_steps.assign_add(1)
  
      # Get data from replay (dropping extras if any). Note there is no
      # extra data here because we do not insert any into Reverb.
      inputs = next(self._iterator)
      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)
          o_t = self._target_observation_network(
              transitions.next_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 = tree.map_structure(tf.stop_gradient, o_t)
  
          # Critic learning.
          q_tm1 = self._critic_network(o_tm1, transitions.action)
          q_t = self._target_critic_network(
              o_t, self._target_policy_network(o_t)
          )
  
          # Squeeze into the shape expected by the td_learning implementation.
          q_tm1 = tf.squeeze(q_tm1, axis=-1)  # [B]
          q_t = tf.squeeze(q_t, axis=-1)  # [B]
  
          # Critic loss.
          critic_loss = trfl.td_learning(
              q_tm1,
              transitions.reward,
              discount * transitions.discount,
              q_t,
          ).loss
          critic_loss = tf.reduce_mean(critic_loss, axis=0)
  
          # Actor learning.
          dpg_a_t = self._policy_network(o_t)
          dpg_q_t = self._critic_network(o_t, dpg_a_t)
  
          # Actor loss. If clipping is true use dqda clipping and clip the norm.
          dqda_clipping = 1.0 if self._clipping else None
          policy_loss = losses.dpg(
              dpg_q_t,
              dpg_a_t,
              tape=tape,
              dqda_clipping=dqda_clipping,
              clip_norm=self._clipping,
          )
          policy_loss = tf.reduce_mean(policy_loss, axis=0)
  
      # Get trainable variables.
      policy_variables = self._policy_network.trainable_variables
      critic_variables = (
          # In this agent, the critic loss trains the observation network.
          self._observation_network.trainable_variables
          + self._critic_network.trainable_variables
      )
  
      # Compute gradients.
      policy_gradients = tape.gradient(policy_loss, policy_variables)
      critic_gradients = tape.gradient(critic_loss, critic_variables)
  
      # Delete the tape manually because of the persistent=True flag.
      del tape
  
      # Maybe clip gradients.
      if self._clipping:
          policy_gradients = tf.clip_by_global_norm(
              policy_gradients, 40.0
          )[0]
          critic_gradients = tf.clip_by_global_norm(
              critic_gradients, 40.0
          )[0]
  
      # Apply gradients.
      self._policy_optimizer.apply(policy_gradients, policy_variables)
      self._critic_optimizer.apply(critic_gradients, critic_variables)
  
      # Losses to track.
      return {
          "critic_loss": critic_loss,
          "policy_loss": policy_loss,
      }
  
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 tree
  import trfl
label_desc: Use image node_id 2 for calling the DDPGLearner obj's underlying member method code with example usage: obj._step() and returns: dict