o
    i ea                     @   s   d Z ddlm  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 deejZG dd deZdS )z-Base class for N-D convolutional LSTM layers.    N)activations)backend)constraints)initializers)regularizers)
base_layer)ConvRNN)DropoutRNNCellMixin)
conv_utilsc                       s   e Zd ZdZ											
									d fdd	Z fddZdddZedd ZdddZ	dd Z
 fddZ  ZS )ConvLSTMCellag  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 to use. If you don't specify anything, no
        activation is applied
        (ie. "linear" activation: `a(x) = 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.
      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.
    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hard_sigmoidTglorot_uniform
orthogonalzeros        c                    sT  t  jdi | || _| jdkrtd| d| || _t|| jd| _tj|| jddd| _t	|| _
t|| _t|| jd| _t|| _t|	| _|
| _t|| _t|| _t|| _|| _t|| _t|| _t|| _t|| _t|| _t|| _t d	t!d
|| _"t d	t!d
|| _#| j| jf| _$d S )N   zRank z< convolutions are not currently implemented. Received: rank=kernel_sizestridesT
allow_zerodilation_rate      ?r    )%super__init__rank
ValueErrorfiltersr
   normalize_tupler   r   normalize_paddingpaddingnormalize_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
state_size)selfr   r    r   r   r#   r%   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r6   r7   kwargs	__class__r   R/var/www/myenv/lib/python3.10/site-packages/keras/src/layers/rnn/base_conv_lstm.pyr   \   sH   

zConvLSTMCell.__init__c                    s   t  |  jdkrd}nd}|| d u rtd| || } j| jd f  _ j j jd f } j j jd j	 j
d _ j| jd j jd _ jrv jra fd	d
}n j} j jd fd| j jd _nd  _d _d S )Nchannels_firstr   zvThe channel dimension of the inputs (last axis) should be defined. Found None. Full input shape received: input_shape=   kernel)shapeinitializernameregularizer
constraintrecurrent_kernelc                    s`   t  j jfg|R i |td jfg|R i | j jd fg|R i |gS )Nones   )r   concatenater,   r    r   r&   )_argsr:   r9   r   r=   r,      s0   
z,ConvLSTMCell.build.<locals>.bias_initializerbias)rB   rD   rC   rE   rF   T)r   buildr%   r   r   r    kernel_shape
add_weightr*   r.   r1   rA   r+   r/   r2   rG   r)   r-   r,   r0   r3   rN   built)r9   input_shapechannel_axis	input_dimrecurrent_kernel_shaper,   r;   rM   r=   rO      sV   



zConvLSTMCell.buildc           )      C   s0  |d }|d }| j ||dd}| j||dd}d| j  k r#dk r>n n||d  }||d  }	||d  }
||d  }n|}|}	|}
|}d| j  k rQdk rln n||d  }||d  }||d  }||d  }n|}|}|}|}tj| jd| jd d\}}}}tj| jd| jd d\}}}}| j	rt| j
d\}}}}nd	\}}}}| j|||| jd
}| j|	||| jd
}| j|
||| jd
}| j|||| jd
}| ||} | ||}!| ||}"| ||}#| ||  }$| ||! }%|%| |$| ||"   }&| ||# }'|'| |& }(|(|(|&gfS )Nr   r   r@   )countr   rI   r   )axis)NNNN)r#   )get_dropout_mask_for_cell#get_recurrent_dropout_mask_for_cellr6   r7   tfsplitrA   r   rG   r)   rN   
input_convr#   recurrent_convr(   r'   ))r9   inputsstatestrainingh_tm1c_tm1dp_maskrec_dp_mask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hr   r   r=   call   sd   zConvLSTMCell.callc                 C   s4   | j dkrtjS | j dkrtjS | j dkrtjS d S )Nr   rI   r   )r   r   conv1dconv2dconv3drM   r   r   r=   
_conv_func  s   


zConvLSTMCell._conv_funcc                 C   s:   | j ||| j|| j| jd}|d urtj||| jd}|S )N)r   r#   r%   r   )r%   )r   r   r%   r   r   bias_add)r9   xwbr#   conv_outr   r   r=   r]   (  s   zConvLSTMCell.input_convc                 C   s.   t jd| jddd}| j|||d| jd}|S )Nr   r   Tr   same)r   r#   r%   )r
   r!   r   r   r%   )r9   r   r   r   r   r   r   r=   r^   7  s   
zConvLSTMCell.recurrent_convc                    s
  i d| j d| jd| jd| jd| jd| jdt| jdt| j	d	| j
d
t| jdt| jdt| jd| jdt| jdt| jdt| jdt| jt| jt| j| j| jd}t  }tt| t|  S )Nr    r   r   r#   r%   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   )r2   r3   r6   r7   )r    r   r   r#   r%   r   r   	serializer'   r(   r)   r   r*   r+   r,   r-   r   r.   r/   r0   r   r1   r2   r3   r6   r7   r   
get_configdictlistitemsr9   configbase_configr;   r   r=   r   @  sl   

#zConvLSTMCell.get_config)r   r   Nr   r   r   Tr   r   r   TNNNNNNr   r   N)Nr   )__name__
__module____qualname____doc__r   rO   r   propertyr   r]   r^   r   __classcell__r   r   r;   r=   r      s:    AB
A@

	r   c                       st  e Zd ZdZ											
														d? fdd	Zd@ fdd	Zedd Zedd Zedd Z	edd Z
edd Zedd Zedd Zedd  Zed!d" Zed#d$ Zed%d& Zed'd( Zed)d* Zed+d, Zed-d. Zed/d0 Zed1d2 Zed3d4 Zed5d6 Zed7d8 Zed9d: Z fd;d<Zed=d> Z  ZS )AConvLSTMaG  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.
      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.
      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.
    r   r   Nr   r   Tr   r   r   Fr   c                    s   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}t j||f||||d| t|| _d S )Nr   r    r   r   r#   r%   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r6   r7   rD   conv_lstm_celldtype)return_sequencesreturn_statego_backwardsstatefulr   )r   r&   r   r   r   activity_regularizer)r9   r   r    r   r   r#   r%   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r   r1   r2   r3   r   r   r   r   r6   r7   r:   cellr;   r   r=   r     sz   	

	zConvLSTM.__init__c                    s   t  j||||dS )N)maskra   initial_state)r   r   )r9   r_   r   ra   r   r;   r   r=   r     s   zConvLSTM.callc                 C      | j jS r   )r   r    rM   r   r   r=   r         zConvLSTM.filtersc                 C   r   r   )r   r   rM   r   r   r=   r     r   zConvLSTM.kernel_sizec                 C   r   r   )r   r   rM   r   r   r=   r   	  r   zConvLSTM.stridesc                 C   r   r   )r   r#   rM   r   r   r=   r#     r   zConvLSTM.paddingc                 C   r   r   )r   r%   rM   r   r   r=   r%     r   zConvLSTM.data_formatc                 C   r   r   )r   r   rM   r   r   r=   r     r   zConvLSTM.dilation_ratec                 C   r   r   )r   r'   rM   r   r   r=   r'     r   zConvLSTM.activationc                 C   r   r   )r   r(   rM   r   r   r=   r(     r   zConvLSTM.recurrent_activationc                 C   r   r   )r   r)   rM   r   r   r=   r)   !  r   zConvLSTM.use_biasc                 C   r   r   )r   r*   rM   r   r   r=   r*   %  r   zConvLSTM.kernel_initializerc                 C   r   r   )r   r+   rM   r   r   r=   r+   )  r   zConvLSTM.recurrent_initializerc                 C   r   r   )r   r,   rM   r   r   r=   r,   -  r   zConvLSTM.bias_initializerc                 C   r   r   )r   r-   rM   r   r   r=   r-   1  r   zConvLSTM.unit_forget_biasc                 C   r   r   )r   r.   rM   r   r   r=   r.   5  r   zConvLSTM.kernel_regularizerc                 C   r   r   )r   r/   rM   r   r   r=   r/   9  r   zConvLSTM.recurrent_regularizerc                 C   r   r   )r   r0   rM   r   r   r=   r0   =  r   zConvLSTM.bias_regularizerc                 C   r   r   )r   r1   rM   r   r   r=   r1   A  r   zConvLSTM.kernel_constraintc                 C   r   r   )r   r2   rM   r   r   r=   r2   E  r   zConvLSTM.recurrent_constraintc                 C   r   r   )r   r3   rM   r   r   r=   r3   I  r   zConvLSTM.bias_constraintc                 C   r   r   )r   r6   rM   r   r   r=   r6   M  r   zConvLSTM.dropoutc                 C   r   r   )r   r7   rM   r   r   r=   r7   Q  r   zConvLSTM.recurrent_dropoutc                    s  i d| j d| jd| jd| jd| jd| jdt| jdt| j	d	| j
d
t| jdt| jdt| jd| jdt| jdt| jdt| jdt| jt| jt| jt| j| j| jd}t  }|d= tt| t|  S )Nr    r   r   r#   r%   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r   )r1   r2   r3   r6   r7   r   ) r    r   r   r#   r%   r   r   r   r'   r(   r)   r   r*   r+   r,   r-   r   r.   r/   r0   r   r   r1   r2   r3   r6   r7   r   r   r   r   r   r   r;   r   r=   r   U  st   


&zConvLSTM.get_configc                 C   s   | di |S )Nr   r   )clsr   r   r   r=   from_config  s   zConvLSTM.from_config)r   r   Nr   r   r   Tr   r   r   TNNNNNNNFFFFr   r   )NNN) r   r   r   r   r   r   r   r    r   r   r#   r%   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r6   r7   r   classmethodr   r   r   r   r;   r=   r   h  s    TD




















+r   )r   tensorflow.compat.v2compatv2r[   	keras.srcr   r   r   r   r   keras.src.enginer   "keras.src.layers.rnn.base_conv_rnnr   +keras.src.layers.rnn.dropout_rnn_cell_mixinr	   keras.src.utilsr
   BaseRandomLayerr   r   r   r   r   r=   <module>   s     K