
    &Vfjj                         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dS )    )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.
       validNtanhsigmoidTglorot_uniform
orthogonalzeros        c                 "    t                      j        di | || _        t          j                            |          | _        || _        | j        dk    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|                    | _(        tS          |dz             | _*        d| _+        d S )N)seed   zRank z< convolutions are not currently implemented. Received: rank=kernel_sizestridesT
allow_zerodilation_rateg      ?r      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_dropoutr
   
input_spec
state_size)selfr,   r.   r   r   r1   r3   r!   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rD   rE   r   kwargs	__class__s                            [/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/layers/rnn/conv_lstm.pyr(   zConvLSTMCell.__init__U   sV   6 	""6"""	%n:::EE	9q==6 6 6/36 6   .@M
 
 +<TY	d
 
 
 +>wGG":;GG0B49o
 
 &/*55$/O4H$I$I! "."23E"F"F%1%56K%L%L" , 01A B B 0"."23E"F"F%1%56K%L%L" , 01A B B!,1B!C!C$/O4H$I$I!*??3C 1 122!$S#c3D*E*E!F!F#22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        d j         j                   _         
                    | j        d j         j                   _         j        rG j        r fd}n j        } 
                     j        d
z  fd| j         j                   _        nd  _        d _        d S )Nchannels_firstr   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$   shape   kernel)rP   initializernameregularizer
constraintrecurrent_kernelc           	          t          j         j        j        fg|R i | t	          j        d          j        fg|R i | j        j        dz  fg|R i |g          S )Nonesr"   )r   concatenater:   r.   r   r4   )_argsrI   rH   s      rK   r:   z,ConvLSTMCell.build.<locals>.bias_initializer   s    ?1D1!%26  :@  5L,V44!%26  :@  2D1!%!1 36:  >D 
  rL   bias)rP   rT   rS   rU   rV   T)r3   spatial_dimsr-   r
   r,   rF   	input_dimr   r.   kernel_shape
add_weightr8   r<   r?   rR   r9   r=   r@   rW   r7   r;   r:   r>   rA   r]   built)rH   inputs_shapestates_shapechannel_axisr_   recurrent_kernel_shaper:   s   `      rK   buildzConvLSTMCell.build   s$   ///L ,QRR 0DL ,QrT 2D4$$$L=IL L  
 %-.+. .  
 $QgQRR0@&@
 
 
 !.	" ,	4<!;K/LL!%!1LL15
 "

 oo#//- & 
 
 !%(2#20 !0 !
 !
 = 	$ 9       $(#8 |a'), 1/ (  DII DI


rL   Fc                    |d         }|d         }|}|}|}|}	|}
|}|}|}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                  }| 	                    |
|          }| 	                    ||          }| 	                    ||          } | 	                    ||          }!| 
                    ||z             }"| 
                    ||z             }#|#|z  |"|                     || z             z  z   }$| 
                    ||!z             }%|%|                     |$          z  }&|&|&|$gfS )Nr   r   rQ   )axis)NNNN)r1   )r   splitrR   r,   rW   r7   r]   
input_convr1   recurrent_convr6   r5   )'rH   inputsstatestrainingh_tm1c_tm1inputs_iinputs_finputs_cinputs_o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'                                          rK   callzConvLSTMCell.call   s%   q	q	 369KQ4
 4
 4
08Xx Id+QTY]CCC	
 = 	D-0Yty!-D-D*FFFFF-C*FFFFooh&$,oOOooh&$,oOOooh&$,oOOooh&$,oOO!!'+=>>!!'+=>>!!'+=>>!!'+=>>%%cCi00%%cCi00IDOOC#I6666%%cCi00"""1a&yrL   c           	          t          j        || j        | j        | j        | j        | j        | j                  }|||gfS )Nr   r1   r3   r!   )r   compute_conv_output_shaper.   r   r   r1   r3   r!   )rH   rc   rd   conv_output_shapes       rK   compute_output_shapez!ConvLSTMCell.compute_output_shape  sR    +ELLL(,
 
 
 !#46G"HHHrL   c                    | j         dk    r|f| j        z   | j        fz   }n|| j        f| j        z   }|                     |          d         }t	          j        || j                  t	          j        || j                  gS )Nchannels_lastr   )dtype)r3   r^   r_   r   r   r   compute_dtype)rH   
batch_sizeinput_shapestate_shapes       rK   get_initial_statezConvLSTMCell.get_initial_state*  s    ..%-$*;;t~>OOKK%t~69JJK//<<Q?Ik);<<<Ik);<<<
 	
rL   c                     t          j        ||| j        || j        | j                  }|P| 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   )r   r   )r   convr   r3   r!   r,   r.   reshape)rH   xwbr1   conv_out
bias_shaper]   s           rK   rk   zConvLSTMCell.input_conv5  s    8L(,
 
 
 =?22!TY]3t|oE

.	1AA
;q*--DHrL   c                 |    t          j        d| j        dd          }t          j        |||d| j                  }|S )Nr   r   Tr   same)r   r1   r3   )r   r/   r,   r   r   r3   )rH   r   r   r   r   s        rK   rl   zConvLSTMCell.recurrent_convG  sQ    %7ty)
 
 
 8q'6t?O
 
 
 rL   c                 2   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                  | j        | j        | j        d}t7                                                      }i ||S )Nr.   r   r   r1   r3   r!   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   )r@   rA   rD   rE   r   )r.   r   r   r1   r3   r!   r   	serializer5   r6   r7   r   r8   r9   r:   r;   r   r<   r=   r>   r   r?   r@   rA   rD   rE   r   r'   
get_configrH   configbase_configrJ   s      rK   r   zConvLSTMCell.get_configP  s   #
t|#
4+#
 t|#
 t|	#

 4+#
 T/#
 +/@@#
 #K$9)% %#
 #
 !,"8'# ##
 $\%;*& &#
$  6t7L M M%#
&  5'#
( !,"8'# #)#
. $\%;*& &/#
4  6t7L M M5#
6  !6t7M!N!N7#
8 %0$9)% %  +4T5IJJ|!%!7IE#
 #
 #
H gg((**(+(((rL   )r   r   Nr   r   r   Tr   r   r   TNNNNNNr   r   NrO   )F)Nr   )__name__
__module____qualname____doc__r(   rg   r   r   r   rk   rl   r   __classcell__rJ   s   @rK   r   r      s%       B BR &+* "!1D D D D D DLK K K KZ4 4 4 4l
I 
I 
I 
I	
 	
 	
 	
   $  &) &) &) &) &) &) &) &) &)rL   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.
    r   r   Nr   r   Tr   r   r   r   Fc                 F   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|                    d          } t                      j        |f||||d| t	          |dz             | _        d S )Nr,   r.   r   r   r1   r3   r!   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rD   rE   r   rT   conv_lstm_cellr   )return_sequencesreturn_statego_backwardsstatefulr   r#   r&   )r   r4   r'   r(   r
   rF   )rH   r,   r.   r   r   r1   r3   r!   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rD   rE   r   r   r   r   r   rI   cellrJ   s                                 rK   r(   zConvLSTM.__init__  s   >  
 
 

G
 $
 G	

 G
 $
 (-
 "z
 "6!5
 X
  21
 #8"7
 .-
 .-
  21
  #8"7!
" .-#
$ 0/%
& "6!5'
( ,O)
* G+
, 0/-
. /
0 "!1
2 **W%%%3
6 		
-%%	
 	
 	
 	
 	
 $222rL   c                 N    t                                          ||||          S )N)initial_statemaskro   )r'   r   )rH   	sequencesr   r   ro   rJ   s        rK   r   zConvLSTM.call  s+    ww||]  
 
 	
rL   c                     |d         }|d         }|f|dd          z   }| j                             |          d         dd          }| j        r||f|z   }n|f|z   }| j        r|f|z   }|||fS |S )Nr   r   r"   )r   r   r   r   )	rH   sequences_shapeinitial_state_shaper   steps
step_shaper   output_shapebatched_state_shapes	            rK   r   zConvLSTM.compute_output_shape  s    $Q'
" ]_QRR%88
i44Z@@CABBG  	7 LL
 '=;6L 	J#--+"=!46IIIrL   c                 r    t          j        |          d         }| j        r|nd }| j        r
d d g}|g|z   S |S )Nr   )r	   flattenr   r   )rH   r[   r   output_mask
state_masks        rK   compute_maskzConvLSTM.compute_mask&  sN    |D!!!$"3=dd 	J=:--rL   c                     | j         j        S rO   )r   r.   rH   s    rK   r.   zConvLSTM.filters/      y  rL   c                     | j         j        S rO   )r   r   r   s    rK   r   zConvLSTM.kernel_size3      y$$rL   c                     | j         j        S rO   )r   r   r   s    rK   r   zConvLSTM.strides7  r   rL   c                     | j         j        S rO   )r   r1   r   s    rK   r1   zConvLSTM.padding;  r   rL   c                     | j         j        S rO   )r   r3   r   s    rK   r3   zConvLSTM.data_format?  r   rL   c                     | j         j        S rO   )r   r!   r   s    rK   r!   zConvLSTM.dilation_rateC  s    y&&rL   c                     | j         j        S rO   )r   r5   r   s    rK   r5   zConvLSTM.activationG  s    y##rL   c                     | j         j        S rO   )r   r6   r   s    rK   r6   zConvLSTM.recurrent_activationK      y--rL   c                     | j         j        S rO   )r   r7   r   s    rK   r7   zConvLSTM.use_biasO  s    y!!rL   c                     | j         j        S rO   )r   r8   r   s    rK   r8   zConvLSTM.kernel_initializerS      y++rL   c                     | j         j        S rO   )r   r9   r   s    rK   r9   zConvLSTM.recurrent_initializerW      y..rL   c                     | j         j        S rO   )r   r:   r   s    rK   r:   zConvLSTM.bias_initializer[      y))rL   c                     | j         j        S rO   )r   r;   r   s    rK   r;   zConvLSTM.unit_forget_bias_  r   rL   c                     | j         j        S rO   )r   r<   r   s    rK   r<   zConvLSTM.kernel_regularizerc  r   rL   c                     | j         j        S rO   )r   r=   r   s    rK   r=   zConvLSTM.recurrent_regularizerg  r   rL   c                     | j         j        S rO   )r   r>   r   s    rK   r>   zConvLSTM.bias_regularizerk  r   rL   c                     | j         j        S rO   )r   r?   r   s    rK   r?   zConvLSTM.kernel_constrainto      y**rL   c                     | j         j        S rO   )r   r@   r   s    rK   r@   zConvLSTM.recurrent_constraints  r   rL   c                     | j         j        S rO   )r   rA   r   s    rK   rA   zConvLSTM.bias_constraintw  s    y((rL   c                     | j         j        S rO   )r   rD   r   s    rK   rD   zConvLSTM.dropout{  r   rL   c                     | j         j        S rO   )r   rE   r   s    rK   rE   zConvLSTM.recurrent_dropout  r   rL   c                 r   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        | j                  | j        | j        | j        j        d}t;                                                      }|d= i ||S )Nr.   r   r   r1   r3   r!   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   activity_regularizer)r?   r@   rA   rD   rE   r   r   )r.   r   r   r1   r3   r!   r   r   r5   r6   r7   r   r8   r9   r:   r;   r   r<   r=   r>   r   r   r?   r@   rA   rD   rE   r   r   r'   r   r   s      rK   r   zConvLSTM.get_config  s   &
t|&
4+&
 t|&
 t|	&

 4+&
 T/&
 +/@@&
 #K$9)% %&
 &
 !,"8'# #&
 $\%;*& &&
$  6t7L M M%&
&  5'&
( !,"8'# #)&
. $\%;*& &/&
4  6t7L M M5&
6 #L$:)% %7&
< "-!6t7M!N!N$/$9)% %  +4T5IJJ|!%!7INK&
 &
 &
N gg((**(+(((rL   c                      | di |S )Nr&   r&   )clsr   s     rK   from_configzConvLSTM.from_config  s    s}}V}}rL   )r   r   Nr   r   r   Tr   r   r   TNNNNNNr   r   NFFFF)NNFrO   )"r   r   r   r   r(   r   r   r   propertyr.   r   r   r1   r3   r!   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rD   rE   r   classmethodr   r   r   s   @rK   r   r   y  sH       N Nj &+* "!9B3 B3 B3 B3 B3 B3H
 
 
 
 
 

   &   ! ! X! % % X% ! ! X! ! ! X! % % X% ' ' X' $ $ X$ . . X. " " X" , , X, / / X/ * * X* * * X* , , X, / / X/ * * X* + + X+ . . X. ) ) X) ! ! X! + + X+*) *) *) *) *)X   [    rL   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&   rL   rK   <module>r      sb   ! ! ! ! ! !       ! ! ! ! ! ! " " " " " "       " " " " " "       1 1 1 1 1 1 ( ( ( ( ( ( @ @ @ @ @ @ ( ( ( ( ( ( ) ) ) ) ) ) / / / / / /f) f) f) f) f)5. f) f) f)Rx x x x xs x x x x xrL   