o
    i e                     @   s   d Z ddlZddlm  mZ ddlmZ dZ	dZ
dZdZdZdd	d
Zdd ZdddZdddZdd Z				dddZdd ZdS )zUtils for preprocessing layers.    N)tf_utilsintone_hot	multi_hotcounttf_idfc                 C   sD   t | tjtjtjfst| |} |dur | j|kr t| |} | S )z<Ensures the input is a Tensor, SparseTensor or RaggedTensor.N)
isinstancetfTensorRaggedTensorSparseTensorconvert_to_tensordtypecast)inputsr    r   a/var/www/myenv/lib/python3.10/site-packages/keras/src/layers/preprocessing/preprocessing_utils.pyensure_tensor   s
   r   c                 C   s*   t | r	|  } t| tjr|  } | S )zFConvert any tensors or numpy arrays to lists for config serialization.)r	   	is_tensornumpyr   npndarraytolist)xr   r   r   listify_tensors&   s
   
r   c                 C   sf   t jj| |||d|d}t ||}| jjdkr|f}nt |d }||f}t j|j|j|d}|S )zFApply binary or count encoding to an input and return a sparse tensor.)weights	minlength	maxlengthaxisbinary_output   r   )indicesvaluesdense_shape)	r	   sparsebincountr   shaperankr   r"   r#   )r   depthr    r   count_weightsresultoutput_shape
batch_sizer   r   r   sparse_bincount/   s"   
r.   c              	   C   sb   t jj| ||||d|d}| jjdkr|t |f |S | j d }|t ||f |S )z+Apply binary or count encoding to an input.r   )r   r   r   r   r   r    r!   r   )r	   mathr&   r'   r(   	set_shapeTensorShapeas_list)r   r)   r    r   r*   r+   r-   r   r   r   dense_bincountE   s   	r3   c                 C   s$   t | rtj| |S t| |S )z0Expand dims on sparse, ragged, or dense tensors.)r   	is_sparser	   r%   expand_dims)r   r   r   r   r   r5   X   s   
r5   float32Fc                 C   s  |t krtt| |S | j}| jjdkrt| d} |tkr+| jd dkr+t| d} | jjdkrAtd| d| d| jj d|t	tfv }|rRt
| ||||}	nt| ||||}	|tkr`|	S |d	u rntd
| d| |rt||	jd	d	df }
t|	j|
|	j |	jS t|	|S )z3Encodes categoical inputs according to output_mode.r   r   r!      zZWhen output_mode is not `'int'`, maximum supported output rank is 2. Received output_mode z and input shape z$, which would result in output rank .NzTWhen output mode is `'tf_idf'`, idf_weights must be provided. Received: output_mode=z and idf_weights=)INTr	   identityr   r'   r(   r5   ONE_HOT
ValueError	MULTI_HOTr.   r3   TF_IDFgatherr"   r   r#   r$   multiply)r   output_moder)   r   r%   r*   idf_weightsoriginal_shaper    	bincountsvalue_weightsr   r   r   encode_categorical_inputs`   sX   




rF   c                 C   s^   |t kr	t| S | st|gS |tkr#| d dkr#t| |g S t| dd |g S )z9Computes the output shape of `encode_categorical_inputs`.r   r!   N)r9   r	   r1   r;   )r'   rA   r)   r   r   r   $compute_shape_for_encode_categorical   s   
rG   )N)r6   FNN)__doc__r   r   tensorflow.compat.v2compatv2r	   keras.src.utilsr   r9   r;   r=   COUNTr>   r   r   r.   r3   r5   rF   rG   r   r   r   r   <module>   s(   
	
	

=