(num_labels, learning_rate, num_train_steps,
                     num_warmup_steps, use_tpu, bert_hub_module_handle)
return model_fn
Returns `model_fn` closure for TPUEstimator.
Returns `model_fn` closure for TPUEstimator.
Returns model_fn closure for TPUEstimator .
def model_fn_builder(num_labels, learning_rate, num_train_steps,
                     num_warmup_steps, use_tpu, bert_hub_module_handle):
  """Returns `model_fn` closure for TPUEstimator."""

  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_training = (mode == tf.estimator.ModeKeys.TRAIN)

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

    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)
    elif mode == tf.estimator.ModeKeys.EVAL:

      def metric_fn(per_example_loss, label_ids, logits):
        predictions = tf.argmax(logits, axis=-1, output_type=tf.int32)
        accuracy = tf.metrics.accuracy(label_ids, predictions)
        loss = tf.metrics.mean(per_example_loss)
        return {
            "eval_accuracy": accuracy,
            "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)
    elif mode == tf.estimator.ModeKeys.PREDICT:
      output_spec = tf.contrib.tpu.TPUEstimatorSpec(
          mode=mode, predictions={"probabilities": probabilities})
    else:
      raise ValueError(
          "Only TRAIN, EVAL and PREDICT modes are supported: %s" % (mode))

    return output_spec

  return model_fn
def model_fn_builder ( num_labels , learning_rate , num_train_steps , num_warmup_steps , use_tpu , bert_hub_module_handle ) : 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_training = ( mode == tf . estimator . ModeKeys . TRAIN ) ( total_loss , per_example_loss , logits , probabilities ) = create_model ( is_training , input_ids , input_mask , segment_ids , label_ids , num_labels , bert_hub_module_handle ) 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 ) elif mode == tf . estimator . ModeKeys . EVAL : def metric_fn ( per_example_loss , label_ids , logits ) : predictions = tf . argmax ( logits , axis = - 1 , output_type = tf . int32 ) accuracy = tf . metrics . accuracy ( label_ids , predictions ) loss = tf . metrics . mean ( per_example_loss ) return { "eval_accuracy" : accuracy , "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 ) elif mode == tf . estimator . ModeKeys . PREDICT : output_spec = tf . contrib . tpu . TPUEstimatorSpec ( mode = mode , predictions = { "probabilities" : probabilities } ) else : raise ValueError ( "Only TRAIN, EVAL and PREDICT modes are supported: %s" % ( mode ) ) return output_spec return model_fn