(model, batcher, vocab)
Repeatedly runs eval iterations, logging to screen and writing summaries. Saves the model with the best loss seen so far.
Repeatedly runs eval iterations, logging to screen and writing summaries. Saves the model with the best loss seen so far.
Repeatedly runs eval iterations logging to screen and writing summaries . Saves the model with the best loss seen so far .
def run_eval(model, batcher, vocab):
  """Repeatedly runs eval iterations, logging to screen and writing summaries. Saves the model with the best loss seen so far."""
  model.build_graph() # build the graph
  saver = tf.train.Saver(max_to_keep=3) # we will keep 3 best checkpoints at a time
  sess = tf.Session(config=util.get_config())
  eval_dir = os.path.join(FLAGS.log_root, "eval") # make a subdir of the root dir for eval data
  bestmodel_save_path = os.path.join(eval_dir, 'bestmodel') # this is where checkpoints of best models are saved
  summary_writer = tf.summary.FileWriter(eval_dir)
  running_avg_loss = 0 # the eval job keeps a smoother, running average loss to tell it when to implement early stopping
  best_loss = None  # will hold the best loss achieved so far

  while True:
    _ = util.load_ckpt(saver, sess) # load a new checkpoint
    batch = batcher.next_batch() # get the next batch

    # run eval on the batch
    t0=time.time()
    results = model.run_eval_step(sess, batch)
    t1=time.time()
    tf.logging.info('seconds for batch: %.2f', t1-t0)

    # print the loss and coverage loss to screen
    loss = results['loss']
    tf.logging.info('loss: %f', loss)
    if FLAGS.coverage:
      coverage_loss = results['coverage_loss']
      tf.logging.info("coverage_loss: %f", coverage_loss)

    # add summaries
    summaries = results['summaries']
    train_step = results['global_step']
    summary_writer.add_summary(summaries, train_step)

    # calculate running avg loss
    running_avg_loss = calc_running_avg_loss(np.asscalar(loss), running_avg_loss, summary_writer, train_step)

    # If running_avg_loss is best so far, save this checkpoint (early stopping).
    # These checkpoints will appear as bestmodel-<iteration_number> in the eval dir
    if best_loss is None or running_avg_loss < best_loss:
      tf.logging.info('Found new best model with %.3f running_avg_loss. Saving to %s', running_avg_loss, bestmodel_save_path)
      saver.save(sess, bestmodel_save_path, global_step=train_step, latest_filename='checkpoint_best')
      best_loss = running_avg_loss

    # flush the summary writer every so often
    if train_step % 100 == 0:
      summary_writer.flush()
def run_eval ( model , batcher , vocab ) : model . build_graph ( ) # build the graph saver = tf . train . Saver ( max_to_keep = 3 ) # we will keep 3 best checkpoints at a time sess = tf . Session ( config = util . get_config ( ) ) eval_dir = os . path . join ( FLAGS . log_root , "eval" ) # make a subdir of the root dir for eval data bestmodel_save_path = os . path . join ( eval_dir , 'bestmodel' ) # this is where checkpoints of best models are saved summary_writer = tf . summary . FileWriter ( eval_dir ) running_avg_loss = 0 # the eval job keeps a smoother, running average loss to tell it when to implement early stopping best_loss = None # will hold the best loss achieved so far while True : _ = util . load_ckpt ( saver , sess ) # load a new checkpoint batch = batcher . next_batch ( ) # get the next batch # run eval on the batch t0 = time . time ( ) results = model . run_eval_step ( sess , batch ) t1 = time . time ( ) tf . logging . info ( 'seconds for batch: %.2f' , t1 - t0 ) # print the loss and coverage loss to screen loss = results [ 'loss' ] tf . logging . info ( 'loss: %f' , loss ) if FLAGS . coverage : coverage_loss = results [ 'coverage_loss' ] tf . logging . info ( "coverage_loss: %f" , coverage_loss ) # add summaries summaries = results [ 'summaries' ] train_step = results [ 'global_step' ] summary_writer . add_summary ( summaries , train_step ) # calculate running avg loss running_avg_loss = calc_running_avg_loss ( np . asscalar ( loss ) , running_avg_loss , summary_writer , train_step ) # If running_avg_loss is best so far, save this checkpoint (early stopping). # These checkpoints will appear as bestmodel-<iteration_number> in the eval dir if best_loss is None or running_avg_loss < best_loss : tf . logging . info ( 'Found new best model with %.3f running_avg_loss. Saving to %s' , running_avg_loss , bestmodel_save_path ) saver . save ( sess , bestmodel_save_path , global_step = train_step , latest_filename = 'checkpoint_best' ) best_loss = running_avg_loss # flush the summary writer every so often if train_step % 100 == 0 : summary_writer . flush ( )