
    2Vh%                         d dl Zd dlmZ d dlmZ d dlmZ  ed      dd       Z ed      dd       Z		 	 	 dd	Z
	 dd
Zy)    N)backend)keras_export)tf_utilszkeras.utils.normalizec                 4   ddl m} t        | t        j                        r[t        j
                  t        j                  j                  | ||            }d||dk(  <   |xs d}| t        j                  ||      z  S |j                  j                  | ||      S )al  Normalizes an array.

    If the input is a NumPy array, a NumPy array will be returned.
    If it's a backend tensor, a backend tensor will be returned.

    Args:
        x: Array to normalize.
        axis: axis along which to normalize.
        order: Normalization order (e.g. `order=2` for L2 norm).

    Returns:
        A normalized copy of the array.
    r   ops   )axisorder)	keras.srcr   
isinstancenpndarray
atleast_1dlinalgnormexpand_dimsnn	normalize)xr   r   r   r   s        O/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/utils/numerical_utils.pyr   r      s     !RZZ }}RYY^^Aud;<TQY zr2>>$--- 66AD66    zkeras.utils.to_categoricalc                    t        j                  |       rt         j                  j                  |       }|Dt	        |      dkD  r6|d   dk(  r.t        |dd       }t         j                  j                  | |      } t         j                  j                  | |      S t        j                  | d      } | j                  }|r$|d   dk(  rt	        |      dkD  rt        |dd       }| j                  d      } |st        j                  |       dz   }| j                  d   }t        j                  ||f      }d|t        j                  |      | f<   ||fz   }t        j                  ||      }|S )aE  Converts a class vector (integers) to binary class matrix.

    E.g. for use with `categorical_crossentropy`.

    Args:
        x: Array-like with class values to be converted into a matrix
            (integers from 0 to `num_classes - 1`).
        num_classes: Total number of classes. If `None`, this would be inferred
            as `max(x) + 1`. Defaults to `None`.

    Returns:
        A binary matrix representation of the input as a NumPy array. The class
        axis is placed last.

    Example:

    >>> a = keras.utils.to_categorical([0, 1, 2, 3], num_classes=4)
    >>> print(a)
    [[1. 0. 0. 0.]
     [0. 1. 0. 0.]
     [0. 0. 1. 0.]
     [0. 0. 0. 1.]]

    >>> b = np.array([.9, .04, .03, .03,
    ...               .3, .45, .15, .13,
    ...               .04, .01, .94, .05,
    ...               .12, .21, .5, .17]).reshape(4,4)
    >>> loss = keras.ops.categorical_crossentropy(a, b)
    >>> print(np.around(loss, 5))
    [0.10536 0.82807 0.1011  1.77196]

    >>> loss = keras.ops.categorical_crossentropy(a, a)
    >>> print(np.around(loss, 5))
    [0. 0. 0. 0.]
    Nr	   r
   int64dtyper   )r   	is_tensorcoreshapelentuplenumpyreshaper   one_hotr   arraymaxzerosarange)r   num_classesinput_shapenewshape
batch_sizecategoricaloutput_shapes          r   to_categoricalr0   &   sG   J ll((+ #K 1$B1$["-.H%%a2Azz!!![11
'"A''K {2!+K0@10DK,-			"AffQi!mJ((J45K,-K		*%q()+/L**[,7Kr   c                    |xs t         }|dk(  r|j                  | |      S t        |j                  |             }|dk(  r|j                  j                  | d      } d}|j                  j                  d      r%|dk  r |dv r	 t        j                  | |||||	      S |d
k(  r|j                  j                  | |||      S |dk(  r||j                  j                  |       }|@t        |      dkD  r2|d   dk(  r*t        |dd       }	|j                  j                  | |	      } |j                  j!                  | |||      S |dk(  rt        | j                        dkD  rdnd}
||j"                  }|j                  j!                  | |||      }|!|j                  j                  |d      }||z  }|j                  j%                  ||
      }|S y# t        $ r Y Bw xY w)aX  Encodes categorical inputs according to output_mode.

    Args:
        inputs: the inputs to encode.
        output_mode: one of `"int"`, `"one_hot"`, `"multi_hot"`, or `"count"`.
        depth: number of classes, this will be the last dimension of the output.
        dtype: the dtype of the output, unless `count_weights` is not `None`.
        sparse: whether the output should be sparse for backends supporting it.
        count_weights: weights to apply if `output_mode` is `"count"`.
        backend_module: the backend to use instead of the current one.

    Returns: the encoded inputs.
    intr   r   r
   r	   
tensorflow   )	multi_hotcount)r   sparsecount_weightsr5   )r   r7   r%   Nr6   )r   )r   castr!   r    r#   r   __name__endswithr   tf_encode_categorical_inputs
ValueErrorr   r5   r   r"   r$   r%   r   sum)inputsoutput_modedepthr   r7   r8   backend_modulerank_of_inputsr+   r,   reduction_axisone_hot_encodingoutputss                r   encode_categorical_inputsrG   h   s@   , $.wNe""6"77--f56N %%11&"= 	((6a11
	88+  k!  **Ev + 
 	
 
		!$))//7 #K 1$B1$["-.H#))11&(CF  ((Ev ) 
 	
 
	 "&,,/!3$!''E),,44Ev 5 
 $*00<<]BOM/-? &&** + 
 + 
 )  		s   G 	G#"G#c           	         ddl m} |j                  |       dk(  r|j                  | d      } |j                  |      dk(  r|j                  |d      }|j	                  | |j                  |            }|j                  |      }|rD|j                  ||j                  |j                  |       |j                  |      dd             }||fS )Nr   r   r	   )r
   r	   bool)kr   )
r   r   ndimr$   equal	transposelogical_notlogical_andeyesize)query_labels
key_labelsremove_diagonalr   positive_masknegative_masks         r   build_pos_neg_masksrW      s    
 
xx"{{<9
xx
q [[W5
IIlCMM*,EFMOOM2MWW&$	   
 -''r   )r
   r4   )N)FNN)T)r#   r   r   r   keras.src.api_exportr   keras.src.utilsr   r   r0   rG   rW    r   r   <module>r[      sb      - $ %&7 '7: *+> ,>L Z@ (r   