
    &Vf                         d dl 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S )    N)backend)keras_exportzkeras.utils.normalize   c                 ,   ddl m} t          | t          j                  rXt          j        t          j                            | ||                    }d||dk    <   |pd}| t          j        ||          z  S |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   r   )axisorder)	keras.srcr   
isinstancenpndarray
atleast_1dlinalgnormexpand_dimsnn	normalize)xr
   r   r   r   s        \/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/utils/numerical_utils.pyr   r      s     !RZ   .}RY^^Aud;;<<TQY zr2>$---- 6AD666    zkeras.utils.to_categoricalc                 "   t          j        |           r t           j                            | |          S t	          j        | d          } | j        }|r6|d         dk    r*t          |          dk    rt          |dd                   }| 	                    d          } |st	          j
        |           dz   }| j        d         }t	          j        ||f          }d|t	          j        |          | f<   ||fz   }t	          j	        ||          }|S )a\  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],
    ...               shape=[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.]
    int64dtyper   r	   Nr   )r   	is_tensorr   one_hotr   arrayshapelentuplereshapemaxzerosarange)r   num_classesinput_shape
batch_sizecategoricaloutput_shapes         r   to_categoricalr,   %   s   L  2z!!![111
'"""A'K  .{2!++K0@0@10D0DK,--			"A $fQii!mJ(J455K,-K	*%%q()+/L*[,77Kr   float32c           	         |pt           }|dk    r|                    | |          S |dv }|                    |           }t          |          }|dk    r|j                            | d          } n!|dk    r|st          d| d| d	| d
          |rz|dk    r|j                            | |          }nt|dk    rQ|j                            | |          }	|j        	                    |j        
                    |	d          dd          }n|j                            | |          }|                    ||          }|S )z4Encodes categorical inputs according to output_mode.intr   )	multi_hotr   r   r   r   z~When output_mode is anything other than `'multi_hot', 'one_hot', or 'int'`, the rank must be 2 or less. Received output_mode: z and input shape: z$, which would result in output rank .r   r0   )r
   r	   )	minlength)r   castr    r!   numpyr   
ValueErrorr   r   whereanybincount)
inputsoutput_modedepthr   backend_modulebinary_outputoriginal_shaperank_of_inputs	bincountsone_hot_inputs
             r   encode_categorical_inputsrC   _   s    $.wNe""6"777#;;M#))&11N((N   %11&"==	!		 	G *5G G %3	G G
 6DG G G    
)##&)11&%@@IIK''*-55feDDM&,22$((R(@@!Q I #(11 2 
 
	 ##Iu55Ir   )r   r   )N)r-   N)	r5   r   r   r   keras.src.api_exportr   r   r,   rC    r   r   <module>rF      s              - - - - - - %&&7 7 7 '&7: *++6 6 6 ,+6z 0 0 0 0 0 0r   