def _create_tpu_estimator_spec(self,
                                   features,
                                   mode,
                                   logits,
                                   labels=None,
                                   optimizer=None,
                                   trainable_variables=None,
                                   train_op_fn=None,
                                   update_ops=None,
                                   regularization_losses=None):
        """See superclass for description."""

        with tf.compat.v1.name_scope(self._name, 'head'):
            # Predict.
            pred_keys = prediction_keys.PredictionKeys
            predictions = self.predictions(logits)
            if mode == ModeKeys.PREDICT:
                probabilities = predictions[pred_keys.PROBABILITIES]
                logistic = predictions[pred_keys.LOGISTIC]
                classifier_output = base_head.classification_output(
                    scores=probabilities,
                    n_classes=2,
                    label_vocabulary=self._label_vocabulary)
                return model_fn._TPUEstimatorSpec(  # pylint: disable=protected-access
                    mode=ModeKeys.PREDICT,
                    predictions=predictions,
                    export_outputs={
                        base_head.DEFAULT_SERVING_KEY: classifier_output,
                        base_head.CLASSIFY_SERVING_KEY: classifier_output,
                        base_head.REGRESS_SERVING_KEY:
                            export_output.RegressionOutput(value=logistic),
                        base_head.PREDICT_SERVING_KEY:
                            export_output.PredictOutput(predictions)
                    })
            regularized_training_loss = self.loss(
                logits=logits,
                labels=labels,
                features=features,
                mode=mode,
                regularization_losses=regularization_losses)
            scalar_loss = tf.reduce_mean(regularized_training_loss)
            # Eval.
            if mode == ModeKeys.EVAL:
                eval_metrics = self.metrics(
                    regularization_losses=regularization_losses)
                return model_fn._TPUEstimatorSpec(  # pylint: disable=protected-access
                    mode=ModeKeys.EVAL,
                    predictions=predictions,
                    loss=scalar_loss,
                    eval_metrics=base_head.create_eval_metrics_tuple(
                        self.update_metrics, {
                            'eval_metrics': eval_metrics,
                            'features': features,
                            'logits': logits,
                            'labels': labels,
                            'regularization_losses': regularization_losses
                        }))
            # Train.
            train_op = base_head.create_estimator_spec_train_op(
                head_name=self._name,
                optimizer=optimizer,
                train_op_fn=train_op_fn,
                update_ops=update_ops,
                trainable_variables=trainable_variables,
                regularized_training_loss=regularized_training_loss,
                loss_reduction=self._loss_reduction)
        # Create summary.
        base_head.create_estimator_spec_summary(
            regularized_training_loss=scalar_loss,
            regularization_losses=regularization_losses,
            summary_key_fn=self._summary_key)
        return model_fn._TPUEstimatorSpec(  # pylint: disable=protected-access
            mode=ModeKeys.TRAIN,
            predictions=predictions,
            loss=scalar_loss,
            train_op=train_op)
示例#2
0
    def _create_tpu_estimator_spec(self,
                                   features,
                                   mode,
                                   logits,
                                   labels=None,
                                   optimizer=None,
                                   trainable_variables=None,
                                   train_op_fn=None,
                                   update_ops=None,
                                   regularization_losses=None):
        """Returns an `model_fn._TPUEstimatorSpec`.

    Args:
      features: Input `dict` of `Tensor` or `SparseTensor` objects.
      mode: Estimator's `ModeKeys`.
      logits: logits `Tensor` with shape `[D0, D1, ... DN, n_classes]`.
        For many applications, the shape is `[batch_size, n_classes]`.
      labels: Labels with shape matching `logits`. Can be multi-hot `Tensor`
        with shape `[D0, D1, ... DN, n_classes]` or `SparseTensor` with
        `dense_shape` `[D0, D1, ... DN, ?]`. `labels` is required argument when
        `mode` equals `TRAIN` or `EVAL`.
      optimizer: An `tf.keras.optimizers.Optimizer` instance to optimize the
         loss in TRAIN mode. Namely, sets
        `train_op = optimizer.get_updates(loss, trainable_variables)`,
        which updates variables to minimize `loss`.able_variables)`,
        which updates variables to minimize `loss`.
      trainable_variables: A list or tuple of `Variable` objects to update to
        minimize `loss`. In Tensorflow 1.x, by default these are the list of
        variables collected in the graph under the key
        `GraphKeys.TRAINABLE_VARIABLES`. As Tensorflow 2.x doesn't have
        collections and GraphKeys, trainable_variables need to be passed
        explicitly here.
      train_op_fn: Function that takes a scalar loss `Tensor` and returns
        `train_op`. Used if `optimizer` is `None`.
      update_ops: A list or tuple of update ops to be run at training time. For
        example, layers such as BatchNormalization create mean and variance
        update ops that need to be run at training time. In Tensorflow 1.x,
        these are thrown into an UPDATE_OPS collection. As Tensorflow 2.x
        doesn't have collections, update_ops need to be passed explicitly here.
      regularization_losses: A list of additional scalar losses to be added to
        the training loss, such as regularization losses. These losses are
        usually expressed as a batch average, so for best results users need to
        set `loss_reduction=SUM_OVER_BATCH_SIZE` when creating the head to
        avoid scaling errors.
    Returns:
      `model_fn._TPUEstimatorSpec`.
    Raises:
      ValueError: If both `train_op_fn` and `optimizer` are `None` in TRAIN
        mode, or if both are set.
    """
        with ops.name_scope(self._name, 'head'):
            # Predict.
            pred_keys = prediction_keys.PredictionKeys
            predictions = self.predictions(logits)
            if mode == ModeKeys.PREDICT:
                probabilities = predictions[pred_keys.PROBABILITIES]
                classifier_output = base_head.classification_output(
                    scores=probabilities,
                    n_classes=self._n_classes,
                    label_vocabulary=self._label_vocabulary)
                return model_fn._TPUEstimatorSpec(  # pylint:disable=protected-access
                    mode=ModeKeys.PREDICT,
                    predictions=predictions,
                    export_outputs={
                        base_head.DEFAULT_SERVING_KEY: classifier_output,
                        base_head.CLASSIFY_SERVING_KEY: classifier_output,
                        base_head.PREDICT_SERVING_KEY: (
                            export_output.PredictOutput(predictions))
                    })

            regularized_training_loss = self.loss(
                logits=logits,
                labels=labels,
                features=features,
                mode=mode,
                regularization_losses=regularization_losses)
            # Eval.
            if mode == ModeKeys.EVAL:
                eval_metrics = self.metrics(
                    regularization_losses=regularization_losses)
                return model_fn._TPUEstimatorSpec(  # pylint:disable=protected-access
                    mode=ModeKeys.EVAL,
                    predictions=predictions,
                    loss=regularized_training_loss,
                    eval_metrics=base_head.create_eval_metrics_tuple(
                        self.update_metrics, {
                            'eval_metrics': eval_metrics,
                            'features': features,
                            'logits': logits,
                            'labels': labels,
                            'regularization_losses': regularization_losses
                        }))
            # Train.
            train_op = base_head.create_estimator_spec_train_op(
                head_name=self._name,
                optimizer=optimizer,
                train_op_fn=train_op_fn,
                update_ops=update_ops,
                trainable_variables=trainable_variables,
                regularized_training_loss=regularized_training_loss,
                loss_reduction=self._loss_reduction)
        # Create summary.
        base_head.create_estimator_spec_summary(
            regularized_training_loss=regularized_training_loss,
            regularization_losses=regularization_losses,
            summary_key_fn=self._summary_key)
        return model_fn._TPUEstimatorSpec(  # pylint: disable=protected-access
            mode=ModeKeys.TRAIN,
            predictions=predictions,
            loss=regularized_training_loss,
            train_op=train_op)
示例#3
0
    def _create_tpu_estimator_spec(self,
                                   features,
                                   mode,
                                   logits,
                                   labels=None,
                                   optimizer=None,
                                   train_op_fn=None,
                                   regularization_losses=None):
        """Returns an `model_fn._TPUEstimatorSpec`.

    Args:
      features: Input `dict` of `Tensor` or `SparseTensor` objects.
      mode: Estimator's `ModeKeys`.
      logits: logits `Tensor` with shape `[D0, D1, ... DN, n_classes]`.
        For many applications, the shape is `[batch_size, n_classes]`.
      labels: Labels with shape matching `logits`. Can be multi-hot `Tensor`
        with shape `[D0, D1, ... DN, n_classes]` or `SparseTensor` with
        `dense_shape` `[D0, D1, ... DN, ?]`. `labels` is required argument when
        `mode` equals `TRAIN` or `EVAL`.
      optimizer: `Optimizer` instance to optimize the loss in TRAIN mode.
        Namely, sets `train_op = optimizer.minimize(loss, global_step)`, which
        updates variables and increments `global_step`.
      train_op_fn: Function that takes a scalar loss `Tensor` and returns
        `train_op`. Used if `optimizer` is `None`.
      regularization_losses: A list of additional scalar losses to be added to
        the training loss, such as regularization losses. These losses are
        usually expressed as a batch average, so for best results users need to
        set `loss_reduction=SUM_OVER_BATCH_SIZE` or
        `loss_reduction=SUM_OVER_NONZERO_WEIGHTS` when creating the head to
        avoid scaling errors.
    Returns:
      `model_fn._TPUEstimatorSpec`.
    Raises:
      ValueError: If both `train_op_fn` and `optimizer` are `None` in TRAIN
        mode, or if both are set.
    """
        with ops.name_scope(self._name, 'head'):
            # Predict.
            pred_keys = prediction_keys.PredictionKeys
            predictions = self.predictions(logits)
            if mode == model_fn.ModeKeys.PREDICT:
                probabilities = predictions[pred_keys.PROBABILITIES]
                classifier_output = base_head.classification_output(
                    scores=probabilities,
                    n_classes=self._n_classes,
                    label_vocabulary=self._label_vocabulary)
                return model_fn._TPUEstimatorSpec(  # pylint:disable=protected-access
                    mode=model_fn.ModeKeys.PREDICT,
                    predictions=predictions,
                    export_outputs={
                        base_head.DEFAULT_SERVING_KEY: classifier_output,
                        base_head.CLASSIFY_SERVING_KEY: classifier_output,
                        base_head.PREDICT_SERVING_KEY: (
                            export_output.PredictOutput(predictions))
                    })

            regularized_training_loss = self.loss(
                logits=logits,
                labels=labels,
                features=features,
                mode=mode,
                regularization_losses=regularization_losses)
            # Eval.
            if mode == model_fn.ModeKeys.EVAL:
                eval_metrics = self.metrics(
                    regularization_losses=regularization_losses)
                return model_fn._TPUEstimatorSpec(  # pylint:disable=protected-access
                    mode=model_fn.ModeKeys.EVAL,
                    predictions=predictions,
                    loss=regularized_training_loss,
                    eval_metrics=base_head.create_eval_metrics_tuple(
                        self.update_metrics, {
                            'eval_metrics': eval_metrics,
                            'features': features,
                            'logits': logits,
                            'labels': labels,
                            'regularization_losses': regularization_losses
                        }))
            # Train.
            train_op = base_head.create_estimator_spec_train_op(
                self._name, optimizer, train_op_fn, regularized_training_loss)
        # Create summary.
        base_head.create_estimator_spec_summary(regularized_training_loss,
                                                regularization_losses,
                                                self._summary_key)
        return model_fn._TPUEstimatorSpec(  # pylint: disable=protected-access
            mode=model_fn.ModeKeys.TRAIN,
            predictions=predictions,
            loss=regularized_training_loss,
            train_op=train_op)