code: def train_agent(
      agent,
      env,
      steps,
      outdir,
      checkpoint_freq=None,
      max_episode_len=None,
      step_offset=0,
      evaluator=None,
      successful_score=None,
      step_hooks=(),
      logger=None,
  ):
      logger = logger or logging.getLogger(__name__)
  
      episode_r = 0
      episode_idx = 0
  
      # o_0, r_0
      obs = env.reset()
      r = 0
  
      t = step_offset
      if hasattr(agent, "t"):
          agent.t = step_offset
  
      episode_len = 0
      try:
          while t < steps:
              # a_t
              action = agent.act_and_train(obs, r)
              # o_{t+1}, r_{t+1}
              obs, r, done, info = env.step(action)
              t += 1
              episode_r += r
              episode_len += 1
  
              for hook in step_hooks:
                  hook(env, agent, t)
  
              reset = episode_len == max_episode_len or info.get(
                  "needs_reset", False
              )
              if done or reset or t == steps:
                  agent.stop_episode_and_train(obs, r, done=done)
                  logger.info(
                      "outdir:%s step:%s episode:%s R:%s",
                      outdir,
                      t,
                      episode_idx,
                      episode_r,
                  )
                  logger.info("statistics:%s", agent.get_statistics())
                  if evaluator is not None:
                      evaluator.evaluate_if_necessary(
                          t=t, episodes=episode_idx + 1
                      )
                      if (
                          successful_score is not None
                          and evaluator.max_score >= successful_score
                      ):
                          break
                  if t == steps:
                      break
                  # Start a new episode
                  episode_r = 0
                  episode_idx += 1
                  episode_len = 0
                  obs = env.reset()
                  r = 0
              if checkpoint_freq and t % checkpoint_freq == 0:
                  save_agent(
                      agent, t, outdir, logger, suffix="_checkpoint"
                  )
  
      except (Exception, KeyboardInterrupt):
          # Save the current model before being killed
          save_agent(agent, t, outdir, logger, suffix="_except")
          raise
  
      # Save the final model
      save_agent(agent, t, outdir, logger, suffix="_finish")
  
file_path: chainerrl/chainerrl/experiments/train_agent.py
imports: import logging
  import os
  from chainerrl.experiments.evaluator import Evaluator
  from chainerrl.experiments.evaluator import save_agent
  from chainerrl.misc.ask_yes_no import ask_yes_no
label_desc: Use image node_id 3 for calling a global function with example usage: train_agent(agent, env, steps, outdir, checkpoint_freq, max_episode_len, step_offset, evaluator, successful_score, step_hooks, logger) without return types