Esempio n. 1
0
def get_preds(data_set, model_type = None, model_path = SAVE_MODEL_DIR, output_file_name='./data/last_preds.json'):
	pred_handler = PredictionHandler() 
	data = DataHolder(data_set)

	with tf.Graph().as_default():
		start = time.time()

		if model_type == 'l2_attn': model = l2_attn.TFModel(EmbeddingHolder().get_embeddings_mat(), True)
		elif model_type == 'l2_2attn': model = l2_2attn.TFModel(EmbeddingHolder().get_embeddings_mat(), True)
		elif model_type == 'l2_double_2attn': model = l2_double_2attn.TFModel(EmbeddingHolder().get_embeddings_mat(), True)
		elif model_type == 'ce_attn': model = ce_attn.TFModel(EmbeddingHolder().get_embeddings_mat(), True)
		elif model_type == 'ce_2attn': model = ce_2attn.TFModel(EmbeddingHolder().get_embeddings_mat(), True)
		else: return

		print "\nRebuild graph took " + str(time.time() - start) + " seconds"

		saver = tf.train.Saver()

		with tf.Session() as session:
			session = session
			saver.restore(session, model_path)
			print 'Restored model. Predicting....'
			preds = model.predict(session, data)
			pred_handler.write_preds(preds, output_file_name)
	print 'Done Predicting'
Esempio n. 2
0
def get_classifier_results(data_set, model_path = SAVE_MODEL_DIR):
	data = DataHolder(data_set)

	with tf.Graph().as_default():
		start = time.time()

		model = PassClassifier(EmbeddingHolder().get_embeddings_mat(), True)

		print "\nRebuild graph took " + str(time.time() - start) + " seconds"

		saver = tf.train.Saver()

		with tf.Session() as session:
			session = session
			saver.restore(session, model_path)
			print 'Restored model. Predicting....'
			preds = model.predict(session, data)
			list_preds = list()
			for batch in preds:
				for row in batch:
					list_preds.append(row)
			preds = np.asarray(list_preds)
			y = data.get_full_selected()
			f = open('classifier_check_log', 'w')
			f.write("File to check results of classifier:\n Preds     Y")
			for p_val, y_val in zip(preds, y):
				f.write(str(p_val) + '        ' + str(y_val))
				f.write('\n')
			classifier_eval(preds, y)
		print 'Done Predicting'
Esempio n. 3
0
def keep_training(model_path = SAVE_MODEL_DIR, prev_best_rouge=0):
	pred_handler = PredictionHandler() 
	data = DataHolder('train')

	with tf.Graph().as_default():
		start = time.time()

		model = l2_double_2attn.TFModel(EmbeddingHolder().get_embeddings_mat())

		print "\nRebuild graph took " + str(time.time() - start) + " seconds"

		saver = tf.train.Saver()

		with tf.Session() as session:
			session = session
			saver.restore(session, model_path)
			print 'Restored model. Predicting....'
			merged = tf.summary.merge_all()
			model.best_rouge = prev_best_rouge
			losses = model.fit(session, saver, merged, data)

	model.train_writer.close()      
	model.test_writer.close()
	model.log.close()
	print 'Done continueing'
Esempio n. 4
0
def build_model():
	model = Sequential()

	embedding_matrix = EmbeddingHolder().get_embeddings_mat()
	
	# BUILD ACTUAL MODEL
	embedding_layer = Embedding(
			228999,
		    EMBEDDING_DIM,
		    weights=[embedding_matrix],
		    input_length=INPUT_MAX_LENGTH,
		    mask_zero=True
    )
	s2s_layer = Seq2Seq(
		batch_input_shape=(None, INPUT_MAX_LENGTH, EMBEDDING_DIM),
		hidden_dim=HIDDEN_DIM, 
		output_length=OUTPUT_MAX_LENGTH, 
		output_dim=228999, 
		depth=DEPTH
	)
	
	# ADD UP ACTUAL MODEL
	model.add(embedding_layer)
	model.add(s2s_layer)
	model.compile(loss='mse', optimizer='adam', metrics=['accuracy', 'precision', 'recall'])

	print 'Model Built'
	return model
Esempio n. 5
0
    def build(self):
        self.add_placeholders()
        self.pred = self.add_prediction_op()
        self.loss = self.add_loss_op(self.pred)
        self.train_op = self.add_training_op(self.loss)

    def __init__(self, embeddings):
        self.pretrained_embeddings = embeddings
        self.log = open(LOG_FILE_DIR, "a")
        self.build()

if __name__ == "__main__":
    print 'Starting, and now printing to log.txt'
    data = TFDataHolder('train')
    embeddings = EmbeddingHolder().get_embeddings_mat()
    with tf.Graph().as_default():
        start = time.time()
        model = TFModel(embeddings)
        model.log.write("\nBuild graph took " + str(time.time() - start) + " seconds")

        init = tf.global_variables_initializer()
        saver = tf.train.Saver()
        model.log.write('\ninitialzed variables')
        config = tf.ConfigProto()
        # config.gpu_options.allow_growth=True
        # config.gpu_options.per_process_gpu_memory_fraction = 0.6
        with tf.Session(config=config) as session:
            session.run(init)
            model.log.write('\nran init, fitting.....')
            losses = model.fit(session, saver, data)