o
    2hk                     @   s   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 deeZG dd deZdS )    )activations)backend)constraints)initializers)ops)regularizers)tree)	InputSpec)Layer)DropoutRNNCell)RNN)operation_utils)argument_validationc                       s   e Zd ZdZ											
										d fdd	ZdddZdddZdddZdddZd ddZ	dd Z
 fddZ  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                    sx  t  jdi | || _tjj|d| _|| _| 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|| _(d| _)t*|d d| _+d| _,d S )N)seed   zRank z< convolutions are not currently implemented. Received: rank=kernel_sizestridesT
allow_zerodilation_rate      ?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_dropoutdropout_mask_countr	   
input_spec
state_size)selfr+   r-   r   r   r0   r2   r   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rC   rD   r   kwargs	__class__r%   Y/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/layers/rnn/conv_lstm.pyr'   U   sP   


zConvLSTMCell.__init__c                    sF   j dkrd}|dd   _n	d}|dd  _d  jv r$td| || d u r1td| t jd d|dd   d	 _|| }| _ j| jd
 f  _	 j j jd
 f } j
 j	 jd j jd _ j
| jd j jd _ jr jr fdd}n j} j
 jd
 fd| j jd _d S 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#   shaper    kernel)rO   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onesr!   )r   concatenater9   r-   r   r3   )_argsrI   rH   r%   rL   r9      s0   
z,ConvLSTMCell.build.<locals>.bias_initializerbias)rO   rR   rQ   rS   rT   )r2   spatial_dimsr,   r	   r+   rF   	input_dimr   r-   kernel_shape
add_weightr7   r;   r>   rP   r8   r<   r?   rU   r6   r:   r9   r=   r@   r[   )rH   inputs_shapestates_shapechannel_axisr]   recurrent_kernel_shaper9   r%   rZ   rL   build   sj   



zConvLSTMCell.buildFc           )      C   s,  |d }|d }|r5d| j   k rdk r5n n| |}||d  }||d  }||d  }	||d  }
n|}|}|}	|}
|rjd| j  k rJdk rjn 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   r   r!   r   r    )axis)NNNN)r0   )rC   get_dropout_maskrD   get_recurrent_dropout_maskr   splitrP   r+   rU   r6   r[   
input_convr0   recurrent_convr5   r4   ))rH   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hr%   r%   rL   call   s`   

zConvLSTMCell.callc              	   C   s0   t j|| j| j| j| j| j| jd}|||gfS )Nr   r0   r2   r   )r   compute_conv_output_shaper-   r   r   r0   r2   r   )rH   r`   ra   conv_output_shaper%   r%   rL   compute_output_shape$  s   	z!ConvLSTMCell.compute_output_shapec                 C   s^   | j dkr|f| j | jf }n|| jf| j }| |d }tj|| jdtj|| jdgS )Nchannels_lastr   )dtype)r2   r\   r]   r   r   r   compute_dtype)rH   
batch_sizeinput_shapestate_shaper%   r%   rL   get_initial_state0  s   
zConvLSTMCell.get_initial_statec                 C   sr   t j||| j|| j| jd}|d ur7| jdkr#d| jd  | jf }n
d| jfd| j  }t ||}||7 }|S )Nr   r   )r   r   )r   convr   r2   r   r+   r-   reshape)rH   xwbr0   conv_out
bias_shaper[   r%   r%   rL   ri   ;  s   
zConvLSTMCell.input_convc                 C   s.   t jd| jddd}tj|||d| jd}|S )Nr   r   Tr   same)r   r0   r2   )r   r.   r+   r   r   r2   )rH   r   r   r   r   r%   r%   rL   rj   M  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| jd}t  }i ||S )Nr-   r   r   r0   r2   r   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   )r?   r@   rC   rD   r   )r-   r   r   r0   r2   r   r   	serializer4   r5   r6   r   r7   r8   r9   r:   r   r;   r<   r=   r   r>   r?   r@   rC   rD   r   r&   
get_configrH   configbase_configrJ   r%   rL   r   V  sn   

$zConvLSTMCell.get_config)r   r   Nr   r   r   Tr   r   r   TNNNNNNr   r   NrN   )F)Nr   )__name__
__module____qualname____doc__r'   rd   r   r   r   ri   rj   r   __classcell__r%   r%   rJ   rL   r      s<    I
G
L
<

	r   c                       s  e Zd ZdZ											
														dC fdd	ZdD fdd	ZdEddZ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ed;d< Zed=d> Z fd?d@ZedAdB Z   Z!S )F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                    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|d}t j|f||||d| t|d d| _d S )Nr+   r-   r   r   r0   r2   r   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rC   rD   r   rR   conv_lstm_cellr   )return_sequencesreturn_statego_backwardsstatefulr   r"   r%   )r   r3   r&   r'   r	   rF   )rH   r+   r-   r   r   r0   r2   r   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rC   rD   r   r   r   r   r   rI   cellrJ   r%   rL   r'     s|   	

zConvLSTM.__init__c                    s   t  j||||dS )N)initial_statemaskrm   )r&   r   )rH   	sequencesr   r   rm   rJ   r%   rL   r     s   zConvLSTM.callc           	      C   sv   |d }|d }|f|dd   }| j |d dd  }| jr'||f| }n|f| }| jr9|f| }|||fS |S )Nr   r   r!   )r   r   r   r   )	rH   sequences_shapeinitial_state_shaper   steps
step_shaper   output_shapebatched_state_shaper%   r%   rL   r     s   


zConvLSTM.compute_output_shapec                 C   s8   t |d }| jr|nd }| jrd d g}|g| S |S )Nr   )r   flattenr   r   )rH   rX   r   output_mask
state_maskr%   r%   rL   compute_mask,  s   
zConvLSTM.compute_maskc                 C      | j jS rN   )r   r-   rZ   r%   r%   rL   r-   5     zConvLSTM.filtersc                 C   r   rN   )r   r   rZ   r%   r%   rL   r   9  r   zConvLSTM.kernel_sizec                 C   r   rN   )r   r   rZ   r%   r%   rL   r   =  r   zConvLSTM.stridesc                 C   r   rN   )r   r0   rZ   r%   r%   rL   r0   A  r   zConvLSTM.paddingc                 C   r   rN   )r   r2   rZ   r%   r%   rL   r2   E  r   zConvLSTM.data_formatc                 C   r   rN   )r   r   rZ   r%   r%   rL   r   I  r   zConvLSTM.dilation_ratec                 C   r   rN   )r   r4   rZ   r%   r%   rL   r4   M  r   zConvLSTM.activationc                 C   r   rN   )r   r5   rZ   r%   r%   rL   r5   Q  r   zConvLSTM.recurrent_activationc                 C   r   rN   )r   r6   rZ   r%   r%   rL   r6   U  r   zConvLSTM.use_biasc                 C   r   rN   )r   r7   rZ   r%   r%   rL   r7   Y  r   zConvLSTM.kernel_initializerc                 C   r   rN   )r   r8   rZ   r%   r%   rL   r8   ]  r   zConvLSTM.recurrent_initializerc                 C   r   rN   )r   r9   rZ   r%   r%   rL   r9   a  r   zConvLSTM.bias_initializerc                 C   r   rN   )r   r:   rZ   r%   r%   rL   r:   e  r   zConvLSTM.unit_forget_biasc                 C   r   rN   )r   r;   rZ   r%   r%   rL   r;   i  r   zConvLSTM.kernel_regularizerc                 C   r   rN   )r   r<   rZ   r%   r%   rL   r<   m  r   zConvLSTM.recurrent_regularizerc                 C   r   rN   )r   r=   rZ   r%   r%   rL   r=   q  r   zConvLSTM.bias_regularizerc                 C   r   rN   )r   r>   rZ   r%   r%   rL   r>   u  r   zConvLSTM.kernel_constraintc                 C   r   rN   )r   r?   rZ   r%   r%   rL   r?   y  r   zConvLSTM.recurrent_constraintc                 C   r   rN   )r   r@   rZ   r%   r%   rL   r@   }  r   zConvLSTM.bias_constraintc                 C   r   rN   )r   rC   rZ   r%   r%   rL   rC     r   zConvLSTM.dropoutc                 C   r   rN   )r   rD   rZ   r%   r%   rL   rD     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| jjd}t  }|d= i ||S )Nr-   r   r   r0   r2   r   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   activity_regularizer)r>   r?   r@   rC   rD   r   r   )r-   r   r   r0   r2   r   r   r   r4   r5   r6   r   r7   r8   r9   r:   r   r;   r<   r=   r   r   r>   r?   r@   rC   rD   r   r   r&   r   r   rJ   r%   rL   r     sv   


'zConvLSTM.get_configc                 C   s   | di |S )Nr%   r%   )clsr   r%   r%   rL   from_config  s   zConvLSTM.from_config)r   r   Nr   r   r   Tr   r   r   TNNNNNNr   r   NFFFF)NNFrN   )"r   r   r   r   r'   r   r   r   propertyr-   r   r   r0   r2   r   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rC   rD   r   classmethodr   r   r%   r%   rJ   rL   r     s    UD
	




















,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%   r%   r%   rL   <module>   s"      q