def main(CONST, data): config = get_config("BiRNN") #if not CONST.TRAIN: # config.num_steps = 1 config.vocab_size = len(data["embeddings"]) config.max_max_epoch = CONST.MAX_EPOCH from util.evaluations import print_config print_config(config) tf.reset_default_graph() # start graph and session # config=tf.ConfigProto(log_device_placement=True) pass to tf.Session # to see which devices all operations can run on with tf.Graph().as_default(), tf.Session(config=session_conf) as session: with tf.variable_scope( "model", reuse=None, initializer=tf.contrib.layers.xavier_initializer()): training_model = RNNModel(is_training=CONST.TRAIN, config=config) # model class if CONST.TRAIN: tf.initialize_all_variables().run() session.run(training_model.embedding.assign( data["embeddings"])) # train # Reload save epoch training time if CONST.RELOAD_TRAIN: saver = tf.train.Saver() saver.restore(sess=session, save_path=CONST.SLOT3_TARGET_MODEL_PATH + "slot3_target") all_losses = [] train_writer = run_metadata = run_options = False if config.get_summary: # session = tf.InteractiveSession() train_writer = tf.train.SummaryWriter( CONST.SLOT3_TARGET_MODEL_PATH + "attention_graph/slot3_target", session.graph) run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() for i in range(config.max_max_epoch): # setup epoch train_perplexity, loss, losses = run_epoch( session, training_model, data["x_train"], data["p_train"], train_writer, run_options, run_metadata, targets=data["l_train"], verbose=True) all_losses = all_losses + [np.mean(losses)] print("Epoch: %d Avg. Total Mean Loss: %.6f" % (i + 1, np.mean(all_losses))) from util.evaluations import print_config print_config(config) import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt # plt.plot([np.mean(all_losses[i-50:i]) for i in range(len(all_losses))]) figure_name = CONST.OUT_DIR + "loss/" + "losses_slot3" + config.__class__.__name__ + ".png" x = [i for i in range(len(all_losses))] plt.plot(np.array(x), np.array(all_losses)) plt.savefig(figure_name) save_pickle( CONST.DATA_DIR + config.__class__.__name__ + "_slot3_target", all_losses) print("saved slot3 losses.png and losses data", figure_name) print("loss: ", figure_name) print( "loss data: ", CONST.DATA_DIR + config.__class__.__name__ + "_slot3_target" + ".pickle") saver = tf.train.Saver(tf.all_variables()) path = saver.save(sess=session, save_path=CONST.SLOT3_TARGET_MODEL_PATH + "slot3_target") print("model saved: " + path) if config.get_summary: train_writer.close() session.close() # doesn't seem to close under scope?? if not CONST.TRAIN: with tf.variable_scope( "model", reuse=True): # reuse scope to evaluate model :-) #config.batch_size = len(data["x_test"]) # tf.initialize_all_variables().run() # set embeddings again session.run(training_model.embedding.assign( data["embeddings"])) # Load Data Back Trained Model saver = tf.train.Saver() saver.restore(sess=session, save_path=CONST.SLOT3_TARGET_MODEL_PATH + "slot3_target") # Initialize Model Graph validation_model = RNNModel(is_training=False, config=config) predictions = [] alphas = [] # Get Predictions for step, (x, y, t) in enumerate( semeval_itterator(data["x_test"], data["p_test"], validation_model.batch_size, validation_model.num_steps, target=data["l_test"], shuffle_examples=False)): # do not pass in test targets if CONST.HEATMAP: alpha = session.run(validation_model.alpha, {validation_model.input_data: x }) #, training_model.targets: y}) alphas = alphas + alpha.tolist() else: pred = session.run(validation_model.predictions, {validation_model.input_data: x }) #, training_model.targets: y}) predictions = predictions + pred.tolist() if not CONST.HEATMAP: even_batch = len(data["x_test"]) % config.batch_size remove_added_batch = config.batch_size - even_batch del predictions[-remove_added_batch:] predictions = np.asarray(predictions) from util.evaluations import print_config print_config(config) from util.evaluations import evaluate_multiclass y = [np.asarray(e) for e in data["p_test"]] save_pickle( CONST.DATA_DIR + config.__class__.__name__ + "slot3_predictions", { "predictions": predictions, "y": y }) evaluate_multiclass(predictions, y, True) print("predictions saved") # session.close() # doesn't seem to close under scope?? elif CONST.HEATMAP: even_batch = len(data["x_test"]) % config.batch_size remove_added_batch = config.batch_size - even_batch del alphas[-remove_added_batch:] distance = 0 sentences = data["test_sentences"] print("computing heatmaps and avg distance...") from util.heatmap import avg_distance_and_heatmaps avg_distance_and_heatmaps(alphas, sentences, CONST.SENTENCE_PATH + "hursh/")
def train_task(CONST, data): config = multitask() config.vocab_size = len(data["embeddings"]) config.max_max_epoch = CONST.MAX_EPOCH tf.reset_default_graph() with tf.Graph().as_default(), tf.Session(config=session_conf) as session: with tf.variable_scope( "model", reuse=None, initializer=tf.contrib.layers.xavier_initializer()): training_model = slot1_and_slot3_model( is_training=CONST.TRAIN, config=config) # model class if CONST.TRAIN: tf.initialize_all_variables().run() # Check if Model is Saved and then Load if CONST.RELOAD_TRAIN: saver = tf.train.Saver() print("CHECKPATH", CONST.MULTITASK_CHECKPOINT_PATH_HURSH + "task") saver.restore(sess=session, save_path=CONST.MULTITASK_CHECKPOINT_PATH_HURSH + "task") session.run( training_model.embeddings.embeddings.assign( data["embeddings"])) # train slot1_losses = [] slot3_losses = [] slot4_losses = [] train_writer = run_metadata = run_options = False if config.get_summary: # session = tf.InteractiveSession() train_writer = tf.train.SummaryWriter( CONST.MULTITASK_CHECKPOINT_PATH_HURSH + "attention_graph/" + config.__class__.__name__, session.graph) run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() for i in range(config.max_max_epoch): # setup epoch train_perplexity, slot1_loss, slot3_loss = run_epoch( session, training_model, data["x_train"], data["y_train"], data["p_train"], train_writer, run_options, run_metadata, target=data["l_train"], verbose=True, config=config) slot1_losses = slot1_losses + [np.mean(slot1_loss)] slot3_losses = slot3_losses + [np.mean(slot3_loss)] print( "Epoch: %d Avg. Total Mean Loss slot1: %.6f slot3: %.6f" % (i + 1, np.mean(slot1_losses), np.mean(slot3_losses))) # Output Config/Losses from util.evaluations import print_config print_config(config) # Save Losses for Later loss = {"slot1": slot1_losses, "slot3": slot3_losses} save_pickle( CONST.DATA_DIR + config.__class__.__name__ + "multi_task", loss) # Save CheckPoint saver = tf.train.Saver(tf.all_variables()) path = saver.save(sess=session, save_path=CONST.MULTITASK_CHECKPOINT_PATH_HURSH + "task") print("model saved: " + path) if config.get_summary: train_writer.close() session.close() # doesn't seem to close under scope?? # Try and plot the losses import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt x = [i for i in range(len(slot1_losses))] plt.plot(np.array(x), np.array(slot1_losses)) plt.plot(np.array(x), np.array(slot3_losses)) plt.legend(['slot1', 'slot3'], loc='upper right') plt.savefig("losses_" + config.__class__.__name__ + ".png") if not CONST.TRAIN: with tf.variable_scope( "model", reuse=True): # reuse scope to evaluate model #validation_model = slot1_and_slot3_model(is_training=False, config=config) # model class session.run( training_model.embeddings.embeddings.assign( data["embeddings"])) # load embeddings saver = tf.train.Saver() saver.restore(sess=session, save_path=CONST.MULTITASK_CHECKPOINT_PATH_HURSH + "task") slot1_predictions = [] slot3_predictions = [] alphas = [] # Get Predictions for step, (x, y_1, y_3, t) in enumerate( semeval_itterator(data["x_test"], data["y_test"], config.batch_size, config.num_steps, target=data["l_test"], polarity=data["p_test"], shuffle_examples=False)): if CONST.HEATMAP: alpha = session.run( [training_model.slot3_model.alpha], { training_model.embeddings.source_data: x, training_model.slot1_model.targets: y_1, training_model.slot3_model.targets: y_3 }) alphas = alphas + alpha[0].tolist() else: slot1_prediction, slot3_prediction = session.run( [ training_model.slot1_model.predictions, training_model.slot3_model.predictions ], { training_model.embeddings.source_data: x, training_model.slot1_model.targets: y_1, training_model.slot3_model.targets: y_3 }) slot1_predictions = slot1_predictions + slot1_prediction.tolist( ) slot3_predictions = slot3_predictions + slot3_prediction.tolist( ) if not CONST.HEATMAP: even_batch = len(data["x_test"]) % config.batch_size remove_added_batch = config.batch_size - even_batch del slot1_predictions[-remove_added_batch:] del slot3_predictions[-remove_added_batch:] slot1_predictions = [ np.asarray(x) for x in slot1_predictions ] slot3_predictions = [ np.asarray(x) for x in slot3_predictions ] # print congiuration for test predictions from util.evaluations import print_config print_config(config) slot3_y = [np.asarray(x) for x in data["p_test"]] # save predictions predictions = { "slot1": slot1_predictions, "slot3": slot3_predictions, "slot1_y": data["y_test"], "slot3_y": slot3_y } save_pickle( CONST.DATA_DIR + config.__class__.__name__ + "_predictions", predictions) print( "predictions saved to file ", CONST.DATA_DIR + config.__class__.__name__ + "_predictions") from util.evaluations import evaluate_multilabel from util.evaluations import evaluate_multiclass from util.evaluations import find_best_slot1 # evaluate_multilabel(predictions["slot1"], predictions["slot1_y"], CONST.THRESHOLD) print("\nslot3 sentiment ...\n") evaluate_multiclass(np.asarray(predictions["slot3"]), predictions["slot3_y"], True) print("\nfinding best threshold slot1 E\A pairs...\n") find_best_slot1("multitask_hursh", np.asarray(predictions["slot1"]), predictions["slot1_y"]) elif CONST.HEATMAP: even_batch = len(data["x_test"]) % config.batch_size remove_added_batch = config.batch_size - even_batch del alphas[-remove_added_batch:] distance = 0 sentences = data["test_sentences"] print("computing heatmaps and avg distance...") from util.heatmap import avg_distance_and_heatmaps avg_distance_and_heatmaps( alphas, sentences, CONST.SENTENCE_PATH + "/hursh_multitask")
def train_task(CONST, data): config = multitask() config.vocab_size = len(data["embeddings"]) tf.reset_default_graph() with tf.Graph().as_default(), tf.Session() as session: with tf.variable_scope( "model", reuse=None, initializer=tf.contrib.layers.xavier_initializer()): training_model = slot1_and_slot3_model( is_training=True, config=config) # model class if CONST.TRAIN: tf.initialize_all_variables().run() # Check if Model is Saved and then Load if check_saved_file(CONST.MULTITASK_CHECKPOINT_PATH + "checkpoint"): saver = tf.train.Saver() saver.restore(sess=session, save_path=CONST.MULTITASK_CHECKPOINT_PATH) session.run( training_model.embeddings.embeddings.assign( data["embeddings"])) # train slot1_losses = [] slot3_losses = [] train_writer = run_metadata = run_options = False if config.get_summary: # session = tf.InteractiveSession() train_writer = tf.train.SummaryWriter( CONST.SLOT1_MODEL_PATH + "attention_graph/" + config.__class__.__name__, session.graph) run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() for i in range(config.max_max_epoch): # setup epoch train_perplexity, slot1_loss, slot3_loss = run_epoch( session, training_model, data["x_train"], data["y_train"], data["p_train"], train_writer, run_options, run_metadata, category=data["a_train"], verbose=True, config=config) slot1_losses = slot1_losses + [np.mean(slot1_loss)] slot3_losses = slot3_losses + [np.mean(slot3_loss)] print( "Epoch: %d Avg. Total Mean Loss slot1: %.6f slot3: %.6f" % (i + 1, np.mean(slot1_losses), np.mean(slot3_losses))) # Output Config/Losses from util.evaluations import print_config print_config(config) # Save CheckPoint saver = tf.train.Saver(tf.all_variables()) path = saver.save(sess=session, save_path=CONST.MULTITASK_CHECKPOINT_PATH) print("model saved: " + path) # Save Losses for Later loss = {"slot1": slot1_losses, "slot3": slot3_losses} save_pickle( CONST.DATA_DIR + config.__class__.__name__ + "multi_task", loss) # Try and plot the losses import matplotlib.pyplot as plt x = [i for i in range(len(slot1_losses))] plt.plot(np.array(x), np.array(slot1_losses)) plt.plot(np.array(x), np.array(slot3_losses)) plt.savefig("losses_" + config.__class__.__name__ + ".png") if not CONST.TRAIN: with tf.variable_scope( "model", reuse=True): # reuse scope to evaluate model validation_model = slot1_and_slot3_model( is_training=True, config=config) # model class session.run( validation_model.embeddings.embeddings.assign( data["embeddings"])) # load embeddings saver = tf.train.Saver() saver.restore(sess=session, save_path=CONST.MULTITASK_CHECKPOINT_PATH) slot1_predictions = [] slot3_predictions = [] # Get Predictions for step, (x, y_1, y_3, e, a) in enumerate( semeval_itterator(data["x_test"], data["y_test"], config.batch_size, config.num_steps, category=data["aspects"], polarity=data["p_test"])): slot1_prediction, slot3_prediction = session.run( [ validation_model.slot1_model.predictions, validation_model.slot3_model.predicitions ], { validation_model.input_data: x, validation_model.targets: y, validation_model.input_e: e, validation_model.input_a: a }) slot1_predictions = slot1_predictions + slot1_prediction.tolist( ) slot3_predictions = slot3_predictions + slot3_prediction.tolist( ) even_batch = len(data["x_test"]) % config.batch_size remove_added_batch = config.batch_size - even_batch del slot1_predictions[-remove_added_batch:] del slot3_predictions[-remove_added_batch:] slot1_predictions = np.asarray(slot1_predictions) slot3_predictions = np.asarray(slot3_predictions) # print congiuration for test predictions from util.evaluations import print_config print_config(config) # save predictions predictions = { "slot1": slot1_predictions, "slot3": slot3_predictions, "slot1_y": data["y_test"], "slot3_y": data["p_test"] } save_pickle( CONST.DATA_DIR + config.__class__.__name__ + "_predictions", predictions) print( "predictions saved to file ", CONST.DATA_DIR + config.__class__.__name__ + "_predictions") from util.evaluations import evaluate_multilabel from util.evaluations import evaluate_multiclass evaluate_multilabel(predictions.slot1, predictions.slot1_y, CONST.THRESHOLD) evaluate_multiclass(predictions.slot3, predictions.slot3_y, True)
def main(CONST, data): config = get_config(CONST.MODEL) #if not CONST.TRAIN: # config.num_steps = 1 config.vocab_size = len(data["embeddings"]) config.max_max_epoch = CONST.MAX_EPOCH tf.reset_default_graph() # start graph and session # config=tf.ConfigProto(log_device_placement=True) pass to tf.Session # to see which devices all operations can run on with tf.Graph().as_default(), tf.Session() as session: # Returns an initializer that generates tensors with a uniform distribution. #initializer = tf.random_uniform_initializer(-config.init_scale, # config.init_scale) # Use initializer, mode with variable scope and both called mode # Returns a context for variable scope. with tf.variable_scope("model", reuse=None, initializer=tf.contrib.layers.xavier_initializer()): training_model = RNNModel(is_training=True, config=config) # model class if CONST.TRAIN: tf.initialize_all_variables().run() session.run(training_model.embedding.assign(data["embeddings"])) # train # Reload save epoch training time if CONST.RELOAD_TRAIN: saver = tf.train.Saver() saver.restore(sess=session, save_path=CONST.SLOT3_MODEL_PATH + "/slot3") # if config.__class__.__name__ is not "BiRNN": # training_model.initial_state.eval() all_losses = [] #training_model.assign_lr(session, 0.945) # RUN GRAPH? but no data? train_writer = run_metadata = run_options = False if config.get_summary: # session = tf.InteractiveSession() train_writer = tf.train.SummaryWriter(CONST.SLOT1_MODEL_PATH + "graph/" + config.__class__.__name__ , session.graph) run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() for i in range(config.max_max_epoch): # setup epoch # lr_decay = config.lr_decay ** max(i - config.max_epoch, 0.0) # learning rate decay? # training_model.assign_lr(session, config.learning_rate * lr_decay) # RUN GRAPH? but no data? #print("Epoch: %d Learning rate: %.3f" % (i + 1, session.run(training_model.lr))) train_perplexity, loss, losses = run_epoch(session, training_model, data["x_train"], data["y_train"], train_writer, run_options, run_metadata, verbose=True) all_losses = all_losses + [np.mean(losses)] print("Epoch: %d Avg. Total Mean Loss: %.6f" % (i + 1, np.mean(all_losses))) from util.evaluations import print_config print_config(config) import matplotlib.pyplot as plt # plt.plot([np.mean(all_losses[i-50:i]) for i in range(len(all_losses))]) figure_name = CONST.OUT_DIR + "loss/" + "losses_slot1" + config.__class__.__name__ + ".png" x = [i for i in range(len(all_losses))] plt.plot(np.array(x), np.array(all_losses)) plt.savefig(figure_name) save_pickle(CONST.DATA_DIR + config.__class__.__name__, all_losses) print("saved losses.png and data") print("loss: ", figure_name) print("loss data: ", CONST.DATA_DIR + config.__class__.__name__ + ".pickle") saver = tf.train.Saver(tf.all_variables()) path = saver.save(sess=session, save_path=CONST.SLOT1_MODEL_PATH + config.__class__.__name__ + "/slot1") print("model saved: " + path) if config.get_summary: train_writer.close() session.close() # doesn't seem to close under scope?? if not CONST.TRAIN: with tf.variable_scope("model", reuse=True): # reuse scope to evaluate model :-) config.batch_size = len(data["x_test"]) # Initialize Model Graph validation_model = RNNModel(is_training=False, config=config) #tf.initialize_all_variables().run() # set embeddings again session.run(validation_model.embedding.assign(data["embeddings"])) # Load Data Back Trained Model saver = tf.train.Saver() saver.restore(sess=session, save_path=CONST.SLOT1_MODEL_PATH + config.__class__.__name__ + "/slot1") x_test = [n[:config.num_steps] for n in data["x_test"]] # Get Predictions predictions = session.run(validation_model.predictions, {validation_model.input_data: x_test, validation_model.targets: data["y_test"]}) from util.evaluations import print_config print_config(config) from util.evaluations import evaluate_multilabel save_pickle(CONST.DATA_DIR + config.__class__.__name__ + "predictions", {"predictions": predictions, "y": data["y_test"]}) evaluate_multilabel(predictions, data["y_test"], CONST.THRESHOLD) print("predictions saved")
def main(CONST, data): config = get_config("CNN") # if not CONST.TRAIN: # config.num_steps = 1 config.vocab_size = len(data["embeddings"]) config.max_max_epoch = CONST.MAX_EPOCH #from util.evaluations import print_config #print_config(config) tf.reset_default_graph() # start graph and session # config=tf.ConfigProto(log_device_placement=True) pass to tf.Session # to see which devices all operations can run on with tf.Graph().as_default(), tf.Session(config=session_conf) as session: with tf.variable_scope( "model", reuse=None, initializer=tf.contrib.layers.xavier_initializer()): training_model = CNNModel(is_training=True, config=config) # model class if CONST.TRAIN: tf.initialize_all_variables().run() session.run(training_model.embedding.assign( data["embeddings"])) # train # Reload save epoch training time if CONST.RELOAD_TRAIN: print("Reloading previous run...") saver = tf.train.Saver() saver.restore(sess=session, save_path=CONST.CNN_MODEL_PATH + "cnn") all_losses = [] train_writer = run_metadata = run_options = False if config.get_summary: # session = tf.InteractiveSession() train_writer = tf.train.SummaryWriter( CONST.SLOT1_MODEL_PATH + "attention_graph/" + config.__class__.__name__, session.graph) run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() for i in range(config.max_max_epoch): # setup epoch train_perplexity, loss, losses = run_epoch( session, training_model, data["x_train"], data["p_train"], train_writer, run_options, run_metadata, category=data["a_train"], verbose=True) all_losses = all_losses + [np.mean(losses)] print("Epoch: %d Avg. Cost: %.6f" % (i + 1, np.mean(all_losses))) from util.evaluations import print_config print_config(config) import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt # plt.plot([np.mean(all_losses[i-50:i]) for i in range(len(all_losses))]) figure_name = CONST.OUT_DIR + "loss/" + "losses_slot3" + config.__class__.__name__ + ".png" x = [i for i in range(len(all_losses))] plt.plot(np.array(x), np.array(all_losses)) plt.savefig(figure_name) save_pickle(CONST.DATA_DIR + config.__class__.__name__ + "_slot3", all_losses) print("saved slot3 losses.png and losses data", figure_name) print("loss: ", figure_name) print( "loss data: ", CONST.DATA_DIR + config.__class__.__name__ + "_slot3" + ".pickle") #saver = tf.train.Saver(tf.all_variables()) #path = saver.save(sess=session, save_path=CONST.CNN_MODEL_PATH + "cnn") #print("model saved: " + path) if config.get_summary: train_writer.close() # session.close() # doesn't seem to close under scope?? if not False: with tf.variable_scope( "model", reuse=True): # reuse scope to evaluate model :-) # Initialize Model Graph validation_model = training_model predictions = [] correct_predictions = [] # Get Predictions for step, (x, y, l, e, a) in enumerate( semeval_itterator(data["x_test"], data["p_test"], validation_model.batch_size, validation_model.num_steps, category=data["a_train"], shuffle_examples=False)): pred, scores = session.run( [ validation_model.predictions, validation_model.scores ], { validation_model.input_data: x, validation_model.targets: y }) predictions = predictions + pred.tolist() correct_predictions = correct_predictions + scores.tolist() even_batch = len(data["x_test"]) % config.batch_size remove_added_batch = config.batch_size - even_batch predictions = correct_predictions del predictions[-remove_added_batch:] predictions = np.asarray(predictions) from util.evaluations import print_config from util.evaluations import evaluate_multiclass y = [np.asarray(e) for e in data["p_test"]] save_pickle( CONST.DATA_DIR + config.__class__.__name__ + "slot3_predictions", { "predictions": predictions, "y": y }) evaluate_multiclass(predictions, y, True) print("predictions saved")