Python util.load_ckpt() Examples
The following are 30
code examples of util.load_ckpt().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
util
, or try the search function
.
Example #1
Source File: run_summarization.py From TransferRL with MIT License | 6 votes |
def convert_to_coverage_model(self): """Load non-coverage checkpoint, add initialized extra variables for coverage, and save as new checkpoint""" tf.logging.info("converting non-coverage model to coverage model..") # initialize an entire coverage model from scratch sess = tf.Session(config=util.get_config()) print("initializing everything...") sess.run(tf.global_variables_initializer()) # load all non-coverage weights from checkpoint saver = tf.train.Saver([v for v in tf.global_variables() if "coverage" not in v.name and "Adagrad" not in v.name]) print("restoring non-coverage variables...") curr_ckpt = util.load_ckpt(saver, sess) print("restored.") # save this model and quit new_fname = curr_ckpt + '_cov_init' print("saving model to %s..." % (new_fname)) new_saver = tf.train.Saver() # this one will save all variables that now exist new_saver.save(sess, new_fname) print("saved.") exit()
Example #2
Source File: run_summarization.py From pointer-generator with Apache License 2.0 | 6 votes |
def convert_to_coverage_model(): """Load non-coverage checkpoint, add initialized extra variables for coverage, and save as new checkpoint""" tf.logging.info("converting non-coverage model to coverage model..") # initialize an entire coverage model from scratch sess = tf.Session(config=util.get_config()) print "initializing everything..." sess.run(tf.global_variables_initializer()) # load all non-coverage weights from checkpoint saver = tf.train.Saver([v for v in tf.global_variables() if "coverage" not in v.name and "Adagrad" not in v.name]) print "restoring non-coverage variables..." curr_ckpt = util.load_ckpt(saver, sess) print "restored." # save this model and quit new_fname = curr_ckpt + '_cov_init' print "saving model to %s..." % (new_fname) new_saver = tf.train.Saver() # this one will save all variables that now exist new_saver.save(sess, new_fname) print "saved." exit()
Example #3
Source File: run_summarization.py From pointer-generator with Apache License 2.0 | 6 votes |
def restore_best_model(): """Load bestmodel file from eval directory, add variables for adagrad, and save to train directory""" tf.logging.info("Restoring bestmodel for training...") # Initialize all vars in the model sess = tf.Session(config=util.get_config()) print "Initializing all variables..." sess.run(tf.initialize_all_variables()) # Restore the best model from eval dir saver = tf.train.Saver([v for v in tf.all_variables() if "Adagrad" not in v.name]) print "Restoring all non-adagrad variables from best model in eval dir..." curr_ckpt = util.load_ckpt(saver, sess, "eval") print "Restored %s." % curr_ckpt # Save this model to train dir and quit new_model_name = curr_ckpt.split("/")[-1].replace("bestmodel", "model") new_fname = os.path.join(FLAGS.log_root, "train", new_model_name) print "Saving model to %s..." % (new_fname) new_saver = tf.train.Saver() # this saver saves all variables that now exist, including Adagrad variables new_saver.save(sess, new_fname) print "Saved." exit()
Example #4
Source File: main.py From docker with MIT License | 6 votes |
def setup_training_discriminator(model): """Does setup before starting training (run_training)""" train_dir = os.path.join(FLAGS.log_root, "train-discriminator") if not os.path.exists(train_dir): os.makedirs(train_dir) model.build_graph() # build the graph saver = tf.train.Saver(max_to_keep=20) # we use this to load checkpoints for decoding sess = tf.Session(config=util.get_config()) #init = tf.global_variables_initializer() #sess.run(init) util.load_ckpt(saver, sess, ckpt_dir="train-discriminator") return sess, saver,train_dir
Example #5
Source File: main.py From docker with MIT License | 6 votes |
def setup_training_generator(model): """Does setup before starting training (run_training)""" train_dir = os.path.join(FLAGS.log_root, "train-generator") if not os.path.exists(train_dir): os.makedirs(train_dir) model.build_graph() # build the graph saver = tf.train.Saver(max_to_keep=20) # we use this to load checkpoints for decoding sess = tf.Session(config=util.get_config()) #sess.run(tf.train.Saver(max_to_keep=20)) #init = tf.global_variables_initializer() #sess.run(init) # Load an initial checkpoint to use for decoding util.load_ckpt(saver, sess, ckpt_dir="train-generator") return sess, saver,train_dir
Example #6
Source File: run_summarization.py From long-summarization with Apache License 2.0 | 6 votes |
def convert_linear_attn_to_hier_model(): """Load non-coverage checkpoint, add initialized extra variables for coverage, and save as new checkpoint""" tf.logging.info("converting linear model to hier model..") # initialize an entire coverage model from scratch sess = tf.Session(config=util.get_config()) print("initializing everything...") sess.run(tf.global_variables_initializer()) # load all non-coverage weights from checkpoint saver = tf.train.Saver([v for v in tf.global_variables( ) if "Linear--Section-Features" not in v.name and "v_sec" not in v.name and "Adagrad" not in v.name]) print("restoring variables...") curr_ckpt = util.load_ckpt(saver, sess) print("restored.") # save this model and quit new_fname = curr_ckpt print(("saving model to %s..." % (new_fname))) new_saver = tf.train.Saver() # this one will save all variables that now exist new_saver.save(sess, new_fname) print("saved.") exit()
Example #7
Source File: run_summarization.py From long-summarization with Apache License 2.0 | 6 votes |
def restore_best_model(): """Load bestmodel file from eval directory, add variables for adagrad, and save to train directory""" tf.logging.info("Restoring bestmodel for training...") # Initialize all vars in the model sess = tf.Session(config=util.get_config()) print("Initializing all variables...") sess.run(tf.initialize_all_variables()) # Restore the best model from eval dir saver = tf.train.Saver([v for v in tf.all_variables() if "Adagrad" not in v.name]) print("Restoring all non-adagrad variables from best model in eval dir...") curr_ckpt = util.load_ckpt(saver, sess, "eval") print("Restored %s." % curr_ckpt) # Save this model to train dir and quit new_model_name = curr_ckpt.split("/")[-1].replace("bestmodel", "model") new_fname = os.path.join(FLAGS.log_root, "train", new_model_name) print("Saving model to %s..." % (new_fname)) new_saver = tf.train.Saver() # this saver saves all variables that now exist, including Adagrad variables new_saver.save(sess, new_fname) print("Saved.") exit()
Example #8
Source File: run_summarization.py From rotational-unit-of-memory with MIT License | 6 votes |
def convert_to_coverage_model(): """Load non-coverage checkpoint, add initialized extra variables for coverage, and save as new checkpoint""" tf.logging.info("converting non-coverage model to coverage model..") # initialize an entire coverage model from scratch sess = tf.Session(config=util.get_config()) print("initializing everything...") sess.run(tf.global_variables_initializer()) # load all non-coverage weights from checkpoint saver = tf.train.Saver([v for v in tf.global_variables( ) if "coverage" not in v.name and "Adagrad" not in v.name]) print("restoring non-coverage variables...") curr_ckpt = util.load_ckpt(saver, sess) print("restored.") # save this model and quit new_fname = curr_ckpt + '_cov_init' print("saving model to %s..." % (new_fname)) new_saver = tf.train.Saver() # this one will save all variables that now exist new_saver.save(sess, new_fname) print("saved.") exit()
Example #9
Source File: run_summarization.py From MAX-Text-Summarizer with Apache License 2.0 | 6 votes |
def convert_to_coverage_model(): """Load non-coverage checkpoint, add initialized extra variables for coverage, and save as new checkpoint""" tf.logging.info("converting non-coverage model to coverage model..") # initialize an entire coverage model from scratch sess = tf.Session(config=util.get_config()) print("initializing everything...") sess.run(tf.global_variables_initializer()) # load all non-coverage weights from checkpoint saver = tf.train.Saver([v for v in tf.global_variables() if "coverage" not in v.name and "Adagrad" not in v.name]) print("restoring non-coverage variables...") curr_ckpt = util.load_ckpt(saver, sess, FLAGS.ckpt_dir) print("restored.") # save this model and quit new_fname = curr_ckpt + '_cov_init' print("saving model to %s..." % new_fname) new_saver = tf.train.Saver() # this one will save all variables that now exist new_saver.save(sess, new_fname) print("saved.") exit()
Example #10
Source File: run_summarization.py From MAX-Text-Summarizer with Apache License 2.0 | 6 votes |
def restore_best_model(): """Load bestmodel file from eval directory, add variables for adagrad, and save to train directory""" tf.logging.info("Restoring best model for training...") # Initialize all vars in the model sess = tf.Session(config=util.get_config()) print("Initializing all variables...") sess.run(tf.initialize_all_variables()) # Restore the best model from eval dir saver = tf.train.Saver([v for v in tf.all_variables() if "Adagrad" not in v.name]) print("Restoring all non-adagrad variables from best model in eval dir...") curr_ckpt = util.load_ckpt(saver, sess, "eval") print("Restored %s." % curr_ckpt) # Save this model to train dir and quit new_model_name = curr_ckpt.split("/")[-1].replace("bestmodel", "model") new_fname = os.path.join(FLAGS.log_root, "train", new_model_name) print("Saving model to %s..." % new_fname) new_saver = tf.train.Saver() # this saver saves all variables that now exist, including Adagrad variables new_saver.save(sess, new_fname) print("Saved.") exit()
Example #11
Source File: run_summarization.py From RLSeq2Seq with MIT License | 6 votes |
def convert_to_reinforce_model(self): """Load non-reinforce checkpoint, add initialized extra variables for reinforce, and save as new checkpoint""" tf.logging.info("converting non-reinforce model to reinforce model..") # initialize an entire reinforce model from scratch sess = tf.Session(config=util.get_config()) print("initializing everything...") sess.run(tf.global_variables_initializer()) # load all non-reinforce weights from checkpoint saver = tf.train.Saver([v for v in tf.global_variables() if "reinforce" not in v.name and "Adagrad" not in v.name]) print("restoring non-reinforce variables...") curr_ckpt = util.load_ckpt(saver, sess) print("restored.") # save this model and quit new_fname = curr_ckpt + '_rl_init' print("saving model to %s..." % (new_fname)) new_saver = tf.train.Saver() # this one will save all variables that now exist new_saver.save(sess, new_fname) print("saved.") exit()
Example #12
Source File: run_summarization.py From TransferRL with MIT License | 6 votes |
def restore_best_model(self): """Load bestmodel file from eval directory, add variables for adagrad, and save to train directory""" tf.logging.info("Restoring bestmodel for training...") # Initialize all vars in the model sess = tf.Session(config=util.get_config()) print("Initializing all variables...") sess.run(tf.initialize_all_variables()) # Restore the best model from eval dir saver = tf.train.Saver([v for v in tf.all_variables() if "Adagrad" not in v.name]) print("Restoring all non-adagrad variables from best model in eval dir...") curr_ckpt = util.load_ckpt(saver, sess, "eval") print("Restored %s." % curr_ckpt) # Save this model to train dir and quit new_model_name = curr_ckpt.split("/")[-1].replace("bestmodel", "model") new_fname = os.path.join(FLAGS.log_root, "train", new_model_name) print("Saving model to %s..." % (new_fname)) new_saver = tf.train.Saver() # this saver saves all variables that now exist, including Adagrad variables new_saver.save(sess, new_fname) print("Saved.") exit()
Example #13
Source File: run_summarization.py From RLSeq2Seq with MIT License | 6 votes |
def convert_to_coverage_model(self): """Load non-coverage checkpoint, add initialized extra variables for coverage, and save as new checkpoint""" tf.logging.info("converting non-coverage model to coverage model..") # initialize an entire coverage model from scratch sess = tf.Session(config=util.get_config()) print("initializing everything...") sess.run(tf.global_variables_initializer()) # load all non-coverage weights from checkpoint saver = tf.train.Saver([v for v in tf.global_variables() if "coverage" not in v.name and "Adagrad" not in v.name]) print("restoring non-coverage variables...") curr_ckpt = util.load_ckpt(saver, sess) print("restored.") # save this model and quit new_fname = curr_ckpt + '_cov_init' print("saving model to %s..." % (new_fname)) new_saver = tf.train.Saver() # this one will save all variables that now exist new_saver.save(sess, new_fname) print("saved.") exit()
Example #14
Source File: run_summarization.py From RLSeq2Seq with MIT License | 6 votes |
def restore_best_model(self): """Load bestmodel file from eval directory, add variables for adagrad, and save to train directory""" tf.logging.info("Restoring bestmodel for training...") # Initialize all vars in the model sess = tf.Session(config=util.get_config()) print("Initializing all variables...") sess.run(tf.initialize_all_variables()) # Restore the best model from eval dir saver = tf.train.Saver([v for v in tf.all_variables() if "Adagrad" not in v.name]) print("Restoring all non-adagrad variables from best model in eval dir...") curr_ckpt = util.load_ckpt(saver, sess, "eval") print("Restored %s." % curr_ckpt) # Save this model to train dir and quit new_model_name = curr_ckpt.split("/")[-1].replace("bestmodel", "model") new_fname = os.path.join(FLAGS.log_root, "train", new_model_name) print("Saving model to %s..." % (new_fname)) new_saver = tf.train.Saver() # this saver saves all variables that now exist, including Adagrad variables new_saver.save(sess, new_fname) print("Saved.") exit()
Example #15
Source File: run_summarization.py From TransferRL with MIT License | 6 votes |
def convert_to_reinforce_model(self): """Load non-reinforce checkpoint, add initialized extra variables for reinforce, and save as new checkpoint""" tf.logging.info("converting non-reinforce model to reinforce model..") # initialize an entire reinforce model from scratch sess = tf.Session(config=util.get_config()) print("initializing everything...") sess.run(tf.global_variables_initializer()) # load all non-reinforce weights from checkpoint saver = tf.train.Saver([v for v in tf.global_variables() if "reinforce" not in v.name and "Adagrad" not in v.name]) print("restoring non-reinforce variables...") curr_ckpt = util.load_ckpt(saver, sess) print("restored.") # save this model and quit new_fname = curr_ckpt + '_rl_init' print("saving model to %s..." % (new_fname)) new_saver = tf.train.Saver() # this one will save all variables that now exist new_saver.save(sess, new_fname) print("saved.") exit()
Example #16
Source File: decode.py From pointer-generator with Apache License 2.0 | 5 votes |
def __init__(self, model, batcher, vocab): """Initialize decoder. Args: model: a Seq2SeqAttentionModel object. batcher: a Batcher object. vocab: Vocabulary object """ self._model = model self._model.build_graph() self._batcher = batcher self._vocab = vocab self._saver = tf.train.Saver() # we use this to load checkpoints for decoding self._sess = tf.Session(config=util.get_config()) # Load an initial checkpoint to use for decoding ckpt_path = util.load_ckpt(self._saver, self._sess) if FLAGS.single_pass: # Make a descriptive decode directory name ckpt_name = "ckpt-" + ckpt_path.split('-')[-1] # this is something of the form "ckpt-123456" self._decode_dir = os.path.join(FLAGS.log_root, get_decode_dir_name(ckpt_name)) if os.path.exists(self._decode_dir): raise Exception("single_pass decode directory %s should not already exist" % self._decode_dir) else: # Generic decode dir name self._decode_dir = os.path.join(FLAGS.log_root, "decode") # Make the decode dir if necessary if not os.path.exists(self._decode_dir): os.mkdir(self._decode_dir) if FLAGS.single_pass: # Make the dirs to contain output written in the correct format for pyrouge self._rouge_ref_dir = os.path.join(self._decode_dir, "reference") if not os.path.exists(self._rouge_ref_dir): os.mkdir(self._rouge_ref_dir) self._rouge_dec_dir = os.path.join(self._decode_dir, "decoded") if not os.path.exists(self._rouge_dec_dir): os.mkdir(self._rouge_dec_dir)
Example #17
Source File: decode.py From MAX-Text-Summarizer with Apache License 2.0 | 5 votes |
def __init__(self, model, batcher, vocab): """Initialize decoder. Args: model: a Seq2SeqAttentionModel object. batcher: a Batcher object. vocab: Vocabulary object """ self._model = model self._model.build_graph() self._batcher = batcher self._vocab = vocab self._saver = tf.train.Saver() # we use this to load checkpoints for decoding self._sess = tf.Session(config=util.get_config()) # Load an initial checkpoint to use for decoding ckpt_path = util.load_ckpt(self._saver, self._sess) # if FLAGS.single_pass: # # Make a descriptive decode directory name # ckpt_name = "ckpt-" + ckpt_path.split('-')[-1] # this is something of the form "ckpt-123456" # self._decode_dir = os.path.join(FLAGS.log_root, get_decode_dir_name(ckpt_name)) # if os.path.exists(self._decode_dir): # raise Exception("single_pass decode directory %s should not already exist" % self._decode_dir) # # else: # Generic decode dir name self._decode_dir = os.path.join(FLAGS.log_root, "decode") # Make the decode dir if necessary if not os.path.exists(self._decode_dir): os.mkdir(self._decode_dir) # if FLAGS.single_pass: # # Make the dirs to contain output written in the correct format for pyrouge # self._rouge_ref_dir = os.path.join(self._decode_dir, "reference") # if not os.path.exists(self._rouge_ref_dir): os.mkdir(self._rouge_ref_dir) # self._rouge_dec_dir = os.path.join(self._decode_dir, "decoded") # if not os.path.exists(self._rouge_dec_dir): os.mkdir(self._rouge_dec_dir)
Example #18
Source File: run_summarization.py From rotational-unit-of-memory with MIT License | 5 votes |
def restore_best_model(): """Load bestmodel file from eval directory, add variables for adagrad, and save to train directory""" tf.logging.info("Restoring bestmodel for training...") # Initialize all vars in the model sess = tf.Session(config=util.get_config()) print("Initializing all variables...") sess.run(tf.initialize_all_variables()) # Restore the best model from eval dir saver = tf.train.Saver( [v for v in tf.all_variables() if "Adagrad" not in v.name]) print("Restoring all non-adagrad variables from best model in eval dir...") curr_ckpt = util.load_ckpt(saver, sess, "eval") print("Restored %s." % curr_ckpt) # Save this model to train dir and quit new_model_name = curr_ckpt.split("/")[-1].replace("bestmodel", "model") new_fname = os.path.join(FLAGS.log_root, "train", new_model_name) print("Saving model to %s..." % (new_fname)) # this saver saves all variables that now exist, including Adagrad # variables new_saver = tf.train.Saver() new_saver.save(sess, new_fname) print("Saved.") exit()
Example #19
Source File: run_summarization.py From long-summarization with Apache License 2.0 | 5 votes |
def convert_to_coverage_model(): """Load non-coverage checkpoint, add initialized extra variables for coverage, and save as new checkpoint""" tf.logging.info("converting non-coverage model to coverage model..") # initialize an entire coverage model from scratch sess = tf.Session(config=util.get_config()) if FLAGS.debug: print('entering debug mode') sess = tf_debug.LocalCLIDebugWrapperSession(sess, ui_type=FLAGS.ui_type) sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan) print("initializing everything...") sess.run(tf.global_variables_initializer()) # load all non-coverage weights from checkpoint saver = tf.train.Saver([v for v in tf.global_variables( ) if "coverage" not in v.name and "Adagrad" not in v.name]) print("restoring non-coverage variables...") curr_ckpt = util.load_ckpt(saver, sess) print("restored.") # save this model and quit new_fname = curr_ckpt + '_cov_init' print(("saving model to %s..." % (new_fname))) new_saver = tf.train.Saver() # this one will save all variables that now exist new_saver.save(sess, new_fname) print("saved.") exit()
Example #20
Source File: decode.py From long-summarization with Apache License 2.0 | 5 votes |
def __init__(self, model, batcher, vocab): """Initialize decoder. Args: model: a Seq2SeqAttentionModel object. batcher: a Batcher object. vocab: Vocabulary object """ self._model = model self._model.build_graph() self._batcher = batcher self._vocab = vocab self._saver = tf.train.Saver() # we use this to load checkpoints for decoding self._sess = tf.Session(config=util.get_config()) # Load an initial checkpoint to use for decoding decode_checkpoint = FLAGS.decode_checkpoint if FLAGS.decode_checkpoint else None ckpt_path = util.load_ckpt(self._saver, self._sess, latest_filename=decode_checkpoint) if FLAGS.single_pass: # Make a descriptive decode directory name ckpt_name = "ckpt-" + ckpt_path.split('-')[-1] # this is something of the form "ckpt-123456" self._decode_dir = os.path.join(FLAGS.log_root, get_decode_dir_name(ckpt_name)) if os.path.exists(self._decode_dir): if not FLAGS.custom_decode_name: raise Exception("single_pass decode directory %s should not already exist" % self._decode_dir) else: self._decode_dir = os.path.join(FLAGS.log_root, get_decode_dir_name(ckpt_name)) + '_' + FLAGS.custom_decode_name else: # Generic decode dir name self._decode_dir = os.path.join(FLAGS.log_root, "decode") # Make the decode dir if necessary if not os.path.exists(self._decode_dir): os.mkdir(self._decode_dir) if FLAGS.single_pass: # Make the dirs to contain output written in the correct format for pyrouge self._rouge_ref_dir = os.path.join(self._decode_dir, "reference") if not os.path.exists(self._rouge_ref_dir): os.mkdir(self._rouge_ref_dir) self._rouge_dec_dir = os.path.join(self._decode_dir, "decoded") if not os.path.exists(self._rouge_dec_dir): os.mkdir(self._rouge_dec_dir)
Example #21
Source File: run_selector.py From unified-summarization with MIT License | 4 votes |
def run_training(model, batcher, sess_context_manager, sv, summary_writer, pretrained_saver=None, saver=None): """Repeatedly runs training iterations, logging loss to screen and writing summaries""" tf.logging.info("starting run_training") ckpt_path = os.path.join(FLAGS.log_root, "train", "model.ckpt") with sess_context_manager as sess: if FLAGS.pretrained_selector_path: tf.logging.info('Loading pretrained selector model') _ = util.load_ckpt(pretrained_saver, sess, ckpt_path=FLAGS.pretrained_selector_path) for _ in range(FLAGS.max_train_iter): # repeats until interrupted batch = batcher.next_batch() tf.logging.info('running training step...') t0=time.time() results = model.run_train_step(sess, batch) t1=time.time() tf.logging.info('seconds for training step: %.3f', t1-t0) loss = results['loss'] tf.logging.info('loss: %f', loss) # print the loss to screen if not np.isfinite(loss): raise Exception("Loss is not finite. Stopping.") train_step = results['global_step'] # we need this to update our running average loss recall, ratio, _ = util.get_batch_ratio(batch.original_articles_sents, \ batch.original_extracts_ids, results['probs']) write_to_summary(ratio, 'SentSelector/select_ratio/recall=0.9', train_step, summary_writer) # get the summaries and iteration number so we can write summaries to tensorboard summaries = results['summaries'] # we will write these summaries to tensorboard using summary_writer summary_writer.add_summary(summaries, train_step) # write the summaries if train_step % 100 == 0: # flush the summary writer every so often summary_writer.flush() if train_step % FLAGS.save_model_every == 0: if FLAGS.pretrained_selector_path: saver.save(sess, ckpt_path, global_step=train_step) else: sv.saver.save(sess, ckpt_path, global_step=train_step) print 'Step: ', train_step
Example #22
Source File: decode.py From TransferRL with MIT License | 4 votes |
def decode(self): """Decode examples until data is exhausted (if FLAGS.single_pass) and return, or decode indefinitely, loading latest checkpoint at regular intervals""" t0 = time.time() counter = FLAGS.decode_after while True: tf.reset_default_graph() batch = self._batcher.next_batch() # 1 example repeated across batch if batch is None: # finished decoding dataset in single_pass mode assert FLAGS.single_pass, "Dataset exhausted, but we are not in single_pass mode" tf.logging.info("Decoder has finished reading dataset for single_pass.") tf.logging.info("Output has been saved in %s and %s. Now starting ROUGE eval...", self._rouge_ref_dir, self._rouge_dec_dir) results_dict = rouge_eval(self._rouge_ref_dir, self._rouge_dec_dir) rouge_log(results_dict, self._decode_dir) return original_article = batch.original_articles[0] # string original_abstract = batch.original_abstracts[0] # string original_abstract_sents = batch.original_abstracts_sents[0] # list of strings if len(original_abstract_sents) == 0: print("NOOOOO!!!!, An empty abstract :(") continue article_withunks = data.show_art_oovs(original_article, self._vocab) # string abstract_withunks = data.show_abs_oovs(original_abstract, self._vocab, (batch.art_oovs[0] if FLAGS.pointer_gen else None)) # string # Run beam search to get best Hypothesis if FLAGS.ac_training: best_hyp = beam_search.run_beam_search(self._sess, self._model, self._vocab, batch, self._dqn, self._dqn_sess, self._dqn_graph) else: best_hyp = beam_search.run_beam_search(self._sess, self._model, self._vocab, batch) # Extract the output ids from the hypothesis and convert back to words output_ids = [int(t) for t in best_hyp.tokens[1:]] decoded_words = data.outputids2words(output_ids, self._vocab, (batch.art_oovs[0] if FLAGS.pointer_gen else None)) # Remove the [STOP] token from decoded_words, if necessary try: fst_stop_idx = decoded_words.index(data.STOP_DECODING) # index of the (first) [STOP] symbol decoded_words = decoded_words[:fst_stop_idx] except ValueError: decoded_words = decoded_words decoded_output = ' '.join(decoded_words) # single string if FLAGS.single_pass: self.write_for_rouge(original_abstract_sents, decoded_words, counter) # write ref summary and decoded summary to file, to eval with pyrouge later counter += 1 # this is how many examples we've decoded else: print_results(article_withunks, abstract_withunks, decoded_output) # log output to screen self.write_for_attnvis(article_withunks, abstract_withunks, decoded_words, best_hyp.attn_dists, best_hyp.p_gens) # write info to .json file for visualization tool # Check if SECS_UNTIL_NEW_CKPT has elapsed; if so return so we can load a new checkpoint t1 = time.time() if t1-t0 > SECS_UNTIL_NEW_CKPT: tf.logging.info('We\'ve been decoding with same checkpoint for %i seconds. Time to load new checkpoint', t1-t0) _ = util.load_ckpt(self._saver, self._sess, FLAGS.decode_from) t0 = time.time()
Example #23
Source File: run_selector.py From unified-summarization with MIT License | 4 votes |
def run_eval(model, batcher): """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_ratio = 0 # the eval job keeps a smoother, running average loss to tell it when to implement early stopping best_ratio = None # will hold the best loss achieved so far train_dir = os.path.join(FLAGS.log_root, "train") while True: ckpt_state = tf.train.get_checkpoint_state(train_dir) #tf.logging.info('max_enc_steps: %d, max_dec_steps: %d', FLAGS.max_enc_steps, FLAGS.max_dec_steps) _ = 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) train_step = results['global_step'] recall, ratio, _ = util.get_batch_ratio(batch.original_articles_sents, \ batch.original_extracts_ids, results['probs']) write_to_summary(ratio, 'SentSelector/select_ratio/recall=0.9', train_step, summary_writer) # add summaries summaries = results['summaries'] summary_writer.add_summary(summaries, train_step) # calculate running avg loss running_avg_ratio = util.calc_running_avg_loss(ratio, running_avg_ratio, summary_writer, train_step, 'running_avg_ratio') # 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_ratio is None or running_avg_ratio < best_ratio: tf.logging.info('Found new best model with %.3f running_avg_ratio. Saving to %s', running_avg_ratio, bestmodel_save_path) saver.save(sess, bestmodel_save_path, global_step=train_step, latest_filename='checkpoint_best') best_ratio = running_avg_ratio # flush the summary writer every so often if train_step % 100 == 0: summary_writer.flush()
Example #24
Source File: run_rewriter.py From unified-summarization with MIT License | 4 votes |
def run_eval(model, batcher): """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_loss") # 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 train_dir = os.path.join(FLAGS.log_root, "train") while True: ckpt_state = tf.train.get_checkpoint_state(train_dir) tf.logging.info('max_enc_steps: %d, max_dec_steps: %d', FLAGS.max_enc_steps, FLAGS.max_dec_steps) _ = 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) train_step = results['global_step'] tf.logging.info("pgen_avg: %f", results['p_gen_avg']) if FLAGS.coverage: tf.logging.info("coverage_loss: %f", results['coverage_loss']) # add summaries summaries = results['summaries'] summary_writer.add_summary(summaries, train_step) # calculate running avg loss running_avg_loss = util.calc_running_avg_loss(np.asscalar(loss), running_avg_loss, summary_writer, train_step, 'running_avg_loss') # 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()
Example #25
Source File: decode.py From unified-summarization with MIT License | 4 votes |
def prepare_evaluate(self, ckpt_path=None): # Load an initial checkpoint to use for decoding if FLAGS.mode == 'evalall': if FLAGS.load_best_eval_model: tf.logging.info('Loading best eval checkpoint') ckpt_path = util.load_ckpt(self._saver, self._sess, ckpt_dir='eval_'+FLAGS.eval_method) elif FLAGS.eval_ckpt_path: ckpt_path = util.load_ckpt(self._saver, self._sess, ckpt_path=FLAGS.eval_ckpt_path) else: tf.logging.info('Loading best train checkpoint') ckpt_path = util.load_ckpt(self._saver, self._sess) elif FLAGS.mode == 'eval': _ = util.load_ckpt(self._saver, self._sess, ckpt_path=ckpt_path) # load a new checkpoint if FLAGS.single_pass: # Make a descriptive decode directory name ckpt_name = "ckpt-" + ckpt_path.split('-')[-1] # this is something of the form "ckpt-123456" self._decode_dir = os.path.join(FLAGS.log_root, get_decode_dir_name(ckpt_name)) tf.logging.info('Save evaluation results to '+ self._decode_dir) if os.path.exists(self._decode_dir): if FLAGS.mode == 'eval': return False # The checkpoint has already been evaluated. Evaluate next one. else: raise Exception("single_pass decode directory %s should not already exist" % self._decode_dir) else: # Generic decode dir name self._decode_dir = os.path.join(FLAGS.log_root, "decode") # Make the decode dir if necessary if not os.path.exists(self._decode_dir): os.mkdir(self._decode_dir) if FLAGS.single_pass: # Make the dirs to contain output written in the correct format for pyrouge self._rouge_ref_dir = os.path.join(self._decode_dir, "reference") if not os.path.exists(self._rouge_ref_dir): os.mkdir(self._rouge_ref_dir) self._rouge_dec_dir = os.path.join(self._decode_dir, "decoded") if not os.path.exists(self._rouge_dec_dir): os.mkdir(self._rouge_dec_dir) if FLAGS.save_vis: self._rouge_vis_dir = os.path.join(self._decode_dir, "visualize") if not os.path.exists(self._rouge_vis_dir): os.mkdir(self._rouge_vis_dir) if FLAGS.save_pkl: self._result_dir = os.path.join(self._decode_dir, "result") if not os.path.exists(self._result_dir): os.mkdir(self._result_dir) return True
Example #26
Source File: run_summarization.py From pointer-generator with Apache License 2.0 | 4 votes |
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()
Example #27
Source File: decode.py From pointer-generator with Apache License 2.0 | 4 votes |
def decode(self): """Decode examples until data is exhausted (if FLAGS.single_pass) and return, or decode indefinitely, loading latest checkpoint at regular intervals""" t0 = time.time() counter = 0 while True: batch = self._batcher.next_batch() # 1 example repeated across batch if batch is None: # finished decoding dataset in single_pass mode assert FLAGS.single_pass, "Dataset exhausted, but we are not in single_pass mode" tf.logging.info("Decoder has finished reading dataset for single_pass.") tf.logging.info("Output has been saved in %s and %s. Now starting ROUGE eval...", self._rouge_ref_dir, self._rouge_dec_dir) results_dict = rouge_eval(self._rouge_ref_dir, self._rouge_dec_dir) rouge_log(results_dict, self._decode_dir) return original_article = batch.original_articles[0] # string original_abstract = batch.original_abstracts[0] # string original_abstract_sents = batch.original_abstracts_sents[0] # list of strings article_withunks = data.show_art_oovs(original_article, self._vocab) # string abstract_withunks = data.show_abs_oovs(original_abstract, self._vocab, (batch.art_oovs[0] if FLAGS.pointer_gen else None)) # string # Run beam search to get best Hypothesis best_hyp = beam_search.run_beam_search(self._sess, self._model, self._vocab, batch) # Extract the output ids from the hypothesis and convert back to words output_ids = [int(t) for t in best_hyp.tokens[1:]] decoded_words = data.outputids2words(output_ids, self._vocab, (batch.art_oovs[0] if FLAGS.pointer_gen else None)) # Remove the [STOP] token from decoded_words, if necessary try: fst_stop_idx = decoded_words.index(data.STOP_DECODING) # index of the (first) [STOP] symbol decoded_words = decoded_words[:fst_stop_idx] except ValueError: decoded_words = decoded_words decoded_output = ' '.join(decoded_words) # single string if FLAGS.single_pass: self.write_for_rouge(original_abstract_sents, decoded_words, counter) # write ref summary and decoded summary to file, to eval with pyrouge later counter += 1 # this is how many examples we've decoded else: print_results(article_withunks, abstract_withunks, decoded_output) # log output to screen self.write_for_attnvis(article_withunks, abstract_withunks, decoded_words, best_hyp.attn_dists, best_hyp.p_gens) # write info to .json file for visualization tool # Check if SECS_UNTIL_NEW_CKPT has elapsed; if so return so we can load a new checkpoint t1 = time.time() if t1-t0 > SECS_UNTIL_NEW_CKPT: tf.logging.info('We\'ve been decoding with same checkpoint for %i seconds. Time to load new checkpoint', t1-t0) _ = util.load_ckpt(self._saver, self._sess) t0 = time.time()
Example #28
Source File: evaluate.py From unified-summarization with MIT License | 4 votes |
def prepare_evaluate(self, ckpt_path=None): # Load an initial checkpoint to use for decoding if FLAGS.mode == 'evalall': if FLAGS.load_best_eval_model: tf.logging.info('Loading best eval checkpoint') ckpt_path = util.load_ckpt(self._saver, self._sess, ckpt_dir='eval'+FLAGS.eval_method) elif FLAGS.eval_ckpt_path: ckpt_path = util.load_ckpt(self._saver, self._sess, ckpt_path=FLAGS.eval_ckpt_path) else: tf.logging.info('Loading best train checkpoint') ckpt_path = util.load_ckpt(self._saver, self._sess) elif FLAGS.mode == 'eval': _ = util.load_ckpt(self._saver, self._sess, ckpt_path=ckpt_path) # load a new checkpoint if FLAGS.single_pass: # Make a descriptive decode directory name ckpt_name = "ckpt-" + ckpt_path.split('-')[-1] # this is something of the form "ckpt-123456" self._decode_dir = os.path.join(FLAGS.log_root, get_decode_dir_name(ckpt_name)) tf.logging.info('Save evaluation results to '+ self._decode_dir) if os.path.exists(self._decode_dir): if FLAGS.mode == 'eval': return False # The checkpoint has already been evaluated. Evaluate next one. else: raise Exception("single_pass decode directory %s should not already exist" % self._decode_dir) else: # Generic decode dir name self._decode_dir = os.path.join(FLAGS.log_root, "decode") # Make the decode dir if necessary if not os.path.exists(self._decode_dir): os.mkdir(self._decode_dir) if FLAGS.single_pass: # Make the dirs to contain output written in the correct format for pyrouge self._rouge_ref_dir = os.path.join(self._decode_dir, "reference") if not os.path.exists(self._rouge_ref_dir): os.mkdir(self._rouge_ref_dir) self._rouge_dec_dir = os.path.join(self._decode_dir, "decoded") if not os.path.exists(self._rouge_dec_dir): os.mkdir(self._rouge_dec_dir) if FLAGS.save_vis: self._rouge_vis_dir = os.path.join(self._decode_dir, "visualize") if not os.path.exists(self._rouge_vis_dir): os.mkdir(self._rouge_vis_dir) if FLAGS.save_pkl: self._result_dir = os.path.join(self._decode_dir, "result") if not os.path.exists(self._result_dir): os.mkdir(self._result_dir) return True
Example #29
Source File: run_end2end.py From unified-summarization with MIT License | 4 votes |
def run_eval(model, batcher): """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_loss") # 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 running_avg_ratio = 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 train_dir = os.path.join(FLAGS.log_root, "train") while True: ckpt_state = tf.train.get_checkpoint_state(train_dir) tf.logging.info('max_enc_steps: %d, max_dec_steps: %d', FLAGS.max_enc_steps, FLAGS.max_dec_steps) _ = 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) train_step = results['global_step'] tf.logging.info("pgen_avg: %f", results['p_gen_avg']) if FLAGS.coverage: tf.logging.info("coverage_loss: %f", results['coverage_loss']) if FLAGS.inconsistent_loss: tf.logging.info('inconsistent_loss: %f', results['inconsist_loss']) tf.logging.info("selector_loss: %f", results['selector_loss']) recall, ratio, _ = util.get_batch_ratio(batch.original_articles_sents, batch.original_extracts_ids, results['probs']) write_to_summary(ratio, 'SentSelector/select_ratio/recall=0.9', train_step, summary_writer) # add summaries summaries = results['summaries'] summary_writer.add_summary(summaries, train_step) # calculate running avg loss running_avg_loss = util.calc_running_avg_loss(np.asscalar(loss), running_avg_loss, summary_writer, train_step, 'running_avg_loss') running_avg_ratio = util.calc_running_avg_loss(ratio, running_avg_ratio, summary_writer, train_step, 'running_avg_ratio') # 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()
Example #30
Source File: run_end2end.py From unified-summarization with MIT License | 4 votes |
def run_training(model, batcher, sess_context_manager, sv, summary_writer, \ selector_saver=None, rewriter_saver=None, all_saver=None): """Repeatedly runs training iterations, logging loss to screen and writing summaries""" tf.logging.info("starting run_training") ckpt_path = os.path.join(FLAGS.log_root, "train", "model.ckpt_cov") with sess_context_manager as sess: if FLAGS.pretrained_selector_path: tf.logging.info('Loading selector model') _ = util.load_ckpt(selector_saver, sess, ckpt_path=FLAGS.pretrained_selector_path) if FLAGS.pretrained_rewriter_path: tf.logging.info('Loading rewriter model') _ = util.load_ckpt(rewriter_saver, sess, ckpt_path=FLAGS.pretrained_rewriter_path) for _ in range(FLAGS.max_train_iter): # repeats until interrupted batch = batcher.next_batch() tf.logging.info('running training step...') t0=time.time() results = model.run_train_step(sess, batch) t1=time.time() tf.logging.info('seconds for training step: %.3f', t1-t0) loss = results['loss'] tf.logging.info('loss: %f', loss) # print the loss to screen train_step = results['global_step'] if not np.isfinite(loss): raise Exception("Loss is not finite. Stopping.") tf.logging.info("pgen_avg: %f", results['p_gen_avg']) if FLAGS.coverage: tf.logging.info("coverage_loss: %f", results['coverage_loss']) # print the coverage loss to screen if FLAGS.inconsistent_loss: tf.logging.info('inconsistent_loss: %f', results['inconsist_loss']) tf.logging.info("selector_loss: %f", results['selector_loss']) recall, ratio, _ = util.get_batch_ratio(batch.original_articles_sents, batch.original_extracts_ids, results['probs']) write_to_summary(ratio, 'SentSelector/select_ratio/recall=0.9', train_step, summary_writer) # get the summaries and iteration number so we can write summaries to tensorboard summaries = results['summaries'] # we will write these summaries to tensorboard using summary_writer summary_writer.add_summary(summaries, train_step) # write the summaries if train_step % 100 == 0: # flush the summary writer every so often summary_writer.flush() if train_step % FLAGS.save_model_every == 0: if FLAGS.pretrained_selector_path and FLAGS.pretrained_rewriter_path: all_saver.save(sess, ckpt_path, global_step=train_step) else: sv.saver.save(sess, ckpt_path, global_step=train_step) print 'Step: ', train_step