
    6bi                       S r SSKrSSKrSSKJs  Jr  SSKJ	r	  SSKJ
r
  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr   " S S\R0                  5      r\" S5       " S S\5      5       r\" S5       " S S\5      5       r\" S5       " S S\5      5       r\" S5       " S S\5      5       r\" S5       " S S\R0                  5      5       r\" S5       " S S\R0                  5      5       r " S S \R0                  \R@                  S!9r!\" S"5       " S# S$\!5      5       r"\" S%5       " S& S'\!5      5       r#\" S(5       " S) S*\!5      5       r$\" S+5       " S, S-\!5      5       r%\" S.5       " S/ S0\R0                  5      5       r&g)1zHConfusion metrics, i.e. metrics based on True/False positives/negatives.    N)activations)backend)utils)base_metric)metrics_utils)to_list)is_tensor_or_variable)keras_exportc                   T   ^  \ rS rSrSr S	U 4S jjrS
S jrS rS rU 4S jr	Sr
U =r$ )_ConfusionMatrixConditionCount"   aO  Calculates the number of the given confusion matrix condition.

Args:
  confusion_matrix_cond: One of `metrics_utils.ConfusionMatrix` conditions.
  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`). One metric
    value is generated for each threshold value. Defaults to `0.5`.
  name: (Optional) string name of the metric instance.
  dtype: (Optional) data type of the metric result.
c                 
  > [         TU ]  X4S9  Xl        X l        [        R
                  " USS9U l        [        R                  " U R                  5      U l        U R                  S[        U R                  5      4SS9U l        g )Nnamedtype      ?default_thresholdaccumulatorzerosshapeinitializer)super__init___confusion_matrix_condinit_thresholdsr   parse_init_thresholds
thresholds is_evenly_distributed_thresholds_thresholds_distributed_evenly
add_weightlenr   )selfconfusion_matrix_condr   r   r   	__class__s        `/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/metrics/confusion_metrics.pyr   '_ConfusionMatrixConditionCount.__init__0   s     	d0&;#)'==#
 ::4??K 	+  ??#doo"6!8g + 
    c           	          [         R                  " U R                  U R                  0UUU R                  U R
                  US9$ )aA  Accumulates the metric statistics.

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

Returns:
  Update op.
)r   thresholds_distributed_evenlysample_weight)r   !update_confusion_matrix_variablesr   r   r   r!   r$   y_truey_predr,   s       r'   update_state+_ConfusionMatrixConditionCount.update_state@   sD     >>(($*:*:;*.*M*M'
 	
r)   c                     [        U R                  5      S:X  a  U R                  S   nOU R                  n[        R                  " U5      $ N   r   )r#   r   r   tfconvert_to_tensorr$   results     r'   r9   %_ConfusionMatrixConditionCount.resultV   s?    t1$%%a(F%%F##F++r)   c           
          [         R                  " U R                   Vs/ s H2  o[        R                  " UR
                  R                  5       5      4PM4     sn5        g s  snf N)r   batch_set_value	variablesnpr   r   as_list)r$   vs     r'   reset_state*_ConfusionMatrixConditionCount.reset_state]   s@    7;~~F~!!''//+,-~F	
Fs   9A c                    > SU R                   0n[        TU ]	  5       n[        [	        UR                  5       5      [	        UR                  5       5      -   5      $ )Nr   )r   r   
get_configdictlistitemsr$   configbase_configr&   s      r'   rE   )_ConfusionMatrixConditionCount.get_configb   sI     4 45g(*D**,-V\\^0DDEEr)   )r   r!   r   r   r   NNNr<   )__name__
__module____qualname____firstlineno____doc__r   r1   r9   rB   rE   __static_attributes____classcell__r&   s   @r'   r   r   "   s/     HL
 
,,

F Fr)   r   zkeras.metrics.FalsePositivesc                   N   ^  \ rS rSrSr\R                  SU 4S jj5       rSrU =r	$ )FalsePositivesh   a?  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) 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. Defaults to `0.5`.
  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='binary_crossentropy',
              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)])
```
c                 V   > [         TU ]  [        R                  R                  UUUS9  g N)r%   r   r   r   )r   r   r   ConfusionMatrixFALSE_POSITIVESr$   r   r   r   r&   s       r'   r   FalsePositives.__init__   -    "/"?"?"O"O!	 	 	
r)    rM   
rN   rO   rP   rQ   rR   dtensor_utilsinject_meshr   rS   rT   rU   s   @r'   rW   rW   h   #    /b 
 
r)   rW   zkeras.metrics.FalseNegativesc                   N   ^  \ rS rSrSr\R                  SU 4S jj5       rSrU =r	$ )FalseNegatives   a?  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) 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. Defaults to `0.5`.
  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='binary_crossentropy',
              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)])
```
c                 V   > [         TU ]  [        R                  R                  UUUS9  g rZ   )r   r   r   r[   FALSE_NEGATIVESr]   s       r'   r   FalseNegatives.__init__   r_   r)   r`   rM   ra   rU   s   @r'   rf   rf      rd   r)   rf   zkeras.metrics.TrueNegativesc                   N   ^  \ rS rSrSr\R                  SU 4S jj5       rSrU =r	$ )TrueNegatives   a9  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) 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. Defaults to `0.5`.
  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='binary_crossentropy',
              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)])
```
c                 V   > [         TU ]  [        R                  R                  UUUS9  g rZ   )r   r   r   r[   TRUE_NEGATIVESr]   s       r'   r   TrueNegatives.__init__  -    "/"?"?"N"N!	 	 	
r)   r`   rM   ra   rU   s   @r'   rl   rl      rd   r)   rl   zkeras.metrics.TruePositivesc                   N   ^  \ rS rSrSr\R                  SU 4S jj5       rSrU =r	$ )TruePositivesi  a<  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) 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. Defaults to `0.5`.
  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='binary_crossentropy',
              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)])
```
c                 V   > [         TU ]  [        R                  R                  UUUS9  g rZ   )r   r   r   r[   TRUE_POSITIVESr]   s       r'   r   TruePositives.__init__R  rq   r)   r`   rM   ra   rU   s   @r'   rs   rs     rd   r)   rs   zkeras.metrics.Precisionc                   r   ^  \ rS rSrSr\R                   S	U 4S jj5       rS
S jrS r	S r
U 4S jrSrU =r$ )	Precisioni\  a  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='binary_crossentropy',
              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)])
```
c                   > [         TU ]  XES9  Xl        X l        X0l        Uc  SO[
        R                  n[
        R                  " XS9U l        [
        R                  " U R                  5      U l
        U R                  S[        U R                  5      4SS9U l        U R                  S[        U R                  5      4SS9U l        g )Nr   r   r   true_positivesr   r   false_positives)r   r   r   top_kclass_idr   NEG_INFr   r   r    r!   r"   r#   rz   r{   r$   r   r|   r}   r   r   r   r&   s          r'   r   Precision.__init__       	d0)
 #(=Cm6K6K'==
 ::4??K 	+ #ooS%9$; . 
  $t')  /  
r)   c                    [         R                  " [         R                  R                  U R                  [         R                  R
                  U R                  0UUU R                  U R                  U R                  U R                  US9$ )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. Can
    be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
    and must be broadcastable to `y_true`. Defaults to `1`.

Returns:
  Update op.
r   r+   r|   r}   r,   )r   r-   r[   ru   rz   r\   r{   r   r!   r|   r}   r.   s       r'   r1   Precision.update_state  s     >>--<<d>Q>Q--==t?S?S *.*M*M**]]'
 	
r)   c                     [         R                  R                  U R                  [         R                  R	                  U R                  U R
                  5      5      n[        U R                  5      S:X  a  US   $ U$ r4   )r6   mathdivide_no_nanrz   addr{   r#   r   r8   s     r'   r9   Precision.result  ^    &&GGKK++T-A-AB
  0A5vayA6Ar)   c           	          [        [        U R                  5      5      n[        R                  " U R
                  U R                  4 Vs/ s H  nU[        R                  " U45      4PM     sn5        g s  snf r<   )	r#   r   r   r   r=   rz   r{   r?   r   r$   num_thresholdsrA   s      r'   rB   Precision.reset_state  g    WT__56 --t/C/CDDA BHHn./0D	
   	#A4c                    > U R                   U R                  U R                  S.n[        TU ]  5       n[        [        UR                  5       5      [        UR                  5       5      -   5      $ N)r   r|   r}   r   r|   r}   r   rE   rF   rG   rH   rI   s      r'   rE   Precision.get_config  Y    ..ZZ

 g(*D**,-V\\^0DDEEr)   )r!   r}   r{   r   r   r|   rz   NNNNNr<   rN   rO   rP   rQ   rR   rb   rc   r   r1   r9   rB   rE   rS   rT   rU   s   @r'   rx   rx   \  sC    N` KO
 
0
:B
F Fr)   rx   zkeras.metrics.Recallc                   r   ^  \ rS rSrSr\R                   S	U 4S jj5       rS
S jrS r	S r
U 4S jrSrU =r$ )Recalli  a	  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='binary_crossentropy',
              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)])
```
c                   > [         TU ]  XES9  Xl        X l        X0l        Uc  SO[
        R                  n[
        R                  " XS9U l        [
        R                  " U R                  5      U l
        U R                  S[        U R                  5      4SS9U l        U R                  S[        U R                  5      4SS9U l        g )Nr   r   r   rz   r   r   false_negatives)r   r   r   r|   r}   r   r~   r   r   r    r!   r"   r#   rz   r   r   s          r'   r   Recall.__init__@  r   r)   c                    [         R                  " [         R                  R                  U R                  [         R                  R
                  U R                  0UUU R                  U R                  U R                  U R                  US9$ )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. Can
    be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
    and must be broadcastable to `y_true`. Defaults to `1`.

Returns:
  Update op.
r   )r   r-   r[   ru   rz   ri   r   r   r!   r|   r}   r.   s       r'   r1   Recall.update_stateY  r   r)   c                     [         R                  R                  U R                  [         R                  R	                  U R                  U R
                  5      5      n[        U R                  5      S:X  a  US   $ U$ r4   )r6   r   r   rz   r   r   r#   r   r8   s     r'   r9   Recall.resultv  r   r)   c           	          [        [        U R                  5      5      n[        R                  " U R
                  U R                  4 Vs/ s H  nU[        R                  " U45      4PM     sn5        g s  snf r<   )	r#   r   r   r   r=   rz   r   r?   r   r   s      r'   rB   Recall.reset_state}  r   r   c                    > U R                   U R                  U R                  S.n[        TU ]  5       n[        [        UR                  5       5      [        UR                  5       5      -   5      $ r   r   rI   s      r'   rE   Recall.get_config  r   r)   )r!   r}   r   r   r   r|   rz   r   r<   r   rU   s   @r'   r   r     sB    >@ KO
 
0
:B
F Fr)   r   c                   T   ^  \ rS rSrSr S	U 4S jjrS
S jrS rU 4S jrS r	Sr
U =r$ )SensitivitySpecificityBasei  zAbstract 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).
c                   > [         TU ]  XES9  US::  a  [        SU 35      eXl        X0l        U R                  SU4SS9U l        U R                  SU4SS9U l        U R                  SU4SS9U l        U R                  S	U4SS9U l	        US
:X  a  S/U l
        SU l        g [        US-
  5       Vs/ s H  nUS
-   S-  US
-
  -  PM     nnS/U-   S/-   U l
        SU l        g s  snf )Nr   r   zKArgument `num_thresholds` must be an integer > 0. Received: num_thresholds=rz   r   r   true_negativesr{   r   r5   r   F         ?        T)r   r   
ValueErrorvaluer}   r"   rz   r   r{   r   r   r!   range)	r$   r   r   r}   r   r   ir   r&   s	           r'   r   #SensitivitySpecificityBase.__init__  sI    	d0Q,,:+;=  
 "oo^$57 . 
 #oo^$57 . 
  $n%6G  /  
  $n%6G  /  

 Q"eDO27D/ ~122A Q#!!342    #ej0C58DO26D/s   1C"c           
         [         R                  " [         R                  R                  U R                  [         R                  R
                  U R                  [         R                  R                  U R                  [         R                  R                  U R                  0UUU R                  U R                  U R                  US9$ )G  Accumulates confusion matrix statistics.

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

Returns:
  Update op.
)r   r+   r}   r,   )r   r-   r[   ru   rz   ro   r   r\   r{   ri   r   r   r!   r}   r.   s       r'   r1   'SensitivitySpecificityBase.update_state  s     >>--<<d>Q>Q--<<d>Q>Q--==t?S?S--==t?S?S	 *.*M*M]]'
 	
r)   c           	         [        U R                  5      nU R                  U R                  U R                  U R
                  4n[        R                  " U Vs/ s H  nU[        R                  " U45      4PM     sn5        g s  snf r<   )
r#   r   rz   r   r{   r   r   r=   r?   r   )r$   r   confusion_matrix_variablesrA   s       r'   rB   &SensitivitySpecificityBase.reset_state  s    T__-    	&
" 	 43A BHHn./03	
s   #Bc                    > SU R                   0n[        TU ]	  5       n[        [	        UR                  5       5      [	        UR                  5       5      -   5      $ )Nr}   )r}   r   rE   rF   rG   rH   rI   s      r'   rE   %SensitivitySpecificityBase.get_config  sG    dmm,g(*D**,-V\\^0DDEEr)   c                 &   [         R                  " U" XR                  5      5      n[         R                  " [         R                  " U5      S5      n[         R
                  " [         R                  " X$5      5      n[         R                  " XVS5      $ )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   )r6   wherer   greatersize
reduce_maxgather)r$   constrained	dependent	predicatefeasiblefeasible_existsmax_dependents          r'   _find_max_under_constraint5SensitivitySpecificityBase._find_max_under_constraint  s\    " 88Ik::>?**RWWX%6:bii	&DExx<<r)   )r!   r}   r   r{   r   r   rz   r      NNNr<   )rN   rO   rP   rQ   rR   r   r1   rB   rE   r   rS   rT   rU   s   @r'   r   r     s/     JN"7H
8
F
= =r)   r   )	metaclassz&keras.metrics.SensitivityAtSpecificityc                   h   ^  \ rS rSrSr\R                      SU 4S jj5       rS rU 4S jr	Sr
U =r$ )SensitivityAtSpecificityi  a'  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) The number of thresholds to
    use for matching the given specificity. Defaults to `200`.
  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='binary_crossentropy',
    metrics=[tf.keras.metrics.SensitivityAtSpecificity()])
```
c                 t   > US:  d  US:  a  [        SU 35      eXl        X l        [        TU ]  UUUUUS9  g )Nr   r5   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&   s         r'   r   !SensitivityAtSpecificity.__init__@  ]     ?kAo))47  ',) 	 	
r)   c                    [         R                  R                  U R                  [         R                  R	                  U R                  U R
                  5      5      n[         R                  R                  U R                  [         R                  R	                  U R                  U R                  5      5      nU R                  X[         R                  5      $ r<   )
r6   r   r   r   r   r{   rz   r   r   greater_equal)r$   specificitiessensitivitiess      r'   r9   SensitivityAtSpecificity.resultX      --GGKK++T-A-AB
 --GGKK++T-A-AB
 .."*:*:
 	
r)   c                    > U R                   U R                  S.n[        TU ]  5       n[	        [        UR                  5       5      [        UR                  5       5      -   5      $ )Nr   r   )r   r   r   rE   rF   rG   rH   rI   s      r'   rE   #SensitivityAtSpecificity.get_confige  T    "11++
 g(*D**,-V\\^0DDEEr)   r   r   rN   rO   rP   rQ   rR   rb   rc   r   r9   rE   rS   rT   rU   s   @r'   r   r     sB    9v  
 
.
F Fr)   r   z&keras.metrics.SpecificityAtSensitivityc                   h   ^  \ rS rSrSr\R                      SU 4S jj5       rS rU 4S jr	Sr
U =r$ )SpecificityAtSensitivityin  a   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) The number of thresholds to
    use for matching the given sensitivity. Defaults to `200`.
  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='binary_crossentropy',
    metrics=[tf.keras.metrics.SpecificityAtSensitivity()])
```
c                 t   > US:  d  US:  a  [        SU 35      eXl        X l        [        TU ]  UUUUUS9  g )Nr   r5   zJArgument `sensitivity` must be in the range [0, 1]. Received: sensitivity=r   )r   sensitivityr   r   r   )r$   r   r   r}   r   r   r&   s         r'   r   !SpecificityAtSensitivity.__init__  r   r)   c                    [         R                  R                  U R                  [         R                  R	                  U R                  U R
                  5      5      n[         R                  R                  U R                  [         R                  R	                  U R                  U R                  5      5      nU R                  X[         R                  5      $ r<   )
r6   r   r   rz   r   r   r   r{   r   r   )r$   r   r   s      r'   r9   SpecificityAtSensitivity.result  r   r)   c                    > U R                   U R                  S.n[        TU ]  5       n[	        [        UR                  5       5      [        UR                  5       5      -   5      $ )Nr   r   )r   r   r   rE   rF   rG   rH   rI   s      r'   rE   #SpecificityAtSensitivity.get_config  r   r)   r   r   r   rU   s   @r'   r   r   n  sB    7r  
 
.
F Fr)   r   zkeras.metrics.PrecisionAtRecallc                   b   ^  \ rS rSrSr\R                   SU 4S jj5       rS rU 4S jr	Sr
U =r$ )PrecisionAtRecalli  ai  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) The number of thresholds to
    use for matching the given recall. Defaults to `200`.
  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='binary_crossentropy',
    metrics=[tf.keras.metrics.PrecisionAtRecall(recall=0.8)])
```
c                 t   > US:  d  US:  a  [        SU 35      eXl        X l        [        TU ]  UUUUUS9  g )Nr   r5   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&   s         r'   r   PrecisionAtRecall.__init__	  s\     A:!$$*8-  ,) 	 	
r)   c                    [         R                  R                  U R                  [         R                  R	                  U R                  U R
                  5      5      n[         R                  R                  U R                  [         R                  R	                  U R                  U R                  5      5      nU R                  X[         R                  5      $ r<   )	r6   r   r   rz   r   r   r{   r   r   )r$   recalls
precisionss      r'   r9   PrecisionAtRecall.result  s    ''''GGKK++T-A-AB
 WW**GGKK++T-A-AB

 ..!1!1
 	
r)   c                    > U R                   U R                  S.n[        TU ]  5       n[	        [        UR                  5       5      [        UR                  5       5      -   5      $ )Nr   r   )r   r   r   rE   rF   rG   rH   rI   s      r'   rE   PrecisionAtRecall.get_config)  sM    $($7$74;;Og(*D**,-V\\^0DDEEr)   r   r   r   rU   s   @r'   r   r     s7    .` JN
 
$
F Fr)   r   zkeras.metrics.RecallAtPrecisionc                   h   ^  \ rS rSrSr\R                      SU 4S jj5       rS rU 4S jr	Sr
U =r$ )RecallAtPrecisioni/  a  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) The number of thresholds to
    use for matching the given precision. Defaults to `200`.
  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='binary_crossentropy',
    metrics=[tf.keras.metrics.RecallAtPrecision(precision=0.8)])
```
c                 t   > US:  d  US:  a  [        SU 35      eXl        X l        [        TU ]  UUUUUS9  g )Nr   r5   zFArgument `precision` must be in the range [0, 1]. Received: precision=r   )r   	precisionr   r   r   )r$   r   r   r}   r   r   r&   s         r'   r   RecallAtPrecision.__init__d  s\     q=IM''0k3  #,) 	 	
r)   c                    [         R                  R                  U R                  [         R                  R	                  U R                  U R
                  5      5      n[         R                  R                  U R                  [         R                  R	                  U R                  U R                  5      5      nU R                  X[         R                  5      $ r<   )	r6   r   r   rz   r   r{   r   r   r   )r$   r   r   s      r'   r9   RecallAtPrecision.result|  s    WW**GGKK++T-A-AB

 ''''GGKK++T-A-AB
 ..!1!1
 	
r)   c                    > U R                   U R                  S.n[        TU ]  5       n[	        [        UR                  5       5      [        UR                  5       5      -   5      $ )Nr   r   )r   r   r   rE   rF   rG   rH   rI   s      r'   rE   RecallAtPrecision.get_config  sR    "11
 g(*D**,-V\\^0DDEEr)   r   r   r   rU   s   @r'   r   r   /  sB    1f  
 
.
F Fr)   r   zkeras.metrics.AUCc                      ^  \ rS rSrSr\R                            SU 4S jj5       r\S 5       r	S r
SS jrS rS rS	 rU 4S
 jrSrU =r$ )AUCi  a  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) The number of thresholds to
    use when discretizing the roc curve. Values must be > 1.
    Defaults to `200`.
  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)])
```
c                 r  > [        U[        R                  5      (       aE  U[        [        R                  5      ;  a(  [	        SU S[        [        R                  5       35      e[        U[        R
                  5      (       aE  U[        [        R
                  5      ;  a(  [	        SU S[        [        R
                  5       35      eUS LU l        UbV  [        U5      S-   U l        [        U5      n[        R                  " [        R                  " S/U-   S/-   5      5      U l        OKUS::  a  [	        SU 35      eXl        [        US-
  5       Vs/ s H  nUS-   S-  US-
  -  PM     nnS	U l        [        R                  " S[        R                   " 5       -
  /U-   S[        R                   " 5       -   /-   5      U l        [        U[        R                  5      (       a  X l        O$[        R                  R'                  U5      U l        [        U[        R
                  5      (       a  X0l        O$[        R
                  R'                  U5      U l        [*        TU ]Y  XES
9  Xpl        Xl        U	bC  [2        R4                  " XR6                  S9n	[2        R8                  R;                  U	SS9  Xl        OS U l        Xl        SU l         U R.                  (       a2  U(       a*  [2        RB                  " S U/5      nU RE                  U5        g g U(       a  [	        S5      eU RE                  S 5        g s  snf )Nz Invalid `curve` argument value "z". Expected one of: z+Invalid `summation_method` argument value "r   r   r   r5   zKArgument `num_thresholds` must be an integer > 1. Received: num_thresholds=Tr   )r   z3All values of `label_weights` must be non-negative.messageFz7`num_labels` is needed only when `multi_label` is True.)#
isinstancer   AUCCurverG   r   AUCSummationMethod_init_from_thresholdsr#   r   sortedr    r?   arrayr!   r   r   epsilon_thresholdscurvefrom_strsummation_methodr   r   multi_label
num_labelsr6   constantr   	debuggingassert_non_negativelabel_weights_from_logits_builtTensorShape_build)r$   r   r  r  r   r   r   r  r  r  from_logitsr   r   r&   s                r'   r   AUC.__init__	  s    e]3344d""G
 :
 25' :$$()?)?$@#AC  m>>
 
d=+K+K&LL##3"4 5$$()I)I$J#KM  &0t%;"!"%j/A"5D
+J>>HHcUZ/3%78 / " 00>/?A  #1 ~122A Q#!!342   37D/ 887??$$%
2cGOO<M6M5NN
 e]3344J&//88?DJ&(H(HII$4!$1$D$D$M$M %D! 	d0 '$$KKZZHMLL,,M -  "/ "&D'j'9:E"   M  KKes   L4c                 ,    [        U R                  5      $ )z'The thresholds used for evaluating AUC.)rG   r
  )r$   s    r'   r   AUC.thresholdsq  s     D$$%%r)   c                    U R                   (       ab  UR                  S:w  a  [        SUR                   SU 35      eUS   U l        [        R
                  " U R                  U R                  /5      nO![        R
                  " U R                  /5      nXl        U R                  SUSS9U l	        U R                  SUSS9U l
        U R                  S	USS9U l        U R                  S
USS9U l        U R                   (       aa  [        R                  " 5          [        R                  " 5       (       d)  [        R                   " [        R"                  " 5       5        SSS5        SU l        g! , (       d  f       N= f)zCInitialize TP, FP, TN, and FN tensors, given the shape of the
data.r   z>`y_pred` must have rank 2 when `multi_label=True`. Found rank z$. Full shape received for `y_pred`: r5   rz   r   r   r   r{   r   NT)r  ndimsr   _num_labelsr6   r  r   _build_input_shaper"   rz   r   r{   r   
init_scopeexecuting_eagerlyr   _initialize_variables_get_sessionr  )r$   r   variable_shapes      r'   r  
AUC._buildv  s_    {{a ""'++ /99>A 
  %QxD^^$$d&6&67N  ^^T-@-@,ABN"'"ooN . 
 #ooN . 
  $^  /  
  $^  /  
  ++--11'2F2F2HI !  !s   AE--
E;c                 "   U R                   (       d/  U R                  [        R                  " UR                  5      5        U R
                  (       d  U R                  b  US4/nU R
                  (       aE  UR                  U R                  S4U R                  S4U R                  S4U R                  S4/5        U R                  b;  UR                  U R                  S45        [        R                  R                  USS9  U R
                  (       a  SOU R                  nU R                  (       a  [         R"                  " U5      n[$        R&                  " [$        R(                  R*                  U R                  [$        R(                  R,                  U R                  [$        R(                  R.                  U R                  [$        R(                  R0                  U R                  0UUU R2                  U R4                  UU R
                  US9$ )r   N)NL)Tr(  )r(  z#Number of labels is not consistent.r  )r+   r,   r  r  )r  r  r6   r  r   r  r  extendrz   r   r{   r   appendr  assert_shapesr  r   sigmoidr   r-   r[   ru   ro   r\   ri   r
  r!   )r$   r/   r0   r,   shapesr  s         r'   r1   AUC.update_state  s    {{KKv||45 2 2 >z*+F ,,j9,,j9--z:--z:	 !!- t116:;**$I +  !% 0 0d6H6H ((0F>>--<<d>Q>Q--<<d>Q>Q--==t?S?S--==t?S?S	 *.*M*M'(('
 	
r)   c                    U R                   SU R                  S-
   U R                   SS -
  n[        R                  R	                  U R                   U R
                  5      nUSU R                  S-
   USS -
  n[        R                  R                  U[        R                  " US5      SS9nU R                   SS [        R                  " XBSS 5      -
  n[        R                  " [        R                  " USU R                  S-
   S:  USS S:  5      [        R                  R                  USU R                  S-
   [        R                  " USS S5      SS9[        R                  " USS 5      5      n[        R                  R                  XAU[        R                  R                  U5      -  -   -  [        R                  " U R                   SS U R                  SS -   S5      SS9nU R                  (       a  [        R                  " XpR                   S-   SS	9nU R"                  c  [        R$                  " XR                   S9$ [        R                  R                  [        R                  " [        R                  " XR"                  5      5      [        R                  " U R"                  5      U R                   S9$ [        R                  " US
S9$ )a  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.
Nr5   r   
prec_sloper   recall_relative_ratiopr_auc_increment	_by_labelr   axisinterpolate_pr_auc)rz   r   r6   r   r   r{   r   maximummultiplyr   logical_and	ones_likelogr   r  
reduce_sumr   r  reduce_mean)	r$   dtppdpr1  	interceptsafe_p_ratior4  by_label_aucs	            r'   r8  AUC.interpolate_pr_auc  s   \  9$"5"5"9:!!!"%& 	 GGKK++T-A-AB($$q()AabE1WW**B" + 

 ''+bkk*e.LL	xxNN16t22Q67!;QqrUQYGGG!!+D''!+,

1QR5!$, " 
 LL12
 7700	BGGKK,E EEFJJt**12.1E1Eab1II1M# 1 
 == yy;'>QL !!)~~lCC ww,,MML2D2DE MM$"4"45 -   ==!18LMMr)   c                 j   U R                   [        R                  R                  :X  a8  U R                  [        R
                  R                  :X  a  U R                  5       $ [        R                  R                  U R                  [        R                  R                  U R                  U R                  5      5      nU R                   [        R                  R                  :X  aa  [        R                  R                  U R                  [        R                  R                  U R                  U R                   5      5      nUnUnO`[        R                  R                  U R                  [        R                  R                  U R                  U R                  5      5      nUnUnU R                  [        R
                  R                  :X  a  US U R"                  S-
   USS  -   S-  nO}U R                  [        R
                  R$                  :X  a+  [        R&                  " US U R"                  S-
   USS  5      nO*[        R(                  " US U R"                  S-
   USS  5      nU R*                  (       a  [        R,                  " US U R"                  S-
   USS  -
  U5      n[        R.                  " XpR0                  S-   SS9nU R2                  c  [        R4                  " XR0                  S9$ [        R                  R                  [        R.                  " [        R,                  " XR2                  5      5      [        R.                  " U R2                  5      U R0                  S9$ [        R.                  " [        R,                  " US U R"                  S-
   USS  -
  U5      U R0                  S9$ )Nr5   g       @r5  r   r6  r2  )r  r   r  PRr  r  INTERPOLATIONr8  r6   r   r   rz   r   r   ROCr{   r   r   MINORINGminimumr9  r  r:  r>  r   r  r?  )	r$   r   fp_ratexyr   heightsriemann_termsrE  s	            r'   r9   
AUC.result9  s   JJ-00333%%//==> **,, &&GGKK++T-A-AB
 :://333gg++$$D00$2E2EFG AA--##D//1E1EFI AA !!//==> 24..23ae;sBG""m&F&F&O&OOjj#<T%8%81%<!=quEG jj#<T%8%81%<!=quEG KK+D''!+,qu4gM ==II$;!L !!)~~lCC ww,,MML2D2DE MM$"4"45 -   ==A7 3 3a 781QR5@'JYY r)   c           
         U R                   (       a  U R                  U R                  U R                  U R                  4nU R
                  (       aT  [        R                  " U Vs/ s H1  nU[        R                  " U R                  U R                  45      4PM3     sn5        g [        R                  " U Vs/ s H&  nU[        R                  " U R                  45      4PM(     sn5        g g s  snf s  snf r<   )r  rz   r   r{   r   r  r   r=   r?   r   r   r  )r$   r   rA   s      r'   rB   AUC.reset_state~  s    ;;####$$$$	*& '' "<!;A BHHd&9&94;K;K%LMN!; '' "<!;A BHHd&9&9%;<=!; s   %8C/9-C4c                    > [        U R                  5      (       a!  [        R                  " U R                  5      nOU R                  nU R                  U R
                  R                  U R                  R                  U R                  U R                  UU R                  S.nU R                  (       a  U R                  SS US'   [        TU ]9  5       n[        [!        UR#                  5       5      [!        UR#                  5       5      -   5      $ )N)r   r  r  r  r  r  r  r5   r   )r	   r  r   evalr   r  r   r  r  r  r  r  r   r   rE   rF   rG   rH   )r$   r  rJ   rK   r&   s       r'   rE   AUC.get_config  s     !3!344#LL););<M ..M"11ZZ%% $ 5 5 ; ;++//*,,
 %% $(??1R#8F< g(*D**,-V\\^0DDEEr)   )r  r  r  r  r  r
  r!   r  r   r{   r  r  r  r   r  r   rz   )
r   rJ  interpolationNNNFNNFr<   )rN   rO   rP   rQ   rR   rb   rc   r   propertyr   r  r1   r8  r9   rB   rE   rS   rT   rU   s   @r'   r   r     s    sj  (e eN & &(T<
|YNvCJ.F Fr)   r   )'rR   abcnumpyr?   tensorflow.compat.v2compatv2r6   tf_keras.srcr   r   tf_keras.src.dtensorr   rb   tf_keras.src.metricsr   tf_keras.src.utilsr    tf_keras.src.utils.generic_utilsr   tf_keras.src.utils.tf_utilsr	    tensorflow.python.util.tf_exportr
   Metricr   rW   rf   rl   rs   rx   r   ABCMetar   r   r   r   r   r   r`   r)   r'   <module>ri     s   O 
  ! ! $   7 , , 4 = :CF[%7%7 CFL ,-9
3 9
 .9
x ,-9
3 9
 .9
x +,9
2 9
 -9
x +,9
2 9
 -9
x '(^F"" ^F )^FB $%NF[ NF &NFbp=!3!3s{{ p=f 67gF9 gF 8gFT 67eF9 eF 8eFP /0TF2 TF 1TFn /0_F2 _F 1_FD !"XF+

 XF #XFr)   