computes kullbackleibler divergence metric between ytrue and ypred formula python metric ytrue logytrue ypred args name optional string name of the metric instance dtype optional data type of the metric result standalone usage m keras metrics kldivergence m updatestate0 1 0 0 0 6 0 4 0 4 0 6 m result 0 45814306 m resetstate m updatestate0 1 0 0 0 6 0 4 0 4 0 6 sampleweight1 0 m result 0 9162892 usage with compile api python model compileoptimizer sgd loss mse metricskeras metrics kldivergence computes the poisson metric between ytrue and ypred formula python metric ypred ytrue logypred args name optional string name of the metric instance dtype optional data type of the metric result examples standalone usage m keras metrics poisson m updatestate0 1 0 0 1 1 0 0 m result 0 49999997 m resetstate m updatestate0 1 0 0 1 1 0 0 sampleweight1 0 m result 0 99999994 usage with compile api python model compileoptimizer sgd loss mse metricskeras metrics poisson computes the crossentropy metric between the labels and predictions this is the crossentropy metric class to be used when there are only two label classes 0 and 1 args name optional string name of the metric instance dtype optional data type of the metric result fromlogits optional whether output is expected to be a logits tensor by default we consider that output encodes a probability distribution labelsmoothing optional float in 0 1 when 0 label values are smoothed meaning the confidence on label values are relaxed e g labelsmoothing0 2 means that we will use a value of 0 1 for label 0 and 0 9 for label 1 examples standalone usage m keras metrics binarycrossentropy m updatestate0 1 0 0 0 6 0 4 0 4 0 6 m result 0 81492424 m resetstate m updatestate0 1 0 0 0 6 0 4 0 4 0 6 sampleweight1 0 m result 0 9162905 usage with compile api python model compile optimizer sgd loss mse metricskeras metrics binarycrossentropy metric should be minimized during optimization computes the crossentropy metric between the labels and predictions this is the crossentropy metric class to be used when there are multiple label classes 2 or more it assumes that labels are onehot encoded e g when labels values are 2 0 1 then ytrue is 0 0 1 1 0 0 0 1 0 args name optional string name of the metric instance dtype optional data type of the metric result fromlogits optional whether output is expected to be a logits tensor by default we consider that output encodes a probability distribution labelsmoothing optional float in 0 1 when 0 label values are smoothed meaning the confidence on label values are relaxed e g labelsmoothing0 2 means that we will use a value of 0 1 for label 0 and 0 9 for label 1 axis optional defaults to 1 the dimension along which entropy is computed examples standalone usage epsilon 1e7 y ytrue y ypred y clipops clipbyvalueoutput epsilon 1 epsilon y 0 05 0 95 epsilon 0 1 0 8 0 1 xent sumy logy axis 1 log 0 95 log 0 1 0 051 2 302 reduced xent 0 051 2 302 2 m keras metrics categoricalcrossentropy m updatestate0 1 0 0 0 1 0 05 0 95 0 0 1 0 8 0 1 m result 1 1769392 m resetstate m updatestate0 1 0 0 0 1 0 05 0 95 0 0 1 0 8 0 1 sampleweightnp array0 3 0 7 m result 1 6271976 usage with compile api python model compile optimizer sgd loss mse metricskeras metrics categoricalcrossentropy metric should be minimized during optimization computes the crossentropy metric between the labels and predictions use this crossentropy metric when there are two or more label classes it expects labels to be provided as integers if you want to provide labels that are onehot encoded please use the categoricalcrossentropy metric instead there should be numclasses floating point values per feature for ypred and a single floating point value per feature for ytrue args name optional string name of the metric instance dtype optional data type of the metric result fromlogits optional whether output is expected to be a logits tensor by default we consider that output encodes a probability distribution axis optional defaults to 1 the dimension along which entropy is computed examples standalone usage ytrue onehotytrue 0 1 0 0 0 1 logits logypred softmax explogits sumexplogits axis1 softmax 0 05 0 95 epsilon 0 1 0 8 0 1 xent sumy logsoftmax 1 logsoftmax 2 9957 0 0513 16 1181 2 3026 0 2231 2 3026 ytrue logsoftmax 0 0 0513 0 0 0 2 3026 xent 0 0513 2 3026 reduced xent 0 0513 2 3026 2 m keras metrics sparsecategoricalcrossentropy m updatestate1 2 0 05 0 95 0 0 1 0 8 0 1 m result 1 1769392 m resetstate m updatestate1 2 0 05 0 95 0 0 1 0 8 0 1 sampleweightnp array0 3 0 7 m result 1 6271976 usage with compile api python model compile optimizer sgd loss mse metricskeras metrics sparsecategoricalcrossentropy metric should be minimized during optimization computes kullback leibler divergence metric between y_true and y_pred formula python metric y_true log y_true y_pred args name optional string name of the metric instance dtype optional data type of the metric result standalone usage m keras metrics kldivergence m update_state 0 1 0 0 0 6 0 4 0 4 0 6 m result 0 45814306 m reset_state m update_state 0 1 0 0 0 6 0 4 0 4 0 6 sample_weight 1 0 m result 0 9162892 usage with compile api python model compile optimizer sgd loss mse metrics keras metrics kldivergence computes the poisson metric between y_true and y_pred formula python metric y_pred y_true log y_pred args name optional string name of the metric instance dtype optional data type of the metric result examples standalone usage m keras metrics poisson m update_state 0 1 0 0 1 1 0 0 m result 0 49999997 m reset_state m update_state 0 1 0 0 1 1 0 0 sample_weight 1 0 m result 0 99999994 usage with compile api python model compile optimizer sgd loss mse metrics keras metrics poisson computes the crossentropy metric between the labels and predictions this is the crossentropy metric class to be used when there are only two label classes 0 and 1 args name optional string name of the metric instance dtype optional data type of the metric result from_logits optional whether output is expected to be a logits tensor by default we consider that output encodes a probability distribution label_smoothing optional float in 0 1 when 0 label values are smoothed meaning the confidence on label values are relaxed e g label_smoothing 0 2 means that we will use a value of 0 1 for label 0 and 0 9 for label 1 examples standalone usage m keras metrics binarycrossentropy m update_state 0 1 0 0 0 6 0 4 0 4 0 6 m result 0 81492424 m reset_state m update_state 0 1 0 0 0 6 0 4 0 4 0 6 sample_weight 1 0 m result 0 9162905 usage with compile api python model compile optimizer sgd loss mse metrics keras metrics binarycrossentropy metric should be minimized during optimization computes the crossentropy metric between the labels and predictions this is the crossentropy metric class to be used when there are multiple label classes 2 or more it assumes that labels are one hot encoded e g when labels values are 2 0 1 then y_true is 0 0 1 1 0 0 0 1 0 args name optional string name of the metric instance dtype optional data type of the metric result from_logits optional whether output is expected to be a logits tensor by default we consider that output encodes a probability distribution label_smoothing optional float in 0 1 when 0 label values are smoothed meaning the confidence on label values are relaxed e g label_smoothing 0 2 means that we will use a value of 0 1 for label 0 and 0 9 for label 1 axis optional defaults to 1 the dimension along which entropy is computed examples standalone usage epsilon 1e 7 y y_true y y_pred y clip_ops clip_by_value output epsilon 1 epsilon y 0 05 0 95 epsilon 0 1 0 8 0 1 xent sum y log y axis 1 log 0 95 log 0 1 0 051 2 302 reduced xent 0 051 2 302 2 m keras metrics categoricalcrossentropy m update_state 0 1 0 0 0 1 0 05 0 95 0 0 1 0 8 0 1 m result 1 1769392 m reset_state m update_state 0 1 0 0 0 1 0 05 0 95 0 0 1 0 8 0 1 sample_weight np array 0 3 0 7 m result 1 6271976 usage with compile api python model compile optimizer sgd loss mse metrics keras metrics categoricalcrossentropy metric should be minimized during optimization computes the crossentropy metric between the labels and predictions use this crossentropy metric when there are two or more label classes it expects labels to be provided as integers if you want to provide labels that are one hot encoded please use the categoricalcrossentropy metric instead there should be num_classes floating point values per feature for y_pred and a single floating point value per feature for y_true args name optional string name of the metric instance dtype optional data type of the metric result from_logits optional whether output is expected to be a logits tensor by default we consider that output encodes a probability distribution axis optional defaults to 1 the dimension along which entropy is computed examples standalone usage y_true one_hot y_true 0 1 0 0 0 1 logits log y_pred softmax exp logits sum exp logits axis 1 softmax 0 05 0 95 epsilon 0 1 0 8 0 1 xent sum y log softmax 1 log softmax 2 9957 0 0513 16 1181 2 3026 0 2231 2 3026 y_true log softmax 0 0 0513 0 0 0 2 3026 xent 0 0513 2 3026 reduced xent 0 0513 2 3026 2 m keras metrics sparsecategoricalcrossentropy m update_state 1 2 0 05 0 95 0 0 1 0 8 0 1 m result 1 1769392 m reset_state m update_state 1 2 0 05 0 95 0 0 1 0 8 0 1 sample_weight np array 0 3 0 7 m result 1 6271976 usage with compile api python model compile optimizer sgd loss mse metrics keras metrics sparsecategoricalcrossentropy metric should be minimized during optimization
from keras.api_export import keras_export
from keras.losses.losses import binary_crossentropy
from keras.losses.losses import categorical_crossentropy
from keras.losses.losses import kl_divergence
from keras.losses.losses import poisson
from keras.losses.losses import sparse_categorical_crossentropy
from keras.metrics import reduction_metrics


@keras_export("keras.metrics.KLDivergence")
class KLDivergence(reduction_metrics.MeanMetricWrapper):
    

    def __init__(self, name="kl_divergence", dtype=None):
        super().__init__(fn=kl_divergence, name=name, dtype=dtype)

    def get_config(self):
        return {"name": self.name, "dtype": self.dtype}


@keras_export("keras.metrics.Poisson")
class Poisson(reduction_metrics.MeanMetricWrapper):
    

    def __init__(self, name="poisson", dtype=None):
        super().__init__(fn=poisson, name=name, dtype=dtype)

    def get_config(self):
        return {"name": self.name, "dtype": self.dtype}


@keras_export("keras.metrics.BinaryCrossentropy")
class BinaryCrossentropy(reduction_metrics.MeanMetricWrapper):
    

    def __init__(
        self,
        name="binary_crossentropy",
        dtype=None,
        from_logits=False,
        label_smoothing=0,
    ):
        super().__init__(
            binary_crossentropy,
            name,
            dtype=dtype,
            from_logits=from_logits,
            label_smoothing=label_smoothing,
        )
        self.from_logits = from_logits
        self.label_smoothing = label_smoothing
        
        self._direction = "down"

    def get_config(self):
        return {
            "name": self.name,
            "dtype": self.dtype,
            "from_logits": self.from_logits,
            "label_smoothing": self.label_smoothing,
        }


@keras_export("keras.metrics.CategoricalCrossentropy")
class CategoricalCrossentropy(reduction_metrics.MeanMetricWrapper):
    

    def __init__(
        self,
        name="categorical_crossentropy",
        dtype=None,
        from_logits=False,
        label_smoothing=0,
        axis=-1,
    ):
        super().__init__(
            categorical_crossentropy,
            name,
            dtype=dtype,
            from_logits=from_logits,
            label_smoothing=label_smoothing,
            axis=axis,
        )
        self.from_logits = from_logits
        self.label_smoothing = label_smoothing
        self.axis = axis
        
        self._direction = "down"

    def get_config(self):
        return {
            "name": self.name,
            "dtype": self.dtype,
            "from_logits": self.from_logits,
            "label_smoothing": self.label_smoothing,
            "axis": self.axis,
        }


@keras_export("keras.metrics.SparseCategoricalCrossentropy")
class SparseCategoricalCrossentropy(reduction_metrics.MeanMetricWrapper):
    

    def __init__(
        self,
        name="sparse_categorical_crossentropy",
        dtype=None,
        from_logits=False,
        axis=-1,
    ):
        super().__init__(
            sparse_categorical_crossentropy,
            name=name,
            dtype=dtype,
            from_logits=from_logits,
            axis=axis,
        )
        self.from_logits = from_logits
        self.axis = axis
        
        self._direction = "down"

    def get_config(self):
        return {
            "name": self.name,
            "dtype": self.dtype,
            "from_logits": self.from_logits,
            "axis": self.axis,
        }