o
    2h%                     @   sp   d dl Zd dlmZ d dlmZ d dlmZ edddd	Zed
dddZ				dddZ
	dddZdS )    N)backend)keras_export)tf_utilszkeras.utils.normalize   c                 C   sd   ddl m} t| tjr)ttj| ||}d||dk< |p d}| t|| S |j	j
| ||d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   r   )axisorder)	keras.srcr   
isinstancenpndarray
atleast_1dlinalgnormexpand_dimsnn	normalize)xr
   r   r   r    r   Z/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/utils/numerical_utils.pyr      s   r   zkeras.utils.to_categoricalc                 C   s  t | r1t j| }|dur*t|dkr*|d dkr*t|dd }t j| |} t j	| |S t
j| dd} | j}|rQ|d dkrQt|dkrQt|dd }| d} |s_t
| d }| jd }t
||f}d|t
|| f< ||f }t
||}|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_shaper   r   r   to_categorical&   s*   
%


r.   Fc                 C   s  |pt }|dkr|j| |dS t|| }|dkr#|j| d} d}|jdrG|dkrG|dv rGztj	| |||||d	W S  t
yF   Y nw |d
krU|jj| |||dS |dkr|j| }|dur~t|dkr~|d dkr~t|dd }	|j| |	} |jj| |||dS |dkrt| jdkrdnd}
|dur|j}|jj| |||d}|dur|j|d}|| }|jj||
d}|S dS )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	   
tensorflowr   )	multi_hotcount)r   sparsecount_weightsr1   )r   r3   r#   Nr2   )r
   )r   castr   r   r!   r   __name__endswithr   tf_encode_categorical_inputs
ValueErrorr   r1   r   r    r"   r#   r   sum)inputsoutput_modedepthr   r3   r4   backend_modulerank_of_inputsr)   r*   reduction_axisone_hot_encodingoutputsr   r   r   encode_categorical_inputsh   sj   
rC   Tc              	   C   s   ddl m} || dkr|| d} ||dkr ||d}|| ||}||}|rD|||j|	| |	|d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_maskr   r   r   build_pos_neg_masks   s$   

rR   )r   r   )N)FNN)T)r!   r   r   r   keras.src.api_exportr   keras.src.utilsr   r   r.   rC   rR   r   r   r   r   <module>   s    F
`