
    2Vh)                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ  G d de      Zy)z2Keras base class for transpose convolution layers.    )activations)constraints)initializers)ops)regularizers)standardize_data_format)#compute_conv_transpose_output_shape)	InputSpec)Layer)standardize_padding)standardize_tuplec                   `     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zd Zd Zd Z fdZ xZ	S )BaseConvTransposea)  Abstract N-D transposed convolution layer.

    The need for transposed convolutions generally arises from the desire to use
    a transformation going in the opposite direction of a normal convolution,
    i.e., from something that has the shape of the output of some convolution to
    something that has the shape of its input while maintaining a connectivity
    pattern that is compatible with said convolution.

    Args:
        rank: int, the rank of the transposed convolution, e.g. 2 for 2D
            transposed convolution.
        filters: int, the dimension of the output space (the number of filters
            in the transposed convolution).
        kernel_size: int or tuple/list of `rank` integers, specifying the size
            of the transposed convolution window.
        strides: int or tuple/list of `rank` integers, specifying the stride
            length of the transposed convolution. If only one int is specified,
            the same stride size will be used for all dimensions.
            `strides > 1` is incompatible with `dilation_rate > 1`.
        padding: string, either `"valid"` or `"same"` (case-insensitive).
            `"valid"` means no padding. `"same"` results in padding evenly to
            the left/right or up/down of the input such that output has the same
            height/width dimension as the input.
        data_format: string, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, steps, features)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, features, steps)`. It defaults to the `image_data_format`
            value found in your Keras config file at `~/.keras/keras.json`.
            If you never set it, then it will be `"channels_last"`.
        dilation_rate: int or tuple/list of `rank` integers, specifying the
            dilation rate to use for dilated convolution. If only one int is
            specified, the same dilation rate will be used for all dimensions.
        activation: Activation function. If `None`, no activation is applied.
        use_bias: bool, if `True`, bias will be added to the output.
        kernel_initializer: Initializer for the convolution kernel. If `None`,
            the default initializer (`"glorot_uniform"`) will be used.
        bias_initializer: 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`.
    c                    t        |   d|||d| || _        || _        t	        ||d      | _        t	        ||d      | _        t	        ||d      | _        t        |      | _	        |d | _
        nt	        ||d      | _
        t        |      | _        t        j                  |	      | _        |
| _        t#        j                  |      | _        t#        j                  |      | _        t)        j                  |      | _        t)        j                  |      | _        t/        j                  |      | _        t/        j                  |      | _        t5        | j                  dz         | _        | j                  | _        | j                  (| j                  dk  rt9        d	| j                   d
      t;        | j
                        st9        d| j
                   d
      t;        | j                        st9        d| j                   d
      t=        | j                        dkD  r>t=        | j                        dkD  r%t9        d| j                   d| j                         y y )N)	trainablenameactivity_regularizerkernel_sizestridesdilation_rateoutput_padding   )min_ndimr   z[Invalid value for argument `filters`. Expected a strictly positive value. Received filters=.zBThe argument `kernel_size` cannot contain 0. Received kernel_size=z;The argument `strides` cannot contains 0. Received strides=   zW`strides > 1` not supported in conjunction with `dilation_rate > 1`. Received: strides=z and dilation_rate= )super__init__rankfiltersr   r   r   r   r   paddingr   r   data_formatr   get
activationuse_biasr   kernel_initializerbias_initializerr   kernel_regularizerbias_regularizerr   kernel_constraintbias_constraintr
   
input_spec
ValueErrorallmax)selfr   r    r   r   r!   r   r"   r   r$   r%   r&   r'   r(   r)   r   r*   r+   r   r   kwargs	__class__s                        b/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/convolutional/base_conv_transpose.pyr   zBaseConvTranspose.__init__G   sY   . 	 	
!5	
 		
 	,[$N($	B.4
 +73!"&D"3 #D
 3;?%//*5 "."2"23E"F , 0 01A B"."2"23E"F , 0 01A B!,1B!C*?#TYY];++<<#(9448LL>D 
 4##$#//03 
 4<< <<.+ 
 t||q S););%<q%@::>,, H!!%!3!3 46  &A     c           	      
   | j                   dk(  rd}|d   }nd}|d   }t        | j                  dz   ||i      | _        | j                  | j
                  |fz   }| j                  d|| j                  | j                  | j                  d| j                        | _        | j                  rQ| j                  d	| j
                  f| j                  | j                  | j                  d| j                        | _        y d | _        y )
Nchannels_lastr   r   )r   axeskernelT)r   shapeinitializerregularizer
constraintr   dtypebias)r"   r
   r   r,   r   r    
add_weightr&   r(   r*   r>   r9   r%   r'   r)   r+   r?   )r0   input_shapechannel_axisinput_channelkernel_shapes        r3   buildzBaseConvTranspose.build   s   .L'OML'NM#YY],)F
 ''LL+
 

 oo////--** & 
 ==||o 11 11//jj ( DI DIr4   c           	      "   t        j                  || j                  t        | j                        | j
                  | j                  | j                  | j                        }| j                  r| j                  dk(  r!d| j                  dz   z  | j                  fz   }nd| j                  fd| j                  z  z   }t        j                  | j                  |      }t        j                  ||      }| j                  | j                  |      S |S )N)r   r!   r   r   r"   r6   )r   r   )r   conv_transposer9   listr   r!   r   r   r"   r%   r   r    reshaper?   addr$   )r0   inputsoutputs
bias_shaper?   s        r3   callzBaseConvTranspose.call   s    $$KK&LL..,,((
 ==?2!TYY]3t||oE
.		1AA
;;tyy*5Dgggt,G??&??7++r4   c           
          t        || j                  | j                  | j                  | j                  | j
                  | j                  | j                        S )N)r   r!   r   r"   r   )r	   r   r    r   r!   r   r"   r   )r0   rA   s     r3   compute_output_shapez&BaseConvTranspose.compute_output_shape   sK    2LLLLLL..((,,	
 		
r4   c                    t         |          }|j                  | j                  | j                  | j
                  | j                  | j                  | j                  t        j                  | j                        | j                  t        j                  | j                        t        j                  | j                        t!        j                  | j"                        t!        j                  | j$                        t!        j                  | j&                        t)        j                  | j*                        t)        j                  | j,                        d       |S )N)r    r   r   r!   r"   r   r$   r%   r&   r'   r(   r)   r   r*   r+   )r   
get_configupdater    r   r   r!   r"   r   r   	serializer$   r%   r   r&   r'   r   r(   r)   r   r   r*   r+   )r0   configr2   s     r3   rR   zBaseConvTranspose.get_config   s   #%<<#//<<<<#//!%!3!3)33DOOD MM&2&<&<++' %1$:$:))% '3&<&<++' %1$:$:))% )5(>(>--) &1%:%:**& $/#8#89M9M#N7	
> r4   )r   validNNr   NTglorot_uniformzerosNNNNNTN)
__name__
__module____qualname____doc__r   rE   rN   rP   rR   __classcell__)r2   s   @r3   r   r      s\    2r + !)Pd#J.

! !r4   r   N)r\   	keras.srcr   r   r   r   r   keras.src.backendr   &keras.src.backend.common.backend_utilsr	   keras.src.layers.input_specr
   keras.src.layers.layerr   #keras.src.utils.argument_validationr   r   r   r   r4   r3   <module>rd      s;    8 ! ! "  " 5 2 ( C Ap pr4   