Esempio n. 1
0
    def __init__(self):
        # 建立两个 graph
        self.g1 = tf.Graph()
        self.g2 = tf.Graph()

        # 为每个 graph 建创建一个 session
        self.sess1 = tf.Session(graph=self.g1)
        self.sess2 = tf.Session(graph=self.g2)

        self.bi_label_list = ['0', '1']
        self.bi_num_labels = len(self.bi_label_list)

        self.muti_label_list = [str(i) for i in range(40)]
        self.muti_num_labels = len(self.muti_label_list)

        self.configuration = cf.get_config()

        self.max_seq_len = self.configuration['max_seq_length']
        self.bi_bert_config_file = self.configuration['bert_config']
        self.muti_bert_config_file = self.configuration['bert_config']

        self.tokenizer = tokenization.FullTokenizer(
            vocab_file=self.configuration['vocab_file'], do_lower_case=True)

        self.bi_init_checkpoint = self.configuration['bi_init_checkpoint']
        self.muti_init_checkpoint = self.configuration['muti_init_checkpoint']

        self._init_session()
    def __init__(self):
        self.configuration = cf_muti.get_config()
        self.bert_config_file = self.configuration['bert_config']
        self.do_lower_case = self.configuration['do_lower_case']
        self.init_checkpoint = self.configuration['init_checkpoint']
        self.max_seq_length = self.configuration['max_seq_length']
        self.vocab_file = self.configuration['vocab_file']
        self.bert_config = modeling.BertConfig.from_json_file(self.bert_config_file)
        self.tokenizer = tokenization.FullTokenizer(
            vocab_file=self.vocab_file, do_lower_case=self.do_lower_case)
        self.label_list = [str(i) for i in range(40)]

        Args = collections.namedtuple("Args", ["init_checkpoint", "max_seq_length", "bert_config", "num_labels"])
        self.args = Args(init_checkpoint=self.init_checkpoint, max_seq_length=self.max_seq_length,
                         bert_config=self.bert_config, num_labels=len(self.label_list))

        self.is_training = False
        self.use_one_hot_embeddings = False
        self.session = None
        self.graph = None

        self.input_ids_ph = None
        self.input_mask_ph = None
        self.segment_ids_ph = None

        self.init_checkpoint = self.args.init_checkpoint
        self.max_seq_length = self.args.max_seq_length
        self.bert_config = self.args.bert_config
        self.num_labels = self.args.num_labels

        self._init_session()

        self._build_graph()
def main(_):
    tf.logging.set_verbosity(tf.logging.INFO)

    processors = {
        "cola": ColaProcessor,
        "mnli": MnliProcessor,
        "mrpc": MrpcProcessor,
        "xnli": XnliProcessor,
        "muti_attr": MutiattrProcessor,
    }

    tokenization.validate_case_matches_checkpoint(FLAGS.muti_do_lower_case,
                                                  FLAGS.muti_init_checkpoint)

    if not FLAGS.muti_do_train and not FLAGS.muti_do_eval and not FLAGS.muti_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.muti_bert_config_file)

    if FLAGS.muti_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.muti_max_seq_length, bert_config.max_position_embeddings))

    tf.gfile.MakeDirs(FLAGS.muti_output_dir)

    task_name = FLAGS.muti_task_name.lower()

    if task_name not in processors:
        raise ValueError("Task not found: %s" % task_name)

    processor = processors[task_name]()

    label_list = processor.get_labels()

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

    tpu_cluster_resolver = None
    if FLAGS.muti_use_tpu and FLAGS.muti_tpu_name:
        tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
            FLAGS.muti_tpu_name,
            zone=FLAGS.muti_tpu_zone,
            project=FLAGS.muti_gcp_project)

    is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2
    run_config = tf.contrib.tpu.RunConfig(
        cluster=tpu_cluster_resolver,
        master=FLAGS.muti_master,
        model_dir=FLAGS.muti_output_dir,
        save_checkpoints_steps=FLAGS.muti_save_checkpoints_steps,
        tpu_config=tf.contrib.tpu.TPUConfig(
            iterations_per_loop=FLAGS.muti_iterations_per_loop,
            num_shards=FLAGS.muti_num_tpu_cores,
            per_host_input_for_training=is_per_host))

    train_examples = None
    num_train_steps = None
    num_warmup_steps = None
    if FLAGS.muti_do_train:
        train_examples = processor.get_train_examples(FLAGS.muti_data_dir)
        num_train_steps = int(
            len(train_examples) / FLAGS.muti_train_batch_size *
            FLAGS.muti_num_train_epochs)
        num_warmup_steps = int(num_train_steps * FLAGS.muti_warmup_proportion)

    model_fn = model_fn_builder(bert_config=bert_config,
                                num_labels=len(label_list),
                                init_checkpoint=FLAGS.muti_init_checkpoint,
                                learning_rate=FLAGS.muti_learning_rate,
                                num_train_steps=num_train_steps,
                                num_warmup_steps=num_warmup_steps,
                                use_tpu=FLAGS.muti_use_tpu,
                                use_one_hot_embeddings=FLAGS.muti_use_tpu)

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

    if FLAGS.muti_do_train:
        train_file = os.path.join(FLAGS.muti_output_dir, "train.tf_record")
        file_based_convert_examples_to_features(train_examples, label_list,
                                                FLAGS.muti_max_seq_length,
                                                tokenizer, train_file)
        tf.logging.info("***** Running training *****")
        tf.logging.info("  Num examples = %d", len(train_examples))
        tf.logging.info("  Batch size = %d", FLAGS.muti_train_batch_size)
        tf.logging.info("  Num steps = %d", num_train_steps)
        train_input_fn = file_based_input_fn_builder(
            input_file=train_file,
            seq_length=FLAGS.muti_max_seq_length,
            is_training=True,
            drop_remainder=True)
        estimator.train(input_fn=train_input_fn, max_steps=num_train_steps)

    if FLAGS.muti_do_eval:
        eval_examples = processor.get_dev_examples(FLAGS.muti_data_dir)
        num_actual_eval_examples = len(eval_examples)
        if FLAGS.muti_use_tpu:
            # TPU requires a fixed batch size for all batches, therefore the number
            # of examples must be a multiple of the batch size, or else examples
            # will get dropped. So we pad with fake examples which are ignored
            # later on. These do NOT count towards the metric (all tf.metrics
            # support a per-instance weight, and these get a weight of 0.0).
            while len(eval_examples) % FLAGS.muti_eval_batch_size != 0:
                eval_examples.append(PaddingInputExample())

        eval_file = os.path.join(FLAGS.muti_output_dir, "eval.tf_record")
        file_based_convert_examples_to_features(eval_examples, label_list,
                                                FLAGS.max_seq_length,
                                                tokenizer, eval_file)

        tf.logging.info("***** Running evaluation *****")
        tf.logging.info("  Num examples = %d (%d actual, %d padding)",
                        len(eval_examples), num_actual_eval_examples,
                        len(eval_examples) - num_actual_eval_examples)
        tf.logging.info("  Batch size = %d", FLAGS.muti_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.muti_use_tpu:
            assert len(eval_examples) % FLAGS.muti_eval_batch_size == 0
            eval_steps = int(len(eval_examples) // FLAGS.muti_eval_batch_size)

        eval_drop_remainder = True if FLAGS.muti_use_tpu else False
        eval_input_fn = file_based_input_fn_builder(
            input_file=eval_file,
            seq_length=FLAGS.muti_max_seq_length,
            is_training=False,
            drop_remainder=eval_drop_remainder)

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

        output_eval_file = os.path.join(FLAGS.muti_output_dir,
                                        "eval_results.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])))

    if FLAGS.muti_do_predict:
        predict_examples = processor.get_test_examples(FLAGS.muti_data_dir)
        num_actual_predict_examples = len(predict_examples)
        if FLAGS.muti_use_tpu:
            # TPU requires a fixed batch size for all batches, therefore the number
            # of examples must be a multiple of the batch size, or else examples
            # will get dropped. So we pad with fake examples which are ignored
            # later on.
            while len(predict_examples) % FLAGS.muti_predict_batch_size != 0:
                predict_examples.append(PaddingInputExample())

        predict_file = os.path.join(FLAGS.muti_output_dir, "predict.tf_record")
        file_based_convert_examples_to_features(predict_examples, label_list,
                                                FLAGS.muti_max_seq_length,
                                                tokenizer, predict_file)

        tf.logging.info("***** Running prediction*****")
        tf.logging.info("  Num examples = %d (%d actual, %d padding)",
                        len(predict_examples), num_actual_predict_examples,
                        len(predict_examples) - num_actual_predict_examples)
        tf.logging.info("  Batch size = %d", FLAGS.muti_predict_batch_size)

        predict_drop_remainder = True if FLAGS.muti_use_tpu else False
        predict_input_fn = file_based_input_fn_builder(
            input_file=predict_file,
            seq_length=FLAGS.muti_max_seq_length,
            is_training=False,
            drop_remainder=predict_drop_remainder)

        result = estimator.predict(input_fn=predict_input_fn)

        output_predict_file = os.path.join(FLAGS.muti_output_dir,
                                           "test_results.tsv")
        with tf.gfile.GFile(output_predict_file, "w") as writer:
            num_written_lines = 0
            tf.logging.info("***** Predict results *****")
            for (i, prediction) in enumerate(result):
                probabilities = prediction["probabilities"]
                if i >= num_actual_predict_examples:
                    break
                output_line = "\t".join(
                    str(class_probability)
                    for class_probability in probabilities) + "\n"
                writer.write(output_line)
                num_written_lines += 1
        assert num_written_lines == num_actual_predict_examples
Esempio n. 4
0
        saver2 = tf.train.Saver()
        saver2.restore(sess2,
                       tf.train.latest_checkpoint("muti_model_bert3_4_0107/"))

        print('Successfully load the model_2!')

from bot_img.run_classifier_bi_attr import InputExample, convert_examples_to_features

bi_label_list = ['0', '1']
bi_num_labels = len(bi_label_list)

muti_label_list = [str(i) for i in range(40)]
muti_num_labels = len(muti_label_list)

tokenizer = tokenization.FullTokenizer(
    vocab_file="../bert_3/chinese_L-3_H-576_A-3/vocab_4380.txt",
    do_lower_case=True)


def bi_predict(sent):
    prediction = None
    result = {}
    if not sent:
        return 0
    examples = [InputExample(0, sent, label='0')]
    features = convert_examples_to_features(examples, bi_label_list, 128,
                                            tokenizer)
    input_ids = [feature.input_ids for feature in features]
    input_mask = [feature.input_mask for feature in features]
    segment_ids = [feature.segment_ids for feature in features]
    label_ids = [feature.label_id for feature in features]