def train(self, x_train, y_train, x_valid=None, y_valid=None): # 获取训练数据和验证数据 train_steps, train_batches = batch_iter( x_train, y_train, self.training_config.batch_size) valid_steps, valid_batches = batch_iter( x_valid, y_valid, self.training_config.batch_size) # 模型训练参数 self.model.model.compile( loss=self.training_config.loss_func, optimizer=Adam(lr=self.training_config.learning_rate), metrics=['accuracy']) if not self.training_mode: callbacks = get_callbacks( log_dir=self.checkpoint_path, tensorBoard=self.tensorboard, LRScheduler=self.lrscheduler, early_stopping=self.training_config.early_stopping, valid=(valid_steps, valid_batches)) else: callbacks = get_callbacks( LRScheduler=self.lrscheduler, early_stopping=self.training_config.early_stopping, valid=(valid_steps, valid_batches)) # 训练模型 self.model.model.fit_generator(generator=train_batches, steps_per_epoch=train_steps, epochs=self.training_config.max_epoch, callbacks=callbacks) return self.model
def eval(self, x_test, y_test): train_steps, train_batches = batch_iter( x_test, y_test, batch_size=64, # Todo: if batch_size=1, eval does not work. shuffle=False) res = self.model.model.evaluate_generator(generator=train_batches, steps=train_steps) return res
def main(_): train_path = os.path.join(FLAGS.data_wdw, 'train') val_path = os.path.join(FLAGS.data_wdw, 'val') test_path = os.path.join(FLAGS.data_wdw, 'test') print("Loading train data from %s" % train_path) train = RawInput(rn.load_data(train_path)) print("Loading val data from %s" % val_path) val = RawInput(rn.load_data(val_path), vocabulary=train.vocab) if len(train.labels_idx) < len(val.labels_idx): print("More validation choices than train") print("Loading test data from %s" % test_path) test = RawInput(rn.load_data(test_path), vocabulary=train.vocab) if len(train.labels_idx) < len(test.labels_idx): print("More test choices than train") with tf.Graph().as_default(): initializer = tf.random_uniform_initializer(-FLAGS.init_scale, FLAGS.init_scale) print("Loading model..") with tf.name_scope("Train"): with tf.variable_scope("Model", reuse=None, initializer=initializer): m = Model(is_training=True, vocab_size=train.vocab_size, labels_idx=train.labels_idx) sv = tf.train.Supervisor(logdir=FLAGS.save_path) with sv.managed_session() as session: for i in range(FLAGS.max_epoch): train_iter = rn.batch_iter( train.contexts, train.questions, train.choices, train.labels, train.choices_map, train.context_lens, train.qs_lens, batch_size=FLAGS.batch_size, context_num_steps=FLAGS.context_steps, question_num_steps=FLAGS.question_steps) # lr_decay = config.lr_decay ** max(i - config.max_epoch, 0.0) # m.assign_lr(session, config.learning_rate * lr_decay) val_iter = rn.batch_iter( val.contexts, val.questions, val.choices, val.labels, val.choices_map, val.context_lens, val.qs_lens, batch_size=FLAGS.batch_size, context_num_steps=FLAGS.context_steps, question_num_steps=FLAGS.question_steps) print("Epoch: %d" % (i + 1)) run_epoch(session, m, train_iter, eval_op=m.train_op, verbose=True) print("Checking on validation set.") ave_cost, ave_acc = run_epoch(session, m, val_iter, eval_op=None, verbose=False) print("Avg. Val Accuracy: %s" % ave_acc) print("Avg. Vac Cost: %s" % ave_cost) test_iter = rn.batch_iter(test.contexts, test.questions, test.choices, test.labels, test.choices_map, test.context_lens, test.qs_lens, batch_size=FLAGS.batch_size, context_num_steps=c_steps, question_num_steps=q_steps) print("\nChecking on test set.") test_acc = run_epoch(session, m, test_iter, eval_op=None, verbose=False) print("\nAvg. Test Accuracy: %s\n" % test_acc) if FLAGS.save_path: print("Saving model to %s." % FLAGS.save_path) sv.saver.save(session, FLAGS.save_path, global_step=sv.global_step)
def main(_): data_path = FLAGS.data_path if ((FLAGS.clear_save) & (os.path.exists(FLAGS.save_path))): shutil.rmtree(FLAGS.save_path) if FLAGS.testing: train_path = os.path.join(data_path, 'test') else: train_path = os.path.join(data_path, 'train') val_path = os.path.join(data_path, 'val') test_path = os.path.join(data_path, 'test') if not os.path.exists(FLAGS.save_path): os.makedirs(FLAGS.save_path) log_fi = os.path.join(FLAGS.save_path,'output.log') lg.basicConfig(filename=log_fi,level=lg.DEBUG,\ format='%(asctime)s %(message)s') # print("Loading train data from %s" % train_path) train = RawInput(rn.load_data(train_path, return_entities=True)) print("Loading val data from %s"%val_path) val = RawInput(rn.load_data(val_path, return_entities=True), vocabulary=train.vocab) print("Loading test data from %s" % test_path) test = RawInput(rn.load_data(test_path, return_entities=True), vocabulary=train.vocab) if FLAGS.use_glove: embedding = rn.glove_embedding(FLAGS.glove_path,train.vocab) else: embedding = None with tf.Graph().as_default(): initializer = tf.random_uniform_initializer(-FLAGS.init_scale, FLAGS.init_scale) print("Loading model..") with tf.name_scope("Train"): with tf.variable_scope("Model", reuse=None, initializer=initializer): if FLAGS.use_glove: m = Model(vocab_size=train.vocab_size, choices_idx=train.transformed_labels_idx, pre_embedding=embedding) else: m = Model(vocab_size=train.vocab_size, choices_idx=train.transformed_labels_idx) with tf.Session() as session: saver = tf.train.Saver(tf.all_variables()) ckpt = tf.train.get_checkpoint_state(FLAGS.save_path) if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path): print("Loading parameters from %s" % ckpt.model_checkpoint_path) lg.info("Loading parameters from %s" % ckpt.model_checkpoint_path) saver.restore(session, ckpt.model_checkpoint_path) else: print("New session.") lg.info("New session.") session.run(tf.initialize_all_variables()) all_st = time.time() for i in range(FLAGS.max_epoch): train_iter = rn.batch_iter( train.contexts, train.questions, train.choices, train.labels, train.choices_map, train.context_lens, train.qs_lens, batch_size=FLAGS.batch_size, entity_inds=train.entities) train_cost, train_acc = run_epoch( session, m, train_iter, train_op=m.train_op, verbose=False, vocab=train.vocab) print("Train cost: after " + str(i) + " epoch is " + str(train_cost)) print("Train acc: after " + str(i) + " epoch is " + str(train_acc)) lg.info("Train cost: after " + str(i) + " epoch is " + str(train_cost)) lg.info("Train acc: after " + str(i) + "epoch is " + str(train_acc)) if i % FLAGS.ckpt_steps == 0: checkpoint_path = os.path.join(FLAGS.save_path, "wdw.ckpt") saver.save(session, checkpoint_path, global_step=i) val_iter = rn.batch_iter( val.contexts, val.questions, val.choices, val.labels, val.choices_map, val.context_lens, val.qs_lens, batch_size=FLAGS.batch_size, entity_inds=val.entities) val_cost, val_acc = run_epoch( session, m, val_iter, train_op=None, verbose=False, vocab=train.vocab, is_testing=True) lg.info("Val cost: after " + str(i) + " epoch is " + str(val_cost)) lg.info("Val acc: after " + str(i) + " epoch is " + str(val_acc)) print("Val cost: after " + str(i) + " epoch is " + str(val_cost)) print("Val acc: after " + str(i) + " epoch is " + str(val_acc)) test_iter = rn.batch_iter( test.contexts, test.questions, test.choices, test.labels, test.choices_map, test.context_lens, test.qs_lens, batch_size=FLAGS.batch_size, entity_inds=test.entities) print("Checking on test set.") test_cost, test_acc = run_epoch(session, m, test_iter, train_op=None, verbose=False, vocab=train.vocab,is_testing=True) test_str = ("Test Accuracy: %s\n" % test_acc) print(test_str) lg.info(test_str)
# Load the saved meta graph and restore variables: saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file)) saver.restore(sess, checkpoint_file) # Get the placeholders from the graph by name: input_x = graph.get_operation_by_name("input_x").outputs[0] dropout_keep_prob = graph.get_operation_by_name("dropout_keep_prob").outputs[0] # Tensors we want to evaluate: predictions = graph.get_operation_by_name("output/predictions").outputs[0] # Generate batches: sn_length = reader._length(input_data) max_length = max(sn_length) mask = reader._mask(input_data, max_length) batches = reader.batch_iter(input_id, max_length, mask) batch_size = graph.get_operation_by_name("batch_size").outputs[0] # Collect the predictions: indx = 0 for batch in batches: x_batch = batch[0] z_batch = batch[1] batch_predictions = sess.run( predictions, {input_x: x_batch, batch_size: 1, dropout_keep_prob: 1.0} ) words = input_data[indx].split() # "E" stands for disfluent words and "F" for fluent words: for i in range(sn_length[indx]): label = 'E' if batch_predictions[i] == 1 else 'F' output_file.write(words[i]+' '+label+' ')
vocab_size=train.vocab_size, choices_idx=train.transformed_labels_idx, keep_prob=config.keep_prob) #tf.scalar_summary("Training Loss", m.cost) #tf.scalar_summary("Accuracy",m.acc) #tf.scalar_summary("Learning Rate", m.lr) # sv = tf.train.Supervisor(logdir="/home/manoj") with tf.Session() as session: all_st = time.time() for i in range(config.max_epoch): train_iter = rn.batch_iter(train.contexts, train.questions, train.choices, train.labels, train.choices_map, train.context_lens, train.qs_lens, batch_size=config.batch_size) session.run(tf.initialize_all_variables()) train_acc = run_epoch(session, m, train_iter, train_op=m.train_op, verbose=True, vocab=train.vocab) # # val_iter = rn.batch_iter( # val.contexts, val.questions, # val.choices, val.labels, val.choices_map, val.context_lens, # val.qs_lens, batch_size=config.batch_size,
from reader import load_data from reader import get_vocab from reader import vocab_transform from reader import batch_iter contexts, questions, choices, labels, choices_map, context_lens, qs_lens =\ load_data(data_path="wdw/test") # # 2. Fit vocabulary with questions and context. vocab = get_vocab(contexts, questions) # # 3. Transform context and questions contexts = vocab_transform(contexts, vocab) questions = vocab_transform(questions, vocab) # 4. Give to batch_iter readers = batch_iter(contexts, questions, choices, labels, choices_map, context_lens, qs_lens) # for q, c, ch, lab, ch_map, c_lens, q_lens in readers: # print(c.shape) # break