
    6biD                         S r SSKJs  Jr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr  SS	KJr   " S
 S\5      rg)z(Keras base class for convolution layers.    N)activations)constraints)initializers)regularizers)Layer)	InputSpec)
conv_utilsc                      ^  \ rS rSrSr                 SU 4S jjrS rS rS r\	R                  " SS9S	 5       rS
 rS rS rS rU 4S jrS rS rS rS rSrU =r$ )Conv   a  Abstract N-D convolution layer (private, used as implementation base).

This layer creates a convolution kernel that is convolved
(actually cross-correlated) with the layer input to produce a tensor of
outputs. If `use_bias` is True (and a `bias_initializer` is provided),
a bias vector is created and added to the outputs. Finally, if
`activation` is not `None`, it is applied to the outputs as well.

Note: layer attributes cannot be modified after the layer has been called
once (except the `trainable` attribute).

Args:
  rank: An integer, the rank of the convolution, e.g. "2" for 2D
    convolution.
  filters: Integer, the dimensionality of the output space (i.e. the number
    of filters in the convolution). Could be "None", eg in the case of
    depth wise convolution.
  kernel_size: An integer or tuple/list of n integers, specifying the
    length of the convolution window.
  strides: An integer or tuple/list of n integers,
    specifying the stride length of the convolution.
    Specifying any stride value != 1 is incompatible with specifying
    any `dilation_rate` value != 1.
  padding: One of `"valid"`,  `"same"`, or `"causal"` (case-insensitive).
    `"valid"` means no padding. `"same"` results in padding with zeros
    evenly to the left/right or up/down of the input such that output has
    the same height/width dimension as the input. `"causal"` results in
    causal (dilated) convolutions, e.g. `output[t]` does not depend on
    `input[t+1:]`.
  data_format: A string, one of `channels_last` (default) or
    `channels_first`.
    The ordering of the dimensions in the inputs.
    `channels_last` corresponds to inputs with shape
    `(batch_size, ..., channels)` while `channels_first` corresponds to
    inputs with shape `(batch_size, channels, ...)`.
  dilation_rate: An integer or tuple/list of n integers, specifying
    the dilation rate to use for dilated convolution.
    Currently, specifying any `dilation_rate` value != 1 is
    incompatible with specifying any `strides` value != 1.
  groups: A positive integer specifying the number of groups in which the
    input is split along the channel axis. Each group is convolved
    separately with `filters / groups` filters. The output is the
    concatenation of all the `groups` results along the channel axis.
    Input channels and `filters` must both be divisible by `groups`.
  activation: Activation function to use.
    If you don't specify anything, no activation is applied.
  use_bias: Boolean, whether the layer uses a bias.
  kernel_initializer: An initializer for the convolution kernel. If None,
    the default initializer (glorot_uniform) will be used.
  bias_initializer: An initializer for the bias vector. If None, the default
    initializer (zeros) will be used.
  kernel_regularizer: Optional regularizer for the convolution kernel.
  bias_regularizer: Optional regularizer for the bias vector.
  activity_regularizer: Optional regularizer function for the output.
  kernel_constraint: Optional projection function to be applied to the
      kernel after being updated by an `Optimizer` (e.g. used to implement
      norm constraints or value constraints for layer weights). The function
      must take as input the unprojected variable and must return the
      projected variable (which must have the same shape). Constraints are
      not safe to use when doing asynchronous distributed training.
  bias_constraint: Optional projection function to be applied to the
      bias after being updated by an `Optimizer`.
Tc                   > [         TU ]  " SUU[        R                  " U5      S.UD6  Xl        [        U[        5      (       a  [        U5      nUb  US::  a  [        SU S35      eX l	        U=(       d    SU l
        [        R                  " X1S5      U l        [        R                  " XASSS	9U l        [        R                  " U5      U l        [        R"                  " U5      U l        [        R                  " XqS
5      U l        [(        R                  " U	5      U l        Xl        [.        R                  " U5      U l        [.        R                  " U5      U l        [        R                  " U5      U l        [        R                  " U5      U l        [8        R                  " U5      U l        [8        R                  " U5      U l        [?        U R                  S-   S9U l         U RC                  5         U R                   S:H  U l"        U R$                  S:H  U l#        [        RH                  " U R$                  U R                  S-   5      U l%        g )N)	trainablenameactivity_regularizerr   z[Invalid value for argument `filters`. Expected a strictly positive value. Received filters=.   kernel_sizestridesT)
allow_zerodilation_rate   )min_ndimcausalchannels_first )&super__init__r   getrank
isinstancefloatint
ValueErrorfiltersgroupsr	   normalize_tupler   r   normalize_paddingpaddingnormalize_data_formatdata_formatr   r   
activationuse_biasr   kernel_initializerbias_initializerkernel_regularizerbias_regularizerr   kernel_constraintbias_constraintr   
input_spec_validate_init
_is_causal_channels_firstconvert_data_format_tf_data_format)selfr   r$   r   r   r(   r*   r   r%   r+   r,   r-   r.   r/   r0   r   r1   r2   r   r   conv_opkwargs	__class__s                         e/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/layers/convolutional/base_conv.pyr   Conv.__init__^   s   0 	 	
!-!1!12F!G	
 		
 	gu%%'lG7a<$$+9A/ 
 k%55}
 "119
 "33G<%;;KH'77
 &//*5 "."2"23E"F , 0 01A B"."2"23E"F , 0 01A B!,1B!C*?#TYY];,,(2#//3CC)==dii!m 
    c                    U R                   bL  U R                   U R                  -  S:w  a/  [        SR                  U R                  U R                   5      5      e[	        U R
                  5      (       d  [        SU R
                  < 35      e[	        U R                  5      (       d  [        SU R                  < 35      eU R                  S:X  a)  SSKJ	n  SSK
Jn  [        XU45      (       d  [        S5      e[        U R                  5      S	:  a?  [        U R                  5      S	:  a%  [        S
U R                   SU R                   35      eg g )Nr   zgThe number of filters must be evenly divisible by the number of groups. Received: groups={}, filters={}z:The argument `kernel_size` cannot contain 0(s). Received: z7The argument `strides` cannot contains 0(s). Received: r   )Conv1D)SeparableConv1DzCCausal padding is only supported for `Conv1D`and `SeparableConv1D`.r   zW`strides > 1` not supported in conjunction with `dilation_rate > 1`. Received: strides=z and dilation_rate=)r$   r%   r#   formatallr   r   r(   (tf_keras.src.layers.convolutional.conv1drA   2tf_keras.src.layers.convolutional.separable_conv1drB   r    maxr   )r9   rA   rB   s      r=   r4   Conv._validate_init   s=   <<#t{{(Ba(GDDJFKKE  4##$$##& 
 4<<  <<" 
 <<8#G d_$=>> - 
 t||q S););%<q%@::>,, H!!%!3!3 46  &A r?   c           
         [         R                  " U5      nU R                  U5      nX R                  -  S:w  a%  [	        SR                  U R                  X!5      5      eU R                  X R                  -  U R                  4-   nU R                  U5        U R                  SUU R                  U R                  U R                  SU R                  S9U l        U R                  (       aN  U R                  SU R                  4U R                   U R"                  U R$                  SU R                  S9U l        OS U l        U R)                  5       n[+        U R,                  S-   XB0S9U l        SU l        g )	Nr   zThe number of input channels must be evenly divisible by the number of groups. Received groups={}, but the input has {} channels (full input shape is {}).kernelT)r   shapeinitializerregularizer
constraintr   dtypebiasr   )r   axes)tfTensorShape_get_input_channelr%   r#   rC   r   r$   compute_output_shape
add_weightr-   r/   r1   rO   rJ   r,   r.   r0   r2   rP   _get_channel_axisr   r   r3   built)r9   input_shapeinput_channelkernel_shapechannel_axiss        r=   build
Conv.build   sW   nn[1//<;;&!+<<BFKK=  ''[[(LL+
 
 	!!+.oo////--** & 
 ==||o 11 11//jj ( DI DI--/#YY],)F
 
r?   c           
         U R                   S:X  a  SnOF[        U R                   [        5      (       a  U R                   R                  5       nOU R                   n[        R
                  R                  UU[        U R                  5      U[        U R                  5      U R                  U R                  R                  S9$ )Nr   VALID)r   r(   	dilationsr*   r   )r(   r    strupperrR   nnconvolutionlistr   r   r8   r<   __name__)r9   inputsrJ   
tf_paddings       r=   convolution_opConv.convolution_op   s    <<8# Jc**++-JJuu  &4--.,,(( ! 
 	
r?   )jit_compilec                 $    U R                  X5      $ )N)rj   )r9   rh   rJ   s      r=   _jit_compiled_convolution_op!Conv._jit_compiled_convolution_op  s    ""622r?   c                   ^  UR                   nT R                  (       a&  [        R                  " UT R	                  U5      5      nT R
                  S:  a1  T R                  U[        R                  " T R                  5      5      nOT R                  UT R                  5      nT R                  (       a  UR                   R                  nT R                  S:X  aD  T R                  (       a3  [        R                  " T R                  ST R                  S45      nX5-  nOrUb<  UST R                  -   :  a)  U 4S jn[         R"                  " X6T R                  S-   S9nO3[        R$                  R'                  UT R                  T R(                  S9n[        R*                  " 5       (       d3  UR                  (       a"  T R-                  U5      nUR/                  U5        T R0                  b  T R1                  U5      $ U$ )Nr   r   c                 j   > [         R                  R                  U TR                  TR                  S9$ )Nr*   )rR   rd   bias_addrP   r8   )or9   s    r=   	_apply_fnConv.call.<locals>._apply_fn-  s.    !uu~~tyyd6J6J  .   r?   )
inner_rankrr   )rK   r5   rR   pad_compute_causal_paddingr%   rn   convert_to_tensorrJ   rj   r,   r   r6   reshaperP   r$   r	   squeeze_batch_dimsrd   rs   r8   executing_eagerlyrU   	set_shaper+   )r9   rh   rY   outputsoutput_rankrP   ru   	out_shapes   `       r=   call	Conv.call  s~   ll??VVFD$@$@$HIF;;?77,,T[[9G ))&$++>G==!--,,KyyA~$"6"6zz$))aq-AB *{Q]/J
 );;tyy1}G !eenn8L8L - G ##%%+*:*:11+>Ii(??&??7++r?   c                     [        U5       VVs/ s HN  u  p#[        R                  " UU R                  U   U R                  U R
                  U   U R                  U   S9PMP     snn$ s  snnf )N)r(   stridedilation)	enumerater	   conv_output_lengthr   r(   r   r   )r9   spatial_input_shapeilengths       r=   _spatial_output_shapeConv._spatial_output_shapeC  sq     '':;	
 <	 ))  #||A++A. <	
 		
 	
s   AA(c                    [         R                  " U5      R                  5       n[        U5      U R                  -
  S-
  n U R
                  S:X  a;  [         R                  " US U U R                  XS 5      -   U R                  /-   5      $ [         R                  " US U U R                  /-   U R                  XS-   S  5      -   5      $ ! [         a    [        SU R                   SU S35      ef = f)Nr   channels_lastzCOne of the dimensions in the output is <= 0 due to downsampling in z;. Consider increasing the input size. Received input shape zO which would produce output shape with a zero or negative value in a dimension.)
rR   rS   as_listlenr   r*   r   r$   r#   r   )r9   rY   
batch_ranks      r=   rU   Conv.compute_output_shapeO  s   nn[199;%		1A5
	?2~~,001KLM||n%  ~~,||n%00!^=M1NOP   	**.)) 5((3} 5 	s   A
C
 =C
 
'C1c                     g)NFr   )r9   rh   s     r=   _recreate_conv_opConv._recreate_conv_opj  s    r?   c                 |  > 0 SU R                   _SU R                  _SU R                  _SU R                  _SU R                  _SU R
                  _SU R                  _S[        R                  " U R                  5      _S	U R                  _S
[        R                  " U R                  5      _S[        R                  " U R                  5      _S[        R                  " U R                  5      _S[        R                  " U R                   5      _S[        R                  " U R"                  5      _S[$        R                  " U R&                  5      _S[$        R                  " U R(                  5      _n[*        TU ]Y  5       n[/        [1        UR3                  5       5      [1        UR3                  5       5      -   5      $ )Nr$   r   r   r(   r*   r   r%   r+   r,   r-   r.   r/   r0   r   r1   r2   )r$   r   r   r(   r*   r   r%   r   	serializer+   r,   r   r-   r.   r   r/   r0   r   r   r1   r2   r   
get_configdictrf   items)r9   configbase_configr<   s      r=   r   Conv.get_configm  s   
t||
4++
 t||
 t||	

 4++
 T//
 dkk
 +//@
 
 !,"8"8''#
  6 6t7L7L M
 !,"8"8''#
"  6 6t7L7L M#
$ #L$:$:))%%
*  !6!6t7M7M!N+
, {44T5I5IJ-
0 g(*D**,-V\\^0DDEEr?   c                    U R                   S   U R                  S   S-
  -  n[        UR                  SS5      c  SnO[	        UR                  5      S-
  nU R
                  S:X  a  SS//U-  US/SS//-   nU$ SS//U-  SS/US//-   nU$ )z;Calculates padding for 'causal' option for 1-d conv layers.r   r   ndimsNr   r   )r   r   getattrrK   r   r*   )r9   rh   left_padr   causal_paddings        r=   ry   Conv._compute_causal_padding  s    %%a(D,<,<Q,?!,CD6<<$/7JV\\*Q.J. !fX
2xmaV5LLN   !!fX
2q!fxm5LLNr?   c                 B    U R                   S:X  a  SU R                  -
  $ g)Nr   r   )r*   r   )r9   s    r=   rW   Conv._get_channel_axis  s"    //		>!r?   c                     U R                  5       nUR                  U   R                  c  [        SU SU S35      e[	        X   5      $ )NzSThe channel dimension of the inputs should be defined. The input_shape received is z, where axis z> (0-based) is the channel dimension, which found to be `None`.)rW   dimsvaluer#   r"   )r9   rY   r\   s      r=   rT   Conv._get_input_channel  sd    --/L)//7//:m <*^ ,FF  ;,--r?   c                     U R                   S:X  a  SnOU R                   n[        U[        [        45      (       d  UR	                  5       nU$ )Nr   valid)r(   r    rf   tuplerc   )r9   
op_paddings     r=   _get_padding_opConv._get_padding_op  sA    <<8# JJ*tUm44#))+Jr?   )r6   r5   r8   r+   rP   r2   r.   r0   rX   r*   r   r$   r%   r3   rJ   r1   r-   r/   r   r(   r   r   r,   )r   r   Nr   r   NTglorot_uniformzerosNNNNNTNN)rg   
__module____qualname____firstlineno____doc__r   r4   r]   rj   rR   functionrn   r   r   rU   r   r   ry   rW   rT   r   __static_attributes____classcell__)r<   s   @r=   r   r      s    >J + !+F
P&P-^
* [[T"3 #3+Z

6F8	. r?   r   )r   tensorflow.compat.v2compatv2rR   tf_keras.srcr   r   r   r   tf_keras.src.engine.base_layerr   tf_keras.src.engine.input_specr   tf_keras.src.utilsr	   r   r   r?   r=   <module>r      s5    / " ! $ $ % % 0 4 )Q5 Qr?   