Esempio n. 1
0
    def update_state(self, context: ClassifierContext) -> None:
        """
        Update the internal state of the metric, using the information from the context object.

        Args:
            context (:py:class:`ashpy.contexts.ClassifierContext`): An AshPy Context holding
                all the information the Metric needs.

        """
        for features, labels in context.dataset:
            predictions = context.classifier_model(
                features, training=context.log_eval_mode == LogEvalMode.TRAIN)

            self._distribute_strategy.experimental_run(
                lambda: self._metric.update_state(
                    labels,
                    self._processing_predictions["fn"]
                    (predictions, **self._processing_predictions["kwargs"]),
                ))
Esempio n. 2
0
    def call(self, context: ClassifierContext, *, features: tf.Tensor,
             labels: tf.Tensor, training: bool, **kwargs) -> tf.Tensor:
        r"""
        Compute the classifier loss.

        Args:
            context (:py:class:`ashpy.ClassifierContext`): Context for classification.
            features (:py:class:`tf.Tensor`): Inputs for the classifier model.
            labels (:py:class:`tf.Tensor`): Target for the classifier model.
            training (bool): Whether is training or not.
            **kwargs:

        Returns:
            :py:class:`tf.Tensor`: Loss value.

        """
        predictions = context.classifier_model(features, training=training)
        loss = self._fn(labels, predictions)
        loss = tf.cond(
            tf.equal(tf.rank(loss), tf.constant(4)),
            lambda: loss,
            lambda: tf.expand_dims(tf.expand_dims(loss, axis=-1), axis=-1),
        )
        return tf.reduce_mean(loss, axis=[1, 2])