コード例 #1
0
    def _test_complete_flow(self, train_input_fn, eval_input_fn,
                            predict_input_fn, input_dimension, label_dimension,
                            batch_size):
        feature_columns = [
            tf.feature_column.numeric_column('x', shape=(input_dimension, ))
        ]
        est = linear.LinearEstimator(
            head=head_lib._regression_head(label_dimension=label_dimension),
            feature_columns=feature_columns,
            model_dir=self._model_dir)

        # Train
        num_steps = 10
        est.train(train_input_fn, steps=num_steps)

        # Evaluate
        scores = est.evaluate(eval_input_fn)
        self.assertEqual(num_steps, scores[tf.compat.v1.GraphKeys.GLOBAL_STEP])
        self.assertIn('loss', six.iterkeys(scores))

        # Predict
        predictions = np.array([
            x[prediction_keys.PredictionKeys.PREDICTIONS]
            for x in est.predict(predict_input_fn)
        ])
        self.assertAllEqual((batch_size, label_dimension), predictions.shape)

        # Export
        feature_spec = tf.compat.v1.feature_column.make_parse_example_spec(
            feature_columns)
        serving_input_receiver_fn = export.build_parsing_serving_input_receiver_fn(
            feature_spec)
        export_dir = est.export_saved_model(tempfile.mkdtemp(),
                                            serving_input_receiver_fn)
        self.assertTrue(tf.compat.v1.gfile.Exists(export_dir))
コード例 #2
0
def _baseline_estimator_fn(weight_column=None,
                           label_dimension=1,
                           **kwargs):
  return baseline.BaselineEstimator(
      head=head_lib._regression_head(
          weight_column=weight_column,
          label_dimension=label_dimension,
          loss_reduction=tf.compat.v1.losses.Reduction.SUM),
      **kwargs)
コード例 #3
0
def _linear_estimator_fn(weight_column=None, label_dimension=1, **kwargs):
    """Returns a LinearEstimator that uses regression_head."""
    return linear.LinearEstimator(
        head=head_lib._regression_head(
            weight_column=weight_column,
            label_dimension=label_dimension,
            # Tests in core (from which this test inherits) test the sum loss.
            loss_reduction=tf.compat.v1.losses.Reduction.SUM),
        **kwargs)
コード例 #4
0
  def _test_ckpt_converter(self, train_input_fn, eval_input_fn,
                           predict_input_fn, input_dimension, label_dimension,
                           batch_size, dnn_optimizer, linear_optimizer):

    # Create checkpoint in CannedEstimator v1.
    linear_feature_columns_v1 = [
        feature_column._numeric_column('x', shape=(input_dimension,))
    ]
    dnn_feature_columns_v1 = [
        feature_column._numeric_column('x', shape=(input_dimension,))
    ]
    est_v1 = dnn_linear_combined.DNNLinearCombinedEstimator(
        head=head_lib._regression_head(label_dimension=label_dimension),
        linear_feature_columns=linear_feature_columns_v1,
        dnn_feature_columns=dnn_feature_columns_v1,
        dnn_hidden_units=(2, 2),
        model_dir=self._old_ckpt_dir,
        dnn_optimizer=dnn_optimizer,
        linear_optimizer=linear_optimizer)
    # Train
    num_steps = 10
    est_v1.train(train_input_fn, steps=num_steps)
    self.assertIsNotNone(est_v1.latest_checkpoint())
    self.assertTrue(est_v1.latest_checkpoint().startswith(self._old_ckpt_dir))

    # Convert checkpoint from v1 to v2.
    source_checkpoint = os.path.join(self._old_ckpt_dir, 'model.ckpt-10')
    source_graph = os.path.join(self._old_ckpt_dir, 'graph.pbtxt')
    target_checkpoint = os.path.join(self._new_ckpt_dir, 'model.ckpt-10')
    checkpoint_converter.convert_checkpoint('combined', source_checkpoint,
                                            source_graph, target_checkpoint)

    # Create CannedEstimator V2 and restore from the converted checkpoint.
    linear_feature_columns_v2 = [
        tf.feature_column.numeric_column('x', shape=(input_dimension,))
    ]
    dnn_feature_columns_v2 = [
        tf.feature_column.numeric_column('x', shape=(input_dimension,))
    ]
    est_v2 = dnn_linear_combined.DNNLinearCombinedEstimatorV2(
        head=regression_head.RegressionHead(label_dimension=label_dimension),
        linear_feature_columns=linear_feature_columns_v2,
        dnn_feature_columns=dnn_feature_columns_v2,
        dnn_hidden_units=(2, 2),
        model_dir=self._new_ckpt_dir,
        dnn_optimizer=dnn_optimizer,
        linear_optimizer=linear_optimizer)
    # Train
    extra_steps = 10
    est_v2.train(train_input_fn, steps=extra_steps)
    self.assertIsNotNone(est_v2.latest_checkpoint())
    self.assertTrue(est_v2.latest_checkpoint().startswith(self._new_ckpt_dir))
    # Make sure estimator v2 restores from the converted checkpoint, and
    # continues training extra steps.
    self.assertEqual(num_steps + extra_steps,
                     est_v2.get_variable_value(tf.compat.v1.GraphKeys.GLOBAL_STEP))
コード例 #5
0
  def __init__(self,
               model_dir=None,
               linear_feature_columns=None,
               linear_optimizer='Ftrl',
               dnn_feature_columns=None,
               dnn_optimizer='Adagrad',
               dnn_hidden_units=None,
               dnn_activation_fn=tf.nn.relu,
               dnn_dropout=None,
               label_dimension=1,
               weight_column=None,
               input_layer_partitioner=None,
               config=None,
               warm_start_from=None,
               loss_reduction=tf.compat.v1.losses.Reduction.SUM,
               batch_norm=False,
               linear_sparse_combiner='sum'):
    self._feature_columns = _validate_feature_columns(
        linear_feature_columns=linear_feature_columns,
        dnn_feature_columns=dnn_feature_columns)
    estimator._canned_estimator_api_gauge.get_cell('Regressor').set(
        'DNNLinearCombined')  # pylint: disable=protected-access

    head = head_lib._regression_head(  # pylint: disable=protected-access
        label_dimension=label_dimension,
        weight_column=weight_column,
        loss_reduction=loss_reduction)

    def _model_fn(features, labels, mode, config):
      """Call the _dnn_linear_combined_model_fn."""
      return _dnn_linear_combined_model_fn(
          features=features,
          labels=labels,
          mode=mode,
          head=head,
          linear_feature_columns=linear_feature_columns,
          linear_optimizer=linear_optimizer,
          dnn_feature_columns=dnn_feature_columns,
          dnn_optimizer=dnn_optimizer,
          dnn_hidden_units=dnn_hidden_units,
          dnn_activation_fn=dnn_activation_fn,
          dnn_dropout=dnn_dropout,
          input_layer_partitioner=input_layer_partitioner,
          config=config,
          batch_norm=batch_norm,
          linear_sparse_combiner=linear_sparse_combiner)

    super(DNNLinearCombinedRegressor, self).__init__(
        model_fn=_model_fn,
        model_dir=model_dir,
        config=config,
        warm_start_from=warm_start_from)
コード例 #6
0
ファイル: baseline.py プロジェクト: AmberCheng/estimator
    def __init__(self,
                 model_dir=None,
                 label_dimension=1,
                 weight_column=None,
                 optimizer='Ftrl',
                 config=None,
                 loss_reduction=losses.Reduction.SUM):
        """Initializes a BaselineRegressor instance.

    Args:
      model_dir: Directory to save model parameters, graph and etc. This can
        also be used to load checkpoints from the directory into a estimator to
        continue training a previously saved model.
      label_dimension: Number of regression targets per example. This is the
        size of the last dimension of the labels and logits `Tensor` objects
        (typically, these have shape `[batch_size, label_dimension]`).
      weight_column: A string or a `_NumericColumn` created by
        `tf.feature_column.numeric_column` defining feature column representing
         weights. It will be multiplied by the loss of the example.
      optimizer: String, `tf.Optimizer` object, or callable that creates the
        optimizer to use for training. If not specified, will use
        `FtrlOptimizer` with a default learning rate of 0.3.
      config: `RunConfig` object to configure the runtime settings.
      loss_reduction: One of `tf.losses.Reduction` except `NONE`. Describes how
        to reduce training loss over batch. Defaults to `SUM`.
    Returns:
      A `BaselineRegressor` estimator.
    """

        # TODO(b/117517419): Update this reference once head moves to core.
        head = head_lib._regression_head(  # pylint: disable=protected-access
            label_dimension=label_dimension,
            weight_column=weight_column,
            loss_reduction=loss_reduction)

        def _model_fn(features, labels, mode, config):
            return _baseline_model_fn(features=features,
                                      labels=labels,
                                      mode=mode,
                                      head=head,
                                      optimizer=optimizer,
                                      config=config)

        super(BaselineRegressor, self).__init__(model_fn=_model_fn,
                                                model_dir=model_dir,
                                                config=config)
コード例 #7
0
ファイル: dnn.py プロジェクト: yupbank/estimator
 def _model_fn(features, labels, mode, config):
     """Call the defined shared _dnn_model_fn."""
     return _dnn_model_fn(
         features=features,
         labels=labels,
         mode=mode,
         head=head_lib._regression_head(  # pylint: disable=protected-access
             label_dimension=label_dimension,
             weight_column=weight_column,
             loss_reduction=loss_reduction),
         hidden_units=hidden_units,
         feature_columns=tuple(feature_columns or []),
         optimizer=optimizer,
         activation_fn=activation_fn,
         dropout=dropout,
         input_layer_partitioner=input_layer_partitioner,
         config=config,
         batch_norm=batch_norm)
コード例 #8
0
ファイル: dnn.py プロジェクト: shaosimon/estimator
  def __init__(
      self,
      hidden_units,
      feature_columns,
      model_dir=None,
      label_dimension=1,
      weight_column=None,
      optimizer='Adagrad',
      activation_fn=tf.nn.relu,
      dropout=None,
      input_layer_partitioner=None,
      config=None,
      warm_start_from=None,
      loss_reduction=tf.compat.v1.losses.Reduction.SUM,
      batch_norm=False,
  ):
    head = head_lib._regression_head(  # pylint: disable=protected-access
        label_dimension=label_dimension,
        weight_column=weight_column,
        loss_reduction=loss_reduction)
    estimator._canned_estimator_api_gauge.get_cell('Regressor').set('DNN')  # pylint: disable=protected-access

    def _model_fn(features, labels, mode, config):
      """Call the defined shared _dnn_model_fn."""
      return _dnn_model_fn(
          features=features,
          labels=labels,
          mode=mode,
          head=head,
          hidden_units=hidden_units,
          feature_columns=tuple(feature_columns or []),
          optimizer=optimizer,
          activation_fn=activation_fn,
          dropout=dropout,
          input_layer_partitioner=input_layer_partitioner,
          config=config,
          batch_norm=batch_norm)

    super(DNNRegressor, self).__init__(
        model_fn=_model_fn,
        model_dir=model_dir,
        config=config,
        warm_start_from=warm_start_from)
コード例 #9
0
def _linear_only_estimator_fn(feature_columns,
                              model_dir=None,
                              label_dimension=1,
                              weight_column=None,
                              optimizer='Ftrl',
                              config=None,
                              partitioner=None,
                              sparse_combiner='sum'):
    return dnn_linear_combined.DNNLinearCombinedEstimator(
        head=head_lib._regression_head(
            weight_column=weight_column,
            label_dimension=label_dimension,
            # Tests in core (from which this test inherits) test the sum loss.
            loss_reduction=losses.Reduction.SUM),
        model_dir=model_dir,
        linear_feature_columns=feature_columns,
        linear_optimizer=optimizer,
        input_layer_partitioner=partitioner,
        config=config,
        linear_sparse_combiner=sparse_combiner)
コード例 #10
0
 def _model_fn(features, labels, mode, config):
     """Call the _dnn_linear_combined_model_fn."""
     return _dnn_linear_combined_model_fn(
         features=features,
         labels=labels,
         mode=mode,
         head=head_lib._regression_head(  # pylint: disable=protected-access
             label_dimension=label_dimension,
             weight_column=weight_column,
             loss_reduction=loss_reduction),
         linear_feature_columns=linear_feature_columns,
         linear_optimizer=linear_optimizer,
         dnn_feature_columns=dnn_feature_columns,
         dnn_optimizer=dnn_optimizer,
         dnn_hidden_units=dnn_hidden_units,
         dnn_activation_fn=dnn_activation_fn,
         dnn_dropout=dnn_dropout,
         input_layer_partitioner=input_layer_partitioner,
         config=config,
         batch_norm=batch_norm,
         linear_sparse_combiner=linear_sparse_combiner)
コード例 #11
0
ファイル: linear.py プロジェクト: AhmedRafat19/estimator
  def __init__(self,
               feature_columns,
               model_dir=None,
               label_dimension=1,
               weight_column=None,
               optimizer='Ftrl',
               config=None,
               partitioner=None,
               warm_start_from=None,
               loss_reduction=losses.Reduction.SUM,
               sparse_combiner='sum'):
    _validate_linear_sdca_optimizer_for_linear_regressor(
        feature_columns=feature_columns,
        label_dimension=label_dimension,
        optimizer=optimizer,
        sparse_combiner=sparse_combiner)

    head = head_lib._regression_head(  # pylint: disable=protected-access
        label_dimension=label_dimension,
        weight_column=weight_column,
        loss_reduction=loss_reduction)

    def _model_fn(features, labels, mode, config):
      """Call the defined shared _linear_model_fn."""
      return _linear_model_fn(
          features=features,
          labels=labels,
          mode=mode,
          head=head,
          feature_columns=tuple(feature_columns or []),
          optimizer=optimizer,
          partitioner=partitioner,
          config=config,
          sparse_combiner=sparse_combiner)

    super(LinearRegressor, self).__init__(
        model_fn=_model_fn,
        model_dir=model_dir,
        config=config,
        warm_start_from=warm_start_from)
コード例 #12
0
    def __init__(self,
                 model_dir=None,
                 label_dimension=1,
                 weight_column=None,
                 optimizer='Ftrl',
                 config=None,
                 loss_reduction=losses.Reduction.SUM):
        head = head_lib._regression_head(  # pylint: disable=protected-access
            label_dimension=label_dimension,
            weight_column=weight_column,
            loss_reduction=loss_reduction)

        def _model_fn(features, labels, mode, config):
            return _baseline_model_fn(features=features,
                                      labels=labels,
                                      mode=mode,
                                      head=head,
                                      optimizer=optimizer,
                                      config=config)

        super(BaselineRegressor, self).__init__(model_fn=_model_fn,
                                                model_dir=model_dir,
                                                config=config)
コード例 #13
0
def _dnn_only_estimator_fn(hidden_units,
                           feature_columns,
                           model_dir=None,
                           label_dimension=1,
                           weight_column=None,
                           optimizer='Adagrad',
                           activation_fn=nn.relu,
                           dropout=None,
                           input_layer_partitioner=None,
                           config=None):
    return dnn_linear_combined.DNNLinearCombinedEstimator(
        head=head_lib._regression_head(
            weight_column=weight_column,
            label_dimension=label_dimension,
            # Tests in core (from which this test inherits) test the sum loss.
            loss_reduction=losses.Reduction.SUM),
        model_dir=model_dir,
        dnn_feature_columns=feature_columns,
        dnn_optimizer=optimizer,
        dnn_hidden_units=hidden_units,
        dnn_activation_fn=activation_fn,
        dnn_dropout=dropout,
        input_layer_partitioner=input_layer_partitioner,
        config=config)
コード例 #14
0
ファイル: head.py プロジェクト: yueyedeai/estimator
def regression_head(weight_column=None,
                    label_dimension=1,
                    loss_reduction=losses.Reduction.SUM_OVER_BATCH_SIZE,
                    loss_fn=None,
                    inverse_link_fn=None,
                    name=None):
    """Creates a `_Head` for regression using the `mean_squared_error` loss.

  The loss is the weighted sum over all input dimensions. Namely, if the input
  labels have shape `[batch_size, label_dimension]`, the loss is the weighted
  sum over both `batch_size` and `label_dimension`.

  The head expects `logits` with shape `[D0, D1, ... DN, label_dimension]`.
  In many applications, the shape is `[batch_size, label_dimension]`.

  The `labels` shape must match `logits`, namely
  `[D0, D1, ... DN, label_dimension]`. If `label_dimension=1`, shape
  `[D0, D1, ... DN]` is also supported.

  If `weight_column` is specified, weights must be of shape
  `[D0, D1, ... DN]`, `[D0, D1, ... DN, 1]` or
  `[D0, D1, ... DN, label_dimension]`.

  Supports custom `loss_fn`. `loss_fn` takes `(labels, logits)` or
  `(labels, logits, features)` as arguments and returns unreduced loss with
  shape `[D0, D1, ... DN, label_dimension]`.

  Also supports custom `inverse_link_fn`, also known as 'mean function'.
  `inverse_link_fn` is only used in `PREDICT` mode. It takes `logits` as
  argument and returns predicted values. This function is the inverse of the
  link function defined in
  https://en.wikipedia.org/wiki/Generalized_linear_model#Link_function
  Namely, for poisson regression, set `inverse_link_fn=tf.exp`.

  The head can be used with a canned estimator. Example:

  ```python
  my_head = tf.contrib.estimator.regression_head()
  my_estimator = tf.contrib.estimator.DNNEstimator(
      head=my_head,
      hidden_units=...,
      feature_columns=...)
  ```

  It can also be used with a custom `model_fn`. Example:

  ```python
  def _my_model_fn(features, labels, mode):
    my_head = tf.contrib.estimator.regression_head()
    logits = tf.keras.Model(...)(features)

    return my_head.create_estimator_spec(
        features=features,
        mode=mode,
        labels=labels,
        optimizer=tf.AdagradOptimizer(learning_rate=0.1),
        logits=logits)

  my_estimator = tf.estimator.Estimator(model_fn=_my_model_fn)
  ```

  Args:
    weight_column: A string or a `_NumericColumn` created by
      `tf.feature_column.numeric_column` defining feature column representing
      weights. It is used to down weight or boost examples during training. It
      will be multiplied by the loss of the example.
    label_dimension: Number of regression labels per example. This is the size
      of the last dimension of the labels `Tensor` (typically, this has shape
      `[batch_size, label_dimension]`).
    loss_reduction: One of `tf.losses.Reduction` except `NONE`. Describes how to
      reduce training loss over batch and label dimension. Defaults to
      `SUM_OVER_BATCH_SIZE`, namely weighted sum of losses divided by
      `batch size * label_dimension`. See `tf.losses.Reduction`.
    loss_fn: Optional loss function. Defaults to `mean_squared_error`.
    inverse_link_fn: Optional inverse link function, also known as 'mean
      function'. Defaults to identity.
    name: name of the head. If provided, summary and metrics keys will be
      suffixed by `"/" + name`. Also used as `name_scope` when creating ops.

  Returns:
    An instance of `_Head` for linear regression.

  Raises:
    ValueError: If `label_dimension` or `loss_reduction` is invalid.
  """
    return head_lib._regression_head(  # pylint:disable=protected-access
        weight_column=weight_column,
        label_dimension=label_dimension,
        loss_reduction=loss_reduction,
        loss_fn=loss_fn,
        inverse_link_fn=inverse_link_fn,
        name=name)
コード例 #15
0
def train_and_evaluate_estimator():
  """Runs Estimator distributed training."""

  # The tf.estimator.RunConfig automatically parses the TF_CONFIG environment
  # variables during construction.
  # For more information on how tf.estimator.RunConfig uses TF_CONFIG, see
  # https://www.tensorflow.org/api_docs/python/tf/estimator/RunConfig.
  config = tf.estimator.RunConfig(
      tf_random_seed=42,
      save_checkpoints_steps=10,
      save_checkpoints_secs=None,
      # Keep all checkpoints to avoid checkpoint GC causing failures during
      # evaluation.
      # TODO: Prevent checkpoints that are currently being
      # evaluated by another process from being garbage collected.
      keep_checkpoint_max=None,
      model_dir=FLAGS.model_dir,
      session_config=tf_compat.v1.ConfigProto(
          log_device_placement=False,
          # Ignore other workers; only talk to parameter servers.
          # Otherwise, when a chief/worker terminates, the others will hang.
          device_filters=["/job:ps"]))

  def input_fn():
    input_features = {"x": tf.constant(features, name="x")}
    input_labels = tf.constant(labels, name="y")
    return tf.data.Dataset.from_tensors((input_features, input_labels)).repeat()

  kwargs = {
      "max_iteration_steps": 100,
      "force_grow": True,
      "delay_secs_per_worker": .2,
      "max_worker_delay_secs": 1,
      "worker_wait_secs": 1,
      # Set low timeout to reduce wait time for failures.
      "worker_wait_timeout_secs": 180,
      "evaluator": Evaluator(input_fn, steps=10),
      "config": config
  }

  head = head_lib._regression_head(  # pylint: disable=protected-access
      loss_reduction=tf.losses.Reduction.SUM_OVER_BATCH_SIZE)
  features = [[1., 0.], [0., 0], [0., 1.], [1., 1.]]
  labels = [[1.], [0.], [1.], [0.]]

  estimator_type = FLAGS.estimator_type
  if FLAGS.placement_strategy == "round_robin":
    kwargs["experimental_placement_strategy"] = RoundRobinStrategy()
  if estimator_type == "autoensemble":
    feature_columns = [tf.feature_column.numeric_column("x", shape=[2])]
    # pylint: disable=g-long-lambda
    # TODO: Switch optimizers to tf.keras.optimizers.Adam once the
    # distribution bug is fixed.
    candidate_pool = {
        "linear":
            tf.estimator.LinearEstimator(
                head=head,
                feature_columns=feature_columns,
                optimizer=lambda: tf_compat.v1.train.AdamOptimizer(
                    learning_rate=.001)),
        "dnn":
            tf.estimator.DNNEstimator(
                head=head,
                feature_columns=feature_columns,
                optimizer=lambda: tf_compat.v1.train.AdamOptimizer(
                    learning_rate=.001),
                hidden_units=[3]),
        "dnn2":
            tf.estimator.DNNEstimator(
                head=head,
                feature_columns=feature_columns,
                optimizer=lambda: tf_compat.v1.train.AdamOptimizer(
                    learning_rate=.001),
                hidden_units=[10, 10]),
    }
    # pylint: enable=g-long-lambda

    estimator = AutoEnsembleEstimator(
        head=head, candidate_pool=candidate_pool, **kwargs)
  elif estimator_type == "estimator":
    subnetwork_generator = SimpleGenerator([
        _DNNBuilder("dnn1", config, layer_size=3),
        _DNNBuilder("dnn2", config, layer_size=4),
        _DNNBuilder("dnn3", config, layer_size=5),
    ])

    estimator = Estimator(
        head=head, subnetwork_generator=subnetwork_generator, **kwargs)
  elif FLAGS.estimator_type == "autoensemble_trees_multiclass":
    if not bt_losses:
      logging.warning(
          "Skipped autoensemble_trees_multiclass test since contrib is missing."
      )
      return
    n_classes = 3
    head = head_lib._multi_class_head_with_softmax_cross_entropy_loss(  # pylint: disable=protected-access
        n_classes=n_classes,
        loss_reduction=tf.losses.Reduction.SUM_OVER_BATCH_SIZE)

    def tree_loss_fn(labels, logits):
      result = bt_losses.per_example_maxent_loss(
          labels=labels, logits=logits, num_classes=n_classes, weights=None)
      return result[0]

    tree_head = head_lib._multi_class_head_with_softmax_cross_entropy_loss(  # pylint: disable=protected-access
        loss_fn=tree_loss_fn,
        n_classes=n_classes,
        loss_reduction=tf.losses.Reduction.SUM_OVER_BATCH_SIZE)
    labels = [[1], [0], [1], [2]]
    feature_columns = [tf.feature_column.numeric_column("x", shape=[2])]
    # TODO: Switch optimizers to tf.keras.optimizers.Adam once the
    # distribution bug is fixed.
    candidate_pool = lambda config: {  # pylint: disable=g-long-lambda
        "linear":
            tf.estimator.LinearEstimator(
                head=head,
                feature_columns=feature_columns,
                optimizer=tf_compat.v1.train.AdamOptimizer(
                    learning_rate=.001),
                config=config),
        "gbdt":
            tf.estimator.BoostedTreesEstimator(
                head=tree_head,
                feature_columns=feature_columns,
                n_trees=10,
                n_batches_per_layer=1,
                center_bias=False,
                config=config),
    }

    estimator = AutoEnsembleEstimator(
        head=head, candidate_pool=candidate_pool, **kwargs)

  elif estimator_type == "estimator_with_experimental_multiworker_strategy":

    def _model_fn(features, labels, mode):
      """Test model_fn."""
      layer = tf.keras.layers.Dense(1)
      logits = layer(features["x"])

      if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {"logits": logits}
        return tf.estimator.EstimatorSpec(mode, predictions=predictions)

      loss = tf.losses.mean_squared_error(
          labels=labels,
          predictions=logits,
          reduction=tf.losses.Reduction.SUM_OVER_BATCH_SIZE)

      if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode, loss=loss)

      if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.GradientDescentOptimizer(0.2)
        train_op = optimizer.minimize(
            loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

    if json.loads(os.environ["TF_CONFIG"])["task"]["type"] == "evaluator":
      # The evaluator job would crash if MultiWorkerMirroredStrategy is called.
      distribution = None
    else:
      distribution = tf.distribute.experimental.MultiWorkerMirroredStrategy()

    multiworker_config = tf.estimator.RunConfig(
        tf_random_seed=42,
        model_dir=FLAGS.model_dir,
        train_distribute=distribution,
        session_config=tf_compat.v1.ConfigProto(log_device_placement=False))
    # TODO: Replace with adanet.Estimator. Currently this just verifies
    # that the distributed testing framework supports distribute strategies.
    estimator = tf.estimator.Estimator(
        model_fn=_model_fn, config=multiworker_config)

  train_hooks = [
      tf.estimator.ProfilerHook(save_steps=50, output_dir=FLAGS.model_dir)
  ]
  # Train for three iterations.
  train_spec = tf.estimator.TrainSpec(
      input_fn=input_fn, max_steps=300, hooks=train_hooks)
  eval_spec = tf.estimator.EvalSpec(
      input_fn=input_fn, steps=1, start_delay_secs=.5, throttle_secs=.05)

  # Calling train_and_evaluate is the official way to perform distributed
  # training with an Estimator. Calling Estimator#train directly results
  # in an error when the TF_CONFIG is setup for a cluster.
  tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
コード例 #16
0
ファイル: head.py プロジェクト: yueyedeai/estimator
def poisson_regression_head(
        weight_column=None,
        label_dimension=1,
        loss_reduction=losses.Reduction.SUM_OVER_BATCH_SIZE,
        compute_full_loss=True,
        name=None):
    """Creates a `_Head` for poisson regression using `tf.nn.log_poisson_loss`.

  The loss is the weighted sum over all input dimensions. Namely, if the input
  labels have shape `[batch_size, label_dimension]`, the loss is the weighted
  sum over both `batch_size` and `label_dimension`.

  The head expects `logits` with shape `[D0, D1, ... DN, label_dimension]`.
  In many applications, the shape is `[batch_size, label_dimension]`.

  The `labels` shape must match `logits`, namely
  `[D0, D1, ... DN, label_dimension]`. If `label_dimension=1`, shape
  `[D0, D1, ... DN]` is also supported.

  If `weight_column` is specified, weights must be of shape
  `[D0, D1, ... DN]`, `[D0, D1, ... DN, 1]` or
  `[D0, D1, ... DN, label_dimension]`.

  This is implemented as a generalized linear model, see
  https://en.wikipedia.org/wiki/Generalized_linear_model.

  The head can be used with a canned estimator. Example:

  ```python
  my_head = tf.contrib.estimator.poisson_regression_head()
  my_estimator = tf.contrib.estimator.DNNEstimator(
      head=my_head,
      hidden_units=...,
      feature_columns=...)
  ```

  It can also be used with a custom `model_fn`. Example:

  ```python
  def _my_model_fn(features, labels, mode):
    my_head = tf.contrib.estimator.poisson_regression_head()
    logits = tf.keras.Model(...)(features)

    return my_head.create_estimator_spec(
        features=features,
        mode=mode,
        labels=labels,
        optimizer=tf.AdagradOptimizer(learning_rate=0.1),
        logits=logits)

  my_estimator = tf.estimator.Estimator(model_fn=_my_model_fn)
  ```

  Args:
    weight_column: A string or a `_NumericColumn` created by
      `tf.feature_column.numeric_column` defining feature column representing
      weights. It is used to down weight or boost examples during training. It
      will be multiplied by the loss of the example.
    label_dimension: Number of regression labels per example. This is the size
      of the last dimension of the labels `Tensor` (typically, this has shape
      `[batch_size, label_dimension]`).
    loss_reduction: One of `tf.losses.Reduction` except `NONE`. Describes how to
      reduce training loss over batch and label dimension. Defaults to
      `SUM_OVER_BATCH_SIZE`, namely weighted sum of losses divided by
      `batch size * label_dimension`. See `tf.losses.Reduction`.
    compute_full_loss: Whether to include the constant `log(z!)` term in
      computing the poisson loss. See `tf.nn.log_poisson_loss` for the full
      documentation.
    name: name of the head. If provided, summary and metrics keys will be
      suffixed by `"/" + name`. Also used as `name_scope` when creating ops.

  Returns:
    An instance of `_Head` for poisson regression.

  Raises:
    ValueError: If `label_dimension` or `loss_reduction` is invalid.
  """
    def _poisson_loss(labels, logits):
        return nn.log_poisson_loss(targets=labels,
                                   log_input=logits,
                                   compute_full_loss=compute_full_loss)

    return head_lib._regression_head(  # pylint:disable=protected-access
        weight_column=weight_column,
        label_dimension=label_dimension,
        loss_reduction=loss_reduction,
        loss_fn=_poisson_loss,
        inverse_link_fn=math_ops.exp,
        name=name)
コード例 #17
0
ファイル: head.py プロジェクト: yueyedeai/estimator
def logistic_regression_head(
        weight_column=None,
        loss_reduction=losses.Reduction.SUM_OVER_BATCH_SIZE,
        name=None):
    """Creates a `_Head` for logistic regression.

  Uses `sigmoid_cross_entropy_with_logits` loss, which is the same as
  `binary_classification_head`. The differences compared to
  `binary_classification_head` are:

  * Does not support `label_vocabulary`. Instead, labels must be float in the
    range [0, 1].
  * Does not calculate some metrics that do not make sense, such as AUC.
  * In `PREDICT` mode, only returns logits and predictions
    (`=tf.sigmoid(logits)`), whereas `binary_classification_head` also returns
    probabilities, classes, and class_ids.
  * Export output defaults to `RegressionOutput`, whereas
    `binary_classification_head` defaults to `PredictOutput`.

  The head expects `logits` with shape `[D0, D1, ... DN, 1]`.
  In many applications, the shape is `[batch_size, 1]`.

  The `labels` shape must match `logits`, namely
  `[D0, D1, ... DN]` or `[D0, D1, ... DN, 1]`.

  If `weight_column` is specified, weights must be of shape
  `[D0, D1, ... DN]` or `[D0, D1, ... DN, 1]`.

  This is implemented as a generalized linear model, see
  https://en.wikipedia.org/wiki/Generalized_linear_model.

  The head can be used with a canned estimator. Example:

  ```python
  my_head = tf.contrib.estimator.logistic_regression_head()
  my_estimator = tf.contrib.estimator.DNNEstimator(
      head=my_head,
      hidden_units=...,
      feature_columns=...)
  ```

  It can also be used with a custom `model_fn`. Example:

  ```python
  def _my_model_fn(features, labels, mode):
    my_head = tf.contrib.estimator.logistic_regression_head()
    logits = tf.keras.Model(...)(features)

    return my_head.create_estimator_spec(
        features=features,
        mode=mode,
        labels=labels,
        optimizer=tf.AdagradOptimizer(learning_rate=0.1),
        logits=logits)

  my_estimator = tf.estimator.Estimator(model_fn=_my_model_fn)
  ```

  Args:
    weight_column: A string or a `_NumericColumn` created by
      `tf.feature_column.numeric_column` defining feature column representing
      weights. It is used to down weight or boost examples during training. It
      will be multiplied by the loss of the example.
    loss_reduction: One of `tf.losses.Reduction` except `NONE`. Describes how to
      reduce training loss over batch and label dimension. Defaults to
      `SUM_OVER_BATCH_SIZE`, namely weighted sum of losses divided by
      `batch size * label_dimension`. See `tf.losses.Reduction`.
    name: name of the head. If provided, summary and metrics keys will be
      suffixed by `"/" + name`. Also used as `name_scope` when creating ops.

  Returns:
    An instance of `_Head` for logistic regression.

  Raises:
    ValueError: If `loss_reduction` is invalid.
  """
    def _logistic_loss(labels, logits):
        labels = head_lib._assert_range(  # pylint:disable=protected-access
            labels,
            n_classes=2,
            message='Labels must be in range [0, 1]')
        return nn.sigmoid_cross_entropy_with_logits(labels=labels,
                                                    logits=logits)

    return head_lib._regression_head(  # pylint:disable=protected-access
        weight_column=weight_column,
        label_dimension=1,
        loss_reduction=loss_reduction,
        loss_fn=_logistic_loss,
        inverse_link_fn=math_ops.sigmoid,
        name=name)
コード例 #18
0
    def __init__(self,
                 feature_columns,
                 model_dir=None,
                 label_dimension=1,
                 weight_column=None,
                 optimizer='Ftrl',
                 config=None,
                 partitioner=None,
                 warm_start_from=None,
                 loss_reduction=losses.Reduction.SUM,
                 sparse_combiner='sum'):
        """Initializes a `LinearRegressor` instance.

    Args:
      feature_columns: An iterable containing all the feature columns used by
        the model. All items in the set should be instances of classes derived
        from `FeatureColumn`.
      model_dir: Directory to save model parameters, graph and etc. This can
        also be used to load checkpoints from the directory into a estimator
        to continue training a previously saved model.
      label_dimension: Number of regression targets per example. This is the
        size of the last dimension of the labels and logits `Tensor` objects
        (typically, these have shape `[batch_size, label_dimension]`).
      weight_column: A string or a `_NumericColumn` created by
        `tf.feature_column.numeric_column` defining feature column representing
        weights. It is used to down weight or boost examples during training. It
        will be multiplied by the loss of the example. If it is a string, it is
        used as a key to fetch weight tensor from the `features`. If it is a
        `_NumericColumn`, raw tensor is fetched by key `weight_column.key`,
        then weight_column.normalizer_fn is applied on it to get weight tensor.
      optimizer: An instance of `tf.Optimizer` or
        `tf.estimator.experimental.LinearSDCA` used to train the model. Can
        also be a string (one of 'Adagrad', 'Adam', 'Ftrl', 'RMSProp', 'SGD'),
        or callable. Defaults to FTRL optimizer.
      config: `RunConfig` object to configure the runtime settings.
      partitioner: Optional. Partitioner for input layer.
      warm_start_from: A string filepath to a checkpoint to warm-start from, or
        a `WarmStartSettings` object to fully configure warm-starting.  If the
        string filepath is provided instead of a `WarmStartSettings`, then all
        weights and biases are warm-started, and it is assumed that vocabularies
        and Tensor names are unchanged.
      loss_reduction: One of `tf.losses.Reduction` except `NONE`. Describes how
        to reduce training loss over batch. Defaults to `SUM`.
      sparse_combiner: A string specifying how to reduce if a categorical column
        is multivalent.  One of "mean", "sqrtn", and "sum" -- these are
        effectively different ways to do example-level normalization, which can
        be useful for bag-of-words features. for more details, see
        `tf.feature_column.linear_model`.
    """
        if isinstance(optimizer, LinearSDCA):
            if sparse_combiner != 'sum':
                raise ValueError(
                    'sparse_combiner must be "sum" when optimizer '
                    'is a LinearSDCA object.')
            if not feature_column_v2.is_feature_column_v2(feature_columns):
                raise ValueError('V2 feature columns required when optimizer '
                                 'is a LinearSDCA object.')
            if label_dimension > 1:
                raise ValueError(
                    'LinearSDCA can only be used with one-dimensional '
                    'label.')

        head = head_lib._regression_head(  # pylint: disable=protected-access
            label_dimension=label_dimension,
            weight_column=weight_column,
            loss_reduction=loss_reduction)

        def _model_fn(features, labels, mode, config):
            """Call the defined shared _linear_model_fn."""
            return _linear_model_fn(features=features,
                                    labels=labels,
                                    mode=mode,
                                    head=head,
                                    feature_columns=tuple(feature_columns
                                                          or []),
                                    optimizer=optimizer,
                                    partitioner=partitioner,
                                    config=config,
                                    sparse_combiner=sparse_combiner)

        super(LinearRegressor, self).__init__(model_fn=_model_fn,
                                              model_dir=model_dir,
                                              config=config,
                                              warm_start_from=warm_start_from)