class_docstr: A TD3 Agent.
class_docstr_tok: ["A","TD3","Agent","."]
code: def _train(self, experience, weights=None):
      transition = self._as_transition(experience)
      time_steps, policy_steps, next_time_steps = transition
      actions = policy_steps.action
  
      trainable_critic_variables = list(
          object_identity.ObjectIdentitySet(
              self._critic_network_1.trainable_variables
              + self._critic_network_2.trainable_variables
          )
      )
      with tf.GradientTape(watch_accessed_variables=False) as tape:
          assert (
              trainable_critic_variables
          ), "No trainable critic variables to optimize."
          tape.watch(trainable_critic_variables)
          critic_loss = self.critic_loss(
              time_steps,
              actions,
              next_time_steps,
              weights=weights,
              training=True,
          )
      tf.debugging.check_numerics(
          critic_loss, "Critic loss is inf or nan."
      )
      critic_grads = tape.gradient(
          critic_loss, trainable_critic_variables
      )
      self._apply_gradients(
          critic_grads,
          trainable_critic_variables,
          self._critic_optimizer,
      )
  
      trainable_actor_variables = (
          self._actor_network.trainable_variables
      )
      with tf.GradientTape(watch_accessed_variables=False) as tape:
          assert (
              trainable_actor_variables
          ), "No trainable actor variables to optimize."
          tape.watch(trainable_actor_variables)
          actor_loss = self.actor_loss(
              time_steps, weights=weights, training=True
          )
      tf.debugging.check_numerics(
          actor_loss, "Actor loss is inf or nan."
      )
  
      # We only optimize the actor every actor_update_period training steps.
      def optimize_actor():
          actor_grads = tape.gradient(
              actor_loss, trainable_actor_variables
          )
          self._apply_gradients(
              actor_grads,
              trainable_actor_variables,
              self._actor_optimizer,
          )
          return tf.no_op()
  
      remainder = tf.math.mod(
          self.train_step_counter, self._actor_update_period
      )
      tf.cond(
          pred=tf.equal(remainder, 0),
          true_fn=optimize_actor,
          false_fn=tf.no_op,
      )
  
      self.train_step_counter.assign_add(1)
      self._update_target()
  
      # TODO(b/124382360): Compute per element TD loss and return in loss_info.
      total_loss = actor_loss + critic_loss
  
      return tf_agent.LossInfo(
          total_loss, Td3Info(actor_loss, critic_loss)
      )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  from typing import Optional, Text
  import gin
  import tensorflow
  import tensorflow_probability
  from tf_agents.agents import data_converter
  from tf_agents.agents import tf_agent
  from tf_agents.networks import network
  from tf_agents.policies import actor_policy
  from tf_agents.policies import gaussian_policy
  from tf_agents.trajectories import time_step
  from tf_agents.typing import types
  from tf_agents.utils import common
  from tf_agents.utils import eager_utils
  from tf_agents.utils import nest_utils
  from tf_agents.utils import object_identity
label_desc: Use image node_id 4 for calling the Td3Agent obj's underlying member method code with example usage: obj._train(experience, weights) and returns: tf_agent, tf