
    6bi                         S r SSKrSSKJs  Jr  SSKJr  Sr	Sr
SrSrSrSS	 jrS
 rSS jrSS jrS r    SS jrS rg)zUtils for preprocessing layers.    N)tf_utilsintone_hot	multi_hotcounttf_idfc                     [        U [        R                  [        R                  [        R                  45      (       d  [        R
                  " X5      n Ub&  U R                  U:w  a  [        R                  " X5      n U $ )z<Ensures the input is a Tensor, SparseTensor or RaggedTensor.)
isinstancetfTensorRaggedTensorSparseTensorconvert_to_tensordtypecast)inputsr   s     o/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/layers/preprocessing/preprocessing_utils.pyensure_tensorr      sV    fryy"//2??KLL%%f4V\\U2'M    c                     [         R                  " U 5      (       a  U R                  5       n [        U [        R
                  5      (       a  U R                  5       n U $ )zFConvert any tensors or numpy arrays to lists for config serialization.)r   	is_tensornumpyr
   npndarraytolist)xs    r   listify_tensorsr   &   s;    	||AGGI!RZZ  HHJHr   c           	      >   [         R                  R                  U UUUSUS9n[         R                  " XS5      nU R                  R
                  S:X  a  U4nO[         R                  " U5      S   nXq4n[         R                  " UR                  UR                  US9nU$ )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_sizes           r   sparse_bincountr2   /   s    YY#   F WWV#F||AxXXf%a(
"*__v}},F Mr   c           
      V   [         R                  R                  U UUUUSUS9nU R                  R                  S:X  a(  UR                  [         R                  " U45      5        U$ U R                  R                  5       S   nUR                  [         R                  " Xa45      5        U$ )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/   r1   s          r   dense_bincountr8   E   s    WW#  F ||A12 M \\))+A.
(;<=Mr   c                     [         R                  " U 5      (       a  [        R                  R	                  X5      $ [        R                  " X5      $ )z0Expand dims on sparse, ragged, or dense tensors.)r   	is_sparser   r)   expand_dims)r   r#   s     r   r;   r;   X   s7    &!!yy$$V22~~f++r   c           	      @   U[         :X  a*  [        R                  " [        R                  " X5      5      $ U R                  nU R                  R
                  S:X  a  [        U S5      n U[        :X  a  U R                  S   S:w  a  [        U S5      n U R                  R
                  S:  a)  [        SU SU SU R                  R
                   S35      eU[        [        4;   nU(       a  [        XXU5      n	O[        XXU5      n	U[        :w  a  U	$ Uc  [        S
U SU 35      eU(       a_  [        R                  " XiR                  S	S	2S4   5      n
[        R                  " U	R                  XR                   -  U	R"                  5      $ [        R$                  " X5      $ )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_HOTr2   r8   TF_IDFgatherr&   r   r'   r(   multiply)r   output_moder-   r   r)   r.   idf_weightsoriginal_shaper$   	bincountsvalue_weightss              r   encode_categorical_inputsrL   `   s    c{{2776122\\N||AVR(g<<q  ,F ||1**56G 1171B1B0C1F
 	
  Iw#77M#=
	 #=
	 f%%0M1B;-Q
 	

 		+/@/@B/GH,,,!!
 	
 {{922r   c                    U[         :X  a  [        R                  " U 5      $ U (       d  [        R                  " U/5      $ U[        :X  a"  U S   S:w  a  [        R                  " X/-   5      $ [        R                  " U SS U/-   5      $ )z9Computes the output shape of `encode_categorical_inputs`.r   r%   N)r?   r   r6   rA   )r+   rG   r-   s      r   $compute_shape_for_encode_categoricalrN      sn    c~~e$$~~ug&&g%)q.~~ego..~~eCRjE7233r   )N)float32FNN)__doc__r   r   tensorflow.compat.v2compatv2r   tf_keras.src.utilsr   r?   rA   rC   COUNTrD   r   r   r2   r8   r;   rL   rN    r   r   <module>rW      sa    &  ! ! '
		,&, :3z	4r   