예제 #1
0
def simple_fixed_prediction_estimator_extra_fields(export_path,
                                                   eval_export_path):
    """Exports a simple fixed prediction estimator that parses extra fields."""
    def model_fn(features, labels, mode, params):
        """Model function for custom estimator."""
        del params
        predictions = features['prediction']
        predictions_dict = {
            prediction_keys.PredictionKeys.PREDICTIONS: predictions,
        }

        if mode == tf.estimator.ModeKeys.PREDICT:
            return tf.estimator.EstimatorSpec(
                mode=mode,
                predictions=predictions_dict,
                export_outputs={
                    tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                    tf.estimator.export.RegressionOutput(predictions)
                })

        loss = tf.losses.mean_squared_error(predictions, labels)
        train_op = tf.assign_add(tf.train.get_global_step(), 1)
        eval_metric_ops = {
            metric_keys.MetricKeys.LOSS_MEAN: tf.metrics.mean(loss),
        }

        return tf.estimator.EstimatorSpec(mode=mode,
                                          loss=loss,
                                          train_op=train_op,
                                          predictions=predictions_dict,
                                          eval_metric_ops=eval_metric_ops)

    def train_input_fn():
        """Train input function."""
        return {
            'prediction': tf.constant([[1.0], [2.0], [3.0], [4.0]]),
        }, tf.constant([[1.0], [2.0], [3.0], [4.0]]),

    feature_spec = {'prediction': tf.FixedLenFeature([1], dtype=tf.float32)}
    eval_feature_spec = {
        'prediction': tf.FixedLenFeature([1], dtype=tf.float32),
        'label': tf.FixedLenFeature([1], dtype=tf.float32),
        'fixed_float': tf.FixedLenFeature([1], dtype=tf.float32),
        'fixed_string': tf.FixedLenFeature([1], dtype=tf.string),
        'fixed_int': tf.FixedLenFeature([1], dtype=tf.int64),
        'var_float': tf.VarLenFeature(dtype=tf.float32),
        'var_string': tf.VarLenFeature(dtype=tf.string),
        'var_int': tf.VarLenFeature(dtype=tf.int64),
    }

    estimator = tf.estimator.Estimator(model_fn=model_fn)
    estimator.train(input_fn=train_input_fn, steps=1)

    return util.export_model_and_eval_model(
        estimator=estimator,
        serving_input_receiver_fn=(
            tf.estimator.export.build_parsing_serving_input_receiver_fn(
                feature_spec)),
        eval_input_receiver_fn=export.build_parsing_eval_input_receiver_fn(
            eval_feature_spec, label_key='label'),
        export_path=export_path,
        eval_export_path=eval_export_path)
def simple_fixed_prediction_estimator_no_labels(
        export_path,
        eval_export_path,
        output_prediction_key=prediction_keys.PredictionKeys.PREDICTIONS):
    """Exports a simple fixed prediction estimator with no labels."""
    def model_fn(features, mode, params):
        """Model function for custom estimator."""
        del params
        predictions = features['prediction']

        if output_prediction_key is not None:
            predictions_dict = {
                output_prediction_key: predictions,
            }
        else:
            # For simulating Estimators which don't return a predictions dict in
            # EVAL mode.
            predictions_dict = {}

        if mode == tf.estimator.ModeKeys.PREDICT:
            return tf.estimator.EstimatorSpec(
                mode=mode,
                predictions=predictions_dict,
                export_outputs={
                    tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                    tf.estimator.export.RegressionOutput(predictions)
                })

        # We use create a nonsensical loss that is easy to compute:
        # loss = mean(predictions^2) and export it as as the average loss for
        # testing that the metrics are computed correctly.
        loss = tf.compat.v1.losses.mean_squared_error(
            predictions, tf.zeros_like(predictions))
        train_op = tf.compat.v1.assign_add(
            tf.compat.v1.train.get_global_step(), 1)
        eval_metric_ops = {
            metric_keys.MetricKeys.LOSS_MEAN: tf.compat.v1.metrics.mean(loss),
        }

        return tf.estimator.EstimatorSpec(mode=mode,
                                          loss=loss,
                                          train_op=train_op,
                                          predictions=predictions_dict,
                                          eval_metric_ops=eval_metric_ops)

    def train_input_fn():
        """Train input function."""
        return {
            'prediction': tf.constant([[1.0], [2.0], [3.0], [4.0]]),
        }

    estimator = tf.estimator.Estimator(model_fn=model_fn)
    estimator.train(input_fn=train_input_fn, steps=1)

    feature_spec = {'prediction': tf.io.FixedLenFeature([1], dtype=tf.float32)}
    eval_feature_spec = {
        'prediction': tf.io.FixedLenFeature([1], dtype=tf.float32),
    }

    return util.export_model_and_eval_model(
        estimator=estimator,
        serving_input_receiver_fn=(
            tf.estimator.export.build_parsing_serving_input_receiver_fn(
                feature_spec)),
        eval_input_receiver_fn=export.build_parsing_eval_input_receiver_fn(
            eval_feature_spec, label_key=None),
        export_path=export_path,
        eval_export_path=eval_export_path)
예제 #3
0
def get_simple_fixed_prediction_estimator_and_metadata(
    output_prediction_key=prediction_keys.PredictionKeys.PREDICTIONS):
  """Returns a simple fixed prediction estimator and metadata.

  Exposed for use with ExporterTest.

  Args:
    output_prediction_key: Output prediction key.

  Returns:
    Dictionary containing estimator, eval_input_receiver_fn,
    serving_input_receiver_fn, train_input_fn and model_fn.
  """

  def model_fn(features, labels, mode, params):
    """Model function for custom estimator."""
    del params
    predictions = features['prediction']

    if output_prediction_key is not None:
      predictions_dict = {
          output_prediction_key: predictions,
      }
    else:
      # For simulating Estimators which don't return a predictions dict in
      # EVAL mode.
      predictions_dict = {}

    if mode == tf.estimator.ModeKeys.PREDICT:
      return tf.estimator.EstimatorSpec(
          mode=mode,
          predictions=predictions_dict,
          export_outputs={
              tf.saved_model.signature_constants
              .DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                  tf.estimator.export.RegressionOutput(predictions)
          })

    loss = tf.losses.mean_squared_error(predictions, labels)
    train_op = tf.assign_add(tf.train.get_global_step(), 1)
    eval_metric_ops = {
        metric_keys.MetricKeys.LOSS_MEAN: tf.metrics.mean(loss),
    }

    return tf.estimator.EstimatorSpec(
        mode=mode,
        loss=loss,
        train_op=train_op,
        predictions=predictions_dict,
        eval_metric_ops=eval_metric_ops)

  def train_input_fn():
    """Train input function."""
    return {
        'prediction': tf.constant([[1.0], [2.0], [3.0], [4.0]]),
    }, tf.constant([[1.0], [2.0], [3.0], [4.0]]),

  estimator = tf.estimator.Estimator(model_fn=model_fn)

  feature_spec = {'prediction': tf.FixedLenFeature([1], dtype=tf.float32)}
  eval_feature_spec = {
      'prediction': tf.FixedLenFeature([1], dtype=tf.float32),
      'label': tf.FixedLenFeature([1], dtype=tf.float32),
  }

  return {
      'estimator':
          estimator,
      'serving_input_receiver_fn': (
          tf.estimator.export.build_parsing_serving_input_receiver_fn(
              feature_spec)),
      'eval_input_receiver_fn':
          export.build_parsing_eval_input_receiver_fn(
              eval_feature_spec, label_key='label'),
      'train_input_fn':
          train_input_fn,
      'model_fn':
          model_fn,
  }
예제 #4
0
def simple_fixed_prediction_classifier_extra_fields(export_path,
                                                    eval_export_path):
    """Exports a simple fixed prediction classifier that parses extra fields."""
    def model_fn(features, labels, mode, params):
        """Model function for custom estimator."""
        del labels
        del params
        classes = tf.sparse_tensor_to_dense(features['classes'],
                                            default_value='?')
        scores = tf.sparse_tensor_to_dense(features['scores'],
                                           default_value=0.0)

        predictions = {
            prediction_keys.PredictionKeys.LOGITS: scores,
            prediction_keys.PredictionKeys.PROBABILITIES: scores,
            prediction_keys.PredictionKeys.CLASSES: classes,
        }

        if mode == tf.estimator.ModeKeys.PREDICT:
            return tf.estimator.EstimatorSpec(
                mode=mode,
                predictions=predictions,
                export_outputs={
                    tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                    tf.estimator.export.ClassificationOutput(scores=scores,
                                                             classes=classes),
                })

        # Note that this is always going to be 0.
        loss = tf.losses.mean_squared_error(scores, tf.ones_like(scores))
        train_op = tf.assign_add(tf.train.get_global_step(), 1)
        eval_metric_ops = {
            metric_keys.MetricKeys.LOSS_MEAN: tf.metrics.mean(loss),
        }

        return tf.estimator.EstimatorSpec(mode=mode,
                                          loss=loss,
                                          train_op=train_op,
                                          predictions=predictions,
                                          eval_metric_ops=eval_metric_ops)

    def train_input_fn():
        """Train input function."""
        classes = tf.SparseTensor(values=[
            'first1',
            'first2',
            'second1',
            'third1',
            'third2',
            'third3',
            'fourth1',
        ],
                                  indices=[[0, 0], [0, 1], [1, 0], [2, 0],
                                           [2, 1], [2, 2], [3, 0]],
                                  dense_shape=[4, 3])
        scores = tf.SparseTensor(values=[0.9, 0.9, 0.9, 0.9, 0.8, 0.7, 0.9],
                                 indices=[[0, 0], [0, 1], [1, 0], [2, 0],
                                          [2, 1], [2, 2], [3, 0]],
                                 dense_shape=[4, 3])
        return {
            'classes': classes,
            'scores': scores,
        }, classes

    estimator = tf.estimator.Estimator(model_fn=model_fn)
    estimator.train(input_fn=train_input_fn, steps=1)

    serving_input_receiver_fn = (
        tf.estimator.export.build_parsing_serving_input_receiver_fn(
            feature_spec={
                'classes': tf.VarLenFeature(dtype=tf.string),
                'scores': tf.VarLenFeature(dtype=tf.float32)
            }))
    eval_input_receiver_fn = export.build_parsing_eval_input_receiver_fn(
        feature_spec={
            'classes': tf.VarLenFeature(dtype=tf.string),
            'scores': tf.VarLenFeature(dtype=tf.float32),
            'labels': tf.VarLenFeature(dtype=tf.string),
            'fixed_float': tf.FixedLenFeature([1], dtype=tf.float32),
            'fixed_string': tf.FixedLenFeature([1], dtype=tf.string),
            'fixed_int': tf.FixedLenFeature([1], dtype=tf.int64),
            'var_float': tf.VarLenFeature(dtype=tf.float32),
            'var_string': tf.VarLenFeature(dtype=tf.string),
            'var_int': tf.VarLenFeature(dtype=tf.int64),
        },
        label_key='labels')

    return util.export_model_and_eval_model(
        estimator=estimator,
        serving_input_receiver_fn=serving_input_receiver_fn,
        eval_input_receiver_fn=eval_input_receiver_fn,
        export_path=export_path,
        eval_export_path=eval_export_path)