Exemple #1
0
def get_params(start_model_path, start_type, info_fn_name, num_gpu):
    hp = hyperparams.HPSENLI3()
    nli_setting = BertNLI()
    set_level_debug()
    train_config = NLIExTrainConfig()
    train_config.num_gpu = num_gpu
    train_config.save_train_payload = True

    tokenizer = get_tokenizer()
    tf_logging.info("Intializing dataloader")
    data_loader = get_modified_data_loader(tokenizer, hp.seq_max,
                                           nli_setting.vocab_filename)
    tf_logging.info("loading batches")
    data = get_nli_data(hp, nli_setting)

    def init_fn(sess):
        start_type_generic = {
            'nli': 'cls',
            'nli_ex': 'cls_ex',
            'bert': 'bert',
            'cold': 'cold'
        }[start_type]
        return init_fn_generic(sess, start_type_generic, start_model_path)

    informative_fn = get_informative_fn_by_name(info_fn_name)
    return data, data_loader, hp, informative_fn, init_fn, train_config
Exemple #2
0
def run(explain_tag, method_name, model_path):
    hp = hyperparams.HPSENLI3()
    nli_setting = BertNLI()
    target_class = enlidef.get_target_class(explain_tag)
    data = get_nli_data(hp, nli_setting)
    train_batches, dev_batches = data
    flat_dev_batches = flatten_filter_batches(dev_batches, target_class)[:2000]

    if method_name in ['deletion_seq', "random", 'idf', 'deletion', 'LIME']:
        predictor = BaselineExPredictor(hp, nli_setting, model_path,
                                        method_name)
        acc_list = eval_fidelity_with_ex_predictor(predictor, flat_dev_batches,
                                                   explain_tag)
    elif method_name.startswith("nli_ex"):
        modeling_option = "co"
        predictor = NLIExPredictor(hp, nli_setting, model_path,
                                   modeling_option)
        acc_list = eval_fidelity_with_ex_predictor(predictor, flat_dev_batches,
                                                   explain_tag)
    elif method_name in [
            "elrp", "deeplift", "saliency", "grad*input", "intgrad"
    ]:
        acc_list = eval_fidelity_gradient(hp, nli_setting, flat_dev_batches,
                                          explain_tag, method_name, model_path)
    else:
        raise Exception(
            "method_name={} is not in the known method list.".format(
                method_name))

    print(method_name)
    for num_delete in sorted(acc_list.keys()):
        print("{}\t{}".format(num_delete, acc_list[num_delete]))
def train_nil_from(model_path, save_dir, resume=False):
    print("Load model path : ", model_path)
    print("Save dir : ", save_dir)

    def load_fn(sess, model_path):
        if not resume:
            return load_model_w_scope(sess, model_path, "bert")
        else:
            return load_model(sess, model_path)

    steps = 67000
    hp = hyperparams.HPSENLI3()
    nli_setting = BertNLI()
    data = get_nli_data(hp, nli_setting)
    set_level_debug()
    hp = hyperparams.HPSENLI3()
    n_gpu = 2
    return train_nli_multi_gpu(hp, nli_setting, save_dir, steps, data,
                               model_path, load_fn, n_gpu)
Exemple #4
0
def train_nil_from_bert(model_path, save_dir):
    def load_fn(sess, model_path):
        return load_model_w_scope(sess, model_path, "bert")

    max_steps = 61358
    max_steps = 36250
    hp = hyperparams.HPSENLI3()
    set_level_debug()
    nli_setting = BertNLI()
    data = get_snli_data(hp, nli_setting)
    n_gpu = 2
    return train_nli_multi_gpu(hp, nli_setting, save_dir, max_steps, data,
                               model_path, load_fn, n_gpu)
Exemple #5
0
def run(args):
    hp = hyperparams.HPSENLI3()
    nli_setting = BertNLI()
    data_loader = get_modified_data_loader2(hp, nli_setting)

    predict_nli_ex(
        hp,
        nli_setting,
        data_loader,
        args.tag,
        args.data_id,
        args.model_path,
        args.run_name,
        args.modeling_option,
    )
Exemple #6
0
def train_nil_from_v2_checkpoint(run_name, model_path):
    hp = hyperparams.HPSENLI3()
    print(hp.batch_size)
    nli_setting = NLI()
    nli_setting.vocab_size = 30522
    nli_setting.vocab_filename = "bert_voca.txt"
    data_loader = nli.DataLoader(hp.seq_max, nli_setting.vocab_filename, True)
    tokenizer = get_tokenizer()
    CLS_ID = tokenizer.convert_tokens_to_ids(["[CLS]"])[0]
    SEP_ID = tokenizer.convert_tokens_to_ids(["[SEP]"])[0]
    data_loader.CLS_ID = CLS_ID
    data_loader.SEP_ID = SEP_ID
    tf_logger.setLevel(logging.INFO)

    steps = 12271
    data = load_cache("nli_batch16")
    if data is None:
        tf_logger.info("Encoding data from csv")
        data = get_nli_batches_from_data_loader(data_loader, hp.batch_size)
        save_to_pickle(data, "nli_batch16")
    train_nli(hp, nli_setting, run_name, steps, data, model_path)
Exemple #7
0
def run(args):
    hp = hyperparams.HPSENLI3()
    nli_setting = BertNLI()
    data_loader = get_modified_data_loader2(hp, nli_setting)

    if args.method_name in [
            'deletion_seq', "random", 'idf', 'deletion', 'LIME'
    ]:
        predictor = nli_baseline_predict
    elif args.method_name in [
            "elrp",
            "deeplift",
            "saliency",
            "grad*input",
            "intgrad",
    ]:
        predictor = nli_attribution_predict
    else:
        raise Exception(
            "method_name={} is not in the known method list.".format(
                args.method_name))

    predictor(hp, nli_setting, data_loader, args.tag, args.method_name,
              args.data_id, args.sub_range, args.model_path)
Exemple #8
0
def train_nil_from(save_dir, model_path, load_fn, max_steps):
    hp = hyperparams.HPSENLI3()
    set_level_debug()
    nli_setting = BertNLI()
    data = get_nli_data(hp, nli_setting)
    train_nli(hp, nli_setting, save_dir, max_steps, data, model_path, load_fn)