code: def train_with_graph(p_graph, qp_pairs, dev_qp_pairs):
      """
      Train a network from a specific graph.
      """
      global sess
      with tf.Graph().as_default():
          train_model = GAG(cfg, embed, p_graph)
          train_model.build_net(is_training=True)
          tf.get_variable_scope().reuse_variables()
          dev_model = GAG(cfg, embed, p_graph)
          dev_model.build_net(is_training=False)
          with tf.Session() as sess:
              if restore_path is not None:
                  restore_mapping = dict(
                      zip(restore_shared, restore_shared)
                  )
                  logger.debug(
                      "init shared variables from {}, restore_scopes: {}".format(
                          restore_path, restore_shared
                      )
                  )
                  init_from_checkpoint(restore_path, restore_mapping)
              logger.debug("init variables")
              logger.debug(
                  sess.run(tf.report_uninitialized_variables())
              )
              init = tf.global_variables_initializer()
              sess.run(init)
              # writer = tf.summary.FileWriter('%s/graph/'%execution_path, sess.graph)
              logger.debug("assign to graph")
  
              saver = tf.train.Saver()
              train_loss = None
              bestacc = 0
              patience = 5
              patience_increase = 2
              improvement_threshold = 0.995
  
              for epoch in range(max_epoch):
                  logger.debug("begin to train")
                  train_batches = data.get_batches(
                      qp_pairs, cfg.batch_size
                  )
                  train_loss = run_epoch(
                      train_batches, train_model, True
                  )
                  logger.debug(
                      "epoch " + str(epoch) + " loss: ", str(train_loss)
                  )
                  dev_batches = list(
                      data.get_batches(dev_qp_pairs, cfg.batch_size)
                  )
                  _, position1, position2, ids, contexts = run_epoch(
                      dev_batches, dev_model, False
                  )
  
                  answers = generate_predict_json(
                      position1, position2, ids, contexts
                  )
                  if save_path is not None:
                      logger.info(
                          "save prediction file to {}".format(save_path)
                      )
                      with open(
                          os.path.join(
                              save_path, "epoch%d.prediction" % epoch
                          ),
                          "w",
                      ) as file:
                          json.dump(answers, file)
                  else:
                      answers = json.dumps(answers)
                      answers = json.loads(answers)
                  iter = epoch + 1
  
                  acc = evaluate.evaluate_with_predictions(
                      args.dev_file, answers
                  )
  
                  logger.debug("Send intermediate acc: %s", str(acc))
                  nni.report_intermediate_result(acc)
  
                  logger.debug("Send intermediate result done.")
  
                  if acc > bestacc:
                      if acc * improvement_threshold > bestacc:
                          patience = max(
                              patience, iter * patience_increase
                          )
                      bestacc = acc
  
                      if save_path is not None:
                          logger.info(
                              "save model & prediction to {}".format(
                                  save_path
                              )
                          )
                          saver.save(
                              sess,
                              os.path.join(
                                  save_path, "epoch%d.model" % epoch
                              ),
                          )
                          with open(
                              os.path.join(
                                  save_path, "epoch%d.score" % epoch
                              ),
                              "wb",
                          ) as file:
                              pickle.dump(
                                  (position1, position2, ids, contexts),
                                  file,
                              )
                  logger.debug(
                      "epoch %d acc %g bestacc %g", epoch, acc, bestacc
                  )
                  if patience <= iter:
                      break
              logger.debug("save done.")
      return train_loss, bestacc
  
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 10 for calling a global function with example usage: train_with_graph(p_graph, qp_pairs, dev_qp_pairs) and returns: train_loss,  bestacc