code: def eager_compute(
      metrics,
      environment,
      policy,
      num_episodes=1,
      train_step=None,
      summary_writer=None,
      summary_prefix="",
      use_function=True,
  ):
      """Compute metrics using `policy` on the `environment`.
  
      *NOTE*: Because placeholders are not compatible with Eager mode we can not use
      python policies. Because we use tf_policies we need the environment time_steps
      to be tensors making it easier to use a tf_env for evaluations. Otherwise this
      method mirrors `compute` directly.
  
      Args:
        metrics: List of metrics to compute.
        environment: tf_environment instance.
        policy: tf_policy instance used to step the environment.
        num_episodes: Number of episodes to compute the metrics over.
        train_step: An optional step to write summaries against.
        summary_writer: An optional writer for generating metric summaries.
        summary_prefix: An optional prefix scope for metric summaries.
        use_function: Option to enable use of `tf.function` when collecting the
          metrics.
  
      Returns:
        A dictionary of results {metric_name: metric_value}
      """
      for metric in metrics:
          metric.reset()
  
      time_step = environment.reset()
      policy_state = policy.get_initial_state(environment.batch_size)
  
      driver = dynamic_episode_driver.DynamicEpisodeDriver(
          environment,
          policy,
          observers=metrics,
          num_episodes=num_episodes,
      )
      if use_function:
          common.function(driver.run)(time_step, policy_state)
      else:
          driver.run(time_step, policy_state)
  
      results = [(metric.name, metric.result()) for metric in metrics]
      # TODO(b/120301678) remove the summaries and merge with compute
      if train_step is not None and summary_writer:
          with summary_writer.as_default():
              for m in metrics:
                  tag = common.join_scope(summary_prefix, m.name)
                  tf.compat.v2.summary.scalar(
                      name=tag, data=m.result(), step=train_step
                  )
      # TODO(b/130249101): Add an option to log metrics.
      return collections.OrderedDict(results)
  
file_path: agents/tf_agents/eval/metric_utils.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  from absl import logging
  import gin
  import tensorflow
  from tf_agents.drivers import dynamic_episode_driver
  from tf_agents.drivers import py_driver
  from tf_agents.metrics import py_metric
  from tf_agents.utils import common
label_desc: Use image node_id 4 for calling a global function with example usage: eager_compute(metrics, environment, policy, num_episodes, train_step, summary_writer, summary_prefix, use_function) and returns: collections