Exemplo n.º 1
0
def do_evaluate(args):
    config = Config(args.model_path)
    helper = ModelHelper.load(args.model_path)
    embeddings = load_glove_vectors("../data/glove.6B.50d.txt", helper)
    config.embed_size = embeddings.shape[1]

    with tf.Graph().as_default():
        logger.info("Building model...", )
        start = time.time()
        model = NGramModel(helper, config, embeddings)

        logger.info("took %.2f seconds", time.time() - start)

        init = tf.global_variables_initializer()
        saver = tf.train.Saver()

        with tf.Session() as session:
            session.run(init)
            saver.restore(session, model.config.model_output)
            for i in range(1, 8):
                input_data = read_book_ngrams('../data/' + str(i) +
                                              '_5grams.txt')
                result = model.predict_book_age(session, input_data[0],
                                                input_data[1])
                print LBLS[result], input_data[1]
Exemplo n.º 2
0
def evaluate(args, config):
    test_dir = args.test
    datautil = DataUtil(config)
    helper = ModelHelper.load(args.model_path)
    test_raw = read_conll(test_dir)
    test_data = helper.vectorize(test_raw, config)
    embeddings = datautil.load_embeddings(args.embedding, helper)

    with tf.Graph().as_default():
        logger.info("Building model...", )
        start = time.time()
        model = None
        if config.model == 'tcn':
            model = TemporalConvnet(helper, config, embeddings, datautil)
        elif config.model == 'bilstm':
            model = BiLSTM(helper, config, embeddings, datautil)
        else:
            logger.warning("Selected model does not exist!")
            assert False
        assert model is not None, "Model is None, stop excuted!"
        logger.info("took %.2f seconds", time.time() - start)
        test_examples = model.preprocess_sequence_data(test_data)

        init = tf.global_variables_initializer()
        saver = tf.train.Saver()
        with tf.Session() as sess:
            sess.run(init)
            saver.restore(sess, config.model_output)
            token_cm, entity_scores = model.evaluate(sess, test_examples,
                                                     test_data)
            logger.debug("Token-level confusion matrix:\n" +
                         token_cm.as_table())
            logger.info("Entity level P/R/F1: %.4f/%.4f/%.4f", *entity_scores)
def do_evaluate(args):
    config = Config(args.model_path)
    helper = ModelHelper.load(args.model_path)
    input_data = read_ngrams(args.data)
    embeddings = load_embeddings(args, helper)
    config.embed_size = embeddings.shape[1]

    with tf.Graph().as_default():
        logger.info("Building model...",)
        start = time.time()
        model = NGramModel(helper, config, embeddings)

        logger.info("took %.2f seconds", time.time() - start)

        init = tf.global_variables_initializer()
        saver = tf.train.Saver()

        with tf.Session() as session:
            session.run(init)
            saver.restore(session, model.config.model_output)
            for sentence, labels, predictions in model.output(session, input_data):
                predictions = [LBLS[l] for l in predictions]
                print_sentence(args.output, sentence, labels, predictions)
Exemplo n.º 4
0
def do_shell(args):
    config = Config(args)
    helper = ModelHelper.load(args.model_path)
    embeddings = load_embeddings(args, helper)
    config.embed_size = embeddings.shape[1]

    with tf.Graph().as_default():
        logger.info("Building model...", )
        start = time.time()
        model = RNNModel(helper, config, embeddings)
        logger.info("took %.2f seconds", time.time() - start)

        init = tf.global_variables_initializer()
        saver = tf.train.Saver()

        with tf.Session() as session:
            session.run(init)
            saver.restore(session, model.config.model_output)

            print("""Welcome!
You can use this shell to explore the behavior of your model.
Please enter sentences with spaces between tokens, e.g.,
input> Germany 's representative to the European Union 's veterinary committee .
""")
            while True:
                # Create simple REPL
                try:
                    sentence = raw_input("input> ")
                    tokens = sentence.strip().split(" ")
                    for sentence, _, predictions in model.output(
                            session, [(tokens, ["O"] * len(tokens))]):
                        predictions = [LBLS[l] for l in predictions]
                        print_sentence(sys.stdout, sentence,
                                       [""] * len(tokens), predictions)
                except EOFError:
                    print("Closing session.")
                    break