def _do_eval(subreddits):
            eval_input_fn = make_input_fn_from_file(
                input_files_or_glob=FLAGS.input_files_or_glob,
                seq_length=FLAGS.max_seq_length,
                num_splits=FLAGS.num_splits,
                dev_splits=dev_splits,
                test_splits=test_splits,
                tokenizer=tokenizer,
                is_training=False,
                filter_test=False,
                seed=FLAGS.seed,
                labeler=labeler,
                subreddits=subreddits)

            result = estimator.evaluate(input_fn=eval_input_fn, steps=eval_steps)

            if len(subreddits) == 1:
                output_eval_file = os.path.join(
                    FLAGS.output_dir, 'eval', "eval_results_{}.txt".format(subreddit_idx_to_subreddit(subreddits[0])))

            else:
                output_eval_file = os.path.join(FLAGS.output_dir, 'eval', "eval_results_all.txt")

            with tf.gfile.GFile(output_eval_file, "w") as writer:
                tf.logging.info("***** Eval results *****")
                for key in sorted(result.keys()):
                    tf.logging.info("  %s = %s", key, str(result[key]))
                    writer.write("%s = %s\n" % (key, str(result[key])))
Beispiel #2
0
def subreddit_sim_ground_truth_and_naive_from_dataset(
        beta0=1.0,
        beta1=1.0,
        gamma=1.0,
        setting='simple',
        tfrecord_file='../dat/reddit/proc.tf_record',
        vocab_file="../../bert/pre-trained/uncased_L-12_H-768_A-12/vocab.txt",
        seed=0
):
    """
    This is a helper function that computes the ground truth and naive estimates
    by creating a tf dataset and 'sampling' the required observed data.
    This is necessary because we simulate outcomes on the fly using tf's dataset abstraction.


    :return:
    """

    tokenizer = tokenization.FullTokenizer(vocab_file=vocab_file, do_lower_case=True)
    labeler = make_subreddit_based_simulated_labeler(treat_strength=beta0, con_strength=beta1, noise_level=gamma,
                                                     setting=setting, seed=seed)

    input_fn = make_input_fn_from_file(
        input_files_or_glob=tfrecord_file,
        seq_length=128,
        num_splits=10,
        dev_splits=0,
        test_splits=0,
        tokenizer=tokenizer,
        is_training=True,
        subreddits=[13, 6, 8],
        shuffle_buffer_size=int(1e6),  # note: bert hardcoded this, and I'm following suit
        seed=seed,
        labeler=labeler)

    # input_fn = make_input_fn_from_tfrecord(tokenizer=tokenizer, tfrecord=tfrecord_file)

    params = {'batch_size': 2048}

    dataset = input_fn(params)
    sampler = dataset.make_one_shot_iterator()
    sample = sampler.get_next()

    with tf.Session() as sess:
        # confounding = sess.run(sample['confounding'])
        y, y0, y1, t = sess.run([sample['outcome'], sample['y0'], sample['y1'], sample['gender']])

    tf.reset_default_graph()

    ground_truth = y1[t == 1].mean() - y0[t == 1].mean()
    very_naive = y1[t == 1].mean() - y0[t == 0].mean()

    return {'ground_truth': ground_truth, 'noiseless_very_naive': very_naive}
def make_dataset(is_training: bool, do_masking=False):
    if FLAGS.simulated == 'real':
        labeler = make_real_labeler(FLAGS.treatment, 'log_score')

    elif FLAGS.simulated == 'attribute':
        labeler = make_subreddit_based_simulated_labeler(FLAGS.beta0,
                                                         FLAGS.beta1,
                                                         FLAGS.gamma,
                                                         FLAGS.simulation_mode,
                                                         seed=0)
    else:
        Exception("simulated flag not recognized")

    tokenizer = tokenization.FullTokenizer(vocab_file=FLAGS.vocab_file,
                                           do_lower_case=FLAGS.do_lower_case)

    dev_splits = [int(s) for s in str.split(FLAGS.dev_splits)]
    test_splits = [int(s) for s in str.split(FLAGS.test_splits)]

    if FLAGS.subreddits == '':
        subreddits = None
    else:
        subreddits = [int(s) for s in FLAGS.subreddits.split(',')]

    train_input_fn = make_input_fn_from_file(
        input_files_or_glob=FLAGS.input_files,
        seq_length=FLAGS.max_seq_length,
        num_splits=FLAGS.num_splits,
        dev_splits=dev_splits,
        test_splits=test_splits,
        tokenizer=tokenizer,
        do_masking=do_masking,
        subreddits=subreddits,
        is_training=is_training,
        shuffle_buffer_size=
        25000,  # note: bert hardcoded this, and I'm following suit
        seed=FLAGS.seed,
        labeler=labeler,
        filter_train=is_training)

    batch_size = FLAGS.train_batch_size if is_training else FLAGS.eval_batch_size

    dataset = train_input_fn(params={'batch_size': batch_size})

    # format expected by Keras for training
    if is_training:
        # dataset = filter_training(dataset)
        dataset = dataset.map(_keras_format,
                              num_parallel_calls=tf.data.experimental.AUTOTUNE)

    dataset = dataset.prefetch(4)

    return dataset
Beispiel #4
0
def subreddit_based_sim_dfs(
        subreddits,
        treat_strength,
        con_strength,
        noise_level,
        setting="simple",
        seed=0,
        base_output_dir='../dat/sim/reddit_subreddit_based/'):

    labeler = make_subreddit_based_simulated_labeler(treat_strength,
                                                     con_strength,
                                                     noise_level,
                                                     setting=setting,
                                                     seed=seed)

    num_splits = 10
    dev_splits = [0]
    test_splits = [0]

    # data_file = '../dat/reddit/proc.tf_record'
    # vocab_file = "../../bert/pre-trained/uncased_L-12_H-768_A-12/vocab.txt"
    tokenizer = tokenization.FullTokenizer(vocab_file=vocab_file,
                                           do_lower_case=True)

    input_dataset_from_filenames = make_input_fn_from_file(
        data_file,
        250,
        num_splits,
        dev_splits,
        test_splits,
        tokenizer,
        subreddits=subreddits,
        is_training=False,
        filter_test=False,
        shuffle_buffer_size=25000,
        seed=seed,
        labeler=labeler)

    all_data = dataset_fn_to_df(input_dataset_from_filenames)
    output_df = all_data[['index', 'gender', 'outcome', 'y0', 'y1']]
    output_df = output_df.rename(index=str, columns={'gender': 'treatment'})

    output_dir = os.path.join(base_output_dir,
                              "subreddits{}".format(subreddits),
                              "mode{}".format(setting))
    os.makedirs(output_dir, exist_ok=True)
    output_path = os.path.join(
        output_dir,
        "beta0{}.beta1{}.gamma{}.tsv".format(treat_strength, con_strength,
                                             noise_level))

    output_df.to_csv(output_path, '\t')
Beispiel #5
0
 def train_input_fn():
     train_input_fn = make_input_fn_from_file(
         input_files_or_glob=FLAGS.input_files,
         seq_length=FLAGS.max_seq_length,
         num_splits=1,
         dev_splits=[2],
         test_splits=[2],
         tokenizer=tokenizer,
         is_training=True,
         shuffle_buffer_size=
         25000,  # note: bert hardcoded this, and I'm following suit
         seed=FLAGS.seed,
         labeler=None)
     return train_input_fn(params={'batch_size': train_batch_size})
        def _do_predict(subreddits):
            predict_input_fn = make_input_fn_from_file(
                input_files_or_glob=FLAGS.input_files_or_glob,
                seq_length=FLAGS.max_seq_length,
                num_splits=FLAGS.num_splits,
                dev_splits=dev_splits,
                test_splits=test_splits,
                tokenizer=tokenizer,
                is_training=False,
                filter_test=False,
                subreddits=subreddits,
                seed=FLAGS.seed,
                labeler=labeler)

            result = estimator.predict(input_fn=predict_input_fn)

            if len(subreddits) == 1:
                output_predict_file = os.path.join(
                    FLAGS.output_dir, 'predict',
                    "test_results_{}.tsv".format(subreddit_idx_to_subreddit(subreddits[0])))
            else:
                output_predict_file = os.path.join(FLAGS.output_dir, 'predict', "test_results_all.tsv")

            with tf.gfile.GFile(output_predict_file, "w") as writer:
                tf.logging.info("***** Predict results *****")

                attribute_names = ['in_test',
                                   'treatment_probability',
                                   'expected_outcome_st_treatment', 'expected_outcome_st_no_treatment',
                                   'outcome', 'treatment',
                                   'index']

                header = "\t".join(
                    attribute_name for attribute_name in attribute_names) + "\n"
                writer.write(header)
                for prediction in result:
                    # prediction['subreddit'] = subreddit_idx_to_subreddit(prediction['subreddit'])
                    output_line = "\t".join(
                        str(prediction[attribute_name]) for attribute_name in attribute_names) + "\n"
                    writer.write(output_line)
def make_dataset(is_training: bool, do_masking=False):
    tokenizer = tokenization.FullTokenizer(
        vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)

    dev_splits = [int(s) for s in str.split(FLAGS.dev_splits)]
    test_splits = [int(s) for s in str.split(FLAGS.test_splits)]

    train_input_fn = make_input_fn_from_file(
        input_files_or_glob=FLAGS.input_files,
        seq_length=FLAGS.max_seq_length,
        num_splits=FLAGS.num_splits,
        dev_splits=dev_splits,
        test_splits=test_splits,
        tokenizer=tokenizer,
        do_masking=do_masking,
        is_training=is_training,
        shuffle_buffer_size=25000,  # note: bert hardcoded this, and I'm following suit
        seed=FLAGS.seed)

    batch_size = FLAGS.train_batch_size if is_training else FLAGS.eval_batch_size

    dataset = train_input_fn(params={'batch_size': batch_size})
    dataset = dataset.map(_make_length_labels)
    return dataset
def main(_):
    # tf.enable_eager_execution()  # for debugging
    tf.set_random_seed(FLAGS.seed)

    tf.logging.set_verbosity(tf.logging.INFO)

    if not FLAGS.do_train and not FLAGS.do_eval and not FLAGS.do_predict:
        raise ValueError(
            "At least one of `do_train`, `do_eval` or `do_predict' must be True.")

    bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file)

    if FLAGS.max_seq_length > bert_config.max_position_embeddings:
        raise ValueError(
            "Cannot use sequence length %d because the BERT model "
            "was only trained up to sequence length %d" %
            (FLAGS.max_seq_length, bert_config.max_position_embeddings))

    tf.gfile.MakeDirs(FLAGS.output_dir)

    tokenizer = tokenization.FullTokenizer(
        vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)

    tpu_cluster_resolver = None
    if FLAGS.use_tpu and FLAGS.tpu_name:
        tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
            FLAGS.tpu_name, zone=FLAGS.tpu_zone, project=FLAGS.gcp_project)

    is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2
    run_config = tf.contrib.tpu.RunConfig(
        cluster=tpu_cluster_resolver,
        master=FLAGS.master,
        model_dir=FLAGS.output_dir,
        save_checkpoints_steps=FLAGS.save_checkpoints_steps,
        keep_checkpoint_max=FLAGS.keep_checkpoints,
        # save_checkpoints_steps=None,
        # save_checkpoints_secs=None,
        save_summary_steps=10,
        tpu_config=tf.contrib.tpu.TPUConfig(
            iterations_per_loop=FLAGS.iterations_per_loop,
            num_shards=FLAGS.num_tpu_cores,
            per_host_input_for_training=is_per_host))

    # Estimator and data pipeline setup

    if FLAGS.simulated == 'real':
        # params = {'outcome': 'standard_score', 'treatment': 'gender'}
        # labeler = make_subreddit_standardized_scores()
        params = {'outcome': 'log_score', 'treatment': 'gender'}
        labeler = make_log_scores()

    elif FLAGS.simulated == 'attribute':
        params = {'outcome': 'outcome', 'treatment': 'gender'}
        labeler = make_subreddit_based_simulated_labeler(FLAGS.beta0, FLAGS.beta1, FLAGS.gamma, FLAGS.simulation_mode,
                                                         seed=FLAGS.seed)
    elif FLAGS.simulated == 'propensity':
        params = {'outcome': 'outcome', 'treatment': 'treatment'}

        output = pd.read_csv(FLAGS.base_propensities_path, '\t')
        base_propensity_scores = output['treatment_probability'].values
        example_indices = output['index'].values

        labeler = make_propensity_based_simulated_labeler(treat_strength=FLAGS.beta0,
                                                          con_strength=FLAGS.beta1,
                                                          noise_level=FLAGS.gamma,
                                                          base_propensity_scores=base_propensity_scores,
                                                          example_indices=example_indices,
                                                          exogeneous_con=FLAGS.exogenous_confounding,
                                                          setting=FLAGS.simulation_mode,
                                                          seed=FLAGS.seed)
    else:
        Exception("simulated flag not recognized")

    dev_splits = [int(s) for s in str.split(FLAGS.dev_splits)]
    test_splits = [int(s) for s in str.split(FLAGS.test_splits)]

    if FLAGS.subreddits == '':
        subreddits = None
    else:
        subreddits = [int(s) for s in FLAGS.subreddits.split(',')]

    num_train_steps = FLAGS.num_train_steps
    num_warmup_steps = FLAGS.num_warmup_steps

    model_fn = predictors.binary_treat_cont_outcome_model_fn_builder(
        bert_config=bert_config,
        init_checkpoint=FLAGS.init_checkpoint,
        learning_rate=FLAGS.learning_rate,
        num_train_steps=num_train_steps,
        num_warmup_steps=num_warmup_steps,
        use_tpu=FLAGS.use_tpu,
        use_one_hot_embeddings=FLAGS.use_tpu,
        label_pred=FLAGS.label_pred,
        unsupervised=FLAGS.unsupervised,
        polyak=False,
        use_extra_features=FLAGS.use_subreddit)

    # If TPU is not available, this will fall back to normal Estimator on CPU
    # or GPU.
    estimator = tf.contrib.tpu.TPUEstimator(
        use_tpu=FLAGS.use_tpu,
        model_fn=model_fn,
        config=run_config,
        train_batch_size=FLAGS.train_batch_size,
        eval_batch_size=FLAGS.eval_batch_size,
        predict_batch_size=FLAGS.predict_batch_size,
        params=params)

    if FLAGS.do_train:

        tf.logging.info("***** Running training *****")
        tf.logging.info("  Batch size = %d", FLAGS.train_batch_size)
        tf.logging.info("  Num steps = %d", num_train_steps)

        # subsample and process the data
        with tf.name_scope("training_data"):
            train_input_fn = make_input_fn_from_file(
                input_files_or_glob=FLAGS.input_files_or_glob,
                seq_length=FLAGS.max_seq_length,
                num_splits=FLAGS.num_splits,
                dev_splits=dev_splits,
                test_splits=test_splits,
                tokenizer=tokenizer,
                is_training=True,
                shuffle_buffer_size=int(1e6),  # note: bert hardcoded this, and I'm following suit
                seed=FLAGS.seed,
                labeler=labeler,
                subreddits=subreddits)

        # additional logging
        hooks = []
        if FLAGS.label_pred:
            hooks += [
                tf.train.LoggingTensorHook({
                    # 'labels_float': 'dragon_net/outcome_st_no_treatment/loss/labels_float',
                    # 'prediction': 'dragon_net/outcome_st_no_treatment/loss/prediction',
                    # 'per_example_loss': 'dragon_net/outcome_st_no_treatment/loss/per_example_loss'

                    # 'token_ids': 'token_ids',
                    # 'token_mask': 'token_mask',
                    # 'label_ids': 'label_ids',
                    # 'pred_in': 'summary/in_split/predictions',
                    # 'pred_out': 'summary/out_split/predictions',
                    # 'ra_in': 'summary/in_split/labels/kappa/batch_random_agreement/random_agreement',
                    # 'ra_out': 'summary/out_split/labels/kappa/batch_random_agreement/random_agreement',
                },
                    every_n_iter=1)
            ]

        estimator.train(input_fn=train_input_fn, max_steps=num_train_steps, hooks=hooks)

    if FLAGS.do_train and (FLAGS.do_eval or FLAGS.do_predict):
        # reload the model to get rid of dropout and input token masking
        trained_model_checkpoint = tf.train.latest_checkpoint(FLAGS.output_dir)
        model_fn = predictors.binary_treat_cont_outcome_model_fn_builder(
            bert_config=bert_config,
            init_checkpoint=trained_model_checkpoint,
            learning_rate=FLAGS.learning_rate,
            num_train_steps=num_train_steps,
            num_warmup_steps=num_warmup_steps,
            use_tpu=FLAGS.use_tpu,
            use_one_hot_embeddings=FLAGS.use_tpu,
            label_pred=True,
            unsupervised=False,
            polyak=False,
            use_extra_features=FLAGS.use_subreddit
        )

        estimator = tf.contrib.tpu.TPUEstimator(
            use_tpu=FLAGS.use_tpu,
            model_fn=model_fn,
            config=run_config,
            train_batch_size=FLAGS.train_batch_size,
            eval_batch_size=FLAGS.eval_batch_size,
            predict_batch_size=FLAGS.predict_batch_size,
            params=params)

    if FLAGS.do_eval:
        tf.logging.info("***** Running evaluation *****")
        # tf.logging.info("  Num examples = %d", len(eval_examples))
        tf.logging.info("  Batch size = %d", FLAGS.eval_batch_size)

        # This tells the estimator to run through the entire set.
        eval_steps = None
        # However, if running eval on the TPU, you will need to specify the
        # number of steps.
        if FLAGS.use_tpu:
            # Eval will be slightly WRONG on the TPU because it will truncate
            # the last batch.
            pass
            # eval_steps = int(len(eval_examples) / FLAGS.eval_batch_size)

        eval_drop_remainder = True if FLAGS.use_tpu else False

        def _do_eval(subreddits):
            eval_input_fn = make_input_fn_from_file(
                input_files_or_glob=FLAGS.input_files_or_glob,
                seq_length=FLAGS.max_seq_length,
                num_splits=FLAGS.num_splits,
                dev_splits=dev_splits,
                test_splits=test_splits,
                tokenizer=tokenizer,
                is_training=False,
                filter_test=False,
                seed=FLAGS.seed,
                labeler=labeler,
                subreddits=subreddits)

            result = estimator.evaluate(input_fn=eval_input_fn, steps=eval_steps)

            if len(subreddits) == 1:
                output_eval_file = os.path.join(
                    FLAGS.output_dir, 'eval', "eval_results_{}.txt".format(subreddit_idx_to_subreddit(subreddits[0])))

            else:
                output_eval_file = os.path.join(FLAGS.output_dir, 'eval', "eval_results_all.txt")

            with tf.gfile.GFile(output_eval_file, "w") as writer:
                tf.logging.info("***** Eval results *****")
                for key in sorted(result.keys()):
                    tf.logging.info("  %s = %s", key, str(result[key]))
                    writer.write("%s = %s\n" % (key, str(result[key])))

        os.makedirs(os.path.join(FLAGS.output_dir, 'eval'), exist_ok=True)
        _do_eval(subreddits)

    if FLAGS.do_predict:
        tf.logging.info("***** Running prediction*****")

        if FLAGS.use_tpu:
            # Warning: According to tpu_estimator.py Prediction on TPU is an
            # experimental feature and hence not supported here
            raise ValueError("Prediction in TPU not supported")

        def _do_predict(subreddits):
            predict_input_fn = make_input_fn_from_file(
                input_files_or_glob=FLAGS.input_files_or_glob,
                seq_length=FLAGS.max_seq_length,
                num_splits=FLAGS.num_splits,
                dev_splits=dev_splits,
                test_splits=test_splits,
                tokenizer=tokenizer,
                is_training=False,
                filter_test=False,
                subreddits=subreddits,
                seed=FLAGS.seed,
                labeler=labeler)

            result = estimator.predict(input_fn=predict_input_fn)

            if len(subreddits) == 1:
                output_predict_file = os.path.join(
                    FLAGS.output_dir, 'predict',
                    "test_results_{}.tsv".format(subreddit_idx_to_subreddit(subreddits[0])))
            else:
                output_predict_file = os.path.join(FLAGS.output_dir, 'predict', "test_results_all.tsv")

            with tf.gfile.GFile(output_predict_file, "w") as writer:
                tf.logging.info("***** Predict results *****")

                attribute_names = ['in_test',
                                   'treatment_probability',
                                   'expected_outcome_st_treatment', 'expected_outcome_st_no_treatment',
                                   'outcome', 'treatment',
                                   'index']

                header = "\t".join(
                    attribute_name for attribute_name in attribute_names) + "\n"
                writer.write(header)
                for prediction in result:
                    # prediction['subreddit'] = subreddit_idx_to_subreddit(prediction['subreddit'])
                    output_line = "\t".join(
                        str(prediction[attribute_name]) for attribute_name in attribute_names) + "\n"
                    writer.write(output_line)

        os.makedirs(os.path.join(FLAGS.output_dir, 'predict'), exist_ok=True)
        # for subreddit in range(20):
        #     _do_predict(subreddit)
        _do_predict(subreddits)