o
    N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intZone_hotZ	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tfZTensorZRaggedTensorSparseTensorZconvert_to_tensordtypecast)inputsr    r   ^D:\Projects\ConvertPro\env\Lib\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   Z	is_tensornumpyr   npZ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resultZ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   	r*   c                 C   s$   t | rtj| |S t| |S )z0Expand dims on sparse, ragged, or dense tensors.)r   Z	is_sparser   r   expand_dims)r
   r   r   r   r   r+   X   s   
r+   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    r+   ONE_HOT
ValueError	MULTI_HOTr%   r*   TF_IDFgatherr   r   r   r   multiply)r
   output_moder!   r   r   r"   Zidf_weightsZoriginal_shaper   Z	bincountsZvalue_weightsr   r   r   encode_categorical_inputs`   sX   




r8   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)r/   r   r(   r1   )r   r7   r!   r   r   r   $compute_shape_for_encode_categorical   s   
r9   )N)r,   FNN)__doc__r   r   Ztensorflow.compat.v2compatv2r   Zkeras.utilsr   r/   r1   r3   ZCOUNTr4   r   r   r%   r*   r+   r8   r9   r   r   r   r   <module>   s(   
	
	

=