code: def evaluate_agent_if_necessary(eval_count, start_time):
      """Evaluate agent every --eval_period steps."""
      if agent.step_count >= eval_count * args.eval_period:
          elapsed_time = time.time() - start_time
          total_reward = 0
          num_episodes = 0
          episode_reward = 0
          i = 0
          agent.enter_evaluation()
  
          observation = new_episode()
          while i < args.eval_steps:
              i += 1
              action = agent.evaluate(observation)
              observation, reward, isTerminal, _ = env.step(action)
              if args.render:
                  env.render()
              episode_reward += reward
              if isTerminal:
                  num_episodes += 1
                  total_reward += episode_reward
                  episode_reward = 0
                  observation = new_episode()
  
          reward = (
              episode_reward
              if num_episodes == 0
              else total_reward / num_episodes
          )
          print(
              "\nAverage reward per episode after training {0} steps: {1}\n"
              "".format(agent.step_count, reward)
          )
          if len(reward_history) == 0 or reward > max(reward_history):
              agent.set_as_best_model()
          reward_history.append(reward)
          if len(training_time) != 0:
              elapsed_time += training_time[-1]
          training_time.append(elapsed_time)
  
          # Save results and update eval_count.
          filename_prefix = os.path.join(
              args.output_dir, args.output_dir
          )
          agent.save(filename_prefix + ".model")
          with closing(
              shelve.open(
                  filename_prefix + ".wks",
                  "n" if eval_count == 1 else "c",
                  0,
                  True,
              )
          ) as shelf:
              if "step_count" not in shelf:
                  shelf["step_count"] = []
              shelf["step_count"].append(agent.step_count)
              shelf["reward_history"] = reward_history
              shelf["training_time_sec"] = training_time
          agent.exit_evaluation()
          eval_count += 1
          start_time = time.time()
  
      return eval_count, start_time
  
file_path: CNTK/Examples/ReinforcementLearning/deeprl/scripts/run.py
imports: import argparse
  import os
  import shelve
  import sys
  import time
  from contextlib import closing
  import numpy
  from gym import envs
  from gym.envs.atari.atari_env import AtariEnv
  from cntk.contrib.deeprl.agent import agent_factory
  from env import env_factory
label_desc: Use image node_id 2 for calling a global function with example usage: evaluate_agent_if_necessary(eval_count, start_time) and returns: eval_count,  start_time