code: def _get_training_loop(
      driver, replay_buffer, agent, steps, async_steps_per_loop
  ):
      """Returns a `tf.function` that runs the driver and training loops.
  
      Args:
        driver: an instance of `Driver`.
        replay_buffer: an instance of `ReplayBuffer`.
        agent: an instance of `TFAgent`.
        steps: an integer indicating how many driver steps should be executed and
          presented to the trainer during each training loop.
        async_steps_per_loop: an integer. In each training loop, the driver runs
          this many times, and then the agent gets asynchronously trained over this
          many batches sampled from the replay buffer.
      """
  
      def _export_metrics_and_summaries(step, metrics):
          """Exports metrics and tf summaries."""
          metric_utils.log_metrics(metrics)
          export_utils.export_metrics(step=step, metrics=metrics)
          for metric in metrics:
              metric.tf_summaries(train_step=step)
  
      def training_loop(train_step, metrics):
          """Returns a function that runs a single training loop and logs metrics."""
          for batch_id in range(async_steps_per_loop):
              driver.run()
              _export_metrics_and_summaries(
                  step=train_step * async_steps_per_loop + batch_id,
                  metrics=metrics,
              )
          batch_size = driver.env.batch_size
          dataset_it = iter(
              replay_buffer.as_dataset(
                  sample_batch_size=batch_size,
                  num_steps=steps,
                  single_deterministic_pass=True,
              )
          )
          for batch_id in range(async_steps_per_loop):
              experience, unused_buffer_info = dataset_it.get_next()
              set_expected_shape(experience, steps)
              loss_info = agent.train(experience)
              export_utils.export_metrics(
                  step=train_step * async_steps_per_loop + batch_id,
                  metrics=[],
                  loss_info=loss_info,
              )
  
          replay_buffer.clear()
  
      return training_loop
  
file_path: agents/tf_agents/bandits/agents/examples/v2/trainer.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import os
  from absl import logging
  import tensorflow
  from tf_agents.bandits.replay_buffers import bandit_replay_buffer
  from tf_agents.drivers import dynamic_step_driver
  from tf_agents.eval import metric_utils
  from tf_agents.metrics import export_utils
  from tf_agents.metrics import tf_metrics
  from tf_agents.policies import policy_saver
label_desc: Use image node_id 3 for calling a global function with example usage: _get_training_loop(driver, replay_buffer, agent, steps, async_steps_per_loop) and returns: training_loop