
    2Vhk                         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 d dlmZ d dlmZ  G d dee      Z G d de      Zy)    )activations)backend)constraints)initializers)ops)regularizers)tree)	InputSpec)Layer)DropoutRNNCell)RNN)operation_utils)argument_validationc                        e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
 fd	ZddZddZddZddZddZ	d Z
 fd	Z xZS )ConvLSTMCella  Cell class for the ConvLSTM layer.

    Args:
        rank: Integer, rank of the convolution, e.g. "2" for 2D convolutions.
        filters: Integer, the dimensionality of the output space
            (i.e. the number of output filters in the convolution).
        kernel_size: An integer or tuple/list of n integers, specifying the
            dimensions of the convolution window.
        strides: An integer or tuple/list of n integers, specifying the strides
            of the convolution. Specifying any stride value != 1
            is incompatible with specifying any `dilation_rate` value != 1.
        padding: One of `"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: A string, one of `channels_last` (default) or
            `channels_first`. When unspecified, uses
            `image_data_format` value found in your Keras config file at
            `~/.keras/keras.json` (if exists) else 'channels_last'.
            Defaults to `'channels_last'`.
        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.
        activation: Activation function. If `None`, no activation is applied.
        recurrent_activation: Activation function to use for the recurrent step.
        use_bias: Boolean, (default `True`), whether the layer
            should use a bias vector.
        kernel_initializer: Initializer for the `kernel` weights matrix,
            used for the linear transformation of the inputs. Default:
            `"glorot_uniform"`.
        recurrent_initializer: Initializer for the `recurrent_kernel`
            weights matrix, used for the linear transformation of the recurrent
            state. Default: `"orthogonal"`.
        bias_initializer: Initializer for the bias vector. Default: `"zeros"`.
        unit_forget_bias: Boolean (default `True`). If `True`,
            add 1 to the bias of the forget gate at initialization.
            Setting it to `True` will also force `bias_initializer="zeros"`.
            This is recommended in [Jozefowicz et al.](
            https://github.com/mlresearch/v37/blob/gh-pages/jozefowicz15.pdf)
        kernel_regularizer: Regularizer function applied to the `kernel` weights
            matrix. Default: `None`.
        recurrent_regularizer: Regularizer function applied to the
            `recurrent_kernel` weights matrix. Default: `None`.
        bias_regularizer: Regularizer function applied to the bias vector.
            Default: `None`.
        activity_regularizer: Regularizer function applied to the output of the
            layer (its "activation"). Default: `None`.
        kernel_constraint: Constraint function applied to the `kernel` weights
            matrix. Default: `None`.
        recurrent_constraint: Constraint function applied to the
            `recurrent_kernel` weights matrix. Default: `None`.
        bias_constraint: Constraint function applied to the bias vector.
            Default: `None`.
        dropout: Float between 0 and 1. Fraction of the units to drop for the
            linear transformation of the inputs. Default: 0.
        recurrent_dropout: Float between 0 and 1. Fraction of the units to drop
            for the linear transformation of the recurrent state. Default: 0.
        seed: Random seed for dropout.

    Call arguments:
        inputs: A (2+ `rank`)D tensor.
        states:  List of state tensors corresponding to the previous timestep.
        training: Python boolean indicating whether the layer should behave in
            training mode or in inference mode. Only relevant when `dropout` or
            `recurrent_dropout` is used.
    c                 0   t        |   di | || _        t        j                  j                  |      | _        || _        | j                  dkD  rt        d| d|       || _	        t        j                  || j                  d      | _        t        j                  || j                  dd      | _        t        j                  |      | _        t        j                   |      | _        t        j                  || j                  d	      | _        t'        j(                  |      | _        t'        j(                  |	      | _        |
| _        t1        j(                  |      | _        t1        j(                  |      | _        t1        j(                  |      | _        || _        t;        j(                  |      | _        t;        j(                  |      | _        t;        j(                  |      | _         tC        j(                  |      | _"        tC        j(                  |      | _#        tC        j(                  |      | _$        tK        d
tM        d|            | _'        tK        d
tM        d|            | _(        d| _)        tU        |dz         | _+        d| _,        y )N)seed   zRank z< convolutions are not currently implemented. Received: rank=kernel_sizestridesT
allow_zerodilation_rate      ?              ndim )-super__init__r   r   randomSeedGeneratorseed_generatorrank
ValueErrorfiltersr   standardize_tupler   r   standardize_paddingpaddingstandardize_data_formatdata_formatr   r   get
activationrecurrent_activationuse_biasr   kernel_initializerrecurrent_initializerbias_initializerunit_forget_biasr   kernel_regularizerrecurrent_regularizerbias_regularizerr   kernel_constraintrecurrent_constraintbias_constraintminmaxdropoutrecurrent_dropoutdropout_mask_countr
   
input_spec
state_size)selfr'   r)   r   r   r,   r.   r   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r?   r@   r   kwargs	__class__s                            N/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/rnn/conv_lstm.pyr#   zConvLSTMCell.__init__U   s#   6 	"6"	%nn:::E	99q=v //3f6  .@@M
 +<<TYY	d
 +>>wG"::;G0BB499o
 &//*5$/OO4H$I! "."2"23E"F%1%5%56K%L" , 0 01A B 0"."2"23E"F%1%5%56K%L" , 0 01A B!,1B!C$/OO4H$I!*?3C 12!$S#c3D*E!F"##2    c                      j                   dk(  rd}|dd   _        nd}|dd  _        d  j                  v rt        d|       ||   t        d|       t         j                  dz   d|dd  z   	       _        ||   }| _         j                  | j                  d
z  fz    _	         j                   j                   j                  d
z  fz   } j                   j                   j                  d j                   j                         _         j                  | j                  d j                    j"                         _         j&                  r\ j(                  r fd}n j*                  } j                   j                  d
z  fd| j,                   j.                         _        y d  _        y )Nchannels_first   r   r    zvConvLSTM layers only support static input shapes for the spatial dimension. Received invalid input shape: input_shape=zvThe channel dimension of the inputs (last axis) should be defined. Found None. Full input shape received: input_shape=r   N)r   shaper   kernel)rM   initializernameregularizer
constraintrecurrent_kernelc           	         t        j                   j                  j                  fg|i | t	        j
                  d      j                  fg|i | j                  j                  dz  fg|i |g      S )Nonesr   )r   concatenater5   r)   r   r/   )_argsrE   rD   s      rG   r5   z,ConvLSTMCell.build.<locals>.bias_initializer   s    ??1D11!%26:@ 5L,,V4!%26:@ 2D11!%!1 36:>D
 rH   bias)rM   rP   rO   rQ   rR   )r.   spatial_dimsr(   r
   r'   rB   	input_dimr   r)   kernel_shape
add_weightr3   r7   r:   rN   r4   r8   r;   rS   r2   r6   r5   r9   r<   rY   )rD   inputs_shapestates_shapechannel_axisr[   recurrent_kernel_shaper5   s   `      rG   buildzConvLSTMCell.build   s   //L ,QR 0DL ,Qr 2D4$$$==INL 
 %-+n. 
 $QgQR0@&@
 !.	" ,,	4<<!;K/LL!%!1!1LLLL15
 "

 oo##////-- & 
 !%(22#2200 !0 !
 ==$$  $(#8#8 ||a'), 11// ( DI DIrH   c                    |d   }|d   }|rJd| j                   cxk  rdk  r5n n2| j                  |      }||d   z  }||d   z  }||d   z  }	||d   z  }
n|}|}|}	|}
|rJd| j                  cxk  rdk  r5n n2| j                  |      }||d   z  }||d   z  }||d   z  }||d   z  }n|}|}|}|}t	        j
                  | j                  d| j                  dz         \  }}}}t	        j
                  | j                  d| j                  dz         \  }}}}| j                  r&t	        j
                  | j                  d      \  }}}}nd	\  }}}}| j                  |||| j                  
      }| j                  |||| j                  
      }| j                  |	||| j                  
      }| j                  |
||| j                  
      }| j                  ||      } | j                  ||      }!| j                  ||      }"| j                  ||      }#| j                  || z         }$| j                  ||!z         }%|%|z  |$| j                  ||"z         z  z   }&| j                  ||#z         }'|'| j                  |&      z  }(|(|(|&gfS )Nr   rK   r   r   r   r   r   )axis)NNNN)r,   )r?   get_dropout_maskr@   get_recurrent_dropout_maskr   splitrN   r'   rS   r2   rY   
input_convr,   recurrent_convr1   r0   ))rD   inputsstatestrainingh_tm1c_tm1dp_maskinputs_iinputs_finputs_cinputs_orec_dp_maskh_tm1_ih_tm1_fh_tm1_ch_tm1_okernel_ikernel_fkernel_ckernel_orecurrent_kernel_irecurrent_kernel_frecurrent_kernel_crecurrent_kernel_obias_ibias_fbias_cbias_ox_ix_fx_cx_oh_ih_fh_ch_oifcohs)                                            rG   callzConvLSTMCell.call   s   q	q	dll0S0++F3G
*H
*H
*H
*HHHHHd44:s:99%@Kk!n,Gk!n,Gk!n,Gk!n,GGGGG3699KKQ4
08Xx IId++QTYY]C	
 ==-0YYtyy!-D*FFFF-C*FFFFooh&$,,oOooh&$,,oOooh&$,,oOooh&$,,oO!!'+=>!!'+=>!!'+=>!!'+=>%%cCi0%%cCi0IDOOC#I666%%cCi0""1a&yrH   c           	          t        j                  || j                  | j                  | j                  | j
                  | j                  | j                        }|||gfS )Nr   r,   r.   r   )r   compute_conv_output_shaper)   r   r   r,   r.   r   )rD   r^   r_   conv_output_shapes       rG   compute_output_shapez!ConvLSTMCell.compute_output_shape$  sZ    +EELLLLLL((,,
 !#46G"HHHrH   c                 @   | j                   dk(  r|f| j                  z   | j                  fz   }n|| j                  f| j                  z   }| j                  |      d   }t	        j
                  || j                        t	        j
                  || j                        gS )Nchannels_lastr   )dtype)r.   rZ   r[   r   r   zeroscompute_dtype)rD   
batch_sizeinput_shapestate_shapes       rG   get_initial_statezConvLSTMCell.get_initial_state0  s    .%-$*;*;;t~~>OOK%t~~69J9JJK//<Q?IIk););<IIk););<
 	
rH   c                 N   t        j                  ||| j                  || j                  | j                        }|i| j                  dk(  r!d| j
                  dz   z  | j                  fz   }nd| j                  fd| j
                  z  z   }t        j                  ||      }||z  }|S )Nr   r   )rK   rK   )r   convr   r.   r   r'   r)   reshape)rD   xwbr,   conv_out
bias_shaperY   s           rG   rh   zConvLSTMCell.input_conv;  s    88LL((,,
 =?2!TYY]3t||oE
.		1AA
;;q*-DHrH   c                     t        j                  d| j                  dd      }t        j                  |||d| j
                        }|S )NrK   r   Tr   same)r   r,   r.   )r   r*   r'   r   r   r.   )rD   r   r   r   r   s        rG   ri   zConvLSTMCell.recurrent_convM  sG    %77tyy)
 88q'6t?O?O
 rH   c                     i d| j                   d| j                  d| j                  d| j                  d| j                  d| j
                  dt        j                  | j                        dt        j                  | j                        d	| j                  d
t        j                  | j                        dt        j                  | j                        dt        j                  | j                        d| j                  dt!        j                  | j"                        dt!        j                  | j$                        dt!        j                  | j&                        dt)        j                  | j*                        t)        j                  | j,                        t)        j                  | j.                        | j0                  | j2                  | j4                  d}t6        | q         }i ||S )Nr)   r   r   r,   r.   r   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   )r;   r<   r?   r@   r   )r)   r   r   r,   r.   r   r   	serializer0   r1   r2   r   r3   r4   r5   r6   r   r7   r8   r9   r   r:   r;   r<   r?   r@   r   r"   
get_configrD   configbase_configrF   s      rG   r   zConvLSTMCell.get_configV  s   #
t||#
4++#
 t||#
 t||	#

 4++#
 T//#
 +//@#
 #K$9$9))%#
 #
 !,"8"8''##
 $\%;%;**&#
$  6 6t7L7L M%#
&  5 5'#
( !,"8"8''#)#
. $\%;%;**&/#
4  6 6t7L7L M5#
6  !6!6t7M7M!N7#
8 %0$9$9))%  +44T5I5IJ||!%!7!7IIE#
H g(*(+(((rH   )rK   validNrK   tanhsigmoidTglorot_uniform
orthogonalr   TNNNNNNr   r   NrL   )F)Nr   )__name__
__module____qualname____doc__r#   rb   r   r   r   rh   ri   r   __classcell__rF   s   @rG   r   r      s{    BR &+* "!1ENJX:x
I	
$&) &)rH   r   c                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zd fd	ZddZd Zed        Z	ed        Z
ed        Zed	        Zed
        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Z fdZed        Z  xZ!S ) ConvLSTMaS  Abstract N-D Convolutional LSTM layer (used as implementation base).

    Similar to an LSTM layer, but the input transformations
    and recurrent transformations are both convolutional.

    Args:
        rank: Integer, rank of the convolution, e.g. "2" for 2D convolutions.
        filters: Integer, the dimensionality of the output space
            (i.e. the number of output filters in the convolution).
        kernel_size: An integer or tuple/list of n integers, specifying the
            dimensions of the convolution window.
        strides: An integer or tuple/list of n integers,
            specifying the strides of the convolution.
            Specifying any stride value != 1 is incompatible with specifying
            any `dilation_rate` value != 1.
        padding: One of `"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: 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, time, ..., channels)`
            while `channels_first` corresponds to
            inputs with shape `(batch, time, channels, ...)`.
            When unspecified, uses
            `image_data_format` value found in your Keras config file at
            `~/.keras/keras.json` (if exists) else 'channels_last'.
            Defaults to `'channels_last'`.
        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.
        activation: Activation function to use.
            By default hyperbolic tangent activation function is applied
            (`tanh(x)`).
        recurrent_activation: Activation function to use
            for the recurrent step.
        use_bias: Boolean, whether the layer uses a bias vector.
        kernel_initializer: Initializer for the `kernel` weights matrix,
            used for the linear transformation of the inputs.
        recurrent_initializer: Initializer for the `recurrent_kernel`
            weights matrix,
            used for the linear transformation of the recurrent state.
        bias_initializer: Initializer for the bias vector.
        unit_forget_bias: Boolean.
            If True, add 1 to the bias of the forget gate at initialization.
            Use in combination with `bias_initializer="zeros"`.
            This is recommended in [Jozefowicz et al., 2015](
            http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf)
        kernel_regularizer: Regularizer function applied to
            the `kernel` weights matrix.
        recurrent_regularizer: Regularizer function applied to
            the `recurrent_kernel` weights matrix.
        bias_regularizer: Regularizer function applied to the bias vector.
        activity_regularizer: Regularizer function applied to.
        kernel_constraint: Constraint function applied to
            the `kernel` weights matrix.
        recurrent_constraint: Constraint function applied to
            the `recurrent_kernel` weights matrix.
        bias_constraint: Constraint function applied to the bias vector.
        dropout: Float between 0 and 1.
            Fraction of the units to drop for
            the linear transformation of the inputs.
        recurrent_dropout: Float between 0 and 1.
            Fraction of the units to drop for
            the linear transformation of the recurrent state.
        seed: Random seed for dropout.
        return_sequences: Boolean. Whether to return the last output
            in the output sequence, or the full sequence. (default False)
        return_state: Boolean Whether to return the last state
            in addition to the output. (default False)
        go_backwards: Boolean (default False).
            If True, process the input sequence backwards.
        stateful: Boolean (default False). If True, the last state
            for each sample at index i in a batch will be used as initial
            state for the sample of index i in the following batch.
    c                    t        di d|d|d|d|d|d|d|d|d	|	d
|
d|d|d|d|d|d|d|d|d|d|d|d|d|ddd|j                  d      }t        |   |f||||d| t	        |dz         | _        y )Nr'   r)   r   r   r,   r.   r   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r?   r@   r   rP   conv_lstm_cellr   )return_sequencesreturn_statego_backwardsstatefulr   r   r!   )r   r/   r"   r#   r
   rB   )rD   r'   r)   r   r   r,   r.   r   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r?   r@   r   r   r   r   r   rE   cellrF   s                                 rG   r#   zConvLSTM.__init__  sM   >  


 $
 	

 
 $
 (
 "
 "6
 
  2
 #8
 .
 .
  2
  #8!
" .#
$ 0%
& "6'
( ,)
* +
, 0-
. /
0 "1
2 **W%3
6 		
-%%	
 	
 $2rH   c                 *    t         |   ||||      S )N)initial_statemaskrl   )r"   r   )rD   	sequencesr   r   rl   rF   s        rG   r   zConvLSTM.call  s"    w|]  
 	
rH   c                     |d   }|d   }|f|dd  z   }| j                   j                  |      d   dd  }| j                  r||f|z   }n|f|z   }| j                  r|f|z   }|||fS |S )Nr   rK   r   )r   r   r   r   )	rD   sequences_shapeinitial_state_shaper   steps
step_shaper   output_shapebatched_state_shapes	            rG   r   zConvLSTM.compute_output_shape  s    $Q'
" ]_QR%88
ii44Z@CABG   L
 '=;6L#--+"=!46IIIrH   c                     t        j                  |      d   }| j                  r|nd }| j                  r
d d g}|g|z   S |S )Nr   )r	   flattenr   r   )rD   rW   r   output_mask
state_masks        rG   compute_maskzConvLSTM.compute_mask,  sI    ||D!!$"33dJ=:--rH   c                 .    | j                   j                  S rL   )r   r)   rD   s    rG   r)   zConvLSTM.filters5      yy   rH   c                 .    | j                   j                  S rL   )r   r   r   s    rG   r   zConvLSTM.kernel_size9      yy$$$rH   c                 .    | j                   j                  S rL   )r   r   r   s    rG   r   zConvLSTM.strides=  r   rH   c                 .    | j                   j                  S rL   )r   r,   r   s    rG   r,   zConvLSTM.paddingA  r   rH   c                 .    | j                   j                  S rL   )r   r.   r   s    rG   r.   zConvLSTM.data_formatE  r   rH   c                 .    | j                   j                  S rL   )r   r   r   s    rG   r   zConvLSTM.dilation_rateI  s    yy&&&rH   c                 .    | j                   j                  S rL   )r   r0   r   s    rG   r0   zConvLSTM.activationM  s    yy###rH   c                 .    | j                   j                  S rL   )r   r1   r   s    rG   r1   zConvLSTM.recurrent_activationQ      yy---rH   c                 .    | j                   j                  S rL   )r   r2   r   s    rG   r2   zConvLSTM.use_biasU  s    yy!!!rH   c                 .    | j                   j                  S rL   )r   r3   r   s    rG   r3   zConvLSTM.kernel_initializerY      yy+++rH   c                 .    | j                   j                  S rL   )r   r4   r   s    rG   r4   zConvLSTM.recurrent_initializer]      yy...rH   c                 .    | j                   j                  S rL   )r   r5   r   s    rG   r5   zConvLSTM.bias_initializera      yy)))rH   c                 .    | j                   j                  S rL   )r   r6   r   s    rG   r6   zConvLSTM.unit_forget_biase  r   rH   c                 .    | j                   j                  S rL   )r   r7   r   s    rG   r7   zConvLSTM.kernel_regularizeri  r   rH   c                 .    | j                   j                  S rL   )r   r8   r   s    rG   r8   zConvLSTM.recurrent_regularizerm  r   rH   c                 .    | j                   j                  S rL   )r   r9   r   s    rG   r9   zConvLSTM.bias_regularizerq  r   rH   c                 .    | j                   j                  S rL   )r   r:   r   s    rG   r:   zConvLSTM.kernel_constraintu      yy***rH   c                 .    | j                   j                  S rL   )r   r;   r   s    rG   r;   zConvLSTM.recurrent_constrainty  r   rH   c                 .    | j                   j                  S rL   )r   r<   r   s    rG   r<   zConvLSTM.bias_constraint}  s    yy(((rH   c                 .    | j                   j                  S rL   )r   r?   r   s    rG   r?   zConvLSTM.dropout  r   rH   c                 .    | j                   j                  S rL   )r   r@   r   s    rG   r@   zConvLSTM.recurrent_dropout  r   rH   c                 V   i d| j                   d| j                  d| j                  d| j                  d| j                  d| j
                  dt        j                  | j                        dt        j                  | j                        d	| j                  d
t        j                  | j                        dt        j                  | j                        dt        j                  | j                        d| j                  dt!        j                  | j"                        dt!        j                  | j$                        dt!        j                  | j&                        dt!        j                  | j(                        t+        j                  | j,                        t+        j                  | j.                        t+        j                  | j0                        | j2                  | j4                  | j6                  j8                  d}t:        | y         }|d= i ||S )Nr)   r   r   r,   r.   r   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   activity_regularizer)r:   r;   r<   r?   r@   r   r   )r)   r   r   r,   r.   r   r   r   r0   r1   r2   r   r3   r4   r5   r6   r   r7   r8   r9   r   r   r:   r;   r<   r?   r@   r   r   r"   r   r   s      rG   r   zConvLSTM.get_config  s/   &
t||&
4++&
 t||&
 t||	&

 4++&
 T//&
 +//@&
 #K$9$9))%&
 &
 !,"8"8''#&
 $\%;%;**&&
$  6 6t7L7L M%&
&  5 5'&
( !,"8"8''#)&
. $\%;%;**&/&
4  6 6t7L7L M5&
6 #L$:$:))%7&
< "-!6!6t7M7M!N$/$9$9))%  +44T5I5IJ||!%!7!7IINNK&
N g(*(+(((rH   c                      | di |S )Nr!   r!   )clsr   s     rG   from_configzConvLSTM.from_config  s    }V}rH   )rK   r   NrK   r   r   Tr   r   r   TNNNNNNr   r   NFFFF)NNFrL   )"r   r   r   r   r#   r   r   r   propertyr)   r   r   r,   r.   r   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r?   r@   r   classmethodr   r   r   s   @rG   r   r     s(   Nj &+* "!9B3H

& ! ! % % ! ! ! ! % % ' ' $ $ . . " " , , / / * * * * , , / / * * + + . . ) ) ! ! + +*)X  rH   r   N)	keras.srcr   r   r   r   r   r   r	   keras.src.layers.input_specr
   keras.src.layers.layerr   %keras.src.layers.rnn.dropout_rnn_cellr   keras.src.layers.rnn.rnnr   keras.src.opsr   keras.src.utilsr   r   r   r!   rH   rG   <module>r      sK    !  ! "  "  1 ( @ ( ) /l)5. l)^xs xrH   