a
    Sic                    @   s  d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlZddl	m
  mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl#m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ e+dG dd de"j,Z-e+d G d!d" d"e"j.Z/e+d#G d$d% d%e"j.Z0e+d&G d'd( d(e"j.Z1e+d)G d*d+ d+e"j.Z2d,Z3e3e2j4_ e+d-G d.d/ d/e"j.Z5e+d0G d1d2 d2e"j.Z6e3e6j4_ G d3d4 d4e"j7Z8e+d5G d6d7 d7e8Z9e+d8G d9d: d:e8Z:e+d;G d<d= d=e8Z;e+d>G d?d@ d@e8Z<e+dAG dBdC dCe"j7Z=e+dDG dEdF dFe"j7Z>G dGdH dHe"j7ej?dIZ@e+dJG dKdL dLe@ZAe+dMG dNdO dOe@ZBe+dPG dQdR dRe@ZCe+dSG dTdU dUe@ZDe+dVG dWdX dXe"j7ZEe+dYG dZd[ d[e"j.ZFe+d\G d]d^ d^e"j.ZGe+d_G d`da dae"j.ZHe+dbG dcdd dde"j.ZIe+deG dfdg dge"j.ZJe+dhG didj dje"j.ZKe+dkG dldm dme"j.ZLe+dnG dodp dpe"j.ZMe+dqG drds dse"j,ZNe+dtG dudv dve"j.ZOe+dwG dxdy dye"j.ZPe+dzG d{d| d|e"j.ZQG d}d~ d~e"j7ZRe+dG dd deRZSe+dG dd deSZTe+dG dd deSZUe+dG dd deSZVe+dG dd deUZWe+dG dd de"j.ZXe+dG dd de"j.ZYe+dG dd de"j.ZZe3eZj4_ dd Z[e+dej\j]j^dddZ_e+dej\j]j^dd Z`e+dej\j]j^dd Zae+dej\j]j^dddZbe+dej\j]j^dddZcdddZddS )zBuilt-in metrics.    N)List)Optional)Tuple)Union)activations)backend)utils)binary_crossentropy)categorical_crossentropy)categorical_hinge)hinge)kullback_leibler_divergence)logcosh)mean_absolute_error)mean_absolute_percentage_error)mean_squared_error)mean_squared_logarithmic_error)poisson)sparse_categorical_crossentropy)squared_hinge)base_metric)losses_utils)metrics_utils)to_list)is_tensor_or_variable)keras_exportzkeras.metrics.MeanRelativeErrorc                       sB   e Zd ZdZejd	 fdd	Zd
 fdd	Z fddZ  Z	S )MeanRelativeErrora  Computes the mean relative error by normalizing with the given values.

    This metric creates two local variables, `total` and `count` that are used
    to compute the mean relative error. This is weighted by `sample_weight`, and
    it is ultimately returned as `mean_relative_error`: an idempotent operation
    that simply divides `total` by `count`.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      normalizer: The normalizer values with same shape as predictions.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.MeanRelativeError(normalizer=[1, 3, 2, 3])
    >>> m.update_state([1, 3, 2, 3], [2, 4, 6, 8])

    >>> # metric = mean(|y_pred - y_true| / normalizer)
    >>> #        = mean([1, 1, 4, 5] / [1, 3, 2, 3]) = mean([1, 1/3, 2, 5/3])
    >>> #        = 5/4 = 1.25
    >>> m.result().numpy()
    1.25

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.MeanRelativeError(normalizer=[1, 3])])
    ```
    Nc                    s(   t  j||d t|| j}|| _d S )Nnamedtype)super__init__tfcast_dtype
normalizer)selfr%   r   r   	__class__ Q/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/metrics/metrics.pyr!   \   s    zMeanRelativeError.__init__c                    s   t || j}t || j}t||g|\\}}}t||\}}t|| j\}| _|j	
|j	 t jt || | j}t j||dS )a  Accumulates metric statistics.

        Args:
          y_true: The ground truth values.
          y_pred: The predicted values.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        sample_weight)r"   r#   r$   r   ,ragged_assert_compatible_and_get_flat_valuesr   squeeze_or_expand_dimensionsremove_squeezable_dimensionsr%   shapeassert_is_compatible_withmathdivide_no_nanabsr    update_state)r&   y_truey_predr,   relative_errorsr'   r)   r*   r5   b   s,    
zMeanRelativeError.update_statec                    sF   | j }dt|rt|n|i}t  }tt| t|  S )Nr%   )	r%   r   r   evalr    
get_configdictlistitems)r&   nconfigbase_configr'   r)   r*   r:      s
    
zMeanRelativeError.get_config)NN)N)
__name__
__module____qualname____doc__dtensor_utilsinject_meshr!   r5   r:   __classcell__r)   r)   r'   r*   r   6   s
   $%r   zkeras.metrics.Accuracyc                       s(   e Zd ZdZejd fdd	Z  ZS )Accuracya9  Calculates how often predictions equal labels.

    This metric creates two local variables, `total` and `count` that are used
    to compute the frequency with which `y_pred` matches `y_true`. This
    frequency is ultimately returned as `binary accuracy`: an idempotent
    operation that simply divides `total` by `count`.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.Accuracy()
    >>> m.update_state([[1], [2], [3], [4]], [[0], [2], [3], [4]])
    >>> m.result().numpy()
    0.75

    >>> m.reset_state()
    >>> m.update_state([[1], [2], [3], [4]], [[0], [2], [3], [4]],
    ...                sample_weight=[1, 1, 0, 0])
    >>> m.result().numpy()
    0.5

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.Accuracy()])
    ```
    accuracyNc                    s   t  jt||d d S Nr   )r    r!   rI   r&   r   r   r'   r)   r*   r!      s    zAccuracy.__init__)rI   NrA   rB   rC   rD   rE   rF   r!   rG   r)   r)   r'   r*   rH      s   $rH   zkeras.metrics.BinaryAccuracyc                       s(   e Zd ZdZejd fdd	Z  ZS )BinaryAccuracya  Calculates how often predictions match binary labels.

    This metric creates two local variables, `total` and `count` that are used
    to compute the frequency with which `y_pred` matches `y_true`. This
    frequency is ultimately returned as `binary accuracy`: an idempotent
    operation that simply divides `total` by `count`.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
      threshold: (Optional) Float representing the threshold for deciding
      whether prediction values are 1 or 0.

    Standalone usage:

    >>> m = tf.keras.metrics.BinaryAccuracy()
    >>> m.update_state([[1], [1], [0], [0]], [[0.98], [1], [0], [0.6]])
    >>> m.result().numpy()
    0.75

    >>> m.reset_state()
    >>> m.update_state([[1], [1], [0], [0]], [[0.98], [1], [0], [0.6]],
    ...                sample_weight=[1, 0, 0, 1])
    >>> m.result().numpy()
    0.5

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.BinaryAccuracy()])
    ```
    binary_accuracyN      ?c                    s   t  jtj|||d d S )N)r   	threshold)r    r!   r   binary_matches)r&   r   r   rQ   r'   r)   r*   r!      s    
zBinaryAccuracy.__init__)rO   NrP   rM   r)   r)   r'   r*   rN      s   &rN   z!keras.metrics.CategoricalAccuracyc                       s(   e Zd ZdZejd fdd	Z  ZS )CategoricalAccuracya  Calculates how often predictions match one-hot labels.

    You can provide logits of classes as `y_pred`, since argmax of
    logits and probabilities are same.

    This metric creates two local variables, `total` and `count` that are used
    to compute the frequency with which `y_pred` matches `y_true`. This
    frequency is ultimately returned as `categorical accuracy`: an idempotent
    operation that simply divides `total` by `count`.

    `y_pred` and `y_true` should be passed in as vectors of probabilities,
    rather than as labels. If necessary, use `tf.one_hot` to expand `y_true` as
    a vector.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.CategoricalAccuracy()
    >>> m.update_state([[0, 0, 1], [0, 1, 0]], [[0.1, 0.9, 0.8],
    ...                 [0.05, 0.95, 0]])
    >>> m.result().numpy()
    0.5

    >>> m.reset_state()
    >>> m.update_state([[0, 0, 1], [0, 1, 0]], [[0.1, 0.9, 0.8],
    ...                 [0.05, 0.95, 0]],
    ...                sample_weight=[0.7, 0.3])
    >>> m.result().numpy()
    0.3

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.CategoricalAccuracy()])
    ```
    categorical_accuracyNc                    s   t  jdd ||d d S )Nc                 S   s   t tjj| dd|S Naxisr   sparse_categorical_matchesr"   r2   argmaxr6   r7   r)   r)   r*   <lambda>  s   z.CategoricalAccuracy.__init__.<locals>.<lambda>rK   r    r!   rL   r'   r)   r*   r!     s
    zCategoricalAccuracy.__init__)rT   NrM   r)   r)   r'   r*   rS      s   .rS   z'keras.metrics.SparseCategoricalAccuracyc                       s(   e Zd ZdZejd fdd	Z  ZS )SparseCategoricalAccuracya7  Calculates how often predictions match integer labels.

    ```python
    acc = np.dot(sample_weight, np.equal(y_true, np.argmax(y_pred, axis=1))
    ```

    You can provide logits of classes as `y_pred`, since argmax of
    logits and probabilities are same.

    This metric creates two local variables, `total` and `count` that are used
    to compute the frequency with which `y_pred` matches `y_true`. This
    frequency is ultimately returned as `sparse categorical accuracy`: an
    idempotent operation that simply divides `total` by `count`.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.SparseCategoricalAccuracy()
    >>> m.update_state([[2], [1]], [[0.1, 0.6, 0.3], [0.05, 0.95, 0]])
    >>> m.result().numpy()
    0.5

    >>> m.reset_state()
    >>> m.update_state([[2], [1]], [[0.1, 0.6, 0.3], [0.05, 0.95, 0]],
    ...                sample_weight=[0.7, 0.3])
    >>> m.result().numpy()
    0.3

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
    ```
    sparse_categorical_accuracyNc                    s   t  jtj||d d S rJ   )r    r!   r   rZ   rL   r'   r)   r*   r!   S  s    z"SparseCategoricalAccuracy.__init__)r`   NrM   r)   r)   r'   r*   r_   %  s   ,r_   a  Accumulates metric statistics.

For sparse categorical metrics, the shapes of `y_true` and `y_pred` are
different.

Args:
  y_true: Ground truth label values. shape = `[batch_size, d0, .. dN-1]` or
    shape = `[batch_size, d0, .. dN-1, 1]`.
  y_pred: The predicted probability values. shape = `[batch_size, d0, .. dN]`.
  sample_weight: Optional `sample_weight` acts as a
    coefficient for the metric. If a scalar is provided, then the metric is
    simply scaled by the given value. If `sample_weight` is a tensor of size
    `[batch_size]`, then the metric for each sample of the batch is rescaled
    by the corresponding element in the `sample_weight` vector. If the shape
    of `sample_weight` is `[batch_size, d0, .. dN-1]` (or can be broadcasted
    to this shape), then each metric element of `y_pred` is scaled by the
    corresponding value of `sample_weight`. (Note on `dN-1`: all metric
    functions reduce by 1 dimension, usually the last axis (-1)).

Returns:
  Update op.
z%keras.metrics.TopKCategoricalAccuracyc                       s(   e Zd ZdZejd fdd	Z  ZS )TopKCategoricalAccuracya  Computes how often targets are in the top `K` predictions.

    Args:
      k: (Optional) Number of top elements to look at for computing accuracy.
        Defaults to 5.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.TopKCategoricalAccuracy(k=1)
    >>> m.update_state([[0, 0, 1], [0, 1, 0]],
    ...                [[0.1, 0.9, 0.8], [0.05, 0.95, 0]])
    >>> m.result().numpy()
    0.5

    >>> m.reset_state()
    >>> m.update_state([[0, 0, 1], [0, 1, 0]],
    ...                [[0.1, 0.9, 0.8], [0.05, 0.95, 0]],
    ...                sample_weight=[0.7, 0.3])
    >>> m.result().numpy()
    0.3

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.TopKCategoricalAccuracy()])
    ```
       top_k_categorical_accuracyNc                    s   t  jdd |||d d S )Nc                 S   s   t tjj| dd||S rU   r    sparse_top_k_categorical_matchesr"   r2   r[   )ytZypkr)   r)   r*   r]     s   z2TopKCategoricalAccuracy.__init__.<locals>.<lambda>r   rg   r^   r&   rg   r   r   r'   r)   r*   r!     s    z TopKCategoricalAccuracy.__init__)rb   rc   NrM   r)   r)   r'   r*   ra   v  s    ra   z+keras.metrics.SparseTopKCategoricalAccuracyc                       s(   e Zd ZdZejd fdd	Z  ZS )SparseTopKCategoricalAccuracyaN  Computes how often integer targets are in the top `K` predictions.

    Args:
      k: (Optional) Number of top elements to look at for computing accuracy.
        Defaults to 5.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.SparseTopKCategoricalAccuracy(k=1)
    >>> m.update_state([2, 1], [[0.1, 0.9, 0.8], [0.05, 0.95, 0]])
    >>> m.result().numpy()
    0.5

    >>> m.reset_state()
    >>> m.update_state([2, 1], [[0.1, 0.9, 0.8], [0.05, 0.95, 0]],
    ...                sample_weight=[0.7, 0.3])
    >>> m.result().numpy()
    0.3

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.SparseTopKCategoricalAccuracy()])
    ```
    rb   !sparse_top_k_categorical_accuracyNc                    s   t  jtj|||d d S )Nrh   )r    r!   r   re   ri   r'   r)   r*   r!     s    z&SparseTopKCategoricalAccuracy.__init__)rb   rk   NrM   r)   r)   r'   r*   rj     s    rj   c                       sH   e Zd ZdZd fdd	ZdddZdd Zd	d
 Z fddZ  Z	S )_ConfusionMatrixConditionCountau  Calculates the number of the given confusion matrix condition.

    Args:
      confusion_matrix_cond: One of `metrics_utils.ConfusionMatrix` conditions.
      thresholds: (Optional) Defaults to 0.5. A float value or a python
        list/tuple of float threshold values in [0, 1]. A threshold is compared
        with prediction values to determine the truth value of predictions
        (i.e., above the threshold is `true`, below is `false`). One metric
        value is generated for each threshold value.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
    Nc                    sX   t  j||d || _|| _tj|dd| _t| j| _| j	dt
| jfdd| _d S )Nr   rP   default_thresholdaccumulatorzerosr0   initializer)r    r!   _confusion_matrix_condinit_thresholdsr   parse_init_thresholds
thresholds is_evenly_distributed_thresholds_thresholds_distributed_evenly
add_weightlenro   )r&   confusion_matrix_condrv   r   r   r'   r)   r*   r!     s    
z'_ConfusionMatrixConditionCount.__init__c                 C   s"   t j| j| ji||| j| j|dS )a  Accumulates the metric statistics.

        Args:
          y_true: The ground truth values.
          y_pred: The predicted values.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        )rv   thresholds_distributed_evenlyr,   )r   !update_confusion_matrix_variablesrs   ro   rv   rx   r&   r6   r7   r,   r)   r)   r*   r5     s    
z+_ConfusionMatrixConditionCount.update_statec                 C   s*   t | jdkr| jd }n| j}t|S N   r   )rz   rv   ro   r"   convert_to_tensorr&   resultr)   r)   r*   r   
  s    z%_ConfusionMatrixConditionCount.resultc                 C   s   t dd | jD  d S )Nc                 S   s    g | ]}|t |j fqS r)   )nprp   r0   as_list.0vr)   r)   r*   
<listcomp>      z>_ConfusionMatrixConditionCount.reset_state.<locals>.<listcomp>)r   batch_set_value	variablesr&   r)   r)   r*   reset_state  s    z*_ConfusionMatrixConditionCount.reset_statec                    s0   d| j i}t  }tt| t|  S )Nrv   )rt   r    r:   r;   r<   r=   r&   r?   r@   r'   r)   r*   r:     s    

z)_ConfusionMatrixConditionCount.get_config)NNN)N)
rA   rB   rC   rD   r!   r5   r   r   r:   rG   r)   r)   r'   r*   rl     s    
rl   zkeras.metrics.FalsePositivesc                       s(   e Zd ZdZejd fdd	Z  ZS )FalsePositivesa  Calculates the number of false positives.

    If `sample_weight` is given, calculates the sum of the weights of
    false positives. This metric creates one local variable, `accumulator`
    that is used to keep track of the number of false positives.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      thresholds: (Optional) Defaults to 0.5. A float value, or a Python
        list/tuple of float threshold values in [0, 1]. A threshold is compared
        with prediction values to determine the truth value of predictions
        (i.e., above the threshold is `true`, below is `false`). If used with a
        loss function that sets `from_logits=True` (i.e. no sigmoid applied to
        predictions), `thresholds` should be set to 0. One metric value is
        generated for each threshold value.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.FalsePositives()
    >>> m.update_state([0, 1, 0, 0], [0, 0, 1, 1])
    >>> m.result().numpy()
    2.0

    >>> m.reset_state()
    >>> m.update_state([0, 1, 0, 0], [0, 0, 1, 1], sample_weight=[0, 0, 1, 0])
    >>> m.result().numpy()
    1.0

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.FalsePositives()])
    ```

    Usage with a loss with `from_logits=True`:

    ```python
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                  metrics=[tf.keras.metrics.FalsePositives(thresholds=0)])
    ```
    Nc                    s   t  jtjj|||d d S N)r{   rv   r   r   )r    r!   r   ConfusionMatrixFALSE_POSITIVESr&   rv   r   r   r'   r)   r*   r!   O  s    zFalsePositives.__init__)NNNrM   r)   r)   r'   r*   r     s   1r   zkeras.metrics.FalseNegativesc                       s(   e Zd ZdZejd fdd	Z  ZS )FalseNegativesa  Calculates the number of false negatives.

    If `sample_weight` is given, calculates the sum of the weights of
    false negatives. This metric creates one local variable, `accumulator`
    that is used to keep track of the number of false negatives.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      thresholds: (Optional) Defaults to 0.5. A float value, or a Python
        list/tuple of float threshold values in [0, 1]. A threshold is compared
        with prediction values to determine the truth value of predictions
        (i.e., above the threshold is `true`, below is `false`). If used with a
        loss function that sets `from_logits=True` (i.e. no sigmoid applied to
        predictions), `thresholds` should be set to 0. One metric value is
        generated for each threshold value.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.FalseNegatives()
    >>> m.update_state([0, 1, 1, 1], [0, 1, 0, 0])
    >>> m.result().numpy()
    2.0

    >>> m.reset_state()
    >>> m.update_state([0, 1, 1, 1], [0, 1, 0, 0], sample_weight=[0, 0, 1, 0])
    >>> m.result().numpy()
    1.0

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.FalseNegatives()])
    ```

    Usage with a loss with `from_logits=True`:

    ```python
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                  metrics=[tf.keras.metrics.FalseNegatives(thresholds=0)])
    ```
    Nc                    s   t  jtjj|||d d S r   )r    r!   r   r   FALSE_NEGATIVESr   r'   r)   r*   r!     s    zFalseNegatives.__init__)NNNrM   r)   r)   r'   r*   r   Y  s   1r   zkeras.metrics.TrueNegativesc                       s(   e Zd ZdZejd fdd	Z  ZS )TrueNegativesa  Calculates the number of true negatives.

    If `sample_weight` is given, calculates the sum of the weights of
    true negatives. This metric creates one local variable, `accumulator`
    that is used to keep track of the number of true negatives.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      thresholds: (Optional) Defaults to 0.5. A float value, or a Python
        list/tuple of float threshold values in [0, 1]. A threshold is compared
        with prediction values to determine the truth value of predictions
        (i.e., above the threshold is `true`, below is `false`). If used with a
        loss function that sets `from_logits=True` (i.e. no sigmoid applied to
        predictions), `thresholds` should be set to 0. One metric value is
        generated for each threshold value.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.TrueNegatives()
    >>> m.update_state([0, 1, 0, 0], [1, 1, 0, 0])
    >>> m.result().numpy()
    2.0

    >>> m.reset_state()
    >>> m.update_state([0, 1, 0, 0], [1, 1, 0, 0], sample_weight=[0, 0, 1, 0])
    >>> m.result().numpy()
    1.0

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.TrueNegatives()])
    ```

    Usage with a loss with `from_logits=True`:

    ```python
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                  metrics=[tf.keras.metrics.TrueNegatives(thresholds=0)])
    ```
    Nc                    s   t  jtjj|||d d S r   )r    r!   r   r   TRUE_NEGATIVESr   r'   r)   r*   r!     s    zTrueNegatives.__init__)NNNrM   r)   r)   r'   r*   r     s   1r   zkeras.metrics.TruePositivesc                       s(   e Zd ZdZejd fdd	Z  ZS )TruePositivesa  Calculates the number of true positives.

    If `sample_weight` is given, calculates the sum of the weights of
    true positives. This metric creates one local variable, `true_positives`
    that is used to keep track of the number of true positives.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      thresholds: (Optional) Defaults to 0.5. A float value, or a Python
        list/tuple of float threshold values in [0, 1]. A threshold is compared
        with prediction values to determine the truth value of predictions
        (i.e., above the threshold is `true`, below is `false`). If used with a
        loss function that sets `from_logits=True` (i.e. no sigmoid applied to
        predictions), `thresholds` should be set to 0. One metric value is
        generated for each threshold value.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.TruePositives()
    >>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
    >>> m.result().numpy()
    2.0

    >>> m.reset_state()
    >>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
    >>> m.result().numpy()
    1.0

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.TruePositives()])
    ```

    Usage with a loss with `from_logits=True`:

    ```python
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                  metrics=[tf.keras.metrics.TruePositives(thresholds=0)])
    ```
    Nc                    s   t  jtjj|||d d S r   )r    r!   r   r   TRUE_POSITIVESr   r'   r)   r*   r!     s    zTruePositives.__init__)NNNrM   r)   r)   r'   r*   r     s   1r   zkeras.metrics.Precisionc                       sN   e Zd ZdZejd fdd	ZdddZdd Zd	d
 Z	 fddZ
  ZS )	Precisiona  Computes the precision of the predictions with respect to the labels.

    The metric creates two local variables, `true_positives` and
    `false_positives` that are used to compute the precision. This value is
    ultimately returned as `precision`, an idempotent operation that simply
    divides `true_positives` by the sum of `true_positives` and
    `false_positives`.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    If `top_k` is set, we'll calculate precision as how often on average a class
    among the top-k classes with the highest predicted values of a batch entry
    is correct and can be found in the label for that entry.

    If `class_id` is specified, we calculate precision by considering only the
    entries in the batch for which `class_id` is above the threshold and/or in
    the top-k highest predictions, and computing the fraction of them for which
    `class_id` is indeed a correct label.

    Args:
      thresholds: (Optional) A float value, or a Python list/tuple of float
        threshold values in [0, 1]. A threshold is compared with prediction
        values to determine the truth value of predictions (i.e., above the
        threshold is `true`, below is `false`). If used with a loss function
        that sets `from_logits=True` (i.e. no sigmoid applied to predictions),
        `thresholds` should be set to 0. One metric value is generated for each
        threshold value. If neither thresholds nor top_k are set, the default is
        to calculate precision with `thresholds=0.5`.
      top_k: (Optional) Unset by default. An int value specifying the top-k
        predictions to consider when calculating precision.
      class_id: (Optional) Integer class ID for which we want binary metrics.
        This must be in the half-open interval `[0, num_classes)`, where
        `num_classes` is the last dimension of predictions.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.Precision()
    >>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
    >>> m.result().numpy()
    0.6666667

    >>> m.reset_state()
    >>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
    >>> m.result().numpy()
    1.0

    >>> # With top_k=2, it will calculate precision over y_true[:2]
    >>> # and y_pred[:2]
    >>> m = tf.keras.metrics.Precision(top_k=2)
    >>> m.update_state([0, 0, 1, 1], [1, 1, 1, 1])
    >>> m.result().numpy()
    0.0

    >>> # With top_k=4, it will calculate precision over y_true[:4]
    >>> # and y_pred[:4]
    >>> m = tf.keras.metrics.Precision(top_k=4)
    >>> m.update_state([0, 0, 1, 1], [1, 1, 1, 1])
    >>> m.result().numpy()
    0.5

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.Precision()])
    ```

    Usage with a loss with `from_logits=True`:

    ```python
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                  metrics=[tf.keras.metrics.Precision(thresholds=0)])
    ```
    Nc                    s   t  j||d || _|| _|| _|d u r.dntj}tj||d| _t	| j| _
| jdt| jfdd| _| jdt| jfdd| _d S )Nr   rP   rm   true_positivesrp   rq   false_positives)r    r!   rt   top_kclass_idr   NEG_INFru   rv   rw   rx   ry   rz   r   r   r&   rv   r   r   r   r   rn   r'   r)   r*   r!   b  s$    

zPrecision.__init__c              
   C   s6   t jt jj| jt jj| ji||| j| j| j	| j
|dS )a  Accumulates true positive and false positive statistics.

        Args:
          y_true: The ground truth values, with the same dimensions as `y_pred`.
            Will be cast to `bool`.
          y_pred: The predicted values. Each element must be in the range
            `[0, 1]`.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        rv   r|   r   r   r,   )r   r}   r   r   r   r   r   rv   rx   r   r   r~   r)   r)   r*   r5   {  s    

zPrecision.update_statec                 C   s8   t j| jt j| j| j}t| jdkr4|d S |S r   )r"   r2   r3   r   addr   rz   rv   r   r)   r)   r*   r     s
    zPrecision.resultc                    s2   t t| j t fdd| j| jfD  d S )Nc                    s   g | ]}|t  ffqS r)   r   rp   r   num_thresholdsr)   r*   r     s   z)Precision.reset_state.<locals>.<listcomp>)rz   r   rv   r   r   r   r   r   r)   r   r*   r     s    

zPrecision.reset_statec                    s8   | j | j| jd}t  }tt| t|  S N)rv   r   r   rt   r   r   r    r:   r;   r<   r=   r   r'   r)   r*   r:     s    
zPrecision.get_config)NNNNN)NrA   rB   rC   rD   rE   rF   r!   r5   r   r   r:   rG   r)   r)   r'   r*   r     s   P 
	r   zkeras.metrics.Recallc                       sN   e Zd ZdZejd fdd	ZdddZdd Zd	d
 Z	 fddZ
  ZS )Recallag
  Computes the recall of the predictions with respect to the labels.

    This metric creates two local variables, `true_positives` and
    `false_negatives`, that are used to compute the recall. This value is
    ultimately returned as `recall`, an idempotent operation that simply divides
    `true_positives` by the sum of `true_positives` and `false_negatives`.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    If `top_k` is set, recall will be computed as how often on average a class
    among the labels of a batch entry is in the top-k predictions.

    If `class_id` is specified, we calculate recall by considering only the
    entries in the batch for which `class_id` is in the label, and computing the
    fraction of them for which `class_id` is above the threshold and/or in the
    top-k predictions.

    Args:
      thresholds: (Optional) A float value, or a Python list/tuple of float
        threshold values in [0, 1]. A threshold is compared with prediction
        values to determine the truth value of predictions (i.e., above the
        threshold is `true`, below is `false`). If used with a loss function
        that sets `from_logits=True` (i.e. no sigmoid applied to predictions),
        `thresholds` should be set to 0. One metric value is generated for each
        threshold value. If neither thresholds nor top_k are set, the default is
        to calculate recall with `thresholds=0.5`.
      top_k: (Optional) Unset by default. An int value specifying the top-k
        predictions to consider when calculating recall.
      class_id: (Optional) Integer class ID for which we want binary metrics.
        This must be in the half-open interval `[0, num_classes)`, where
        `num_classes` is the last dimension of predictions.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.Recall()
    >>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
    >>> m.result().numpy()
    0.6666667

    >>> m.reset_state()
    >>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
    >>> m.result().numpy()
    1.0

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.Recall()])
    ```

    Usage with a loss with `from_logits=True`:

    ```python
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                  metrics=[tf.keras.metrics.Recall(thresholds=0)])
    ```
    Nc                    s   t  j||d || _|| _|| _|d u r.dntj}tj||d| _t	| j| _
| jdt| jfdd| _| jdt| jfdd| _d S )Nr   rP   rm   r   rp   rq   false_negatives)r    r!   rt   r   r   r   r   ru   rv   rw   rx   ry   rz   r   r   r   r'   r)   r*   r!     s$    

zRecall.__init__c              
   C   s6   t jt jj| jt jj| ji||| j| j| j	| j
|dS )a  Accumulates true positive and false negative statistics.

        Args:
          y_true: The ground truth values, with the same dimensions as `y_pred`.
            Will be cast to `bool`.
          y_pred: The predicted values. Each element must be in the range
            `[0, 1]`.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        r   )r   r}   r   r   r   r   r   rv   rx   r   r   r~   r)   r)   r*   r5     s    

zRecall.update_statec                 C   s8   t j| jt j| j| j}t| jdkr4|d S |S r   )r"   r2   r3   r   r   r   rz   rv   r   r)   r)   r*   r   *  s
    zRecall.resultc                    s2   t t| j t fdd| j| jfD  d S )Nc                    s   g | ]}|t  ffqS r)   r   r   r   r)   r*   r   4  s   z&Recall.reset_state.<locals>.<listcomp>)rz   r   rv   r   r   r   r   r   r)   r   r*   r   1  s    

zRecall.reset_statec                    s8   | j | j| jd}t  }tt| t|  S r   r   r   r'   r)   r*   r:   :  s    
zRecall.get_config)NNNNN)Nr   r)   r)   r'   r*   r     s   @ 
	r   c                       sH   e Zd ZdZd fdd	ZdddZdd	 Z fd
dZdd Z  Z	S )SensitivitySpecificityBasezAbstract base class for computing sensitivity and specificity.

    For additional information about specificity and sensitivity, see
    [the following](https://en.wikipedia.org/wiki/Sensitivity_and_specificity).
       Nc                    s   t  j||d  dkr&td  || _|| _| jd fdd| _| jd fdd| _| jd fdd| _| jd	 fdd| _	 d
krdg| _
d| _n2 fddt d D }dg| dg | _
d| _d S )Nr   r   zKArgument `num_thresholds` must be an integer > 0. Received: num_thresholds=r   rp   rq   true_negativesr   r   r   rP   Fc                    s    g | ]}|d  d  d   qS r         ?r)   r   ir   r)   r*   r   h  s   z7SensitivitySpecificityBase.__init__.<locals>.<listcomp>           r   T)r    r!   
ValueErrorvaluer   ry   r   r   r   r   rv   rx   range)r&   r   r   r   r   r   rv   r'   r   r*   r!   K  s:    

z#SensitivitySpecificityBase.__init__c              	   C   sF   t jt jj| jt jj| jt jj| jt jj	| j
i||| j| j| j|dS )  Accumulates confusion matrix statistics.

        Args:
          y_true: The ground truth values.
          y_pred: The predicted values.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        )rv   r|   r   r,   )r   r}   r   r   r   r   r   r   r   r   r   rv   rx   r   r~   r)   r)   r*   r5   o  s    



z'SensitivitySpecificityBase.update_statec                    s:   t | j | j| j| j| jf}t fdd|D  d S )Nc                    s   g | ]}|t  ffqS r)   r   r   r   r)   r*   r     s   z:SensitivitySpecificityBase.reset_state.<locals>.<listcomp>)rz   rv   r   r   r   r   r   r   r&   confusion_matrix_variablesr)   r   r*   r     s    

z&SensitivitySpecificityBase.reset_statec                    s0   d| j i}t  }tt| t|  S )Nr   )r   r    r:   r;   r<   r=   r   r'   r)   r*   r:     s    

z%SensitivitySpecificityBase.get_configc                 C   sD   t ||| j}t t |d}t t ||}t ||dS )a  Returns the maximum of dependent_statistic that satisfies the
        constraint.

        Args:
          constrained: Over these values the constraint
            is specified. A rank-1 tensor.
          dependent: From these values the maximum that satiesfies the
            constraint is selected. Values in this tensor and in
            `constrained` are linked by having the same threshold at each
            position, hence this tensor must have the same shape.
          predicate: A binary boolean functor to be applied to arguments
          `constrained` and `self.value`, e.g. `tf.greater`.

        Returns:
          maximal dependent value, if no value satiesfies the constraint 0.0.
        r   r   )r"   wherer   greatersize
reduce_maxgather)r&   constrained	dependent	predicatefeasiblefeasible_existsmax_dependentr)   r)   r*   _find_max_under_constraint  s    z5SensitivitySpecificityBase._find_max_under_constraint)r   NNN)N)
rA   rB   rC   rD   r!   r5   r   r:   r   rG   r)   r)   r'   r*   r   D  s    $
r   )	metaclassz&keras.metrics.SensitivityAtSpecificityc                       s<   e Zd ZdZejd
 fdd	Zdd Z fdd	Z  Z	S )SensitivityAtSpecificitya  Computes best sensitivity where specificity is >= specified value.

    the sensitivity at a given specificity.

    `Sensitivity` measures the proportion of actual positives that are correctly
    identified as such (tp / (tp + fn)).
    `Specificity` measures the proportion of actual negatives that are correctly
    identified as such (tn / (tn + fp)).

    This metric creates four local variables, `true_positives`,
    `true_negatives`, `false_positives` and `false_negatives` that are used to
    compute the sensitivity at the given specificity. The threshold for the
    given specificity value is computed and used to evaluate the corresponding
    sensitivity.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    If `class_id` is specified, we calculate precision by considering only the
    entries in the batch for which `class_id` is above the threshold
    predictions, and computing the fraction of them for which `class_id` is
    indeed a correct label.

    For additional information about specificity and sensitivity, see
    [the following](https://en.wikipedia.org/wiki/Sensitivity_and_specificity).

    Args:
      specificity: A scalar value in range `[0, 1]`.
      num_thresholds: (Optional) Defaults to 200. The number of thresholds to
        use for matching the given specificity.
      class_id: (Optional) Integer class ID for which we want binary metrics.
        This must be in the half-open interval `[0, num_classes)`, where
        `num_classes` is the last dimension of predictions.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.SensitivityAtSpecificity(0.5)
    >>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8])
    >>> m.result().numpy()
    0.5

    >>> m.reset_state()
    >>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8],
    ...                sample_weight=[1, 1, 2, 2, 1])
    >>> m.result().numpy()
    0.333333

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.SensitivityAtSpecificity()])
    ```
    r   Nc                    sD   |dk s|dkrt d| || _|| _t j|||||d d S )Nr   r   zJArgument `specificity` must be in the range [0, 1]. Received: specificity=r   r   r   r   )r   specificityr   r    r!   )r&   r   r   r   r   r   r'   r)   r*   r!     s    	z!SensitivityAtSpecificity.__init__c                 C   sL   t j| jt j| j| j}t j| jt j| j| j}| ||t j	S N)
r"   r2   r3   r   r   r   r   r   r   greater_equal)r&   specificitiessensitivitiesr)   r)   r*   r     s    zSensitivityAtSpecificity.resultc                    s4   | j | jd}t  }tt| t|  S )N)r   r   )r   r   r    r:   r;   r<   r=   r   r'   r)   r*   r:     s
    
z#SensitivityAtSpecificity.get_config)r   NNN
rA   rB   rC   rD   rE   rF   r!   r   r:   rG   r)   r)   r'   r*   r     s   ;    r   z&keras.metrics.SpecificityAtSensitivityc                       s<   e Zd ZdZejd
 fdd	Zdd Z fdd	Z  Z	S )SpecificityAtSensitivitya  Computes best specificity where sensitivity is >= specified value.

    `Sensitivity` measures the proportion of actual positives that are correctly
    identified as such (tp / (tp + fn)).
    `Specificity` measures the proportion of actual negatives that are correctly
    identified as such (tn / (tn + fp)).

    This metric creates four local variables, `true_positives`,
    `true_negatives`, `false_positives` and `false_negatives` that are used to
    compute the specificity at the given sensitivity. The threshold for the
    given sensitivity value is computed and used to evaluate the corresponding
    specificity.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    If `class_id` is specified, we calculate precision by considering only the
    entries in the batch for which `class_id` is above the threshold
    predictions, and computing the fraction of them for which `class_id` is
    indeed a correct label.

    For additional information about specificity and sensitivity, see
    [the following](https://en.wikipedia.org/wiki/Sensitivity_and_specificity).

    Args:
      sensitivity: A scalar value in range `[0, 1]`.
      num_thresholds: (Optional) Defaults to 200. The number of thresholds to
        use for matching the given sensitivity.
      class_id: (Optional) Integer class ID for which we want binary metrics.
        This must be in the half-open interval `[0, num_classes)`, where
        `num_classes` is the last dimension of predictions.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.SpecificityAtSensitivity(0.5)
    >>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8])
    >>> m.result().numpy()
    0.66666667

    >>> m.reset_state()
    >>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8],
    ...                sample_weight=[1, 1, 2, 2, 2])
    >>> m.result().numpy()
    0.5

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.SpecificityAtSensitivity()])
    ```
    r   Nc                    sD   |dk s|dkrt d| || _|| _t j|||||d d S )Nr   r   zJArgument `sensitivity` must be in the range [0, 1]. Received: sensitivity=r   )r   sensitivityr   r    r!   )r&   r   r   r   r   r   r'   r)   r*   r!   ]  s    	z!SpecificityAtSensitivity.__init__c                 C   sL   t j| jt j| j| j}t j| jt j| j| j}| ||t j	S r   )
r"   r2   r3   r   r   r   r   r   r   r   )r&   r   r   r)   r)   r*   r   u  s    zSpecificityAtSensitivity.resultc                    s4   | j | jd}t  }tt| t|  S )N)r   r   )r   r   r    r:   r;   r<   r=   r   r'   r)   r*   r:     s
    
z#SpecificityAtSensitivity.get_config)r   NNNr   r)   r)   r'   r*   r   "  s   9    r   zkeras.metrics.PrecisionAtRecallc                       s<   e Zd ZdZejd
 fdd	Zdd Z fdd	Z  Z	S )PrecisionAtRecalla  Computes best precision where recall is >= specified value.

    This metric creates four local variables, `true_positives`,
    `true_negatives`, `false_positives` and `false_negatives` that are used to
    compute the precision at the given recall. The threshold for the given
    recall value is computed and used to evaluate the corresponding precision.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    If `class_id` is specified, we calculate precision by considering only the
    entries in the batch for which `class_id` is above the threshold
    predictions, and computing the fraction of them for which `class_id` is
    indeed a correct label.

    Args:
      recall: A scalar value in range `[0, 1]`.
      num_thresholds: (Optional) Defaults to 200. The number of thresholds to
        use for matching the given recall.
      class_id: (Optional) Integer class ID for which we want binary metrics.
        This must be in the half-open interval `[0, num_classes)`, where
        `num_classes` is the last dimension of predictions.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.PrecisionAtRecall(0.5)
    >>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8])
    >>> m.result().numpy()
    0.5

    >>> m.reset_state()
    >>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8],
    ...                sample_weight=[2, 2, 2, 1, 1])
    >>> m.result().numpy()
    0.33333333

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.PrecisionAtRecall(recall=0.8)])
    ```
    r   Nc                    sD   |dk s|dkrt d| || _|| _t j|||||d d S )Nr   r   z@Argument `recall` must be in the range [0, 1]. Received: recall=r   r   r   r   r   )r   recallr   r    r!   )r&   r   r   r   r   r   r'   r)   r*   r!     s    zPrecisionAtRecall.__init__c                 C   sL   t j| jt j| j| j}t j| jt j| j| j}| ||t jS r   )	r"   r2   r3   r   r   r   r   r   r   )r&   recalls
precisionsr)   r)   r*   r     s    zPrecisionAtRecall.resultc                    s4   | j | jd}t  }tt| t|  S )N)r   r   )r   r   r    r:   r;   r<   r=   r   r'   r)   r*   r:     s    
zPrecisionAtRecall.get_config)r   NNNr   r)   r)   r'   r*   r     s   0 r   zkeras.metrics.RecallAtPrecisionc                       s<   e Zd ZdZejd
 fdd	Zdd Z fdd	Z  Z	S )RecallAtPrecisiona`  Computes best recall where precision is >= specified value.

    For a given score-label-distribution the required precision might not
    be achievable, in this case 0.0 is returned as recall.

    This metric creates four local variables, `true_positives`,
    `true_negatives`, `false_positives` and `false_negatives` that are used to
    compute the recall at the given precision. The threshold for the given
    precision value is computed and used to evaluate the corresponding recall.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    If `class_id` is specified, we calculate precision by considering only the
    entries in the batch for which `class_id` is above the threshold
    predictions, and computing the fraction of them for which `class_id` is
    indeed a correct label.

    Args:
      precision: A scalar value in range `[0, 1]`.
      num_thresholds: (Optional) Defaults to 200. The number of thresholds to
        use for matching the given precision.
      class_id: (Optional) Integer class ID for which we want binary metrics.
        This must be in the half-open interval `[0, num_classes)`, where
        `num_classes` is the last dimension of predictions.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.RecallAtPrecision(0.8)
    >>> m.update_state([0, 0, 1, 1], [0, 0.5, 0.3, 0.9])
    >>> m.result().numpy()
    0.5

    >>> m.reset_state()
    >>> m.update_state([0, 0, 1, 1], [0, 0.5, 0.3, 0.9],
    ...                sample_weight=[1, 0, 0, 1])
    >>> m.result().numpy()
    1.0

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.RecallAtPrecision(precision=0.8)])
    ```
    r   Nc                    sD   |dk s|dkrt d| || _|| _t j|||||d d S )Nr   r   zFArgument `precision` must be in the range [0, 1]. Received: precision=r   )r   	precisionr   r    r!   )r&   r   r   r   r   r   r'   r)   r*   r!     s    	zRecallAtPrecision.__init__c                 C   sL   t j| jt j| j| j}t j| jt j| j| j}| ||t jS r   )	r"   r2   r3   r   r   r   r   r   r   )r&   r   r   r)   r)   r*   r   0  s    zRecallAtPrecision.resultc                    s4   | j | jd}t  }tt| t|  S )N)r   r   )r   r   r    r:   r;   r<   r=   r   r'   r)   r*   r:   =  s
    
zRecallAtPrecision.get_config)r   NNNr   r)   r)   r'   r*   r     s   3    r   zkeras.metrics.AUCc                       sj   e Zd ZdZejd fdd	Zed	d
 Zdd Z	dddZ
dd Zdd Zdd Z fddZ  ZS )AUCa  Approximates the AUC (Area under the curve) of the ROC or PR curves.

    The AUC (Area under the curve) of the ROC (Receiver operating
    characteristic; default) or PR (Precision Recall) curves are quality
    measures of binary classifiers. Unlike the accuracy, and like cross-entropy
    losses, ROC-AUC and PR-AUC evaluate all the operational points of a model.

    This class approximates AUCs using a Riemann sum. During the metric
    accumulation phrase, predictions are accumulated within predefined buckets
    by value. The AUC is then computed by interpolating per-bucket averages.
    These buckets define the evaluated operational points.

    This metric creates four local variables, `true_positives`,
    `true_negatives`, `false_positives` and `false_negatives` that are used to
    compute the AUC.  To discretize the AUC curve, a linearly spaced set of
    thresholds is used to compute pairs of recall and precision values. The area
    under the ROC-curve is therefore computed using the height of the recall
    values by the false positive rate, while the area under the PR-curve is the
    computed using the height of the precision values by the recall.

    This value is ultimately returned as `auc`, an idempotent operation that
    computes the area under a discretized curve of precision versus recall
    values (computed using the aforementioned variables). The `num_thresholds`
    variable controls the degree of discretization with larger numbers of
    thresholds more closely approximating the true AUC. The quality of the
    approximation may vary dramatically depending on `num_thresholds`. The
    `thresholds` parameter can be used to manually specify thresholds which
    split the predictions more evenly.

    For a best approximation of the real AUC, `predictions` should be
    distributed approximately uniformly in the range [0, 1] (if
    `from_logits=False`). The quality of the AUC approximation may be poor if
    this is not the case. Setting `summation_method` to 'minoring' or 'majoring'
    can help quantify the error in the approximation by providing lower or upper
    bound estimate of the AUC.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      num_thresholds: (Optional) Defaults to 200. The number of thresholds to
        use when discretizing the roc curve. Values must be > 1.
      curve: (Optional) Specifies the name of the curve to be computed, 'ROC'
        [default] or 'PR' for the Precision-Recall-curve.
      summation_method: (Optional) Specifies the [Riemann summation method](
          https://en.wikipedia.org/wiki/Riemann_sum) used.
          'interpolation' (default) applies mid-point summation scheme for
          `ROC`.  For PR-AUC, interpolates (true/false) positives but not the
          ratio that is precision (see Davis & Goadrich 2006 for details);
          'minoring' applies left summation for increasing intervals and right
          summation for decreasing intervals; 'majoring' does the opposite.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
      thresholds: (Optional) A list of floating point values to use as the
        thresholds for discretizing the curve. If set, the `num_thresholds`
        parameter is ignored. Values should be in [0, 1]. Endpoint thresholds
        equal to {-epsilon, 1+epsilon} for a small positive epsilon value will
        be automatically included with these to correctly handle predictions
        equal to exactly 0 or 1.
      multi_label: boolean indicating whether multilabel data should be
        treated as such, wherein AUC is computed separately for each label and
        then averaged across labels, or (when False) if the data should be
        flattened into a single label before AUC computation. In the latter
        case, when multilabel data is passed to AUC, each label-prediction pair
        is treated as an individual data point. Should be set to False for
        multi-class data.
      num_labels: (Optional) The number of labels, used when `multi_label` is
        True. If `num_labels` is not specified, then state variables get created
        on the first call to `update_state`.
      label_weights: (Optional) list, array, or tensor of non-negative weights
        used to compute AUCs for multilabel data. When `multi_label` is True,
        the weights are applied to the individual label AUCs when they are
        averaged to produce the multi-label AUC. When it's False, they are used
        to weight the individual label predictions in computing the confusion
        matrix on the flattened data. Note that this is unlike class_weights in
        that class_weights weights the example depending on the value of its
        label, whereas label_weights depends only on the index of that label
        before flattening; therefore `label_weights` should not be used for
        multi-class data.
      from_logits: boolean indicating whether the predictions (`y_pred` in
        `update_state`) are probabilities or sigmoid logits. As a rule of thumb,
        when using a keras loss, the `from_logits` constructor argument of the
        loss should match the AUC `from_logits` constructor argument.

    Standalone usage:

    >>> m = tf.keras.metrics.AUC(num_thresholds=3)
    >>> m.update_state([0, 0, 1, 1], [0, 0.5, 0.3, 0.9])
    >>> # threshold values are [0 - 1e-7, 0.5, 1 + 1e-7]
    >>> # tp = [2, 1, 0], fp = [2, 0, 0], fn = [0, 1, 2], tn = [0, 2, 2]
    >>> # tp_rate = recall = [1, 0.5, 0], fp_rate = [1, 0, 0]
    >>> # auc = ((((1+0.5)/2)*(1-0)) + (((0.5+0)/2)*(0-0))) = 0.75
    >>> m.result().numpy()
    0.75

    >>> m.reset_state()
    >>> m.update_state([0, 0, 1, 1], [0, 0.5, 0.3, 0.9],
    ...                sample_weight=[1, 0, 0, 1])
    >>> m.result().numpy()
    1.0

    Usage with `compile()` API:

    ```python
    # Reports the AUC of a model outputting a probability.
    model.compile(optimizer='sgd',
                  loss=tf.keras.losses.BinaryCrossentropy(),
                  metrics=[tf.keras.metrics.AUC()])

    # Reports the AUC of a model outputting a logit.
    model.compile(optimizer='sgd',
                  loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                  metrics=[tf.keras.metrics.AUC(from_logits=True)])
    ```
    r   ROCinterpolationNFc                    s   t |tjr4|ttjvr4td| dttj t |tjrh|ttjvrhtd| dttj |d u| _|d urt|d | _t	|}t
tdg| dg | _n< dkrtd   | _ fd	d
t d D }d| _tdt  g| dt  g | _t |tjr*|| _ntj|| _t |tjrN|| _ntj|| _t j||d || _|| _|	d urtj|	| jd}	tjj|	dd |	| _nd | _|
| _d| _ | jr|rt!d |g}| "| n|rtd| "d  d S )Nz Invalid `curve` argument value "z". Expected one of: z+Invalid `summation_method` argument value "r   r   r   r   zKArgument `num_thresholds` must be an integer > 1. Received: num_thresholds=c                    s    g | ]}|d  d  d   qS r   r)   r   r   r)   r*   r     s   z AUC.__init__.<locals>.<listcomp>Tr   rK   z3All values of `label_weights` must be non-negative.messageFz7`num_labels` is needed only when `multi_label` is True.)#
isinstancer   AUCCurver<   r   AUCSummationMethod_init_from_thresholdsrz   r   sortedrw   r   arrayrx   r   r   epsilon_thresholdscurvefrom_strsummation_methodr    r!   multi_label
num_labelsr"   constantr   	debuggingassert_non_negativelabel_weights_from_logits_builtTensorShape_build)r&   r   r   r   r   r   rv   r   r   r   from_logitsr0   r'   r   r*   r!     s    



zAUC.__init__c                 C   s
   t | jS )z'The thresholds used for evaluating AUC.)r<   r   r   r)   r)   r*   rv   $  s    zAUC.thresholdsc                 C   s   | j rD|jdkr&td|j d| |d | _t| j| jg}nt| jg}|| _| jd|dd| _	| jd|dd| _
| jd	|dd| _| jd
|dd| _| j rt & t stt  W d   n1 s0    Y  d| _dS )zKInitialize TP, FP, TN, and FN tensors, given the shape of the
        data.r   z>`y_true` must have rank 2 when `multi_label=True`. Found rank z$. Full shape received for `y_true`: r   r   rp   rq   r   r   r   NT)r   ndimsr   _num_labelsr"   r   r   _build_input_shapery   r   r   r   r   
init_scopeexecuting_eagerlyr   _initialize_variables_get_sessionr   )r&   r0   variable_shaper)   r)   r*   r   )  s@    



,z
AUC._buildc              
   C   s   | j s| t|j | js(| jdur|dfg}| jrb|| jdf| j	df| j
df| jdfg | jdur|| jdf tjj|dd | jrdn| j}| jrt|}tjtjj| jtjj| j	tjj| j
tjj| ji||| j| j|| j|dS )r   N)NL)Tr   )r   z#Number of labels is not consistent.r   )r|   r,   r   r   )r   r   r"   r   r0   r   r   extendr   r   r   r   appendr   assert_shapesr   r   sigmoidr   r}   r   r   r   r   r   r   rx   )r&   r6   r7   r,   shapesr   r)   r)   r*   r5   S  sD    






zAUC.update_statec           	   
   C   s  | j d| jd  | j dd  }tj| j | j}|d| jd  |dd  }tjj|t|ddd}| j dd t||dd  }t	t
|d| jd  dk|dd dktjj|d| jd  t|dd dddt|dd }tjj|||tj|   t| j dd | jdd  ddd}| jrtj|| jd dd	}| jdu r|tj|| jdS tjjtt|| jt| j| jdS ntj|d
dS dS )ak  Interpolation formula inspired by section 4 of Davis & Goadrich 2006.

        https://www.biostat.wisc.edu/~page/rocpr.pdf

        Note here we derive & use a closed formula not present in the paper
        as follows:

          Precision = TP / (TP + FP) = TP / P

        Modeling all of TP (true positive), FP (false positive) and their sum
        P = TP + FP (predicted positive) as varying linearly within each
        interval [A, B] between successive thresholds, we get

          Precision slope = dTP / dP
                          = (TP_B - TP_A) / (P_B - P_A)
                          = (TP - TP_A) / (P - P_A)
          Precision = (TP_A + slope * (P - P_A)) / P

        The area within the interval is (slope / total_pos_weight) times

          int_A^B{Precision.dP} = int_A^B{(TP_A + slope * (P - P_A)) * dP / P}
          int_A^B{Precision.dP} = int_A^B{slope * dP + intercept * dP / P}

        where intercept = TP_A - slope * P_A = TP_B - slope * P_B, resulting in

          int_A^B{Precision.dP} = TP_B - TP_A + intercept * log(P_B / P_A)

        Bringing back the factor (slope / total_pos_weight) we'd put aside, we
        get

          slope * [dTP + intercept *  log(P_B / P_A)] / total_pos_weight

        where dTP == TP_B - TP_A.

        Note that when P_A == 0 the above calculation simplifies into

          int_A^B{Precision.dTP} = int_A^B{slope * dTP} = slope * (TP_B - TP_A)

        which is really equivalent to imputing constant precision throughout the
        first bucket having >0 true positives.

        Returns:
          pr_auc: an approximation of the area under the P-R curve.
        Nr   r   
prec_sloper   recall_relative_ratiopr_auc_increment	_by_labelr   rX   interpolate_pr_auc)r   r   r"   r2   r   r   r3   maximummultiplyr   logical_and	ones_likelogr   r   
reduce_sumr   r   reduce_mean)	r&   dtppdpr  	interceptsafe_p_ratior  by_label_aucr)   r)   r*   r
    sL    ."(
"
zAUC.interpolate_pr_aucc           	      C   s  | j tjjkr$| jtjjkr$|  S tj	
| jtj	| j| j}| j tjjkrxtj	
| jtj	| j| j}|}|}n&tj	
| jtj	| j| j}|}|}| jtjjkr|d | jd  |dd   d }nV| jtjjkrt|d | jd  |dd  }n"t|d | jd  |dd  }| jrt|d | jd  |dd   |}tj|| jd dd}| jd u rtj|| jdS tj	j
tt|| jt| j| jdS n2tjt|d | jd  |dd   || jdS d S )Nr   g       @r  r   r	  r  )r   r   r   PRr   r   INTERPOLATIONr
  r"   r2   r3   r   r   r   r   r   r   r   MINORINGminimumr  r   r  r  r   r   r  )	r&   r   fp_ratexyr   heightsriemann_termsr  r)   r)   r*   r     sh    $$"
$z
AUC.resultc                    sV    j rR j j j jf} jr:t fdd|D  nt fdd|D  d S )Nc                    s"   g | ]}|t  j jffqS r)   )r   rp   r   r   r   r   r)   r*   r   ;  s   z#AUC.reset_state.<locals>.<listcomp>c                    s   g | ]}|t  jffqS r)   )r   rp   r   r   r   r)   r*   r   B  s   )r   r   r   r   r   r   r   r   r   r)   r   r*   r   1  s"    

zAUC.reset_statec                    s   t | jrt| j}n| j}| j| jj| jj| j| j	|| j
d}| jrZ| jdd |d< t  }tt| t|  S )N)r   r   r   r   r   r   r   r   rV   rv   )r   r   r   r9   r   r   r   r   r   r   r   r   rv   r    r:   r;   r<   r=   )r&   r   r?   r@   r'   r)   r*   r:   H  s    

zAUC.get_config)
r   r   r   NNNFNNF)N)rA   rB   rC   rD   rE   rF   r!   propertyrv   r   r5   r
  r   r   r:   rG   r)   r)   r'   r*   r   F  s*   t          g
*
>[Er   zkeras.metrics.CosineSimilarityc                       s(   e Zd ZdZejd fdd	Z  ZS )CosineSimilaritya  Computes the cosine similarity between the labels and predictions.

    `cosine similarity = (a . b) / ||a|| ||b||`

    See: [Cosine Similarity](https://en.wikipedia.org/wiki/Cosine_similarity).

    This metric keeps the average cosine similarity between `predictions` and
    `labels` over a stream of data.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
      axis: (Optional) Defaults to -1. The dimension along which the cosine
        similarity is computed.

    Standalone usage:

    >>> # l2_norm(y_true) = [[0., 1.], [1./1.414, 1./1.414]]
    >>> # l2_norm(y_pred) = [[1., 0.], [1./1.414, 1./1.414]]
    >>> # l2_norm(y_true) . l2_norm(y_pred) = [[0., 0.], [0.5, 0.5]]
    >>> # result = mean(sum(l2_norm(y_true) . l2_norm(y_pred), axis=1))
    >>> #        = ((0. + 0.) +  (0.5 + 0.5)) / 2
    >>> m = tf.keras.metrics.CosineSimilarity(axis=1)
    >>> m.update_state([[0., 1.], [1., 1.]], [[1., 0.], [1., 1.]])
    >>> m.result().numpy()
    0.49999997

    >>> m.reset_state()
    >>> m.update_state([[0., 1.], [1., 1.]], [[1., 0.], [1., 1.]],
    ...                sample_weight=[0.3, 0.7])
    >>> m.result().numpy()
    0.6999999

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.CosineSimilarity(axis=1)])
    ```
    cosine_similarityNrV   c                    s   t  jt|||d d S )N)r   rX   )r    r!   r#  )r&   r   r   rX   r'   r)   r*   r!     s    zCosineSimilarity.__init__)r#  NrV   rM   r)   r)   r'   r*   r"  a  s   +r"  zkeras.metrics.MeanAbsoluteErrorc                       s(   e Zd ZdZejd fdd	Z  ZS )MeanAbsoluteErrora  Computes the mean absolute error between the labels and predictions.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.MeanAbsoluteError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.25

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.5

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.MeanAbsoluteError()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!     s    zMeanAbsoluteError.__init__)r   NrM   r)   r)   r'   r*   r$    s   r$  z)keras.metrics.MeanAbsolutePercentageErrorc                       s(   e Zd ZdZejd fdd	Z  ZS )MeanAbsolutePercentageErrora  Computes the mean absolute percentage error between `y_true` and
    `y_pred`.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.MeanAbsolutePercentageError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    250000000.0

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    500000000.0

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.MeanAbsolutePercentageError()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!     s    z$MeanAbsolutePercentageError.__init__)r   NrM   r)   r)   r'   r*   r%    s   r%  zkeras.metrics.MeanSquaredErrorc                       s(   e Zd ZdZejd fdd	Z  ZS )MeanSquaredErrora  Computes the mean squared error between `y_true` and `y_pred`.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.MeanSquaredError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.25

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.5

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.MeanSquaredError()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!     s    zMeanSquaredError.__init__)r   NrM   r)   r)   r'   r*   r&    s   r&  z)keras.metrics.MeanSquaredLogarithmicErrorc                       s(   e Zd ZdZejd fdd	Z  ZS )MeanSquaredLogarithmicErrora  Computes the mean squared logarithmic error between `y_true` and
    `y_pred`.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.MeanSquaredLogarithmicError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.12011322

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.24022643

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.MeanSquaredLogarithmicError()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!    	  s    z$MeanSquaredLogarithmicError.__init__)r   NrM   r)   r)   r'   r*   r'   	  s   r'  zkeras.metrics.Hingec                       s(   e Zd ZdZejd fdd	Z  ZS )Hingea  Computes the hinge metric between `y_true` and `y_pred`.

    `y_true` values are expected to be -1 or 1. If binary (0 or 1) labels are
    provided we will convert them to -1 or 1.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.Hinge()
    >>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
    >>> m.result().numpy()
    1.3

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    1.1

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd', loss='mse', metrics=[tf.keras.metrics.Hinge()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!   E	  s    zHinge.__init__)r   NrM   r)   r)   r'   r*   r(  %	  s   r(  zkeras.metrics.SquaredHingec                       s(   e Zd ZdZejd fdd	Z  ZS )SquaredHingeaC  Computes the squared hinge metric between `y_true` and `y_pred`.

    `y_true` values are expected to be -1 or 1. If binary (0 or 1) labels are
    provided we will convert them to -1 or 1.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.SquaredHinge()
    >>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
    >>> m.result().numpy()
    1.86

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    1.46

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.SquaredHinge()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!   l	  s    zSquaredHinge.__init__)r   NrM   r)   r)   r'   r*   r)  J	  s    r)  zkeras.metrics.CategoricalHingec                       s(   e Zd ZdZejd fdd	Z  ZS )CategoricalHingea  Computes the categorical hinge metric between `y_true` and `y_pred`.

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.CategoricalHinge()
    >>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
    >>> m.result().numpy()
    1.4000001

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    1.2

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.CategoricalHinge()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!   	  s    zCategoricalHinge.__init__)r   NrM   r)   r)   r'   r*   r*  q	  s   r*  z"keras.metrics.RootMeanSquaredErrorc                       s>   e Zd ZdZejd
 fdd	Zd fdd	Zdd	 Z  Z	S )RootMeanSquaredErroraS  Computes root mean squared error metric between `y_true` and `y_pred`.

    Standalone usage:

    >>> m = tf.keras.metrics.RootMeanSquaredError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.5

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.70710677

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.RootMeanSquaredError()])
    ```
    root_mean_squared_errorNc                    s   t  j||d d S rJ   r^   rL   r'   r)   r*   r!   	  s    zRootMeanSquaredError.__init__c                    sJ   t || j}t || j}t||\}}t j||}t j||dS )a  Accumulates root mean squared error statistics.

        Args:
          y_true: The ground truth values.
          y_pred: The predicted values.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        r+   )	r"   r#   r$   r   r.   r2   squared_differencer    r5   )r&   r6   r7   r,   error_sqr'   r)   r*   r5   	  s    z!RootMeanSquaredError.update_statec                 C   s   t t j| j| jS r   )r"   sqrtr2   r3   totalcountr   r)   r)   r*   r   	  s    zRootMeanSquaredError.result)r,  N)N)
rA   rB   rC   rD   rE   rF   r!   r5   r   rG   r)   r)   r'   r*   r+  	  s
   r+  zkeras.metrics.LogCoshErrorc                       s(   e Zd ZdZejd fdd	Z  ZS )LogCoshErrora,  Computes the logarithm of the hyperbolic cosine of the prediction error.

    `logcosh = log((exp(x) + exp(-x))/2)`, where x is the error (y_pred -
    y_true)

    Args:
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.LogCoshError()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.10844523

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.21689045

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.LogCoshError()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!   	  s    zLogCoshError.__init__)r   NrM   r)   r)   r'   r*   r2  	  s   r2  zkeras.metrics.Poissonc                       s(   e Zd ZdZejd fdd	Z  ZS )Poissona  Computes the Poisson metric between `y_true` and `y_pred`.

    `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.

    Standalone usage:

    >>> m = tf.keras.metrics.Poisson()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
    >>> m.result().numpy()
    0.49999997

    >>> m.reset_state()
    >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
    ...                sample_weight=[1, 0])
    >>> m.result().numpy()
    0.99999994

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.Poisson()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!   
  s    zPoisson.__init__)r   NrM   r)   r)   r'   r*   r3  	  s   r3  zkeras.metrics.KLDivergencec                       s(   e Zd ZdZejd fdd	Z  ZS )KLDivergencea  Computes Kullback-Leibler divergence metric between `y_true` and
    `y_pred`.

    `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 = tf.keras.metrics.KLDivergence()
    >>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
    >>> m.result().numpy()
    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().numpy()
    0.9162892

    Usage with `compile()` API:

    ```python
    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=[tf.keras.metrics.KLDivergence()])
    ```
    r   Nc                    s   t  jt||d d S rJ   )r    r!   r   rL   r'   r)   r*   r!   9
  s    zKLDivergence.__init__)r   NrM   r)   r)   r'   r*   r4  
  s   r4  c                	       s^   e Zd ZdZdeee eeeej	j
f  ee eeed fddZddd	Zd
d Z  ZS )_IoUBasea7  Computes the confusion matrix for Intersection-Over-Union metrics.

    Intersection-Over-Union is a common evaluation metric for semantic image
    segmentation.

    For an individual class, the IoU metric is defined as follows:

    ```
    iou = true_positives / (true_positives + false_positives + false_negatives)
    ```

    From IoUs of individual classes, the MeanIoU can be computed as the mean of
    the individual IoUs.

    To compute IoUs, the predictions are accumulated in a confusion matrix,
    weighted by `sample_weight` and the metric is then calculated from it.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Args:
      num_classes: The possible number of labels the prediction task can have.
        This value must be provided, since a confusion matrix of size
        `(num_classes, num_classes)` will be allocated.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
      ignore_class: Optional integer. The ID of a class to be ignored during
        metric computation. This is useful, for example, in segmentation
        problems featuring a "void" class (commonly -1 or 255) in segmentation
        maps. By default (`ignore_class=None`), all classes are considered.
      sparse_y_true: Whether labels are encoded using integers or
        dense floating point vectors. If `False`, the `tf.argmax` function
        will be used to determine each sample's most likely associated label.
      sparse_y_pred: Whether predictions are encoded using integers or
        dense floating point vectors. If `False`, the `tf.argmax` function
        will be used to determine each sample's most likely associated label.
      axis: (Optional) Defaults to -1. The dimension containing the logits.
    NTrV   num_classesr   r   ignore_classsparse_y_truesparse_y_predrX   c                    sH   t  j||d || _|| _|| _|| _|| _| jd||fdd| _d S )Nr   total_confusion_matrixrp   rq   )	r    r!   r7  r8  r9  r:  rX   ry   total_cm)r&   r7  r   r   r8  r9  r:  rX   r'   r)   r*   r!   f
  s    
z_IoUBase.__init__c                 C   s  | j stj|| jd}| js,tj|| jd}t|| j}t|| j}|jjdkrbt	|dg}|jjdkr|t	|dg}|durt|| j}|jjdkrt	|dg}| j
durt| j
|j}t||}|| }|| }|dur|| }tjj||| j|| jd}| j|S )a  Accumulates the confusion matrix statistics.

        Args:
          y_true: The ground truth values.
          y_pred: The predicted values.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        rW   r   rV   N)weightsr   )r9  r"   r[   rX   r:  r#   r$   r0   r   reshaper8  r   	not_equalr2   confusion_matrixr7  r<  
assign_add)r&   r6   r7   r,   r8  
valid_mask
current_cmr)   r)   r*   r5   ~
  s:    
z_IoUBase.update_statec                 C   s    t | jt| j| jf d S r   )r   	set_valuer<  r   rp   r7  r   r)   r)   r*   r   
  s    z_IoUBase.reset_state)NNNTTrV   )N)rA   rB   rC   rD   intr   strr   r"   dtypesDTypeboolr!   r5   r   rG   r)   r)   r'   r*   r5  >
  s$   *      
4r5  zkeras.metrics.IoUc                       s|   e Zd ZdZejdeeee e	edf f e
e e
eeejjf  e
e eeed fddZd	d
 Z fddZ  ZS )IoUa  Computes the Intersection-Over-Union metric for specific target classes.

    General definition and computation:

    Intersection-Over-Union is a common evaluation metric for semantic image
    segmentation.

    For an individual class, the IoU metric is defined as follows:

    ```
    iou = true_positives / (true_positives + false_positives + false_negatives)
    ```

    To compute IoUs, the predictions are accumulated in a confusion matrix,
    weighted by `sample_weight` and the metric is then calculated from it.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Note, this class first computes IoUs for all individual classes, then
    returns the mean of IoUs for the classes that are specified by
    `target_class_ids`. If `target_class_ids` has only one id value, the IoU of
    that specific class is returned.

    Args:
      num_classes: The possible number of labels the prediction task can have.
        A confusion matrix of dimension = [num_classes, num_classes] will be
        allocated to accumulate predictions from which the metric is calculated.
      target_class_ids: A tuple or list of target class ids for which the metric
        is returned. To compute IoU for a specific class, a list (or tuple) of a
        single id value should be provided.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
      ignore_class: Optional integer. The ID of a class to be ignored during
        metric computation. This is useful, for example, in segmentation
        problems featuring a "void" class (commonly -1 or 255) in segmentation
        maps. By default (`ignore_class=None`), all classes are considered.
      sparse_y_true: Whether labels are encoded using integers or
        dense floating point vectors. If `False`, the `tf.argmax` function
        will be used to determine each sample's most likely associated label.
      sparse_y_pred: Whether predictions are encoded using integers or
        dense floating point vectors. If `False`, the `tf.argmax` function
        will be used to determine each sample's most likely associated label.
      axis: (Optional) Defaults to -1. The dimension containing the logits.

    Standalone usage:

    >>> # cm = [[1, 1],
    >>> #        [1, 1]]
    >>> # sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1]
    >>> # iou = true_positives / (sum_row + sum_col - true_positives))
    >>> # iou = [0.33, 0.33]
    >>> m = tf.keras.metrics.IoU(num_classes=2, target_class_ids=[0])
    >>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1])
    >>> m.result().numpy()
    0.33333334

    >>> m.reset_state()
    >>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1],
    ...                sample_weight=[0.3, 0.3, 0.3, 0.1])
    >>> # cm = [[0.3, 0.3],
    >>> #        [0.3, 0.1]]
    >>> # sum_row = [0.6, 0.4], sum_col = [0.6, 0.4],
    >>> # true_positives = [0.3, 0.1]
    >>> # iou = [0.33, 0.14]
    >>> m.result().numpy()
    0.33333334

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.IoU(num_classes=2, target_class_ids=[0])])
    ```
    NTrV   .r7  target_class_idsr   r   r8  r9  r:  rX   c	           	   	      sT   t  j|||||||d t||krFtdt| dd d| dt|| _d S )N)r   r7  r8  r9  r:  rX   r   zTarget class id z is out of range, which is [r   z, z).)r    r!   maxr   r<   rL  )	r&   r7  rL  r   r   r8  r9  r:  rX   r'   r)   r*   r!     s$    	zIoU.__init__c                 C   s   t jt j| jdd| jd}t jt j| jdd| jd}t jt j| j| jd}|| | }t || j}t || j}t t jt 	|d| jd}t j
||}t j
t j|dd|S )z=Compute the intersection-over-union via the confusion matrix.r   rW   rK   r   mean_iour  )r"   r#   r  r<  r$   linalgtensor_diag_partr   rL  r?  r2   r3   )r&   sum_over_rowsum_over_colr   denominatornum_valid_entriesiour)   r)   r*   r   %  s&    z
IoU.resultc                    sD   | j | j| j| j| j| jd}t  }tt	|
 t	|
  S )N)r7  rL  r8  r9  r:  rX   )r7  rL  r8  r9  r:  rX   r    r:   r;   r<   r=   r   r'   r)   r*   r:   D  s    
zIoU.get_config)NNNTTrV   )rA   rB   rC   rD   rE   rF   rE  r   r   r   r   rF  r"   rG  rH  rI  r!   r   r:   rG   r)   r)   r'   r*   rJ  
  s(   N      rJ  zkeras.metrics.BinaryIoUc                       sX   e Zd ZdZejdeee e	edf f d fddZ
d fd	d
	Zdd Z  ZS )	BinaryIoUa+
  Computes the Intersection-Over-Union metric for class 0 and/or 1.

    General definition and computation:

    Intersection-Over-Union is a common evaluation metric for semantic image
    segmentation.

    For an individual class, the IoU metric is defined as follows:

    ```
    iou = true_positives / (true_positives + false_positives + false_negatives)
    ```

    To compute IoUs, the predictions are accumulated in a confusion matrix,
    weighted by `sample_weight` and the metric is then calculated from it.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    This class can be used to compute IoUs for a binary classification task
    where the predictions are provided as logits. First a `threshold` is applied
    to the predicted values such that those that are below the `threshold` are
    converted to class 0 and those that are above the `threshold` are converted
    to class 1.

    IoUs for classes 0 and 1 are then computed, the mean of IoUs for the classes
    that are specified by `target_class_ids` is returned.

    Note: with `threshold=0`, this metric has the same behavior as `IoU`.

    Args:
      target_class_ids: A tuple or list of target class ids for which the metric
        is returned. Options are `[0]`, `[1]`, or `[0, 1]`. With `[0]` (or
        `[1]`), the IoU metric for class 0 (or class 1, respectively) is
        returned. With `[0, 1]`, the mean of IoUs for the two classes is
        returned.
      threshold: A threshold that applies to the prediction logits to convert
        them to either predicted class 0 if the logit is below `threshold` or
        predicted class 1 if the logit is above `threshold`.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.

    Standalone usage:

    >>> m = tf.keras.metrics.BinaryIoU(target_class_ids=[0, 1], threshold=0.3)
    >>> m.update_state([0, 1, 0, 1], [0.1, 0.2, 0.4, 0.7])
    >>> m.result().numpy()
    0.33333334

    >>> m.reset_state()
    >>> m.update_state([0, 1, 0, 1], [0.1, 0.2, 0.4, 0.7],
    ...                sample_weight=[0.2, 0.3, 0.4, 0.1])
    >>> # cm = [[0.2, 0.4],
    >>> #        [0.3, 0.1]]
    >>> # sum_row = [0.6, 0.4], sum_col = [0.5, 0.5],
    >>> # true_positives = [0.2, 0.1]
    >>> # iou = [0.222, 0.125]
    >>> m.result().numpy()
    0.17361112

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.BinaryIoU(target_class_ids=[0], threshold=0.5)])
    ```
    r   r   rP   N.)rL  c                    s   t  jd|||d || _d S )Nr   )r7  rL  r   r   )r    r!   rQ   )r&   rL  rQ   r   r   r'   r)   r*   r!     s    	zBinaryIoU.__init__c                    s2   t || j}t || jk| j}t |||S )av  Accumulates the confusion matrix statistics.

        Before the confusion matrix is updated, the predicted values are
        thresholded to be:
          0 for values that are smaller than the `threshold`
          1 for values that are larger or equal to the `threshold`

        Args:
          y_true: The ground truth values.
          y_pred: The predicted values.
          sample_weight: Optional weighting of each example. Defaults to 1. Can
            be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns:
          Update op.
        )r"   r#   r$   rQ   r    r5   r~   r'   r)   r*   r5     s    zBinaryIoU.update_statec                 C   s   | j | j| j| jdS )N)rL  rQ   r   r   )rL  rQ   r   r$   r   r)   r)   r*   r:     s
    zBinaryIoU.get_config)rW  rP   NN)N)rA   rB   rC   rD   rE   rF   r   r   rE  r   r!   r5   r:   rG   r)   r)   r'   r*   rV  Q  s   F    rV  zkeras.metrics.MeanIoUc                
       sZ   e Zd ZdZejd
eee ee	ee
jjf  ee eeed fddZdd	 Z  ZS )MeanIoUaf
  Computes the mean Intersection-Over-Union metric.

    General definition and computation:

    Intersection-Over-Union is a common evaluation metric for semantic image
    segmentation.

    For an individual class, the IoU metric is defined as follows:

    ```
    iou = true_positives / (true_positives + false_positives + false_negatives)
    ```

    To compute IoUs, the predictions are accumulated in a confusion matrix,
    weighted by `sample_weight` and the metric is then calculated from it.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    Note that this class first computes IoUs for all individual classes, then
    returns the mean of these values.

    Args:
      num_classes: The possible number of labels the prediction task can have.
        This value must be provided, since a confusion matrix of dimension =
        [num_classes, num_classes] will be allocated.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
      ignore_class: Optional integer. The ID of a class to be ignored during
        metric computation. This is useful, for example, in segmentation
        problems featuring a "void" class (commonly -1 or 255) in segmentation
        maps. By default (`ignore_class=None`), all classes are considered.
      sparse_y_true: Whether labels are encoded using integers or
        dense floating point vectors. If `False`, the `tf.argmax` function
        will be used to determine each sample's most likely associated label.
      sparse_y_pred: Whether predictions are encoded using integers or
        dense floating point vectors. If `False`, the `tf.argmax` function
        will be used to determine each sample's most likely associated label.
      axis: (Optional) Defaults to -1. The dimension containing the logits.

    Standalone usage:

    >>> # cm = [[1, 1],
    >>> #        [1, 1]]
    >>> # sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1]
    >>> # iou = true_positives / (sum_row + sum_col - true_positives))
    >>> # result = (1 / (2 + 2 - 1) + 1 / (2 + 2 - 1)) / 2 = 0.33
    >>> m = tf.keras.metrics.MeanIoU(num_classes=2)
    >>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1])
    >>> m.result().numpy()
    0.33333334

    >>> m.reset_state()
    >>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1],
    ...                sample_weight=[0.3, 0.3, 0.3, 0.1])
    >>> m.result().numpy()
    0.23809525

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.MeanIoU(num_classes=2)])
    ```
    NTrV   r6  c           	   
      s,   t t|}t j||||||||d d S )N)r   r7  rL  rX   r   r8  r9  r:  )r<   r   r    r!   )	r&   r7  r   r   r8  r9  r:  rX   rL  r'   r)   r*   r!     s    zMeanIoU.__init__c                 C   s"   | j | j| j| j| j| j| jdS )Nr6  )r7  r   r$   r8  r9  r:  rX   r   r)   r)   r*   r:   &  s    zMeanIoU.get_config)NNNTTrV   )rA   rB   rC   rD   rE   rF   rE  r   rF  r   r"   rG  rH  rI  r!   r:   rG   r)   r)   r'   r*   rX    s$   D      rX  zkeras.metrics.OneHotIoUc                       sV   e Zd ZdZejdeeee e	edf f e
e eed fddZd	d
 Z  ZS )	OneHotIoUa  Computes the Intersection-Over-Union metric for one-hot encoded labels.

    General definition and computation:

    Intersection-Over-Union is a common evaluation metric for semantic image
    segmentation.

    For an individual class, the IoU metric is defined as follows:

    ```
    iou = true_positives / (true_positives + false_positives + false_negatives)
    ```

    To compute IoUs, the predictions are accumulated in a confusion matrix,
    weighted by `sample_weight` and the metric is then calculated from it.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    This class can be used to compute IoU for multi-class classification tasks
    where the labels are one-hot encoded (the last axis should have one
    dimension per class). Note that the predictions should also have the same
    shape. To compute the IoU, first the labels and predictions are converted
    back into integer format by taking the argmax over the class axis. Then the
    same computation steps as for the base `IoU` class apply.

    Note, if there is only one channel in the labels and predictions, this class
    is the same as class `IoU`. In this case, use `IoU` instead.

    Also, make sure that `num_classes` is equal to the number of classes in the
    data, to avoid a "labels out of bound" error when the confusion matrix is
    computed.

    Args:
      num_classes: The possible number of labels the prediction task can have.
        A confusion matrix of shape `(num_classes, num_classes)` will be
        allocated to accumulate predictions from which the metric is calculated.
      target_class_ids: A tuple or list of target class ids for which the metric
        is returned. To compute IoU for a specific class, a list (or tuple) of a
        single id value should be provided.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
      ignore_class: Optional integer. The ID of a class to be ignored during
        metric computation. This is useful, for example, in segmentation
        problems featuring a "void" class (commonly -1 or 255) in segmentation
        maps. By default (`ignore_class=None`), all classes are considered.
      sparse_y_pred: Whether predictions are encoded using natural numbers or
        probability distribution vectors. If `False`, the `tf.argmax` function
        will be used to determine each sample's most likely associated label.
      axis: (Optional) Defaults to -1. The dimension containing the logits.

    Standalone usage:

    >>> y_true = tf.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0], [1, 0, 0]])
    >>> y_pred = tf.constant([[0.2, 0.3, 0.5], [0.1, 0.2, 0.7], [0.5, 0.3, 0.1],
    ...                       [0.1, 0.4, 0.5]])
    >>> sample_weight = [0.1, 0.2, 0.3, 0.4]
    >>> m = tf.keras.metrics.OneHotIoU(num_classes=3, target_class_ids=[0, 2])
    >>> m.update_state(
    ...     y_true=y_true, y_pred=y_pred, sample_weight=sample_weight)
    >>> # cm = [[0, 0, 0.2+0.4],
    >>> #       [0.3, 0, 0],
    >>> #       [0, 0, 0.1]]
    >>> # sum_row = [0.3, 0, 0.7], sum_col = [0.6, 0.3, 0.1]
    >>> # true_positives = [0, 0, 0.1]
    >>> # single_iou = true_positives / (sum_row + sum_col - true_positives))
    >>> # mean_iou = (0 / (0.3 + 0.6 - 0) + 0.1 / (0.7 + 0.1 - 0.1)) / 2
    >>> m.result().numpy()
    0.071

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.OneHotIoU(num_classes=3, target_class_id=[1])])
    ```
    NFrV   .)r7  rL  r8  r:  rX   c              
      s    t  j|||||d||d d S )NFrK  r^   )r&   r7  rL  r   r   r8  r:  rX   r'   r)   r*   r!     s    zOneHotIoU.__init__c                 C   s"   | j | j| j| j| j| j| jdS )N)r7  rL  r   r   r8  r:  rX   )r7  rL  r   r$   r8  r:  rX   r   r)   r)   r*   r:     s    zOneHotIoU.get_config)NNNFrV   )rA   rB   rC   rD   rE   rF   rE  r   r   r   r   rI  r!   r:   rG   r)   r)   r'   r*   rY  2  s   P     rY  zkeras.metrics.OneHotMeanIoUc                	       sT   e Zd ZdZejd
eeee	ee
jjf  ee eed fddZdd	 Z  ZS )OneHotMeanIoUa2  Computes mean Intersection-Over-Union metric for one-hot encoded labels.

    General definition and computation:

    Intersection-Over-Union is a common evaluation metric for semantic image
    segmentation.

    For an individual class, the IoU metric is defined as follows:

    ```
    iou = true_positives / (true_positives + false_positives + false_negatives)
    ```

    To compute IoUs, the predictions are accumulated in a confusion matrix,
    weighted by `sample_weight` and the metric is then calculated from it.

    If `sample_weight` is `None`, weights default to 1.
    Use `sample_weight` of 0 to mask values.

    This class can be used to compute the mean IoU for multi-class
    classification tasks where the labels are one-hot encoded (the last axis
    should have one dimension per class). Note that the predictions should also
    have the same shape. To compute the mean IoU, first the labels and
    predictions are converted back into integer format by taking the argmax over
    the class axis. Then the same computation steps as for the base `MeanIoU`
    class apply.

    Note, if there is only one channel in the labels and predictions, this class
    is the same as class `MeanIoU`. In this case, use `MeanIoU` instead.

    Also, make sure that `num_classes` is equal to the number of classes in the
    data, to avoid a "labels out of bound" error when the confusion matrix is
    computed.

    Args:
      num_classes: The possible number of labels the prediction task can have.
        A confusion matrix of shape `(num_classes, num_classes)` will be
        allocated to accumulate predictions from which the metric is calculated.
      name: (Optional) string name of the metric instance.
      dtype: (Optional) data type of the metric result.
      ignore_class: Optional integer. The ID of a class to be ignored during
        metric computation. This is useful, for example, in segmentation
        problems featuring a "void" class (commonly -1 or 255) in segmentation
        maps. By default (`ignore_class=None`), all classes are considered.
      sparse_y_pred: Whether predictions are encoded using natural numbers or
        probability distribution vectors. If `False`, the `tf.argmax` function
        will be used to determine each sample's most likely associated label.
      axis: (Optional) Defaults to -1. The dimension containing the logits.

    Standalone usage:

    >>> y_true = tf.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0], [1, 0, 0]])
    >>> y_pred = tf.constant([[0.2, 0.3, 0.5], [0.1, 0.2, 0.7], [0.5, 0.3, 0.1],
    ...                       [0.1, 0.4, 0.5]])
    >>> sample_weight = [0.1, 0.2, 0.3, 0.4]
    >>> m = tf.keras.metrics.OneHotMeanIoU(num_classes=3)
    >>> m.update_state(
    ...     y_true=y_true, y_pred=y_pred, sample_weight=sample_weight)
    >>> # cm = [[0, 0, 0.2+0.4],
    >>> #       [0.3, 0, 0],
    >>> #       [0, 0, 0.1]]
    >>> # sum_row = [0.3, 0, 0.7], sum_col = [0.6, 0.3, 0.1]
    >>> # true_positives = [0, 0, 0.1]
    >>> # single_iou = true_positives / (sum_row + sum_col - true_positives))
    >>> # mean_iou = (0 + 0 + 0.1 / (0.7 + 0.1 - 0.1)) / 3
    >>> m.result().numpy()
    0.048

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.OneHotMeanIoU(num_classes=3)])
    ```
    NFrV   r7  r   r   r8  r:  rX   c              	      s   t  j|||||d|d d S )NF)r7  rX   r   r   r8  r9  r:  r^   )r&   r7  r   r   r8  r:  rX   r'   r)   r*   r!     s    
zOneHotMeanIoU.__init__c                 C   s   | j | j| j| j| j| jdS )Nr[  )r7  r   r$   r8  r:  rX   r   r)   r)   r*   r:   
  s    zOneHotMeanIoU.get_config)NNNFrV   )rA   rB   rC   rD   rE   rF   rE  rF  r   r   r"   rG  rH  rI  r!   r:   rG   r)   r)   r'   r*   rZ    s    N     rZ  z keras.metrics.BinaryCrossentropyc                       s(   e Zd ZdZejd fdd	Z  ZS )	BinaryCrossentropya  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`".

    Standalone usage:

    >>> m = tf.keras.metrics.BinaryCrossentropy()
    >>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
    >>> m.result().numpy()
    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().numpy()
    0.9162905

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='mse',
        metrics=[tf.keras.metrics.BinaryCrossentropy()])
    ```
    r	   NFr   c                    s   t  jt||||d d S )N)r   r   label_smoothing)r    r!   r	   )r&   r   r   r   r]  r'   r)   r*   r!   =  s    zBinaryCrossentropy.__init__)r	   NFr   rM   r)   r)   r'   r*   r\    s   &    r\  z%keras.metrics.CategoricalCrossentropyc                       s(   e Zd ZdZejd	 fdd	Z  ZS )
CategoricalCrossentropya  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). Here we assume that labels are given as a
    `one_hot` representation. eg., When labels values are [2, 0, 1],
     `y_true` = [[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.

    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 = tf.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().numpy()
    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=tf.constant([0.3, 0.7]))
    >>> m.result().numpy()
    1.6271976

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.CategoricalCrossentropy()])
    ```
    r
   NFr   rV   c                    s   t  jt|||||d d S )N)r   r   r]  rX   )r    r!   r
   )r&   r   r   r   r]  rX   r'   r)   r*   r!     s    	z CategoricalCrossentropy.__init__)r
   NFr   rV   rM   r)   r)   r'   r*   r^  N  s   3     r^  z+keras.metrics.SparseCategoricalCrossentropyc                       sJ   e Zd ZdZejd	eeeee	j
jf  eee ed fddZ  ZS )
SparseCategoricalCrossentropya	  Computes the crossentropy metric between the labels and predictions.

    Use this crossentropy metric when there are two or more label classes.
    We expect labels to be provided as integers. If you want to provide labels
    using `one-hot` representation, please use `CategoricalCrossentropy` metric.
    There should be `# classes` floating point values per feature for `y_pred`
    and a single floating point value per feature for `y_true`.

    In the snippet below, there is a single floating point value per example for
    `y_true` and `# classes` floating pointing values per example for `y_pred`.
    The shape of `y_true` is `[batch_size]` and the shape of `y_pred` is
    `[batch_size, num_classes]`.

    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.
      ignore_class: Optional integer. The ID of a class to be ignored during
        metric computation. This is useful, for example, in segmentation
        problems featuring a "void" class (commonly -1 or 255) in segmentation
        maps. By default (`ignore_class=None`), all classes are considered.
      axis: (Optional) Defaults to -1. The dimension along which entropy is
        computed.

    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 = tf.keras.metrics.SparseCategoricalCrossentropy()
    >>> m.update_state([1, 2],
    ...                [[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
    >>> m.result().numpy()
    1.1769392

    >>> m.reset_state()
    >>> m.update_state([1, 2],
    ...                [[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
    ...                sample_weight=tf.constant([0.3, 0.7]))
    >>> m.result().numpy()
    1.6271976

    Usage with `compile()` API:

    ```python
    model.compile(
      optimizer='sgd',
      loss='mse',
      metrics=[tf.keras.metrics.SparseCategoricalCrossentropy()])
    ```
    r   NFrV   )r   r   r   r8  rX   c                    s   t  jt|||||d d S )N)r   r   r8  rX   )r    r!   r   )r&   r   r   r   r8  rX   r'   r)   r*   r!     s    	z&SparseCategoricalCrossentropy.__init__)r   NFNrV   )rA   rB   rC   rD   rE   rF   rF  r   r   r"   rG  rH  rI  rE  r!   rG   r)   r)   r'   r*   r_    s   <     r_  c                 C   sV   t || g\\}} }| j|j | j|jkr>t|| j}tt| |t	 S r   )
r   r-   r0   r1   r   r"   r#   equalr   floatx)r6   r7   _r)   r)   r*   rI     s    rI   zkeras.metrics.binary_accuracyrP   c                 C   s   t jt| ||ddS )a  Calculates how often predictions match binary labels.

    Standalone usage:
    >>> y_true = [[1], [1], [0], [0]]
    >>> y_pred = [[1], [1], [0], [0]]
    >>> m = tf.keras.metrics.binary_accuracy(y_true, y_pred)
    >>> assert m.shape == (4,)
    >>> m.numpy()
    array([1., 1., 1., 1.], dtype=float32)

    Args:
      y_true: Ground truth values. shape = `[batch_size, d0, .. dN]`.
      y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`.
      threshold: (Optional) Float representing the threshold for deciding
        whether prediction values are 1 or 0.

    Returns:
      Binary accuracy values. shape = `[batch_size, d0, .. dN-1]`
    rV   rW   )r"   r  r   rR   )r6   r7   rQ   r)   r)   r*   rO     s    rO   z"keras.metrics.categorical_accuracyc                 C   s   t tjj| dd|S )a;  Calculates how often predictions match one-hot labels.

    Standalone usage:
    >>> y_true = [[0, 0, 1], [0, 1, 0]]
    >>> y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0]]
    >>> m = tf.keras.metrics.categorical_accuracy(y_true, y_pred)
    >>> assert m.shape == (2,)
    >>> m.numpy()
    array([0., 1.], dtype=float32)

    You can provide logits of classes as `y_pred`, since argmax of
    logits and probabilities are same.

    Args:
      y_true: One-hot ground truth values.
      y_pred: The prediction values.

    Returns:
      Categorical accuracy values.
    rV   rW   rY   r\   r)   r)   r*   rT     s    rT   z)keras.metrics.sparse_categorical_accuracyc                 C   s8   t | |}|jjdkr4|jd dkr4t|dg}|S )a9  Calculates how often predictions match integer labels.

    Standalone usage:
    >>> y_true = [2, 1]
    >>> y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0]]
    >>> m = tf.keras.metrics.sparse_categorical_accuracy(y_true, y_pred)
    >>> assert m.shape == (2,)
    >>> m.numpy()
    array([0., 1.], dtype=float32)

    You can provide logits of classes as `y_pred`, since argmax of
    logits and probabilities are same.

    Args:
      y_true: Integer ground truth values.
      y_pred: The prediction values.

    Returns:
      Sparse categorical accuracy values.
    r   rV   )r   rZ   r0   r   r"   squeeze)r6   r7   matchesr)   r)   r*   r`   9  s    r`   z(keras.metrics.top_k_categorical_accuracyrb   c                 C   s   t tjj| dd||S )aE  Computes how often targets are in the top `K` predictions.

    Standalone usage:
    >>> y_true = [[0, 0, 1], [0, 1, 0]]
    >>> y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0]]
    >>> m = tf.keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=3)
    >>> assert m.shape == (2,)
    >>> m.numpy()
    array([1., 1.], dtype=float32)

    Args:
      y_true: The ground truth values.
      y_pred: The prediction values.
      k: (Optional) Number of top elements to look at for computing accuracy.
        Defaults to 5.

    Returns:
      Top K categorical accuracy value.
    rV   rW   rd   r6   r7   rg   r)   r)   r*   rc   ^  s    rc   z/keras.metrics.sparse_top_k_categorical_accuracyc                 C   s   t | ||S )a]  Computes how often integer targets are in the top `K` predictions.

    Standalone usage:
    >>> y_true = [2, 1]
    >>> y_pred = [[0.1, 0.9, 0.8], [0.05, 0.95, 0]]
    >>> m = tf.keras.metrics.sparse_top_k_categorical_accuracy(
    ...     y_true, y_pred, k=3)
    >>> assert m.shape == (2,)
    >>> m.numpy()
    array([1., 1.], dtype=float32)

    Args:
      y_true: tensor of true targets.
      y_pred: tensor of predicted targets.
      k: (Optional) Number of top elements to look at for computing accuracy.
        Defaults to 5.

    Returns:
      Sparse top K categorical accuracy value.
    )r   re   re  r)   r)   r*   rk   }  s    rk   rV   c                 C   s2   t jj| |d} t jj||d}t j| | |dS )a3  Computes the cosine similarity between labels and predictions.

    Args:
      y_true: The ground truth values.
      y_pred: The prediction values.
      axis: (Optional) Defaults to -1. The dimension along which the cosine
        similarity is computed.

    Returns:
      Cosine similarity value.
    rW   )r"   rO  l2_normalizer  )r6   r7   rX   r)   r)   r*   r#    s    r#  )rP   )rb   )rb   )rV   )erD   abctypingr   r   r   r   numpyr   tensorflow.compat.v2compatv2r"   kerasr   r   keras.dtensorr   rE   Zkeras.lossesr	   r
   r   r   r   r   r   r   r   r   r   r   r   keras.metricsr   keras.utilsr   r   keras.utils.generic_utilsr   keras.utils.tf_utilsr    tensorflow.python.util.tf_exportr   Meanr   MeanMetricWrapperrH   rN   rS   r_   Z*_SPARSE_CATEGORICAL_UPDATE_STATE_DOCSTRINGr5   ra   rj   Metricrl   r   r   r   r   r   r   ABCMetar   r   r   r   r   r   r"  r$  r%  r&  r'  r(  r)  r*  r+  r2  r3  r4  r5  rJ  rV  rX  rY  rZ  r\  r^  r_  rI   __internal__dispatchadd_dispatch_supportrO   rT   r`   rc   rk   r#  r)   r)   r)   r*   <module>   s  Y*.:4--F<<<< " sjhWb    1#$#$$&#7%$%z whsn8GQ#