Beispiel #1
0
def main(_):
    # set GPU device
    os.environ["CUDA_VISIBLE_DEVICES"] = FLAGS.gpu

    # set up data and model directories
    FLAGS.data_dir = os.path.join(os.path.dirname(__file__), "..", "data",
                                  FLAGS.dataset)
    print("Reading data from {}".format(FLAGS.data_dir))

    # set up source and target length
    FLAGS.max_sc_length = FLAGS.max_sc_length \
        if not _buckets else _buckets[-1][0]
    FLAGS.max_tg_length = FLAGS.max_tg_length \
        if not _buckets else _buckets[-1][1]

    # set up encoder/decider dropout rate
    if FLAGS.universal_keep >= 0 and FLAGS.universal_keep < 1:
        FLAGS.sc_input_keep = FLAGS.universal_keep
        FLAGS.sc_output_keep = FLAGS.universal_keep
        FLAGS.tg_input_keep = FLAGS.universal_keep
        FLAGS.tg_output_keep = FLAGS.universal_keep
        FLAGS.attention_input_keep = FLAGS.universal_keep
        FLAGS.attention_output_keep = FLAGS.universal_keep

    # set up source and target vocabulary size
    FLAGS.sc_token_embedding_size = FLAGS.cm_known_vocab_size \
        if FLAGS.explain else FLAGS.nl_known_vocab_size
    FLAGS.sc_vocab_size = FLAGS.cm_vocab_size \
        if FLAGS.explain else FLAGS.nl_vocab_size
    FLAGS.tg_token_embedding_size = FLAGS.nl_known_vocab_size \
        if FLAGS.explain else FLAGS.cm_known_vocab_size
    FLAGS.tg_vocab_size = FLAGS.nl_vocab_size \
        if FLAGS.explain else FLAGS.cm_vocab_size

    # adjust hyperparameters for batch normalization
    if FLAGS.recurrent_batch_normalization:
        # larger batch size
        FLAGS.batch_size *= 4
        # larger initial learning rate
        FLAGS.learning_rate *= 10

    if FLAGS.decoder_topology in ['basic_tree']:
        FLAGS.model_root_dir = os.path.join(os.path.dirname(__file__), "..",
                                            FLAGS.model_root_dir, "seq2tree")
    elif FLAGS.decoder_topology in ['rnn']:
        FLAGS.model_root_dir = os.path.join(os.path.dirname(__file__), "..",
                                            FLAGS.model_root_dir, "seq2seq")
    else:
        raise ValueError("Unrecognized decoder topology: {}.".format(
            FLAGS.decoder_topology))
    print("Saving models to {}".format(FLAGS.model_root_dir))

    if FLAGS.data_stats:
        data_statistics()
    elif FLAGS.process_data:
        process_data()

    elif FLAGS.write_predictions_to_file:
        write_predictions_to_file(
            os.path.join(FLAGS.data_dir, '..', 'reader', 'data.final0502',
                         'test.3112.cm'), 'test.3112.explain')

    elif FLAGS.eval_local_slot_filling:
        train_path = os.path.join(FLAGS.model_dir, 'train.mappings.X.Y.npz')
        dev_path = os.path.join(FLAGS.model_dir, 'dev.mappings.X.Y.npz')
        eval_local_slot_filling(train_path, dev_path)

    elif FLAGS.demo:
        demo()

    else:
        train_set, dev_set, test_set = load_data(
            load_mappings=FLAGS.dataset.startswith("bash"),
            load_pointers=FLAGS.dataset.startswith("bash"))
        dataset = test_set if FLAGS.test else dev_set
        if FLAGS.gen_eval_sheet:
            gen_eval_sheet(dataset)
        elif FLAGS.eval:
            eval(dataset, verbose=True)
        elif FLAGS.manual_eval:
            manual_eval(dataset, 100)
        elif FLAGS.eval_slot_filling:
            eval_slot_filling(dataset)
        elif FLAGS.gen_slot_filling_training_data:
            gen_slot_filling_training_data(train_set, dev_set)
        elif FLAGS.decode:
            model_dir, decode_sig = decode(dataset)
            if not FLAGS.explain:
                eval(dataset, model_dir, decode_sig, verbose=False)
        elif FLAGS.grid_search:
            meta_experiments.grid_search(train, decode, eval, train_set,
                                         dataset, FLAGS)
        elif FLAGS.schedule_experiments:
            schedule_experiments(train, decode, eval, train_set, dataset)
        elif FLAGS.cross_valid:
            cross_validation(train_set)
        else:
            # Train the model.
            train(train_set, dataset)

            # Decode the new model on the development set.
            tf.reset_default_graph()
            model_dir, decode_sig = decode(dataset)

            # Run automatic evaluation on the development set.
            if not FLAGS.explain:
                eval(dataset, model_dir, decode_sig, verbose=False)
Beispiel #2
0
def main(_):
    # set GPU device
    os.environ["CUDA_VISIBLE_DEVICES"] = FLAGS.gpu
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
 
    # set up data and model directories
    FLAGS.data_dir = os.path.join(
        os.path.dirname(__file__), "..", "data", FLAGS.dataset)
    print("Reading data from {}".format(FLAGS.data_dir))

    # set up encoder/decider dropout rate
    if FLAGS.universal_keep >= 0 and FLAGS.universal_keep < 1:
        FLAGS.sc_input_keep = FLAGS.universal_keep
        FLAGS.sc_output_keep = FLAGS.universal_keep
        FLAGS.tg_input_keep = FLAGS.universal_keep
        FLAGS.tg_output_keep = FLAGS.universal_keep
        FLAGS.attention_input_keep = FLAGS.universal_keep
        FLAGS.attention_output_keep = FLAGS.universal_keep

    # adjust hyperparameters for batch normalization
    if FLAGS.recurrent_batch_normalization:
        # larger batch size
        FLAGS.batch_size *= 4
        # larger initial learning rate
        FLAGS.learning_rate *= 10

    if FLAGS.decoder_topology in ['basic_tree']:
        FLAGS.model_root_dir = os.path.join(
            os.path.dirname(__file__), "..", FLAGS.model_root_dir, "seq2tree")
    elif FLAGS.decoder_topology in ['rnn']:
        FLAGS.model_root_dir = os.path.join(
            os.path.dirname(__file__), "..", FLAGS.model_root_dir, "seq2seq")
    else:
        raise ValueError("Unrecognized decoder topology: {}."
                         .format(FLAGS.decoder_topology))
    print("Saving models to {}".format(FLAGS.model_root_dir))

    if FLAGS.process_data:
        process_data()

    else:
        train_set, dev_set, test_set = \
            data_utils.load_data(FLAGS, use_buckets=True, load_mappings=False)
        vocab = data_utils.load_vocabulary(FLAGS)

        print("Set dataset parameters")
        FLAGS.max_sc_length = train_set.max_sc_length if not train_set.buckets else \
            train_set.buckets[-1][0]
        FLAGS.max_tg_length = train_set.max_tg_length if not train_set.buckets else \
            train_set.buckets[-1][1]
        FLAGS.sc_vocab_size = len(vocab.sc_vocab)
        FLAGS.tg_vocab_size = len(vocab.tg_vocab)
        FLAGS.max_sc_token_size = vocab.max_sc_token_size
        FLAGS.max_tg_token_size = vocab.max_tg_token_size

        dataset = test_set if FLAGS.test else dev_set
        if FLAGS.eval:
            eval(dataset)
            save_hyperparameters()
        elif FLAGS.gen_error_analysis_sheet:
            gen_error_analysis_sheets(dataset, group_by_utility=True)
        elif FLAGS.gen_manual_evaluation_sheet:
            error_analysis.gen_manual_evaluation_csv(dataset, FLAGS)
        elif FLAGS.gen_manual_evaluation_sheet_single_model:
            error_analysis.gen_manual_evaluation_csv_single_model(dataset, FLAGS)
        elif FLAGS.gen_manual_evaluation_table:
            if FLAGS.test:
                eval_tools.gen_evaluation_table(dataset, FLAGS)
            else:
                eval_tools.gen_evaluation_table(dataset, FLAGS, num_examples=100)
        elif FLAGS.gen_auto_evaluation_table:
            eval_tools.gen_automatic_evaluation_table(dataset, FLAGS)
        elif FLAGS.tabulate_example_predictions:
            error_analysis.tabulate_example_predictions(dataset, FLAGS, num_examples=100)

        elif FLAGS.gen_slot_filling_training_data:
            gen_slot_filling_training_data(FLAGS, [train_set, dev_set, test_set])

        elif FLAGS.decode:
            model = decode(dataset, buckets=train_set.buckets)
            if not FLAGS.explain:
                eval(dataset, model.model_dir, model.decode_sig, verbose=False)

        elif FLAGS.demo:
            demo(buckets=train_set.buckets)

        elif FLAGS.grid_search:
            meta_experiments.grid_search(
                train, decode, eval, train_set, dataset, FLAGS)
        elif FLAGS.schedule_experiments:
            schedule_experiments(
                train, decode, eval, train_set, dataset)
        else:
            # Train the model.
            train(train_set, dataset)

            if FLAGS.normalized:
                tf.reset_default_graph()
                gen_slot_filling_training_data(FLAGS, [train_set, dev_set, test_set])
                FLAGS.fill_argument_slots = True

            # save model hyperparameters
            save_hyperparameters() 

            # Decode the new model on the development set.
            tf.reset_default_graph()
            model = decode(dataset, buckets=train_set.buckets)

            # Run automatic evaluation on the development set.
            if not FLAGS.explain:
                eval(dataset, model.model_dir, model.decode_sig, verbose=False)