From 0501662121ed685b9c1dcf787fae5c7136261ee4 Mon Sep 17 00:00:00 2001 From: tbeiko Date: Fri, 27 Oct 2017 14:09:19 -0400 Subject: [PATCH] Print best accuracy, test with movie rating data --- cnn.py | 2 +- cnn.pyc | Bin 3266 -> 3271 bytes train.py | 19 +++++++++++++------ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/cnn.py b/cnn.py index 5c5a2a0..71a2061 100644 --- a/cnn.py +++ b/cnn.py @@ -22,7 +22,7 @@ def __init__( with tf.device('/cpu:0'), tf.name_scope("embedding"): self.W = tf.Variable( tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0), - name="W") + name="W", trainable=False) self.embedded_chars = tf.nn.embedding_lookup(self.W, self.input_x) self.embedded_chars_expanded = tf.expand_dims(self.embedded_chars, -1) diff --git a/cnn.pyc b/cnn.pyc index 1deac34fd4efdb18ae162d03c61f5eadd0afbee3..2b9edb61c76a98bcc8641fa4e47f715d6f3a4a96 100644 GIT binary patch delta 1194 zcmZuwSx*yD6h3#_PCL_1_fopBFIL=rffz|hB)pj*npBL2RBizqC`*T^@lKjh-h8O! zzVZWn=O6gOZ}3l;82kaAbEZXzOlEG+JOt5LAOmm=&;eL|6FRB>5>>qi#qvwtMP!?2(a2Marinj)MHh2i_Cv^0_k!d5G z37bLiVyySAIZ!c>36Mz)77~lMN${L2J`u9Y6o-qxTbkW2D<0soGN2rg8IbXRFJ_ck zkhwMx`T$lP@DhxUjpqBpAgBbWIQ2wsc}7q6tp!k6!61Pu0V)cEDCPYugnx5JMk*ju z(8J=}7Bq^WQg{bZl>jeZ?-xZCXX2K6v&(vE&R7Ozfh_h1JjBXj?r?|(uhOt-^sOUo zMOsUQ+@0`g#@L{U%|!w^{_l1wZV;<0`zYOzG3mVcfOw&+H!5J~V6Zx9evq zGc3d-T?lt=H-wg@+IFk1zlXabq30s&BCSs&Td5qyn0V9OHUI~6#nkabo!dUOUp34sZhjM;=+W5k{A=?BZQiWuptc>iVaAhB@&&n33cb9 zAvba7UvR_Dr5jeR{2y-k1D+%;}wT@A=O6opYw=?swNSFH(u0k1y6X(f^qA zyF_>E{;4-Q2}prm3+oo7fvpI(1*D0OKo?kBw@;t`5-Vy^oa8=0w1H*Payu1im79fjU6gfmR%^)*~Dh(E8_@pw;BKTNIgNG8S0K*;lq_M2I3YIWT@Z~TC zOy*qzZ*@RZMbXkq-kc3KVC~`_KY|>){1r53YzrX>0l<5`A zJmEL~0M_{xnbwHUL&TdJV*W2iHZ!6s(-jo^pe*VZe44!(_Ol#D(R^?B;puI8QLU)Y zk)t!$jtX%{O+|a^TeK(A>Pc)<C{!m-pu@6&fdw2hB%~K1GYd6U8dQUxY-e>r4sKq25L0{uH D@?X>M diff --git a/train.py b/train.py index 63819aa..2880eaf 100644 --- a/train.py +++ b/train.py @@ -10,19 +10,19 @@ # ================================================== # Data loading params tf.flags.DEFINE_float("dev_sample_percentage", .1, "Percentage of the training data to use for validation") -tf.flags.DEFINE_string("coherent_data_file", "./data/padded/coherent_sentences.txt", "Data source for the coherent data.") -tf.flags.DEFINE_string("incoherent_data_file", "./data/padded/incoherent_sentences_arg2_diff_sense.txt", "Data source for the incoherent data.") +tf.flags.DEFINE_string("coherent_data_file", "./data/rt-polaritydata/rt-polarity.pos", "Data source for the coherent data.") +tf.flags.DEFINE_string("incoherent_data_file", "./data/rt-polaritydata/rt-polarity.neg", "Data source for the incoherent data.") # Model Hyperparameters tf.flags.DEFINE_string("word2vec", "./data/model/GoogleNews-vectors-negative300.bin", "Word2vec file with pre-trained embeddings (default: None)") tf.flags.DEFINE_integer("embedding_dim", 300, "Dimensionality of character embedding (default: 300, to match GoogleNews embeddings)") tf.flags.DEFINE_string("filter_sizes", "4", "Comma-separated filter sizes (default: '3,4,5')") -tf.flags.DEFINE_integer("num_filters", 32, "Number of filters per filter size (default: 128)") +tf.flags.DEFINE_integer("num_filters", 16, "Number of filters per filter size (default: 128)") tf.flags.DEFINE_float("dropout_keep_prob", 0.5, "Dropout keep probability (default: 0.5)") tf.flags.DEFINE_float("l2_reg_lambda", 0, "L2 regularization lambda (default: 0.0)") # Training parameters -tf.flags.DEFINE_integer("batch_size", 32, "Batch Size (default: 64)") +tf.flags.DEFINE_integer("batch_size", 64, "Batch Size (default: 64)") tf.flags.DEFINE_integer("num_epochs", 200, "Number of training epochs (default: 200)") tf.flags.DEFINE_integer("evaluate_every", 100, "Evaluate model on dev set after this many steps (default: 100)") tf.flags.DEFINE_integer("checkpoint_every", 100, "Save model after this many steps (default: 100)") @@ -185,7 +185,7 @@ def train_step(x_batch, y_batch): 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): + def dev_step(x_batch, y_batch, best_dev_acc, best_dev_step, writer=None): """ Evaluates model on a dev set """ @@ -198,9 +198,14 @@ def dev_step(x_batch, y_batch, writer=None): [global_step, dev_summary_op, cnn.loss, cnn.accuracy], feed_dict) time_str = datetime.datetime.now().isoformat() + if accuracy > best_dev_acc: + best_dev_acc = accuracy + best_dev_step = step print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy)) + print("Best accuracy: {:g} at step {}".format(best_dev_acc, best_dev_step)) if writer: writer.add_summary(summaries, step) + return best_dev_acc, best_dev_step def batch_iter(data, batch_size, num_epochs, shuffle=True): """ @@ -226,13 +231,15 @@ def batch_iter(data, batch_size, num_epochs, shuffle=True): list(zip(x_train, y_train)), FLAGS.batch_size, FLAGS.num_epochs) # Training loop. For each batch... + best_dev_acc = 0 + best_dev_step = 0 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) + best_dev_acc, best_dev_step = dev_step(x_dev, y_dev, best_dev_acc, best_dev_step, writer=dev_summary_writer) print("") if current_step % FLAGS.checkpoint_every == 0: path = saver.save(sess, checkpoint_prefix, global_step=current_step)