code: def run_epoch(batches, answer_net, is_training):
      if not is_training:
          position1_result = []
          position2_result = []
          contexts = []
          ids = []
  
      loss_sum = 0
      timer = Timer()
      count = 0
      for batch in batches:
          used = timer.get_elapsed(False)
          count += 1
          qps = batch["qp_pairs"]
          question_tokens = [qp["question_tokens"] for qp in qps]
          passage_tokens = [qp["passage_tokens"] for qp in qps]
          context = [
              (qp["passage"], qp["passage_tokens"]) for qp in qps
          ]
          sample_id = [qp["id"] for qp in qps]
  
          _, query, query_mask, query_lengths = data.get_word_input(
              data=question_tokens,
              word_dict=word_vcb,
              embed=embed,
              embed_dim=cfg.word_embed_dim,
          )
          (
              _,
              passage,
              passage_mask,
              passage_lengths,
          ) = data.get_word_input(
              data=passage_tokens,
              word_dict=word_vcb,
              embed=embed,
              embed_dim=cfg.word_embed_dim,
          )
  
          query_char, query_char_lengths = data.get_char_input(
              data=question_tokens,
              char_dict=char_vcb,
              max_char_length=cfg.max_char_length,
          )
  
          passage_char, passage_char_lengths = data.get_char_input(
              data=passage_tokens,
              char_dict=char_vcb,
              max_char_length=cfg.max_char_length,
          )
  
          if is_training:
              answer_begin, answer_end = data.get_answer_begin_end(qps)
  
          if is_training:
              feed_dict = {
                  answer_net.query_word: query,
                  answer_net.query_mask: query_mask,
                  answer_net.query_lengths: query_lengths,
                  answer_net.passage_word: passage,
                  answer_net.passage_mask: passage_mask,
                  answer_net.passage_lengths: passage_lengths,
                  answer_net.query_char_ids: query_char,
                  answer_net.query_char_lengths: query_char_lengths,
                  answer_net.passage_char_ids: passage_char,
                  answer_net.passage_char_lengths: passage_char_lengths,
                  answer_net.answer_begin: answer_begin,
                  answer_net.answer_end: answer_end,
              }
              (
                  loss,
                  _,
              ) = sess.run(
                  [answer_net.loss, answer_net.train_op],
                  feed_dict=feed_dict,
              )
              if count % 100 == 0:
                  logger.debug(
                      "%d %g except:%g, loss:%g",
                      count,
                      used,
                      used / count * len(batches),
                      loss,
                  )
              loss_sum += loss
          else:
              feed_dict = {
                  answer_net.query_word: query,
                  answer_net.query_mask: query_mask,
                  answer_net.query_lengths: query_lengths,
                  answer_net.passage_word: passage,
                  answer_net.passage_mask: passage_mask,
                  answer_net.passage_lengths: passage_lengths,
                  answer_net.query_char_ids: query_char,
                  answer_net.query_char_lengths: query_char_lengths,
                  answer_net.passage_char_ids: passage_char,
                  answer_net.passage_char_lengths: passage_char_lengths,
              }
              position1, position2 = sess.run(
                  [answer_net.begin_prob, answer_net.end_prob],
                  feed_dict=feed_dict,
              )
              position1_result += position1.tolist()
              position2_result += position2.tolist()
              contexts += context
              ids = np.concatenate((ids, sample_id))
              if count % 100 == 0:
                  logger.debug(
                      "%d %g except:%g",
                      count,
                      used,
                      used / count * len(batches),
                  )
      loss = loss_sum / len(batches)
      if is_training:
          return loss
      return loss, position1_result, position2_result, ids, contexts
  
imports: import argparse
  import heapq
  import json
  import os
  import pickle
  import logging
  import numpy
  from tensorflow.train import init_from_checkpoint
  import graph
  from util import Timer
  import nni
  import data
  import evaluate
  from train_model import *
label_desc: Use image node_id 7 for calling a global function with example usage: run_epoch(batches, answer_net, is_training) and returns: loss,  position1_result,  position2_result,  ids,  contexts, loss