def __init__(self): self.config = TCNNConfig() self.categories, self.cat_to_id = read_category() self.words, self.word_to_id = read_vocab(vocab_dir) self.config.vocab_size = len(self.words) self.model = TextCNN(self.config) self.session = tf.Session() self.session.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess=self.session, save_path=save_path)
def model_select(op='cnn'): model = None if op == 'cnn': from model.cnn_model import TextCNN from conf.cnn_model_config import model_submission_path model = TextCNN() config.model_submission_path = model_submission_path elif op == 'bert': from model.bert_model import BertModel from conf.bert_model_config import model_submission_path model = BertModel() config.model_submission_path = model_submission_path return model
print("Vocabulary Size: {:d}".format(len(vocab_processor.vocabulary_))) print("Train/Dev split: {:d}/{:d}".format(len(y_train), len(y_dev))) with tf.Graph().as_default(): session_conf = tf.ConfigProto( allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement) session_conf.gpu_options.allow_growth = False session_conf.gpu_options.per_process_gpu_memory_fraction = 0.45 sess = tf.Session(config=session_conf) with sess.as_default(): cnn = TextCNN(sequence_length=x_train.shape[1], num_classes=y_train.shape[1], vocab_size=len(vocab_processor.vocabulary_), embedding_size=my_embedding_dim, filter_sizes=list( map(int, FLAGS.filter_sizes.split(","))), num_filters=FLAGS.num_filters, l2_reg_lambda=FLAGS.l2_reg_lambda) cnn.add_placeholders() cnn.add_embedding() cnn.add_conv_pool() cnn.add_dropout() cnn.add_output() cnn.add_loss() cnn.add_loss() cnn.add_accuracy() # Define Training procedure global_step = tf.Variable(0,
with sess.as_default(): # TODO(andrei): Warn on all unused flags (e.g. set CNN options when # using an LSTM). if FLAGS.lstm: print("\nUsing LSTM.") model = TextLSTM(sequence_length=x_train.shape[1], vocab_size=len(vocabulary), embedding_size=embedding_dim, hidden_size=FLAGS.lstm_hidden_size, layer_count=FLAGS.lstm_hidden_layers) else: print("\nUsing CNN.") model = TextCNN( sequence_length=x_train.shape[1], num_classes=2, vocab_size=len(vocabulary), embedding_size=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(FLAGS.learning_rate) # 'compute_gradients' returns a list of (gradient, variable) pairs. grads_and_vars = optimizer.compute_gradients(model.loss) # If enabled, we apply gradient clipping, in order to deal with the # exploding gradient problem common when training LSTMs. # TODO(andrei): Gradient magnitude tracing to see effectiveness of # clipping.
feed_dict=feed_dict) print("Precision, Recall and F1-Score...") print(metrics.classification_report(y_test_cls, y_pred_cls)) print("Confusion Matrix...") cm = metrics.confusion_matrix(y_test_cls, y_pred_cls) print(cm) time_dif = get_time_diff(start_time) print("Time usage:", time_dif) if __name__ == "__main__": data_processor = DataProcessor() parser = argparse.ArgumentParser() parser.add_argument('--pattern', choices=['train', 'test']) args = parser.parse_args() pattern = args.pattern if pattern not in ['train', 'test']: raise ValueError('Invalid pattern, must be train or test') config = TextCNNConfig() if not os.path.exists(vocab_path): data_processor.build_vocob(train_path, vocab_path, config.vocab_size) categories, cat2id = data_processor.read_category() words, word2id = data_processor.read_vocab(vocab_path) config.vocab_size = len(words) model = TextCNN(config) if pattern == 'train': train(data_processor, word2id, cat2id, model, config) else: test(data_processor, word2id, cat2id, model, config)