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] # input_y = graph.get_operation_by_name("input_y").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 for one epoch batches = processData.batch_iter(list(x_test), FLAGS.batch_size, 1, shuffle=False) # Collect the predictions here all_predictions = [] for x_test_batch in batches: batch_predictions = sess.run(predictions, { input_x: x_test_batch, dropout_keep_prob: 1.0 }) all_predictions = np.concatenate( [all_predictions, batch_predictions]) print all_predictions
feed_dict = { cnn.input_x: x_batch, cnn.input_y: y_batch, cnn.dropout_keep_prob: 1.0 } step, summaries, loss, accuracy = sess.run( [global_step, dev_summary_op, cnn.loss, cnn.accuracy], feed_dict) time_str = datetime.datetime.now().isoformat() print("{}: step {}, loss {:g}, acc {:g}".format( time_str, step, loss, accuracy)) if writer: writer.add_summary(summaries, step) # Generate batches batches = processData.batch_iter(list(zip(x_train, y_train)), FLAGS.batch_size, FLAGS.num_epochs) # Training loop. For each batch... for batch in batches: x_batch, y_batch = zip(*batch) train_step(x_batch, y_batch) current_step = tf.train.global_step(sess, global_step) if current_step % FLAGS.evaluate_every == 0: print("\nEvaluation:") dev_step(x_dev, y_dev, writer=dev_summary_writer) print("") if current_step % FLAGS.checkpoint_every == 0: path = saver.save(sess, checkpoint_prefix, global_step=current_step) print("Saved model checkpoint to {}\n".format(path))
def train_tensorflow(x_train, x_test, y_train, y_test): # Training with tf.Graph().as_default(): session_conf = tf.ConfigProto( allow_soft_placement=False, log_device_placement=False) sess = tf.Session(config=session_conf) with sess.as_default(): cnn = TextCNN( sequence_length=100, num_classes=2, vocab_size=len(x_train), embedding_size=100, filter_sizes=list(map(int, filter_sizes.split(","))), num_filters=128, l2_reg_lambda=0) # Define Training procedure global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(1e-3) grads_and_vars = optimizer.compute_gradients(cnn.loss) train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step) # Keep track of gradient values and sparsity (optional) grad_summaries = [] for g, v in grads_and_vars: if g is not None: grad_hist_summary = tf.summary.histogram("{}/grad/hist".format(v.name), g) sparsity_summary = tf.summary.scalar("{}/grad/sparsity".format(v.name), tf.nn.zero_fraction(g)) grad_summaries.append(grad_hist_summary) grad_summaries.append(sparsity_summary) grad_summaries_merged = tf.summary.merge(grad_summaries) # Output directory for models and summaries timestamp = str(int(time.time())) out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs", timestamp)) print("Writing to {}\n".format(out_dir)) # Summaries for loss and accuracy loss_summary = tf.summary.scalar("loss", cnn.loss) acc_summary = tf.summary.scalar("accuracy", cnn.accuracy) # Train Summaries train_summary_op = tf.summary.merge([loss_summary, acc_summary, grad_summaries_merged]) train_summary_dir = os.path.join(out_dir, "summaries", "train") # out_dir --> runs #train_summary_writer = tf.summary.FileWriter("tensorboard_train/", graph=tf.get_default_graph()) train_summary_writer = tf.summary.FileWriter(train_summary_dir, graph=sess.graph) # Dev summaries dev_summary_op = tf.summary.merge([loss_summary, acc_summary]) dev_summary_dir = os.path.join(out_dir, "summaries", "dev") # out_dir --> runs dev_summary_writer = tf.summary.FileWriter(dev_summary_dir, graph=sess.graph) #dev_summary_writer = tf.summary.FileWriter("tensorboard_dev/", graph=tf.get_default_graph()) # Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints")) checkpoint_prefix = os.path.join(checkpoint_dir, "model") if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) saver = tf.train.Saver(tf.global_variables(), max_to_keep=5) # Write vocabulary #vocab_processor.save(os.path.join(out_dir, "vocab")) # Initialize all variables sess.run(tf.global_variables_initializer()) def train_step(x_batch, y_batch): """ A single training step """ feed_dict = { cnn.input_x: x_batch, cnn.input_y: y_batch, cnn.dropout_keep_prob: 1.0 } _, step, summaries, loss, accuracy = sess.run( [train_op, global_step, train_summary_op, cnn.loss, cnn.accuracy], feed_dict) time_str = datetime.datetime.now().isoformat() print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy)) train_summary_writer.add_summary(summaries, step) def dev_step(x_batch, y_batch, writer=None): """ Evaluates model on a dev set """ feed_dict = { cnn.input_x: x_batch, cnn.input_y: y_batch, cnn.dropout_keep_prob: 1.0 } step, summaries, loss, accuracy = sess.run( [global_step, dev_summary_op, cnn.loss, cnn.accuracy], feed_dict) time_str = datetime.datetime.now().isoformat() print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy)) if writer: writer.add_summary(summaries, step) # Generate batches train_batches = batch_iter( list(zip(x_train, y_train)), batch_size, training_epochs) #batch size, training_epochs dev_batches = batch_iter( list(zip(x_test, y_test)), batch_size, training_epochs) # Training loop. For each batch... for train_batch, dev_batch in zip(train_batches, dev_batches): x_train_batch, y_train_batch = zip(*train_batch) train_step(x_train_batch, y_train_batch) current_step = tf.train.global_step(sess, global_step) print current_step if current_step % 100 == 0: print("\nEvaluation:") x_dev_batch, y_dev_batch = zip(*dev_batch) dev_step(x_dev_batch, y_dev_batch, writer=dev_summary_writer) print("") if current_step % 100 == 0: path = saver.save(sess, checkpoint_prefix, global_step=current_step) print("Saved model checkpoint to {}\n".format(path))
def oneLayer(): train_X, dev_X, test_X, train_y, dev_y, test_y = get_glove_data() # Parameters RANDOM_SEED = 42 tf.set_random_seed(RANDOM_SEED) learning_rate = 0.01 training_epochs = 100 batch_size = 500 display_step = 1 evaluate_every = 25 # Network Parameters n_hidden_1 = 256 n_hidden_2 = 128 n_input = train_X.shape[1] # MNIST data input (img shape: 28*28) n_classes = train_y.shape[1] # MNIST total classes (0-9 digits) global_step = tf.Variable(0, name="global_step", trainable=False) # tf Graph Input x = tf.placeholder(tf.float32, [None, n_input], name='X') # 0-9 digits recognition => 10 classes y = tf.placeholder(tf.float32, [None, n_classes], name='y') # Store layers weight & bias weights = { 'w1': tf.Variable(tf.random_normal([n_input, n_classes]), name='W1'), } biases = { 'b1': tf.Variable(tf.random_normal([n_classes]), name='b1'), } pred = single_perceptron(x, weights, biases) loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) # Gradient Descent optimizer = tf.train.GradientDescentOptimizer(learning_rate) # Op to calculate every variable gradient grads = tf.gradients(loss, tf.trainable_variables()) grads = list(zip(grads, tf.trainable_variables())) # Op to update all variables according to their gradient apply_grads = optimizer.apply_gradients(grads_and_vars=grads, global_step=global_step) acc = tf.equal(tf.argmax(pred, axis=1), tf.argmax(y, axis=1)) acc = tf.reduce_mean(tf.cast(acc, tf.float32)) # Initializing the variables init = tf.global_variables_initializer() # Launch the graph with tf.Session() as sess: sess.run(init) # Output directory for models and summaries timestamp = str(int(time.time())) out_dir = os.path.abspath(os.path.join(os.path.curdir, "oneLayer_runs", timestamp)) print("Writing to {}\n".format(out_dir)) # Create a summary to monitor cost tensor loss_summary = tf.summary.scalar("loss", loss) # Create a summary to monitor accuracy tensor acc_summary = tf.summary.scalar("accuracy", acc) # Summarize all gradients grad_summaries = [] for g, v in grads: if g is not None: grad_hist_summary = tf.summary.histogram("{}/grad/hist".format(v.name), g) sparsity_summary = tf.summary.scalar("{}/grad/sparsity".format(v.name), tf.nn.zero_fraction(g)) grad_summaries.append(grad_hist_summary) grad_summaries.append(sparsity_summary) grad_summaries_merged = tf.summary.merge(grad_summaries) # Train Summaries train_summary_op = tf.summary.merge([loss_summary, acc_summary, grad_summaries_merged]) train_summary_dir = os.path.join(out_dir, "summaries", "train") train_summary_writer = tf.summary.FileWriter(train_summary_dir, sess.graph) # # Dev summaries dev_summary_op = tf.summary.merge([loss_summary, acc_summary]) dev_summary_dir = os.path.join(out_dir, "summaries", "dev") dev_summary_writer = tf.summary.FileWriter(dev_summary_dir, sess.graph) # Generate batches train_batches = batch_iter( list(zip(train_X, train_y)), batch_size, training_epochs) dev_batches = batch_iter( list(zip(test_X, test_y)), batch_size, training_epochs) # Training cycle for train_batch, dev_batch in zip(train_batches, dev_batches): x_train_batch, y_train_batch = zip(*train_batch) _, step, summaries, loss_1, accuracy = sess.run([apply_grads, global_step, train_summary_op, loss, acc], feed_dict={x: x_train_batch, y: y_train_batch}) time_str = datetime.datetime.now().isoformat() print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss_1, accuracy)) train_summary_writer.add_summary(summaries, step) train_summary_writer.flush() current_step = tf.train.global_step(sess, global_step) if current_step % evaluate_every == 0: print("\nEvaluation:") x_dev_batch, y_dev_batch = zip(*dev_batch) step, summaries, y_pred, loss_1, accuracy = sess.run([global_step, dev_summary_op, pred, loss, acc], feed_dict={x: x_dev_batch, y: y_dev_batch}) time_str = datetime.datetime.now().isoformat() print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss_1, accuracy)) dev_summary_writer.add_summary(summaries, step) dev_summary_writer.flush() print("") # Report precision, recall, F1 print("Precision: {}%".format(100*metrics.precision_score(np.argmax(y_dev_batch, axis=1), np.argmax(y_pred, axis=1), average="weighted"))) print("Recall: {}%".format(100*metrics.recall_score(np.argmax(y_dev_batch, axis=1), np.argmax(y_pred, axis=1), average="weighted"))) print("f1_score: {}%".format(100*metrics.f1_score(np.argmax(y_dev_batch, axis=1), np.argmax(y_pred, axis=1), average="weighted"))) train_summary_writer.close() dev_summary_writer.close()
def train_tensorflow(x_train, x_test, y_train, y_test, vocab_processor): # Training with tf.Graph().as_default(): session_conf = tf.ConfigProto( allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement) sess = tf.Session(config=session_conf) with sess.as_default(): cnn = TextCNN( sequence_length=x_train.shape[1], num_classes=2, vocab_size=len(vocab_processor.vocabulary_), embedding_size=FLAGS.embedding_dim, filter_sizes=list(map(int, FLAGS.filter_sizes.split(","))), num_filters=FLAGS.num_filters, l2_reg_lambda=FLAGS.l2_reg_lambda) # Define Training procedure global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(1e-3) grads_and_vars = optimizer.compute_gradients(cnn.loss) train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step) # Keep track of gradient values and sparsity (optional) grad_summaries = [] for g, v in grads_and_vars: if g is not None: grad_hist_summary = tf.summary.histogram("{}/grad/hist".format(v.name), g) sparsity_summary = tf.summary.scalar("{}/grad/sparsity".format(v.name), tf.nn.zero_fraction(g)) grad_summaries.append(grad_hist_summary) grad_summaries.append(sparsity_summary) grad_summaries_merged = tf.summary.merge(grad_summaries) # Output directory for models and summaries timestamp = str(int(time.time())) out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs", timestamp)) print("Writing to {}\n".format(out_dir)) # Summaries for loss and accuracy loss_summary = tf.summary.scalar("loss", cnn.loss) acc_summary = tf.summary.scalar("accuracy", cnn.accuracy) # Train Summaries train_summary_op = tf.summary.merge([loss_summary, acc_summary, grad_summaries_merged]) train_summary_dir = os.path.join(out_dir, "summaries", "train") train_summary_writer = tf.summary.FileWriter(train_summary_dir, sess.graph) # Dev summaries dev_summary_op = tf.summary.merge([loss_summary, acc_summary]) dev_summary_dir = os.path.join(out_dir, "summaries", "dev") dev_summary_writer = tf.summary.FileWriter(dev_summary_dir, sess.graph) # Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints")) checkpoint_prefix = os.path.join(checkpoint_dir, "model") if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) saver = tf.train.Saver(tf.global_variables(), max_to_keep=FLAGS.num_checkpoints) # Write vocabulary vocab_processor.save(os.path.join(out_dir, "vocab")) # Initialize all variables sess.run(tf.global_variables_initializer()) if FLAGS.enable_word_embeddings and cfg['word_embeddings']['default'] is not None: vocabulary = vocab_processor.vocabulary_ initEmbedding = None if embedding_name == 'word2vec': # load embedding vectors from the word2vec print("Load word2vec file {}".format(cfg['word_embeddings']['word2vec']['path'])) initEmbedding = load_embedding_vectors_word2vec(vocabulary, cfg['word_embeddings']['word2vec']['path'], cfg['word_embeddings']['word2vec']['binary']) print("word2vec file has been loaded") elif embedding_name == 'glove': # load embedding vectors from the glove print("Load glove file {}".format(cfg['word_embeddings']['glove']['path'])) initEmbedding = load_embedding_vectors_glove(vocabulary, cfg['word_embeddings']['glove']['path'], embedding_dimension) print("glove file has been loaded\n") sess.run(cnn.E.assign(initEmbedding)) def train_step(x_batch, y_batch): """ A single training step """ feed_dict = { cnn.input_x: x_batch, cnn.input_y: y_batch, cnn.dropout_keep_prob: FLAGS.dropout_keep_prob } _, step, summaries, loss, accuracy = sess.run( [train_op, global_step, train_summary_op, cnn.loss, cnn.accuracy], feed_dict) time_str = datetime.datetime.now().isoformat() print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy)) train_summary_writer.add_summary(summaries, step) def dev_step(x_batch, y_batch, writer=None): """ Evaluates model on a dev set """ feed_dict = { cnn.input_x: x_batch, cnn.input_y: y_batch, cnn.dropout_keep_prob: 1.0 } step, summaries, loss, accuracy = sess.run( [global_step, dev_summary_op, cnn.loss, cnn.accuracy], feed_dict) time_str = datetime.datetime.now().isoformat() print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy)) if writer: writer.add_summary(summaries, step) # Generate batches train_batches = batch_iter( list(zip(x_train, y_train)), FLAGS.batch_size, FLAGS.num_epochs) dev_batches = batch_iter( list(zip(x_test, y_test)), FLAGS.batch_size, FLAGS.num_epochs) # Training loop. For each batch... for train_batch, dev_batch in zip(train_batches, dev_batches): x_train_batch, y_train_batch = zip(*train_batch) train_step(x_train_batch, y_train_batch) current_step = tf.train.global_step(sess, global_step) # if current_step == 1000: # # Test on Arun's data after 10 epochs. # x_dev_batch = generateVinodTrain() # y_dev_batch = generateVinodTest() # dev_step(x_dev_batch, y_dev_batch, writer=dev_summary_writer) # break if current_step % FLAGS.evaluate_every == 0: print("\nEvaluation:") x_dev_batch, y_dev_batch = zip(*dev_batch) dev_step(x_dev_batch, y_dev_batch, writer=dev_summary_writer) print("") if current_step % FLAGS.checkpoint_every == 0: path = saver.save(sess, checkpoint_prefix, global_step=current_step) print("Saved model checkpoint to {}\n".format(path))
# Save parameters to file params_file = open(os.path.join(outdir, 'params.pkl'), 'wb') pkl.dump(params, params_file, True) params_file.close() # Simple Cross validation # TODO use k-fold 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 = processData.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 == '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) optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)