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.device("/gpu:0"):
        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", )
                    score, dependencies = parser.parse(test_set)
                    print("- test score: {:.2f}".format(score * 100.0))
                    print("Writing predictions")
                    with open('q2_test.predicted.pkl', 'wb') as f:
                        pickle.dump(dependencies, f, -1)
                    print("Done!")
def main(debug=True):
    print(80 * "=")
    print("INITIALIZING")
    con = Config()
    print((80 * "="), con)
    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...")
        t = time.time()
        model = ParserModel(con, 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() - t))
    graph.finalize()

    with tf.Session(graph=graph) as session:
        parser.session = session
        session.run(init_op)

        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:
                pickle.dump(dependencies, f, -1)
            print("Done!")
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/')

    print ("Building model...",)
    start = time.time()
    model = ParserModel(config, embeddings)
    parser.model = model
    print ("took {:.2f} seconds\n".format(time.time() - start))
    print( 80 * "=")
    print( "TRAINING")
    print( 80 * "=")
    save = False if debug else True
    model.fit(parser, save, train_examples, dev_set)

    if not debug:
        print (80 * "=")
        print ("TESTING")
        print (80 * "=")
        print ("Restoring the best model weights found on the dev set")
        model.W = t.load('./data/weights/W2.pth')
        model.U = t.load('./data/weights/U2.pth')
        model.embedded = t.load('./data/weights/embedded2.pth')
        model.V = t.load('./data/weights/V2.pth')
        model.b1 = t.load('./data/weights/b1_ext.pth')
        model.b2 = t.load('./data/weights/b2_ext.pth')
        model.b3 = t.load('./data/weights/b3_ext.pth')
        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_extension.predicted.pkl', 'wb') as f:
            pickle.dump(dependencies, f, 1)
        print ("Done!")
Exemple #4
0
    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("INITIALIZING")
    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)