def main(debug=True): print 80 * "=" print "INITIALIZING" print 80 * "=" config = Config() parser, embeddings, train_examples, dev_set, test_set = load_and_preprocess_data(debug) if not os.path.exists('./data/weights/'): os.makedirs('./data/weights/') with tf.Graph().as_default(): print "Building model...", start = time.time() model = ParserModel(config, embeddings) parser.model = model print "took {:.2f} seconds\n".format(time.time() - start) init = tf.global_variables_initializer() # If you are using an old version of TensorFlow, you may have to use # this initializer instead. # init = tf.initialize_all_variables() saver = None if debug else tf.train.Saver() with tf.Session() as session: parser.session = session session.run(init) print 80 * "=" print "TRAINING" print 80 * "=" model.fit(session, saver, parser, train_examples, dev_set) if not debug: print 80 * "=" print "TESTING" print 80 * "=" print "Restoring the best model weights found on the dev set" saver.restore(session, './data/weights/parser.weights') print "Final evaluation on test set", UAS, dependencies = parser.parse(test_set) print "- test UAS: {:.2f}".format(UAS * 100.0) print "Writing predictions" with open('q2_test.predicted.pkl', 'w') as f: cPickle.dump(dependencies, f, -1) print "Done!"
dev_UAS, _ = parser.parse(dev_data) print("- dev UAS: {:.2f}".format(dev_UAS * 100.0)) return dev_UAS if __name__ == "__main__": # Note: Set debug to False, when training on entire corpus # debug = True debug = False assert(torch.__version__ == "1.0.0"), "Please install torch version 1.0.0" print(80 * "=") print(f"INITIALIZING debug={debug}") print(80 * "=") parser, embeddings, train_data, dev_data, test_data = load_and_preprocess_data( debug) start = time.time() model = ParserModel(embeddings) parser.model = model print("took {:.2f} seconds\n".format(time.time() - start)) print(80 * "=") print("TRAINING") print(80 * "=") output_dir = "results/{:%Y%m%d_%H%M%S}/".format(datetime.now()) output_path = output_dir + "model.weights" if not os.path.exists(output_dir): os.makedirs(output_dir)
def __init__(self, config, pretrained_embeddings): self.pretrained_embeddings = pretrained_embeddings self.config = config self.build() <<<<<<< HEAD def main(debug=True): ======= def main(debug=False): >>>>>>> master print(80 * "=") print("INITIALIZING") print(80 * "=") config = Config() parser, embeddings, train_examples, dev_set, test_set = load_and_preprocess_data(debug) if not os.path.exists('./data/weights/'): os.makedirs('./data/weights/') with tf.Graph().as_default() as graph: print("Building model...", end=' ') start = time.time() model = ParserModel(config, embeddings) parser.model = model init_op = tf.global_variables_initializer() saver = None if debug else tf.train.Saver() print("took {:.2f} seconds\n".format(time.time() - start)) graph.finalize() with tf.Session(graph=graph).as_default() as session: parser.session = session