a
    Sic                     @   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countZ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   j/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/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r8|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   s`   t jj| ||||d|d}| jjdkr:|t |f n"| 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    	r2   c                 C   s(   t | rtj| |S t| |S dS )z0Expand dims on sparse, ragged, or dense tensors.N)r   	is_sparser   r$   expand_dims)r   r   r   r   r   r4   X   s    
r4   float32Fc                 C   s$  |t krtt| |S | j}| jjdkr6t| d} |tkrV| jd dkrVt| d} | jjdkrtd| d| d| jj d|t	tfv }|rt
| ||||}	nt| ||||}	|tkr|	S |d	u rtd
| d| |rt||	jd	d	df }
t|	j|
|	j |	jS t|	|S d	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'   r4   ONE_HOT
ValueError	MULTI_HOTr-   r2   TF_IDFgatherr!   r   r"   r#   multiply)r   output_moder(   r   r$   r)   Zidf_weightsoriginal_shaper   Z	bincountsZvalue_weightsr   r   r   encode_categorical_inputs`   sX    




rB   c                 C   sb   |t krt| S | s"t|gS |tkrF| d dkrFt| |g S t| dd |g S dS )z9Computes the output shape of `encode_categorical_inputs`.r   r    N)r8   r   r0   r:   )r&   r@   r(   r   r   r   $compute_shape_for_encode_categorical   s    
rC   )N)N)N)r5   FNN)__doc__r   r   tensorflow.compat.v2compatv2r   keras.utilsr   r8   r:   r<   COUNTr=   r   r   r-   r2   r4   rB   rC   r   r   r   r   <module>   s&   
		

    
=