data, labels, lengths, test_size=FLAGS.test_size, random_state=22) # Batch iterator train_data = data_helper.batch_iter(x_train, y_train, train_lengths, FLAGS.batch_size, FLAGS.num_epochs) # Train # ============================================================================= with tf.Graph().as_default(): with tf.Session() as sess: if FLAGS.clf == 'cnn': classifier = cnn_clf(FLAGS) elif FLAGS.clf == 'lstm' or FLAGS.clf == 'blstm': classifier = rnn_clf(FLAGS) elif FLAGS.clf == 'clstm': classifier = clstm_clf(FLAGS) else: raise ValueError('clf should be one of [cnn, lstm, blstm, clstm]') # Train procedure global_step = tf.Variable(0, name='global_step', trainable=False) # Learning rate decay starter_learning_rate = FLAGS.learning_rate learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, FLAGS.decay_steps, FLAGS.decay_rate, staircase=True) optimizer = tf.train.AdamOptimizer(learning_rate) grads_and_vars = optimizer.compute_gradients(classifier.cost) train_op = optimizer.apply_gradients(grads_and_vars,
def main(_): if FLAGS.clf == 'lstm': FLAGS.embedding_size = FLAGS.hidden_size elif FLAGS.clf == 'clstm': FLAGS.hidden_size = len( FLAGS.filter_sizes.split(",")) * FLAGS.num_filters # Output files directory timestamp = str(int(time.time())) model_dir = os.path.join(os.path.curdir, 'model') params_dir = os.path.join(os.path.curdir, 'params') if not os.path.exists(params_dir): os.makedirs(params_dir) # Load and save data # ============================================================================= data, labels, lengths, vocab_processor = data_helper.load_data( file_path=FLAGS.data_file, sw_path=FLAGS.stop_word_file, min_frequency=FLAGS.min_frequency, max_length=FLAGS.max_length, language=FLAGS.language, shuffle=True) # Save vocabulary processor vocab_processor.save(os.path.join(params_dir, 'vocab')) FLAGS.vocab_size = len(vocab_processor.vocabulary_._mapping) FLAGS.max_length = vocab_processor.max_document_length params = FLAGS.flag_values_dict() # Print parameters model = params['clf'] if model == 'cnn': del params['hidden_size'] del params['num_layers'] elif model == 'lstm' or model == 'blstm': del params['num_filters'] del params['filter_sizes'] params['embedding_size'] = params['hidden_size'] elif model == 'clstm': params['hidden_size'] = len( list(map( int, params['filter_sizes'].split(",")))) * params['num_filters'] params_dict = sorted(params.items(), key=lambda x: x[0]) print('Parameters:') for item in params_dict: print('{}: {}'.format(item[0], item[1])) print('') # Save parameters to file params_file = open(os.path.join(params_dir, 'params.pkl'), 'wb') pkl.dump(params, params_file, True) params_file.close() # Simple Cross validation x_train, x_valid, y_train, y_valid, train_lengths, valid_lengths = train_test_split( data, labels, lengths, test_size=FLAGS.test_size, random_state=22) # Batch iterator train_data = data_helper.batch_iter(x_train, y_train, train_lengths, FLAGS.batch_size, FLAGS.num_epochs) # Train # ============================================================================= with tf.Graph().as_default(): with tf.Session() as sess: if FLAGS.clf == 'cnn': classifier = cnn_clf(FLAGS) elif FLAGS.clf == 'lstm' or FLAGS.clf == 'blstm': classifier = rnn_clf(FLAGS) elif FLAGS.clf == 'clstm': classifier = clstm_clf(FLAGS) else: raise ValueError( 'clf should be one of [cnn, lstm, blstm, clstm]') # Train procedure global_step = tf.Variable(0, name='global_step', trainable=False) # Learning rate decay starter_learning_rate = FLAGS.learning_rate learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, FLAGS.decay_steps, FLAGS.decay_rate, staircase=True) optimizer = tf.train.AdamOptimizer(learning_rate) grads_and_vars = optimizer.compute_gradients(classifier.cost) train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step, name='op_to_store') # Summaries loss_summary = tf.summary.scalar('Loss', classifier.cost) accuracy_summary = tf.summary.scalar('Accuracy', classifier.accuracy) # Train summary train_summary_op = tf.summary.merge_all() train_summary_dir = os.path.join(os.path.curdir, 'summaries', 'train') train_summary_writer = tf.summary.FileWriter( train_summary_dir, sess.graph) # Validation summary valid_summary_op = tf.summary.merge_all() valid_summary_dir = os.path.join(os.path.curdir, 'summaries', 'valid') valid_summary_writer = tf.summary.FileWriter( valid_summary_dir, sess.graph) saver = tf.train.Saver(max_to_keep=FLAGS.num_checkpoint) sess.run(tf.global_variables_initializer()) def run_step(input_data, is_training=True): """Run one step of the training process.""" input_x, input_y, sequence_length = input_data fetches = { 'step': global_step, 'cost': classifier.cost, 'accuracy': classifier.accuracy, 'learning_rate': learning_rate } feed_dict = { classifier.input_x: input_x, classifier.input_y: input_y } if FLAGS.clf != 'cnn': fetches['final_state'] = classifier.final_state feed_dict[classifier.batch_size] = len(input_x) feed_dict[classifier.sequence_length] = sequence_length if is_training: fetches['train_op'] = train_op fetches['summaries'] = train_summary_op feed_dict[classifier.keep_prob] = FLAGS.keep_prob else: fetches['summaries'] = valid_summary_op feed_dict[classifier.keep_prob] = 1.0 vars = sess.run(fetches, feed_dict) step = vars['step'] cost = vars['cost'] accuracy = vars['accuracy'] summaries = vars['summaries'] # Write summaries to file if is_training: train_summary_writer.add_summary(summaries, step) else: valid_summary_writer.add_summary(summaries, step) time_str = datetime.datetime.now().isoformat() print("{}: step: {}, loss: {:g}, accuracy: {:g}".format( time_str, step, cost, accuracy)) return accuracy print('Start training ...') for train_input in train_data: input_x, input_y, _ = train_input run_step(train_input, is_training=True) current_step = tf.train.global_step(sess, global_step) if current_step % FLAGS.evaluate_every_steps == 0: print('\nValidation') run_step((x_valid, y_valid, valid_lengths), is_training=False) print('') if current_step % FLAGS.save_every_steps == 0: # saver = tf.train.Saver() # save_path = saver.save(sess, os.path.join('./model2', 'clf'),current_step) if os.path.exists(model_dir): os.system('rm -rf ' + model_dir) # prediction_signature = tf.saved_model.signature_def_utils.predict_signature_def( # inputs={"input_x": classifier.input_x},outputs={"input_y":classifier.input_y}, # ) # builder = tf.saved_model.builder.SavedModelBuilder(os.path.join(model_dir,timestamp)) # # 构造模型保存的内容,指定要保存的 session,特定的 tag, # # 输入输出信息字典,额外的信息 # builder.add_meta_graph_and_variables(sess, # [tf.saved_model.SERVING], # signature_def_map={"classification":prediction_signature}, # # assets_collection={"params":params,"vocab":vocab_processor} # ) # builder.save() tf.saved_model.simple_save( sess, os.path.join(model_dir, timestamp), inputs={"input_x": classifier.input_x}, outputs={"input_y": classifier.input_y}) print('\nAll the files have been saved to {}\n'.format(model_dir))
elif model == 'cnn': config = cnn_config() data_loader = DataLoader(config) # Output files directory timestamp = str(int(time.time())) outdir = os.path.abspath(os.path.join(os.path.curdir, "runs", timestamp)) if not os.path.exists(outdir): os.makedirs(outdir) # Train # ============================================================================= with tf.Graph().as_default(): with tf.Session() as sess: if model == 'clstm': classifier = clstm_clf(config) elif model == 'cnn': classifier = cnn_clf(config) # Train procedure global_step = tf.Variable(0, name='global_step', trainable=False) # Learning rate decay starter_learning_rate = config.learning_rate learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, config.decay_steps, config.decay_rate, staircase=True) optimizer = tf.train.AdamOptimizer(learning_rate) grads_and_vars = optimizer.compute_gradients(classifier.cost) train_op = optimizer.apply_gradients(grads_and_vars,
embedding_size = params['embedding_size'] filter_sizes = params['filter_sizes'] num_filters = params['num_filters'] hidden_size = params['hidden_size'] num_layers = params['num_layers'] l2_reg_lambda = params['l2_reg_lambda'] batch_size = params['batch_size'] keep_prob = 1.0 with tf.Graph().as_default(): session_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess = tf.Session(config=session_conf) with sess.as_default(): clstm = clstm_clf(config) # sess.run(tf.global_variables_initializer()) # Restore model checkpoint_file = os.path.join(FLAGS.file_path, 'model/', FLAGS.model_file) saver = tf.train.Saver(tf.all_variables()) saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file)) saver.restore(sess, checkpoint_file) def do_predict(x_batch, real_len): def predict_step(x_batch, real_len): feed_dict = { clstm.input_x: x_batch, clstm.keep_prob: config.keep_prob, clstm.batch_size: len(x_batch), clstm.sequence_length: real_len