def model_fn(features, labels, mode, params):  # pylint: disable=unused-argument
    """The `model_fn` for TPUEstimator."""

    tf.logging.info("*** Features ***")
    for name in sorted(features.keys()):
      tf.logging.info("  name = %s, shape = %s" % (name, features[name].shape))

    input_ids = features["input_ids"]
    input_mask = features["input_mask"]
    segment_ids = features["segment_ids"]
    label_ids = features["label_ids"]
    # is_real_example = None
    # if "is_real_example" in features:
    #   is_real_example = tf.cast(features["is_real_example"], dtype=tf.float32)
    # else:
    #   is_real_example = tf.ones(tf.shape(label_ids), dtype=tf.float32)

    is_training = (mode == tf.estimator.ModeKeys.TRAIN)

    (total_loss, per_example_loss, logits, predicts) = create_model(
        bert_config, is_training, input_ids, input_mask, segment_ids, label_ids,
        num_labels, use_one_hot_embeddings)

    tvars = tf.trainable_variables()
    initialized_variable_names = {}
    scaffold_fn = None
    if init_checkpoint:
      (assignment_map, initialized_variable_names
      ) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint)
      if use_tpu:

        def tpu_scaffold():
          tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
          return tf.train.Scaffold()

        scaffold_fn = tpu_scaffold
      else:
        tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

    tf.logging.info("**** Trainable Variables ****")
    for var in tvars:
      init_string = ""
      if var.name in initialized_variable_names:
        init_string = ", *INIT_FROM_CKPT*"
      tf.logging.info("  name = %s, shape = %s%s", var.name, var.shape,
                      init_string)

    output_spec = None
    if mode == tf.estimator.ModeKeys.TRAIN:

      train_op = optimization.create_optimizer(
          total_loss, learning_rate, num_train_steps, num_warmup_steps, use_tpu)

      output_spec = tf.contrib.tpu.TPUEstimatorSpec(
          mode=mode,
          loss=total_loss,
          train_op=train_op,
          scaffold_fn=scaffold_fn)
    elif mode == tf.estimator.ModeKeys.EVAL:

      def metric_fn(per_example_loss, label_ids, logits):
        # def metric_fn(label_ids, logits):
        predictions = tf.argmax(logits, axis=-1, output_type=tf.int32)
        precision = tf_metrics.precision(label_ids,predictions,output_num,[2,3,4,5,6,7],average="macro")
        recall = tf_metrics.recall(label_ids,predictions,output_num,[2,3,4,5,6,7],average="macro")
        f = tf_metrics.f1(label_ids,predictions,output_num,[2,3,4,5,6,7],average="macro")
        #
        return {
          "eval_precision":precision,
          "eval_recall":recall,
          "eval_f": f,
          #"eval_loss": loss,
        }
      eval_metrics = (metric_fn, [per_example_loss, label_ids, logits])
      output_spec = tf.contrib.tpu.TPUEstimatorSpec(
          mode=mode,
          loss=total_loss,
          eval_metrics=eval_metrics,
          scaffold_fn=scaffold_fn)
    else:
      output_spec = tf.contrib.tpu.TPUEstimatorSpec(
          mode=mode,
          predictions= predicts,
          scaffold_fn=scaffold_fn)
    return output_spec
Example #2
0
    def model_fn(features, labels, mode, params):  # pylint: disable=unused-argument
        """The `model_fn` for TPUEstimator."""

        tf.logging.info("*** Features ***")
        for name in sorted(features.keys()):
            tf.logging.info("  name = %s, shape = %s" %
                            (name, features[name].shape))

        input_ids = features["input_ids"]
        input_mask = features["input_mask"]
        segment_ids = features["segment_ids"]
        input_value = features["input_value"]
        is_real_example = None
        if "is_real_example" in features:
            is_real_example = tf.cast(features["is_real_example"],
                                      dtype=tf.float32)
        else:
            is_real_example = tf.ones(tf.shape(input_value), dtype=tf.float32)

        is_training = (mode == tf.estimator.ModeKeys.TRAIN)

        (total_loss, logits,
         probabilities) = create_model(bert_config, is_training, input_ids,
                                       input_mask, segment_ids, input_value,
                                       use_one_hot_embeddings)

        tvars = tf.trainable_variables()
        initialized_variable_names = {}
        scaffold_fn = None
        if init_checkpoint:
            (assignment_map, initialized_variable_names
             ) = modeling.get_assignment_map_from_checkpoint(
                 tvars, init_checkpoint)
            if use_tpu:

                def tpu_scaffold():
                    tf.train.init_from_checkpoint(init_checkpoint,
                                                  assignment_map)
                    return tf.train.Scaffold()

                scaffold_fn = tpu_scaffold
            else:
                tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

        tf.logging.info("**** Trainable Variables ****")
        for var in tvars:
            init_string = ""
            if var.name in initialized_variable_names:
                init_string = ", *INIT_FROM_CKPT*"
            tf.logging.info("  name = %s, shape = %s%s", var.name, var.shape,
                            init_string)

        output_spec = None
        if mode == tf.estimator.ModeKeys.TRAIN:

            train_op = optimization.create_optimizer(total_loss, learning_rate,
                                                     num_train_steps,
                                                     num_warmup_steps, use_tpu)

            output_spec = tf.contrib.tpu.TPUEstimatorSpec(
                mode=mode,
                loss=total_loss,
                train_op=train_op,
                scaffold_fn=scaffold_fn)
        elif mode == tf.estimator.ModeKeys.EVAL:

            #(total_loss, logits, probabilities)
            def metric_fn(input_value, logits, is_real_example):
                # predictions = tf.argmax(logits, axis=-1, output_type=tf.int32)
                input_value = tf.cast(input_value, dtype=tf.float32)
                tf.logging.info(
                    "**********************************************************"
                )
                tf.logging.info(input_value)
                tf.logging.info(
                    "**********************************************************"
                )
                predictions = tf.nn.tanh(logits)
                tf.logging.info(
                    "**********************************************************"
                )
                tf.logging.info(predictions)
                tf.logging.info(
                    "**********************************************************"
                )
                # accuracy = tf.metrics.accuracy(labels=label_ids, predictions=predictions, weights=is_real_example)
                mse = tf.metrics.mean_squared_error(labels=input_value,
                                                    predictions=predictions,
                                                    weights=is_real_example)

                return {
                    "eval_mse": mse,
                }

            eval_metrics = (metric_fn, [input_value, logits, is_real_example])
            output_spec = tf.contrib.tpu.TPUEstimatorSpec(
                mode=mode,
                loss=total_loss,
                eval_metrics=eval_metrics,
                scaffold_fn=scaffold_fn)
        else:
            output_spec = tf.contrib.tpu.TPUEstimatorSpec(
                mode=mode,
                predictions={"probabilities": probabilities},
                scaffold_fn=scaffold_fn)
        return output_spec
    def model_fn(features, labels, mode, params):  # pylint: disable=unused-argument
        """The `model_fn` for TPUEstimator."""

        tf.logging.info("*** Features ***")
        for name in sorted(features.keys()):
            tf.logging.info("  name = %s, shape = %s" % (name, features[name].shape))

        unique_ids = features["unique_ids"]
        input_ids = features["input_ids"]
        input_mask = features["input_mask"]
        segment_ids = features["segment_ids"]
        input_span_mask = features["input_span_mask"]

        is_training = (mode == tf.estimator.ModeKeys.TRAIN)

        (start_logits, end_logits) = create_model(
            bert_config=bert_config,
            is_training=is_training,
            input_ids=input_ids,
            input_mask=input_mask,
            segment_ids=segment_ids,
            input_span_mask=input_span_mask,
            use_one_hot_embeddings=use_one_hot_embeddings)

        tvars = tf.trainable_variables()

        initialized_variable_names = {}
        scaffold_fn = None
        if init_checkpoint:
            (assignment_map, initialized_variable_names
             ) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint)
            if use_tpu:

                def tpu_scaffold():
                    tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
                    return tf.train.Scaffold()

                scaffold_fn = tpu_scaffold
            else:
                tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

        tf.logging.info("**** Trainable Variables ****")
        for var in tvars:
            init_string = ""
            if var.name in initialized_variable_names:
                init_string = ", *INIT_FROM_CKPT*"
            tf.logging.info("  name = %s, shape = %s%s", var.name, var.shape, init_string)

        output_spec = None
        if mode == tf.estimator.ModeKeys.TRAIN:
            seq_length = modeling.get_shape_list(input_ids)[1]

            def compute_loss(logits, positions):
                on_hot_pos = tf.one_hot(positions, depth=seq_length, dtype=tf.float32)
                log_probs = tf.nn.log_softmax(logits, axis=-1)
                loss = -tf.reduce_mean(tf.reduce_sum(on_hot_pos * log_probs, axis=-1))
                return loss

            start_positions = features["start_positions"]
            end_positions = features["end_positions"]

            start_loss = compute_loss(start_logits, start_positions)
            end_loss = compute_loss(end_logits, end_positions)
            total_loss = (start_loss + end_loss) / 2

            train_op = optimization.create_optimizer(
                total_loss, learning_rate, num_train_steps, num_warmup_steps, use_tpu)

            output_spec = tf.contrib.tpu.TPUEstimatorSpec(
                mode=mode,
                loss=total_loss,
                train_op=train_op,
                scaffold_fn=scaffold_fn)
        elif mode == tf.estimator.ModeKeys.PREDICT:
            start_logits = tf.nn.log_softmax(start_logits, axis=-1)
            end_logits = tf.nn.log_softmax(end_logits, axis=-1)

            predictions = {
                "unique_ids": unique_ids,
                "start_logits": start_logits,
                "end_logits": end_logits,
            }
            output_spec = tf.contrib.tpu.TPUEstimatorSpec(
                mode=mode, predictions=predictions, scaffold_fn=scaffold_fn)
        else:
            raise ValueError("Only TRAIN and PREDICT modes are supported: %s" % (mode))

        return output_spec
    def model_fn(features, labels, mode, params):  # pylint: disable=unused-argument
        """The `model_fn` for TPUEstimator."""

        # tf.logging.info("*** Features ***")
        # for name in sorted(features.keys()):
        #   tf.logging.info("  name = %s, shape = %s" % (name, features[name].shape))

        input_ids = features["input_ids"]
        input_mask = features["input_mask"]
        segment_ids = features["segment_ids"]
        label_ids = features["label_ids"]
        # is_real_example = None
        # if "is_real_example" in features:
        #   is_real_example = tf.cast(features["is_real_example"], dtype=tf.float32)
        # else:
        #   is_real_example = tf.ones(tf.shape(label_ids), dtype=tf.float32)

        is_training = (mode == tf.estimator.ModeKeys.TRAIN)

        (total_loss, per_example_loss, logits,
         predicts) = create_model(bert_config, is_training, input_ids,
                                  input_mask, segment_ids, label_ids,
                                  num_labels, use_one_hot_embeddings)

        tvars = tf.trainable_variables()
        initialized_variable_names = {}
        if init_checkpoint:
            (assignment_map, initialized_variable_names) = \
                modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint)

            tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

        tf.logging.info("**** Trainable Variables ****")
        for var in tvars:
            init_string = ""
            if var.name in initialized_variable_names:
                init_string = ", *INIT_FROM_CKPT*"
            tf.logging.info("  name = %s, shape = %s%s", var.name, var.shape,
                            init_string)

        output_spec = None
        if mode == tf.estimator.ModeKeys.TRAIN:
            if params['data_type'] == 'sup':
                train_op = optimization.create_optimizer(total_loss,
                                                         learning_rate,
                                                         num_train_steps,
                                                         num_warmup_steps,
                                                         use_tpu=False)

                train_hook_list = []
                train_tensors_log = {
                    'loss': total_loss,
                    'global_step': tf.train.get_global_step()
                }
                train_hook_list.append(
                    tf.train.LoggingTensorHook(
                        tensors=train_tensors_log,
                        every_n_iter=FLAGS.iterations_per_hook_output))
                output_spec = tf.estimator.EstimatorSpec(
                    mode=mode,
                    loss=total_loss,
                    train_op=train_op,
                    training_hooks=train_hook_list)
            elif params['data_type'] == 'unsup':
                train_op = optimization.create_optimizer(total_loss,
                                                         learning_rate,
                                                         num_train_steps,
                                                         num_warmup_steps,
                                                         use_tpu=False)

                train_hook_list = []
                train_tensors_log = {
                    'loss': total_loss,
                    'global_step': tf.train.get_global_step()
                }
                train_hook_list.append(
                    tf.train.LoggingTensorHook(
                        tensors=train_tensors_log,
                        every_n_iter=FLAGS.iterations_per_hook_output))
                output_spec = tf.estimator.EstimatorSpec(
                    mode=mode,
                    loss=total_loss,
                    train_op=train_op,
                    training_hooks=train_hook_list)

        elif mode == tf.estimator.ModeKeys.EVAL:

            def metric_fn(per_example_loss, label_ids, logits):
                # def metric_fn(label_ids, logits):
                predictions = tf.argmax(logits, axis=-1, output_type=tf.int32)
                precision = tf_metrics.precision(label_ids,
                                                 predictions,
                                                 11, [2, 3, 4, 5, 6, 7],
                                                 average="macro")
                recall = tf_metrics.recall(label_ids,
                                           predictions,
                                           11, [2, 3, 4, 5, 6, 7],
                                           average="macro")
                f = tf_metrics.f1(label_ids,
                                  predictions,
                                  11, [2, 3, 4, 5, 6, 7],
                                  average="macro")
                #
                return {
                    "eval_precision": precision,
                    "eval_recall": recall,
                    "eval_f": f,
                    #"eval_loss": loss,
                }

            eval_metric_ops = metric_fn(per_example_loss, label_ids, logits)
            output_spec = tf.estimator.EstimatorSpec(
                mode=mode, loss=total_loss, eval_metric_ops=eval_metric_ops)
        else:
            output_spec = tf.estimator.EstimatorSpec(
                mode=mode,
                predictions={
                    'predicts': predicts,
                    'logits': logits,
                    'labels': label_ids
                })  #}predictions can be a tensor or a dict of tensors
        return output_spec
Example #5
0
    def model_fn(features, labels, mode, params):  # pylint: disable=unused-argument
        """The `model_fn` for TPUEstimator."""

        tf.logging.info("*** Features ***")
        for name in sorted(features.keys()):
            tf.logging.info("  name = %s, shape = %s" %
                            (name, features[name].shape))

        input_ids = features["input_ids"]
        input_mask = features["input_mask"]
        segment_ids = features["segment_ids"]
        label_ids = features["label_ids"]
        is_real_example = None
        if "is_real_example" in features:
            is_real_example = tf.cast(features["is_real_example"],
                                      dtype=tf.float32)
        else:
            is_real_example = tf.ones(tf.shape(label_ids), dtype=tf.float32)

        is_training = (mode == tf.estimator.ModeKeys.TRAIN)

        (total_loss, per_example_loss, logits,
         probabilities) = create_model(bert_config, is_training, input_ids,
                                       input_mask, segment_ids, label_ids,
                                       num_labels, use_one_hot_embeddings)

        tvars = tf.trainable_variables()
        initialized_variable_names = {}
        scaffold_fn = None
        if init_checkpoint:
            (assignment_map, initialized_variable_names
             ) = modeling.get_assignment_map_from_checkpoint(
                 tvars, init_checkpoint)
            if use_tpu:

                def tpu_scaffold():
                    tf.train.init_from_checkpoint(init_checkpoint,
                                                  assignment_map)
                    return tf.train.Scaffold()

                scaffold_fn = tpu_scaffold
            else:
                tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

        tf.logging.info("**** Trainable Variables ****")
        for var in tvars:
            init_string = ""
            if var.name in initialized_variable_names:
                init_string = ", *INIT_FROM_CKPT*"
            tf.logging.info("  name = %s, shape = %s%s", var.name, var.shape,
                            init_string)

        output_spec = None
        if mode == tf.estimator.ModeKeys.TRAIN:

            train_op = optimization.create_optimizer(total_loss, learning_rate,
                                                     num_train_steps,
                                                     num_warmup_steps, use_tpu)

            output_spec = tf.contrib.tpu.TPUEstimatorSpec(
                mode=mode,
                loss=total_loss,
                train_op=train_op,
                scaffold_fn=scaffold_fn)
        elif mode == tf.estimator.ModeKeys.EVAL:

            def metric_fn(per_example_loss, label_ids, logits,
                          is_real_example):
                predictions = tf.argmax(logits, axis=-1, output_type=tf.int32)
                accuracy = tf.metrics.accuracy(labels=label_ids,
                                               predictions=predictions,
                                               weights=is_real_example)
                loss = tf.metrics.mean(values=per_example_loss,
                                       weights=is_real_example)

                auc = tf.metrics.auc(labels=label_ids,
                                     predictions=predictions,
                                     weights=is_real_example)
                precision = tf.metrics.precision(
                    labels=label_ids,
                    predictions=predictions,
                    weights=is_real_example)  # 精准率
                recall = tf.metrics.recall(labels=label_ids,
                                           predictions=predictions,
                                           weights=is_real_example)  # 召回率

                # precision, precision_update_op = tf.metrics.precision(labels=labels,
                #                                                       predictions=predictions,
                #                                                       name='precision')

                # recall, recall_update_op = tf.metrics.recall(labels=labels,
                #                                              predictions=predictions,
                #                                              name='recall')

                # f1_score, f1_update_op = tf.metrics.mean((2*precision*recall)/(precision+recall), name='f1_score')  # f1值

                print("accuracy: {} loss: {}".format(accuracy, loss))
                return {
                    "eval_accuracy": accuracy,
                    "eval_loss": loss,
                    "auc": auc,
                    "precision": precision,
                    "recall": recall,
                    # "f1_score":f1_score
                }

            eval_metrics = (metric_fn, [
                per_example_loss, label_ids, logits, is_real_example
            ])
            output_spec = tf.contrib.tpu.TPUEstimatorSpec(
                mode=mode,
                loss=total_loss,
                eval_metrics=eval_metrics,
                scaffold_fn=scaffold_fn)
        else:
            output_spec = tf.contrib.tpu.TPUEstimatorSpec(
                mode=mode,
                predictions={"probabilities": probabilities},
                scaffold_fn=scaffold_fn)
        return output_spec
Example #6
0
    def model_fn(features, labels, mode, params):
        '''
        tf.logging.info("*** Features ***")
        for name in sorted(features.keys()):
            tf.logging.info("   name = %s, shape = %s", name, features[name].shape)
        '''
        input_ids = features["input_ids"]
        input_mask = features["input_mask"]
        segment_ids = features["segment_ids"]
        labele_ids = features["labele_ids"]
        labelr_id = features["labelr_id"]

        is_training = (mode == tf.estimator.ModeKeys.TRAIN)

        total_loss, task1_loss, task2_loss, task1_predict, task2_predict, task1_probabilities, task2_probabilities, score = create_model(
            bert_config, is_training, input_ids, input_mask, segment_ids,
            labele_ids, labelr_id, num_labeles, num_labelrs,
            use_one_hot_embeddings)

        ### only task2:
        # total_loss = task1_loss

        tvars = tf.trainable_variables()
        initialized_variable_names = {}
        if init_checkpoint:
            assignment_map, initialized_variable_names = modeling.get_assignment_map_from_checkpoint(
                tvars, init_checkpoint)
            tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

        output_spec = None
        if mode == tf.estimator.ModeKeys.TRAIN:
            train_op = optimization.create_optimizer(total_loss, learning_rate,
                                                     num_train_steps,
                                                     num_warmup_steps, False)

            output_spec = tf.estimator.EstimatorSpec(mode=mode,
                                                     loss=total_loss,
                                                     train_op=train_op)

        elif mode == tf.estimator.ModeKeys.EVAL:

            def metric_fn(labele_ids, task1_predict, num_labeles, labelr_id,
                          task2_predict):
                task1_precision = tf_metrics.precision(
                    labele_ids,
                    task1_predict,
                    num_labeles, [i + 1 for i in range(num_labeles - 4)],
                    average="macro")
                task1_recall = tf_metrics.recall(
                    labele_ids,
                    task1_predict,
                    num_labeles, [i + 1 for i in range(num_labeles - 4)],
                    average="macro")
                task1_f1 = tf_metrics.f1(
                    labele_ids,
                    task1_predict,
                    num_labeles, [i + 1 for i in range(num_labeles - 4)],
                    average="macro")
                task1_accuracy = tf_metrics.sentence_accuracy(
                    labels=labele_ids, predictions=task1_predict)
                task2_accuracy = tf.metrics.accuracy(labels=labelr_id,
                                                     predictions=task2_predict)
                return {
                    "task1_precision": task1_precision,
                    "task1_recall": task1_recall,
                    "task1_f1": task1_f1,
                    "task1_accuracy": task1_accuracy,
                    "task2_accuracy": task2_accuracy,
                }

            eval_metrics = metric_fn(labele_ids, task1_predict, num_labeles,
                                     labelr_id, task2_predict)
            output_spec = tf.estimator.EstimatorSpec(
                mode=mode, loss=total_loss, eval_metric_ops=eval_metrics)

        else:
            output_spec = tf.estimator.EstimatorSpec(mode=mode,
                                                     predictions={
                                                         "predict_e":
                                                         task1_predict,
                                                         "probilities_e":
                                                         task1_probabilities,
                                                         "predict_r":
                                                         task2_predict,
                                                         "probilities_r":
                                                         task2_probabilities,
                                                         "score": score
                                                     })
        return output_spec