o
    2hB                     @   sf   d dl mZ d dlmZ dd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S )    )backend)
tensorflowFNc                 C   s   t | tjr	| }n"t | tjjr| j}nt| dr)t| dr)tj| j| j	|d}ndS |s/|S |j}|j
du s<|j
dkr>|S | }d|d< t|}||_|S )z?Returns a `TensorSpec` given a single `Tensor` or `TensorSpec`.shapedtype)r   r   nameNr   )
isinstancetfTypeSpec__internal__CompositeTensor
_type_spechasattr
TensorSpecr   r   rankas_listTensorShape_shape)tdynamic_batchr   specr   
shape_list r   S/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/utils/tf_utils.pyget_tensor_spec   s"   
r   c                 C   sd   t | tjtjtjfs!t dkrt| rt| } t| |} |dur0| j	|kr0t
| |} | S )z<Ensures the input is a Tensor, SparseTensor or RaggedTensor.torchN)r   r   TensorSparseTensorRaggedTensorr   	is_tensorconvert_to_numpyconvert_to_tensorr   cast)inputsr   r   r   r   ensure_tensor   s   
r#   c                 C   s   dt t| v S )Nzragged_tensor.RaggedTensor)strtype)xr   r   r   is_ragged_tensor+   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!   r   r   r   r/   r0   )r"   depthr-   r   count_weightsresultoutput_shape
batch_sizer   r   r   sparse_bincount/   s"   
r9   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   mathr3   r   r   	set_shaper   r   )r"   r4   r-   r   r5   r6   r8   r   r   r   dense_bincountE   s   	r<   c                 C   s&   t | tjrtj| |S t| |S )z0Expand dims on sparse, ragged, or dense tensors.)r   r   r   r2   expand_dims)r"   r,   r   r   r   r=   X   s   r=   float32c                 C   s&  |dkrt t | |S | j}| jjdkrt| d} |dkr+| jd dkr+t| d} | jjdkrAtd| d| d	| jj d
|dv }|rPt| ||||}	nt| ||||}	t |	|}	|dkrd|	S |du rrtd| d| |rt 	||	j
dddf }
t |	j
|
|	j |	jS t |	|S )zeEncodes categorical inputs according to output_mode.

    Faster method that relies on bincount.
    intr   r(   one_hotr.      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 .)	multi_hotr@   tf_idfNzTWhen output mode is `'tf_idf'`, idf_weights must be provided. Received: output_mode=z and idf_weights=)r   identityr!   r   r   r=   
ValueErrorr9   r<   gatherr/   r   r0   r1   multiply)r"   output_moder4   r   r2   r5   idf_weightsoriginal_shaper-   	bincountsvalue_weightsr   r   r   tf_encode_categorical_inputs_   sZ   



rN   )FN)N)r>   FNN)	keras.srcr   keras.src.utils.module_utilsr   r   r   r#   r'   r9   r<   r=   rN   r   r   r   r   <module>   s    



