
    AVh)                     |   d 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Z edg       ej"                  ddej$                  dfd              Z eddg      ej"                   ej(                  dd      dej$                  ddfd                     Zy)z#Confusion matrix related utilities.    )dtypes)ops)	array_ops)array_ops_stack)	check_ops)cond)control_flow_ops)math_ops)deprecation)dispatch)	tf_exportNc                     t        j                  |d g      5  t        j                        t        j                          j                         }|j                  } j                         }|j                  }||||z
  }||dz   k(  r6|j
                  d   j                  d      rt        j                  dg      n=||dz
  k(  r5|j
                  d   j                  d      rt        j                   dg        fcddd       S t        j                        t        j                         z
  }||j
                  d   j                  d      r4t        j                  t        j                  |dz   |      fdfd      ||j
                  d   j                  d      r4t        j                  t        j                  |dz
  |       fd fd        fcddd       S # 1 sw Y   yxY w)	a$  Squeeze last dim if ranks differ from expected by exactly 1.

  In the common case where we expect shapes to match, `expected_rank_diff`
  defaults to 0, and we squeeze the last dimension of the larger rank if they
  differ by 1.

  But, for example, if `labels` contains class IDs and `predictions` contains 1
  probability per class, we expect `predictions` to have 1 more dimension than
  `labels`, so `expected_rank_diff` would be 1. In this case, we'd squeeze
  `labels` if `rank(predictions) - rank(labels) == 0`, and
  `predictions` if `rank(predictions) - rank(labels) == 2`.

  This will use static shape if available. Otherwise, it will add graph
  operations, which could result in a performance hit.

  Args:
    labels: Label values, a `Tensor` whose dimensions match `predictions`.
    predictions: Predicted values, a `Tensor` of arbitrary dimensions.
    expected_rank_diff: Expected result of `rank(predictions) - rank(labels)`.
    name: Name of the op.

  Returns:
    Tuple of `labels` and `predictions`, possibly with last dim squeezed.
  remove_squeezable_dimensionsN   c                  2    t        j                   dg      S Nr   r   squeezepredictionss   V/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/confusion_matrix.py<lambda>z.remove_squeezable_dimensions.<locals>.<lambda>Q   s    )##K"6     c                       S N r   s   r   r   z.remove_squeezable_dimensions.<locals>.<lambda>R   s    + r   c                  2    t        j                   dg      S r   r   labelss   r   r   z.remove_squeezable_dimensions.<locals>.<lambda>W   s    )##FRD1 r   c                       S r   r   r   s   r   r   z.remove_squeezable_dimensions.<locals>.<lambda>X   s    & r   )r   
name_scopeconvert_to_tensor	get_shapendimsdimsis_compatible_withr   r   rankr   r
   equal)	r    r   expected_rank_diffnamepredictions_shapepredictions_ranklabels_shapelabels_rank	rank_diffs	   ``       r   r   r      s   4 ~~d:{+- !''4K""6*F#--/(..##%L$$K&6&B"[0i
)A-
-

 
 
$
7
7
:''bT:+a//b!44Q7""6B40[ #! !( {+innV.DDI r"55a8II
..+a/
;
6
k 	"003yy
..+a/
;
1
f ;C! ! !s   C*G0CG00G9zmath.confusion_matrix)v1c           	      V   t        j                  |d|| ||f      5 }t        t        j                  | d      t        j                  |d            \  } }t	        j
                  |t        j                        }t	        j
                  | t        j                        } t        j                  t        j                  | d      g|       } t        j                  t        j                  |d      g|      }|@t	        j                  t	        j                  |      t	        j                  |             d	z   }n~t	        j
                  |t        j                        }t        j                  t        j                  | |d
      g|       } t        j                  t        j                  ||d      g|      }|Zt        j                  |d      }|j                         j!                  |j                                t	        j
                  ||      }t#        j$                  ||g      }t#        j$                  | |gd	      }|t'        j(                  ||      n|}	t'        j*                  ||	t	        j
                  |t        j                              cddd       S # 1 sw Y   yxY w)a  Computes the confusion matrix from predictions and labels.

  The matrix columns represent the prediction labels and the rows represent the
  real labels. The confusion matrix is always a 2-D array of shape `[n, n]`,
  where `n` is the number of valid labels for a given classification task. Both
  prediction and labels must be 1-D arrays of the same shape in order for this
  function to work.

  If `num_classes` is `None`, then `num_classes` will be set to one plus the
  maximum value in either predictions or labels. Class labels are expected to
  start at 0. For example, if `num_classes` is 3, then the possible labels
  would be `[0, 1, 2]`.

  If `weights` is not `None`, then each prediction contributes its
  corresponding weight to the total value of the confusion matrix cell.

  For example:

  ```python
    tf.math.confusion_matrix([1, 2, 4], [2, 2, 4]) ==>
        [[0 0 0 0 0]
         [0 0 1 0 0]
         [0 0 1 0 0]
         [0 0 0 0 0]
         [0 0 0 0 1]]
  ```

  Note that the possible labels are assumed to be `[0, 1, 2, 3, 4]`,
  resulting in a 5x5 confusion matrix.

  Args:
    labels: 1-D `Tensor` of real labels for the classification task.
    predictions: 1-D `Tensor` of predictions for a given classification.
    num_classes: The possible number of labels the classification task can
                 have. If this value is not provided, it will be calculated
                 using both predictions and labels array.
    weights: An optional `Tensor` whose shape matches `predictions`.
    dtype: Data type of the confusion matrix.
    name: Scope name.

  Returns:
    A `Tensor` of type `dtype` with shape `[n, n]` representing the confusion
    matrix, where `n` is the number of possible labels in the classification
    task.

  Raises:
    ValueError: If both predictions and labels are not 1-D vectors and have
      mismatched shapes, or if `weights` is not `None` and its shape doesn't
      match `predictions`.
  confusion_matrixr    )r+   r   z!`labels` contains negative values)messagez&`predictions` contains negative valuesNr   z`labels` out of boundz`predictions` out of boundweights)axis)indicesupdatesshape)r   r"   r   r#   r
   castr   int64r	   with_dependenciesr   assert_non_negativemaximum
reduce_maxassert_lessr$   assert_is_compatible_withr   stackr   	ones_like
scatter_nd)
r    r   num_classesr5   dtyper+   num_classes_int64r9   r7   valuess
             r   r3   r3   \   sk   t ~~d."FKAC .2FJ6f84m	-.FK --V\\:K]]66<<0F //		&	&?
A 	BF #44		&	&!I
K 	LK
 $$X%8%8%E%-%8%8%@BDEFk #--V\\B11  '1HJ K
f %66  ,24 5 	k %%gI>g778I8I8KLgu-g!!;"<=E##V[$9BG !!+u5&- mmE6<<02W.2 .2 .2s   I9JJ(r3   ztrain.confusion_matrixc                 "    t        | |||||      S )a  Computes the confusion matrix from predictions and labels.

  The matrix columns represent the prediction labels and the rows represent the
  real labels. The confusion matrix is always a 2-D array of shape `[n, n]`,
  where `n` is the number of valid labels for a given classification task. Both
  prediction and labels must be 1-D arrays of the same shape in order for this
  function to work.

  If `num_classes` is `None`, then `num_classes` will be set to one plus the
  maximum value in either predictions or labels. Class labels are expected to
  start at 0. For example, if `num_classes` is 3, then the possible labels
  would be `[0, 1, 2]`.

  If `weights` is not `None`, then each prediction contributes its
  corresponding weight to the total value of the confusion matrix cell.

  For example:

  ```python
    tf.math.confusion_matrix([1, 2, 4], [2, 2, 4]) ==>
        [[0 0 0 0 0]
         [0 0 1 0 0]
         [0 0 1 0 0]
         [0 0 0 0 0]
         [0 0 0 0 1]]
  ```

  Note that the possible labels are assumed to be `[0, 1, 2, 3, 4]`,
  resulting in a 5x5 confusion matrix.

  Args:
    labels: 1-D `Tensor` of real labels for the classification task.
    predictions: 1-D `Tensor` of predictions for a given classification.
    num_classes: The possible number of labels the classification task can have.
      If this value is not provided, it will be calculated using both
      predictions and labels array.
    dtype: Data type of the confusion matrix.
    name: Scope name.
    weights: An optional `Tensor` whose shape matches `predictions`.

  Returns:
    A `Tensor` of type `dtype` with shape `[n, n]` representing the confusion
    matrix, where `n` is the number of possible labels in the classification
    task.

  Raises:
    ValueError: If both predictions and labels are not 1-D vectors and have
      mismatched shapes, or if `weights` is not `None` and its shape doesn't
      match `predictions`.
  )r3   )r    r   rE   rF   r+   r5   s         r   confusion_matrix_v1rJ      s    v 
&+{GU
   r   )r   N)__doc__tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r   r   r	   r
   tensorflow.python.utilr   r    tensorflow.python.util.tf_exportr   r   add_dispatch_supportint32r3   deprecated_endpointsrJ   r   r   r   <module>rS      s    * . + + 1 + & 2 * . + 6 59;| "r*	 "&!!<<f2  +f2R &(:;<	!!!"46NO %)$ll! $9  P  =9 r   