コード例 #1
0
def simple_linear_classifier(export_path, eval_export_path):
    """Trains and exports a simple linear classifier."""

    feature_spec = tf.feature_column.make_parse_example_spec(
        util.linear_columns(False))
    eval_feature_spec = tf.feature_column.make_parse_example_spec(
        util.linear_columns(True) + [
            tf.feature_column.categorical_column_with_hash_bucket(
                'slice_key', 100)
        ])

    classifier = tf.estimator.LinearClassifier(
        feature_columns=util.linear_columns(),
        loss_reduction=tf.compat.v1.losses.Reduction.SUM)
    classifier = tf.estimator.add_metrics(classifier,
                                          util.classifier_extra_metrics)
    classifier.train(input_fn=util.make_classifier_input_fn(
        tf.feature_column.make_parse_example_spec(util.linear_columns(True))),
                     steps=1000)

    return util.export_model_and_eval_model(
        estimator=classifier,
        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_classifier(export_path, eval_export_path):
  """Exports a simple fixed prediction classifier."""

  estimator = tf.estimator.Estimator(
      model_fn=fixed_prediction_classifier.model_fn)
  estimator.train(input_fn=fixed_prediction_classifier.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),
          'label': tf.FixedLenFeature([1], dtype=tf.int64),
          'language': tf.FixedLenFeature([1], dtype=tf.string),
          'age': tf.FixedLenFeature([1], dtype=tf.float32),
      },
      label_key='label')

  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)
コード例 #3
0
def simple_dnn_regressor(export_path, eval_export_path):
    """Trains and exports a simple DNN regressor."""

    feature_spec = tf.feature_column.make_parse_example_spec(
        feature_columns=util.dnn_columns(True))
    regressor = tf.estimator.DNNRegressor(
        hidden_units=[4],
        feature_columns=util.dnn_columns(False),
        loss_reduction=tf.losses.Reduction.SUM)
    regressor = tf.estimator.add_metrics(regressor,
                                         util.regressor_extra_metrics)
    regressor.train(input_fn=util.make_regressor_input_fn(feature_spec),
                    steps=3000)

    return util.export_model_and_eval_model(
        estimator=regressor,
        serving_input_receiver_fn=(
            tf.estimator.export.build_parsing_serving_input_receiver_fn(
                tf.feature_column.make_parse_example_spec(
                    util.dnn_columns(False)))),
        eval_input_receiver_fn=export.build_parsing_eval_input_receiver_fn(
            tf.feature_column.make_parse_example_spec(util.dnn_columns(True)),
            label_key='label'),
        export_path=export_path,
        eval_export_path=eval_export_path)
コード例 #4
0
def simple_batch_size_limited_classifier(export_path, eval_export_path):
    """Exports a simple fixed prediction classifier."""

    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.io.FixedLenFeature([], dtype=tf.string),
                'scores': tf.io.FixedLenFeature([], dtype=tf.float32)
            }))
    eval_input_receiver_fn = export.build_parsing_eval_input_receiver_fn(
        feature_spec={
            'classes': tf.io.FixedLenFeature([], dtype=tf.string),
            'scores': tf.io.FixedLenFeature([], dtype=tf.float32),
            'labels': tf.io.FixedLenFeature([], dtype=tf.string),
        },
        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)
コード例 #5
0
def simple_dnn_classifier(export_path, eval_export_path, n_classes=2):
    """Trains and exports a simple DNN classifier."""

    feature_spec = tf.feature_column.make_parse_example_spec(
        feature_columns=util.dnn_columns(True, n_classes=n_classes))
    classifier = tf.estimator.DNNClassifier(
        hidden_units=[4],
        feature_columns=util.dnn_columns(False),
        n_classes=n_classes)
    classifier = tf.contrib.estimator.add_metrics(
        classifier, util.classifier_extra_metrics)
    classifier.train(input_fn=util.make_classifier_input_fn(
        feature_spec, n_classes),
                     steps=1000)

    return util.export_model_and_eval_model(
        estimator=classifier,
        serving_input_receiver_fn=(
            tf.estimator.export.build_parsing_serving_input_receiver_fn(
                tf.feature_column.make_parse_example_spec(
                    util.dnn_columns(False)))),
        eval_input_receiver_fn=build_parsing_eval_input_receiver_fn(
            tf.feature_column.make_parse_example_spec(
                util.dnn_columns(True, n_classes=n_classes)),
            label_key='label'),
        export_path=export_path,
        eval_export_path=eval_export_path)
コード例 #6
0
def simple_linear_regressor(export_path, eval_export_path):
  """Trains and exports a simple linear regressor."""

  feature_spec = tf.feature_column.make_parse_example_spec(
      util.linear_columns(False))
  eval_feature_spec = tf.feature_column.make_parse_example_spec(
      util.linear_columns(True) +
      [tf.feature_column.categorical_column_with_hash_bucket('slice_key', 100)])

  regressor = tf.estimator.LinearRegressor(
      feature_columns=util.linear_columns())
  regressor = tf.contrib.estimator.add_metrics(regressor,
                                               util.regressor_extra_metrics)
  regressor.train(
      input_fn=util.make_regressor_input_fn(
          tf.feature_column.make_parse_example_spec(util.linear_columns(True))),
      steps=3000)

  return util.export_model_and_eval_model(
      estimator=regressor,
      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)
コード例 #7
0
def simple_csv_linear_classifier(export_path, eval_export_path):
    """Trains and exports a simple linear classifier."""
    def parse_csv(rows_string_tensor):
        """Takes the string input tensor and returns a dict of rank-2 tensors."""

        csv_columns = ['age', 'language', 'label']
        csv_column_defaults = [[0.0], ['unknown'], [0.0]]

        # Takes a rank-1 tensor and converts it into rank-2 tensor
        # Example if the data is ['csv,line,1', 'csv,line,2', ..] to
        # [['csv,line,1'], ['csv,line,2']] which after parsing will result in a
        # tuple of tensors: [['csv'], ['csv']], [['line'], ['line']], [[1], [2]]
        row_columns = tf.expand_dims(rows_string_tensor, -1)
        columns = tf.io.decode_csv(records=row_columns,
                                   record_defaults=csv_column_defaults)
        features = dict(zip(csv_columns, columns))
        return features

    def eval_input_receiver_fn():
        """Eval input receiver function."""
        csv_row = tf.compat.v1.placeholder(dtype=tf.string,
                                           shape=[None],
                                           name='input_csv_row')
        features = parse_csv(csv_row)
        receiver_tensors = {'examples': csv_row}

        return export.EvalInputReceiver(features=features,
                                        labels=features['label'],
                                        receiver_tensors=receiver_tensors)

    def input_fn():
        """Train input function."""
        return {
            'age':
            tf.constant([[1], [2], [3], [4]]),
            'language':
            tf.SparseTensor(
                values=['english', 'english', 'chinese', 'chinese'],
                indices=[[0, 0], [1, 0], [2, 0], [3, 0]],
                dense_shape=[4, 1])
        }, tf.constant([[1], [1], [0], [0]])

    language = tf.feature_column.categorical_column_with_vocabulary_list(
        'language', ['english', 'chinese'])
    age = tf.feature_column.numeric_column('age')
    all_features = [age, language]
    feature_spec = tf.feature_column.make_parse_example_spec(all_features)

    classifier = tf.estimator.LinearClassifier(
        feature_columns=all_features, loss_reduction=tf.losses.Reduction.SUM)
    classifier.train(input_fn=input_fn, steps=1000)

    return util.export_model_and_eval_model(
        estimator=classifier,
        serving_input_receiver_fn=(
            tf.estimator.export.build_parsing_serving_input_receiver_fn(
                feature_spec)),
        eval_input_receiver_fn=eval_input_receiver_fn,
        export_path=export_path,
        eval_export_path=eval_export_path)
コード例 #8
0
def simple_fixed_prediction_estimator(export_path, eval_export_path):
    """Exports a simple fixed prediction estimator."""
    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]]),

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

    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 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)
コード例 #9
0
def fake_multi_examples_per_input_estimator(export_path, eval_export_path):
    """Trains and exports a model that treats 1 input as 0 to n examples ."""
    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=_serving_input_receiver_fn,
        eval_input_receiver_fn=_eval_input_receiver_fn,
        export_path=export_path,
        eval_export_path=eval_export_path)
コード例 #10
0
def bad_multi_examples_per_input_estimator_misaligned_input_refs(
    export_path, eval_export_path):
  """Like the above (good) estimator, but the input_refs is misaligned."""
  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=_serving_input_receiver_fn,
      eval_input_receiver_fn=_bad_eval_input_receiver_fn_misaligned_input_refs,
      export_path=export_path,
      eval_export_path=eval_export_path)
コード例 #11
0
def bad_multi_examples_per_input_estimator_out_of_range_input_refs(
        export_path, eval_export_path):
    """Like the above (good) estimator, but the input_refs is out of range."""
    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=_serving_input_receiver_fn,
        eval_input_receiver_fn=(
            _bad_eval_input_receiver_fn_out_of_range_input_refs),
        export_path=export_path,
        eval_export_path=eval_export_path)
コード例 #12
0
def simple_fixed_prediction_estimator(
    export_path,
    eval_export_path,
    output_prediction_key=prediction_keys.PredictionKeys.PREDICTIONS):
  estimator_metadata = get_simple_fixed_prediction_estimator_and_metadata(
      output_prediction_key)
  estimator_metadata['estimator'].train(
      input_fn=estimator_metadata['train_input_fn'], steps=1)
  return util.export_model_and_eval_model(
      estimator=estimator_metadata['estimator'],
      serving_input_receiver_fn=estimator_metadata['serving_input_receiver_fn'],
      eval_input_receiver_fn=estimator_metadata['eval_input_receiver_fn'],
      export_path=export_path,
      eval_export_path=eval_export_path)
コード例 #13
0
def simple_dnn_classifier(export_path, eval_export_path, n_classes=2):
    """Trains and exports a simple DNN classifier."""

    estimator_metadata = get_simple_dnn_classifier_and_metadata(
        n_classes=n_classes)
    estimator_metadata['estimator'].train(
        input_fn=estimator_metadata['train_input_fn'], steps=1000)

    return util.export_model_and_eval_model(
        estimator=estimator_metadata['estimator'],
        serving_input_receiver_fn=estimator_metadata[
            'serving_input_receiver_fn'],
        eval_input_receiver_fn=estimator_metadata['eval_input_receiver_fn'],
        export_path=export_path,
        eval_export_path=eval_export_path)
コード例 #14
0
def simple_linear_classifier_multivalent(export_path, eval_export_path):
  """Trains and exports a simple linear classifier with multivalent features."""

  def input_fn():
    """Train input function."""
    return {
        'animals':
            tf.SparseTensor(
                values=[
                    'cat', 'dog', 'bird', 'cat', 'dog', 'cat', 'bird', 'dog',
                    'bird', 'cat', 'dog', 'bird'
                ],
                indices=[[0, 0], [1, 0], [2, 0], [4, 0], [4, 1], [5, 0], [5, 1],
                         [6, 0], [6, 1], [7, 0], [7, 1], [7, 2]],
                dense_shape=[8, 3])
    }, tf.constant([[0], [0], [0], [0], [1], [0], [0], [1]])

  animals = tf.feature_column.categorical_column_with_vocabulary_list(
      'animals', ['bird', 'cat', 'dog'])
  label = tf.feature_column.numeric_column('label')

  all_features = [animals]
  feature_spec = tf.feature_column.make_parse_example_spec(all_features)
  eval_feature_spec = tf.feature_column.make_parse_example_spec(all_features +
                                                                [label])

  classifier = tf.estimator.LinearClassifier(
      feature_columns=all_features,
      loss_reduction=tf.compat.v1.losses.Reduction.SUM)
  classifier.train(input_fn=input_fn, steps=5000)

  return util.export_model_and_eval_model(
      estimator=classifier,
      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)
コード例 #15
0
def simple_linear_classifier_multivalent(export_path, eval_export_path):
    """Trains and exports a simple linear classifier with multivalent features."""
    def input_fn():
        """Train input function."""
        return {
            'animals':
            tf.SparseTensor(values=[
                'cat', 'dog', 'bird', 'cat', 'dog', 'cat', 'bird', 'dog',
                'bird', 'cat', 'dog', 'bird'
            ],
                            indices=[[0, 0], [1, 0], [2, 0], [4, 0], [4, 1],
                                     [5, 0], [5, 1], [6, 0], [6, 1], [7, 0],
                                     [7, 1], [7, 2]],
                            dense_shape=[8, 3])
        }, tf.constant([[0], [0], [0], [0], [1], [0], [0], [1]])

    animals = tf.contrib.layers.sparse_column_with_keys(
        'animals', ['bird', 'cat', 'dog'])
    label = tf.contrib.layers.real_valued_column('label')

    all_features = [animals]
    feature_spec = tf.contrib.layers.create_feature_spec_for_parsing(
        all_features)
    eval_feature_spec = tf.contrib.layers.create_feature_spec_for_parsing(
        all_features + [label])

    classifier = tf.estimator.LinearClassifier(feature_columns=all_features)
    classifier.train(input_fn=input_fn, steps=5000)

    return util.export_model_and_eval_model(
        estimator=classifier,
        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)
コード例 #16
0
def simple_multi_head(export_path, eval_export_path):
    """Trains and exports a simple multi-headed model."""
    def eval_input_receiver_fn():
        """Eval input receiver function."""
        serialized_tf_example = tf.compat.v1.placeholder(
            dtype=tf.string, shape=[None], name='input_example_tensor')

        language = tf.feature_column.categorical_column_with_vocabulary_list(
            'language', ['english', 'chinese', 'other'])
        age = tf.feature_column.numeric_column('age')
        english_label = tf.feature_column.numeric_column('english_label')
        chinese_label = tf.feature_column.numeric_column('chinese_label')
        other_label = tf.feature_column.numeric_column('other_label')
        all_features = [
            age, language, english_label, chinese_label, other_label
        ]
        feature_spec = tf.feature_column.make_parse_example_spec(all_features)
        receiver_tensors = {'examples': serialized_tf_example}
        features = tf.io.parse_example(serialized=serialized_tf_example,
                                       features=feature_spec)

        labels = {
            'english_head': features['english_label'],
            'chinese_head': features['chinese_label'],
            'other_head': features['other_label'],
        }

        return export.EvalInputReceiver(features=features,
                                        receiver_tensors=receiver_tensors,
                                        labels=labels)

    def input_fn():
        """Train input function."""
        labels = {
            'english_head': tf.constant([[1], [1], [0], [0], [0], [0]]),
            'chinese_head': tf.constant([[0], [0], [1], [1], [0], [0]]),
            'other_head': tf.constant([[0], [0], [0], [0], [1], [1]])
        }
        features = {
            'age':
            tf.constant([[1], [2], [3], [4], [5], [6]]),
            'language':
            tf.SparseTensor(values=[
                'english', 'english', 'chinese', 'chinese', 'other', 'other'
            ],
                            indices=[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0],
                                     [5, 0]],
                            dense_shape=[6, 1]),
        }
        return features, labels

    language = tf.feature_column.categorical_column_with_vocabulary_list(
        'language', ['english', 'chinese', 'other'])
    age = tf.feature_column.numeric_column('age')
    all_features = [age, language]
    feature_spec = tf.feature_column.make_parse_example_spec(all_features)

    # TODO(b/130299739): Update with tf.estimator.BinaryClassHead and
    #   tf.estimator.MultiHead
    english_head = binary_class_head.BinaryClassHead(name='english_head')
    chinese_head = binary_class_head.BinaryClassHead(name='chinese_head')
    other_head = binary_class_head.BinaryClassHead(name='other_head')
    combined_head = multi_head.MultiHead(
        [english_head, chinese_head, other_head])

    estimator = tf_compat_v1_estimator.DNNLinearCombinedEstimator(
        head=combined_head,
        dnn_feature_columns=[],
        dnn_optimizer=tf.compat.v1.train.AdagradOptimizer(learning_rate=0.01),
        dnn_hidden_units=[],
        linear_feature_columns=[language, age],
        linear_optimizer=tf.compat.v1.train.FtrlOptimizer(learning_rate=0.05))
    estimator.train(input_fn=input_fn, steps=1000)

    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=eval_input_receiver_fn,
        export_path=export_path,
        eval_export_path=eval_export_path)
コード例 #17
0
def simple_control_dependency_estimator(export_path, eval_export_path):
    """Exports a simple estimator with control dependencies."""
    def control_dependency_metric(increment, target):
        """Metric that introduces a control dependency on target.

    The value is incremented by increment each time the metric is called
    (so the value can vary depending on how things are batched). This is mainly
    to verify that the metric was called.

    Args:
      increment: Amount to increment the value by each time the metric is
        called.
      target: Tensor to introduce the control dependency on.

    Returns:
      value_op, update_op for the metric.
    """

        total_value = tf.Variable(initial_value=0.0,
                                  dtype=tf.float64,
                                  trainable=False,
                                  collections=[
                                      tf.GraphKeys.METRIC_VARIABLES,
                                      tf.GraphKeys.LOCAL_VARIABLES
                                  ],
                                  validate_shape=True)

        with tf.control_dependencies([target]):
            update_op = tf.assign_add(total_value, increment)
        value_op = tf.identity(total_value)
        return value_op, update_op

    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['actual_label'])
        train_op = tf.assign_add(tf.train.get_global_step(), 1)

        eval_metric_ops = {}
        if mode == tf.estimator.ModeKeys.EVAL:
            eval_metric_ops = {
                metric_keys.MetricKeys.LOSS_MEAN:
                tf.metrics.mean(loss),
                'control_dependency_on_fixed_float':
                control_dependency_metric(1.0, features['fixed_float']),
                # Introduce a direct dependency on the values Tensor. If we
                # introduce another intervening op like sparse_tensor_to_dense then
                # regardless of whether TFMA correctly wrap SparseTensors we will not
                # encounter the TF bug.
                'control_dependency_on_var_float':
                control_dependency_metric(10.0, features['var_float'].values),
                'control_dependency_on_actual_label':
                control_dependency_metric(100.0, labels['actual_label']),
                'control_dependency_on_var_int_label':
                control_dependency_metric(1000.0, labels['var_int'].values),
                # Note that TFMA does *not* wrap predictions, so in most cases
                # if there's a control dependency on predictions they will be
                # recomputed.
                'control_dependency_on_prediction':
                control_dependency_metric(10000.0, predictions),
            }

        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]]),
        }, {
            'actual_label': 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)

    def eval_input_receiver_fn():
        """An input_fn that expects a serialized tf.Example."""
        serialized_tf_example = tf.placeholder(dtype=tf.string,
                                               shape=[None],
                                               name='input_example_tensor')
        features = tf.parse_example(serialized_tf_example, eval_feature_spec)
        labels = {
            'actual_label': features['label'],
            'var_int': features['var_int']
        }
        return export.EvalInputReceiver(
            features=features,
            labels=labels,
            receiver_tensors={'examples': serialized_tf_example})

    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=eval_input_receiver_fn,
        export_path=export_path,
        eval_export_path=eval_export_path)
コード例 #18
0
def simple_custom_estimator(export_path, eval_export_path):
    """Trains and exports a simple custom estimator."""
    def model_fn(features, labels, mode, params):
        """Model function for custom estimator."""
        del params
        m = tf.Variable(0.0, dtype=tf.float32, name='m')
        c = tf.Variable(0.0, dtype=tf.float32, name='c')
        predictions = m * features['age'] + c

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

        loss = tf.compat.v1.losses.mean_squared_error(labels, predictions)
        eval_metric_ops = {
            'mean_absolute_error':
            tf.compat.v1.metrics.mean_absolute_error(
                tf.cast(labels, tf.float64), tf.cast(predictions, tf.float64)),
            'mean_prediction':
            tf.compat.v1.metrics.mean(predictions),
            'mean_label':
            tf.compat.v1.metrics.mean(labels),
        }

        optimizer = tf.compat.v1.train.GradientDescentOptimizer(
            learning_rate=0.001)
        train_op = optimizer.minimize(
            loss=loss, global_step=tf.compat.v1.train.get_global_step())

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

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

    def serving_input_receiver_fn():
        """Serving input receiver function."""
        serialized_tf_example = tf.compat.v1.placeholder(
            dtype=tf.string, shape=[None], name='input_example_tensor')
        feature_spec = {'age': tf.io.FixedLenFeature([1], dtype=tf.float32)}
        receiver_tensors = {'examples': serialized_tf_example}
        features = tf.io.parse_example(serialized=serialized_tf_example,
                                       features=feature_spec)
        return tf.estimator.export.ServingInputReceiver(
            features, receiver_tensors)

    def eval_input_receiver_fn():
        """Eval input receiver function."""
        serialized_tf_example = tf.compat.v1.placeholder(
            dtype=tf.string, shape=[None], name='input_example_tensor')
        feature_spec = {
            'age': tf.io.FixedLenFeature([1], dtype=tf.float32),
            'label': tf.io.FixedLenFeature([1], dtype=tf.float32)
        }
        receiver_tensors = {'examples': serialized_tf_example}
        features = tf.io.parse_example(serialized=serialized_tf_example,
                                       features=feature_spec)

        return export.EvalInputReceiver(features=features,
                                        labels=features['label'],
                                        receiver_tensors=receiver_tensors)

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

    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)
コード例 #19
0
ファイル: multi_head.py プロジェクト: norah98/model-analysis
def simple_multi_head(export_path, eval_export_path):
  """Trains and exports a simple multi-headed model."""

  def eval_input_receiver_fn():
    """Eval input receiver function."""
    serialized_tf_example = tf.placeholder(
        dtype=tf.string, shape=[None], name='input_example_tensor')

    language = tf.contrib.layers.sparse_column_with_keys(
        'language', ['english', 'chinese', 'other'])
    age = tf.contrib.layers.real_valued_column('age')
    english_label = tf.contrib.layers.real_valued_column('english_label')
    chinese_label = tf.contrib.layers.real_valued_column('chinese_label')
    other_label = tf.contrib.layers.real_valued_column('other_label')
    all_features = [age, language, english_label, chinese_label, other_label]
    feature_spec = tf.contrib.layers.create_feature_spec_for_parsing(
        all_features)
    receiver_tensors = {'examples': serialized_tf_example}
    features = tf.parse_example(serialized_tf_example, feature_spec)

    labels = {
        'english_head': features['english_label'],
        'chinese_head': features['chinese_label'],
        'other_head': features['other_label'],
    }

    return export.EvalInputReceiver(
        features=features, receiver_tensors=receiver_tensors, labels=labels)

  def input_fn():
    """Train input function."""
    labels = {
        'english_head': tf.constant([[1], [1], [0], [0], [0], [0]]),
        'chinese_head': tf.constant([[0], [0], [1], [1], [0], [0]]),
        'other_head': tf.constant([[0], [0], [0], [0], [1], [1]])
    }
    features = {
        'age':
            tf.constant([[1], [2], [3], [4], [5], [6]]),
        'language':
            tf.SparseTensor(
                values=[
                    'english', 'english', 'chinese', 'chinese', 'other', 'other'
                ],
                indices=[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0]],
                dense_shape=[6, 1]),
    }
    return features, labels

  language = tf.contrib.layers.sparse_column_with_keys(
      'language', ['english', 'chinese', 'other'])
  age = tf.contrib.layers.real_valued_column('age')
  all_features = [age, language]
  feature_spec = tf.contrib.layers.create_feature_spec_for_parsing(all_features)

  english_head = tf.contrib.estimator.binary_classification_head(
      name='english_head')
  chinese_head = tf.contrib.estimator.binary_classification_head(
      name='chinese_head')
  other_head = tf.contrib.estimator.binary_classification_head(
      name='other_head')
  combined_head = tf.contrib.estimator.multi_head(
      [english_head, chinese_head, other_head])

  # TODO(b/118630716): Remove this check once we depend on TF 1.13.
  if hasattr(tf.estimator, 'DNNLinearCombinedEstimator'):
    estimator_fn = tf.estimator.DNNLinearCombinedEstimator
  else:
    estimator_fn = tf.contrib.estimator.DNNLinearCombinedEstimator
  estimator = estimator_fn(
      head=combined_head,
      dnn_feature_columns=[],
      dnn_optimizer=tf.train.AdagradOptimizer(learning_rate=0.01),
      dnn_hidden_units=[],
      linear_feature_columns=[language, age],
      linear_optimizer=tf.train.FtrlOptimizer(learning_rate=0.05))
  estimator.train(input_fn=input_fn, steps=1000)

  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=eval_input_receiver_fn,
      export_path=export_path,
      eval_export_path=eval_export_path)
コード例 #20
0
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, config):
        """Model function for custom estimator."""
        del config
        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)
コード例 #21
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)
コード例 #22
0
def simple_fixed_prediction_estimator_extra_fields(export_path,
                                                   eval_export_path,
                                                   include_metrics=True):
    """Exports a simple fixed prediction estimator that parses extra fields."""
    def model_fn(features, labels, mode, config):
        """Model function for custom estimator."""
        del config
        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.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                    tf.estimator.export.RegressionOutput(predictions)
                })

        loss = tf.compat.v1.losses.mean_squared_error(predictions, labels)
        train_op = tf.compat.v1.assign_add(
            tf.compat.v1.train.get_global_step(), 1)

        eval_metric_ops = {}
        if include_metrics:
            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]]),
        }, tf.constant([[1.0], [2.0], [3.0], [4.0]]),

    feature_spec = {'prediction': tf.io.FixedLenFeature([1], dtype=tf.float32)}
    eval_feature_spec = {
        'prediction':
        tf.io.FixedLenFeature([1], dtype=tf.float32),
        'label':
        tf.io.FixedLenFeature([1], dtype=tf.float32),
        'fixed_float':
        tf.io.FixedLenFeature([1], dtype=tf.float32, default_value=0.0),
        'fixed_string':
        tf.io.FixedLenFeature([1], dtype=tf.string, default_value=''),
        'fixed_int':
        tf.io.FixedLenFeature([1], dtype=tf.int64, default_value=0),
        'var_float':
        tf.io.VarLenFeature(dtype=tf.float32),
        'var_string':
        tf.io.VarLenFeature(dtype=tf.string),
        'var_int':
        tf.io.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)