
    2Vhl                         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  e	d       G d dee             Z e	d       G d de             Zy)    )activations)backend)constraints)initializers)ops)regularizers)tree)keras_export)	InputSpec)Layer)DropoutRNNCell)RNNzkeras.layers.LSTMCellc                   t     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	 fd	Z fdZd Zd Zd
dZ fdZ	ddZ
 xZS )LSTMCellax  Cell class for the LSTM layer.

    This class processes one step within the whole time sequence input, whereas
    `keras.layer.LSTM` processes the whole sequence.

    Args:
        units: Positive integer, dimensionality of the output space.
        activation: Activation function to use. Default: hyperbolic tangent
            (`tanh`). If you pass None, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        recurrent_activation: Activation function to use for the recurrent step.
            Default: sigmoid (`sigmoid`). If you pass `None`, no activation is
            applied (ie. "linear" activation: `a(x) = x`).
        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`.
        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 2D tensor, with shape `(batch, features)`.
        states: A 2D tensor with shape `(batch, units)`, which is the state
            from the previous time step.
        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.

    Example:

    >>> inputs = np.random.random((32, 10, 8))
    >>> rnn = keras.layers.RNN(keras.layers.LSTMCell(4))
    >>> output = rnn(inputs)
    >>> output.shape
    (32, 4)
    >>> rnn = keras.layers.RNN(
    ...    keras.layers.LSTMCell(4),
    ...    return_sequences=True,
    ...    return_state=True)
    >>> whole_sequence_output, final_state = rnn(inputs)
    >>> whole_sequence_output.shape
    (32, 10, 4)
    >>> final_state.shape
    (32, 4)
    c                 \   |dk  rt        d| d      |j                  dd      }t        |   di | || _        || _        t        j                  |      | _        t        j                  |      | _	        || _
        t        j                  |      | _        t        j                  |      | _        t        j                  |      | _        t        j                  |	      | _        t        j                  |
      | _        t        j                  |      | _        t'        j                  |      | _        t'        j                  |      | _        t'        j                  |      | _        t/        dt1        d|            | _        t/        dt1        d|            | _        | j4                  dk7  rd| _        | j                  dk(  rd	| _        || _        t:        j<                  j?                  |
      | _         || _!        | j
                  | j
                  g| _"        | j
                  | _#        y )Nr   zQReceived an invalid value for argument `units`, expected a positive integer, got .implementation         ?              )seed )$
ValueErrorpopsuper__init__r   unitsr   get
activationrecurrent_activationuse_biasr   kernel_initializerrecurrent_initializerbias_initializerr   kernel_regularizerrecurrent_regularizerbias_regularizerr   kernel_constraintrecurrent_constraintbias_constraintminmaxdropoutrecurrent_dropoutdropout_mask_countr   r   randomSeedGeneratorseed_generatorunit_forget_bias
state_sizeoutput_size)selfr   r!   r"   r#   r$   r%   r&   r5   r'   r(   r)   r*   r+   r,   r/   r0   r   kwargsr   	__class__s                       I/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/rnn/lstm.pyr   zLSTMCell.__init__X   s   * A:4497!=   $4a8"6",
%//*5$/OO4H$I! "."2"23E"F%1%5%56K%L" , 0 01A B"."2"23E"F%1%5%56K%L" , 0 01A B!,1B!C$/OO4H$I!*?3C 12!$S#c3D*E!F!!S("#D!#&'D#	%nn:::E 0::tzz2::    c                 B    t            |       |d   } j                  | j                  dz  fd j                   j
                   j                         _         j                   j                   j                  dz  fd j                   j                   j                         _         j                  r\ j                  r fd}n j                  } j                   j                  dz  fd| j                   j                          _        y d  _        y )Nr   kernel)shapenameinitializer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   concatenater&   r   r   r    )_argsr9   r8   s      r;   r&   z(LSTMCell.build.<locals>.bias_initializer   s    ??1D11!%048> 5L,,V4!%048> 2D11!%a 148<B
 r<   bias)r   build
add_weightr   r$   r'   r*   r?   r%   r(   r+   rE   r#   r5   r&   r)   r,   rK   )r8   input_shape	input_dimr&   r:   s   `   r;   rL   zLSTMCell.build   s   k"O	oodjj1n-////-- & 
 !%::tzzA~.#222200 !0 !
 ==$$  $(#8#8 zzA~', 11// ( DI DIr<   c                    |\  }}}}|\  }}	}
}| j                  |t        j                  || j                  ddd| j                  f         z         }| j                  |t        j                  |	| j                  dd| j                  | j                  dz  f         z         }||z  || j                  |t        j                  |
| j                  dd| j                  dz  | j                  dz  f         z         z  z   }| j                  |t        j                  || j                  dd| j                  dz  df         z         }||fS )z.Computes carry and output using split kernels.Nr      )r"   r   matmulrE   r   r!   )r8   xh_tm1c_tm1x_ix_fx_cx_oh_tm1_ih_tm1_fh_tm1_ch_tm1_oifcos                   r;   _compute_carry_and_outputz"LSTMCell._compute_carry_and_output   sT   S#s-2*'7%%#**Wd&;&;A||O&LMM
 %%jj..q$**tzzA~2M/MN
 IDOOjj%%aa$**q.)H&HI
 
 
 %%jj$"7"74::>;K8K"LMN
 !tr<   c                     |\  }}}}| j                  |      }| j                  |      }||z  || j                  |      z  z   }	| j                  |      }
|	|
fS )z.Computes carry and output using fused kernels.)r"   r!   )r8   zrU   z0z1z2z3r^   r_   r`   ra   s              r;   _compute_carry_and_output_fusedz(LSTMCell._compute_carry_and_output_fused   sd    BB%%b)%%b)IDOOB///%%b)!tr<   c                    |d   }|d   }| j                   dk(  r|rJd| j                  cxk  rdk  r5n n2| j                  |      }||d   z  }||d   z  }||d   z  }	||d   z  }
n|}|}|}	|}
t        j                  | j
                  dd      \  }}}}t        j                  ||      }t        j                  ||      }t        j                  |	|      }t        j                  |
|      }| j                  r;t        j                  | j                  dd      \  }}}}||z  }||z  }||z  }||z  }|rJd| j                  cxk  rdk  r5n n2| j                  |      }||d   z  }||d   z  }||d   z  }||d   z  }n|}|}|}|}||||f}||||f}| j                  |||      \  }}n|r.d| j                  cxk  rdk  rn n| j                  |      }||z  }t        j                  || j
                        }|t        j                  || j                        z  }| j                  r|| j                  z  }t        j                  |dd      }| j                  ||      \  }}|| j                  |      z  } | | |gfS )	Nr   r   r   r   r   rQ   r   axis)r   r/   get_dropout_maskr   splitr?   rR   r#   rK   r0   get_recurrent_dropout_maskrb   rE   ri   r!   )!r8   inputsstatestrainingrT   rU   dp_maskinputs_iinputs_finputs_cinputs_ok_ik_fk_ck_orV   rW   rX   rY   b_ib_fb_cb_orec_dp_maskrZ   r[   r\   r]   rS   r`   ra   rd   hs!                                    r;   callzLSTMCell.call   s   q	q	!#C$,,44//7!GAJ.!GAJ.!GAJ.!GAJ.!!!!!$4;;!BCc3**Xs+C**Xs+C**Xs+C**Xs+C}}%(YYtyy!!%D"S#ss
s
s
s
C$"8"8>3>"==eD+a.0+a.0+a.0+a.0c3$Agw8E11!UEBDAqC$,,44//7')

64;;/AE4#8#899A}}TYY		!QQ'A775ADAq""1a&yr<   c                    i d| j                   dt        j                  | j                        dt        j                  | j                        d| j
                  d| j                  dt        j                  | j                        dt        j                  | j                        dt        j                  | j                        d	t        j                  | j                        d
t        j                  | j                        dt        j                  | j                        dt        j                  | j                         dt        j                  | j"                        dt        j                  | j$                        d| j&                  d| j(                  d| j*                  }t,        | ]         }i ||S )Nr   r!   r"   r#   r5   r$   r%   r&   r'   r(   r)   r*   r+   r,   r/   r0   r   )r   r   	serializer!   r"   r#   r5   r   r$   r%   r&   r   r'   r(   r)   r   r*   r+   r,   r/   r0   r   r   
get_configr8   configbase_configr:   s      r;   r   zLSTMCell.get_config!  s   
TZZ
+//@
 #K$9$9))%
 
  5 5
 !,"8"8''#
 $\%;%;**&
  6 6t7L7L M
 !,"8"8''#
$ $\%;%;**&%
*  6 6t7L7L M+
,  !6!6t7M7M!N-
. #K$9$9))%/
4 {44T5I5IJ5
6 t||7
8  !7!79
: DII;
> g(*(+(((r<   c                     | j                   D cg c]%  }t        j                  ||f| j                        ' c}S c c}w )N)dtype)r6   r   zeroscompute_dtype)r8   
batch_sizeds      r;   get_initial_statezLSTMCell.get_initial_stateD  s=     __
 IIz1oT-?-?@
 	
 
s   *<)tanhsigmoidTglorot_uniform
orthogonalr   TNNNNNNr   r   NFN)__name__
__module____qualname____doc__r   rL   rb   ri   r   r   r   __classcell__r:   s   @r;   r   r      sc    ET &+* "!%9&v.`49v!)F
r<   r   zkeras.layers.LSTMc                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zd fd	Zd f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 )LSTMa@  Long Short-Term Memory layer - Hochreiter 1997.

    Based on available runtime hardware and constraints, this layer
    will choose different implementations (cuDNN-based or backend-native)
    to maximize the performance. If a GPU is available and all
    the arguments to the layer meet the requirement of the cuDNN kernel
    (see below for details), the layer will use a fast cuDNN implementation
    when using the TensorFlow backend.
    The requirements to use the cuDNN implementation are:

    1. `activation` == `tanh`
    2. `recurrent_activation` == `sigmoid`
    3. `recurrent_dropout` == 0
    4. `unroll` is `False`
    5. `use_bias` is `True`
    6. Inputs, if use masking, are strictly right-padded.
    7. Eager execution is enabled in the outermost context.

    For example:

    >>> inputs = np.random.random((32, 10, 8))
    >>> lstm = keras.layers.LSTM(4)
    >>> output = lstm(inputs)
    >>> output.shape
    (32, 4)
    >>> lstm = keras.layers.LSTM(
    ...     4, return_sequences=True, return_state=True)
    >>> whole_seq_output, final_memory_state, final_carry_state = lstm(inputs)
    >>> whole_seq_output.shape
    (32, 10, 4)
    >>> final_memory_state.shape
    (32, 4)
    >>> final_carry_state.shape
    (32, 4)

    Args:
        units: Positive integer, dimensionality of the output space.
        activation: Activation function to use.
            Default: hyperbolic tangent (`tanh`).
            If you pass `None`, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        recurrent_activation: Activation function to use
            for the recurrent step.
            Default: sigmoid (`sigmoid`).
            If you pass `None`, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        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.
        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 and return the
            reversed sequence.
        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.
        unroll: Boolean (default False).
            If `True`, the network will be unrolled,
            else a symbolic loop will be used.
            Unrolling can speed-up a RNN,
            although it tends to be more memory-intensive.
            Unrolling is only suitable for short sequences.
        use_cudnn: Whether to use a cuDNN-backed implementation. `"auto"` will
            attempt to use cuDNN when feasible, and will fallback to the
            default implementation if not.

    Call arguments:
        inputs: A 3D tensor, with shape `(batch, timesteps, feature)`.
        mask: Binary tensor of shape `(samples, timesteps)` indicating whether
            a given timestep should be masked  (optional).
            An individual `True` entry indicates that the corresponding timestep
            should be utilized, while a `False` entry indicates that the
            corresponding timestep should be ignored. Defaults to `None`.
        training: Python boolean indicating whether the layer should behave in
            training mode or in inference mode. This argument is passed to the
            cell when calling it. This is only relevant if `dropout` or
            `recurrent_dropout` is used  (optional). Defaults to `None`.
        initial_state: List of initial state tensors to be passed to the first
            call of the cell (optional, `None` causes creation
            of zero-filled initial state tensors). Defaults to `None`.
    c           
      >   t        |fi d|d|d|d|d|d|d|d|	d	|
d
|d|d|d|d|d|d|j                  dd       d|j                  dd      ddd|d|j                  dd      }t        |   |f||||||d| t        d      | _        |dvrt        d|       || _        t        j                         dk(  rNt        j                  |j                  |j                  | j                  |j                        r|dv rd| _        y y y y ) Nr!   r"   r#   r$   r5   r%   r&   r'   r(   r)   r*   r+   r,   r/   r0   r   	trainableTrA   	lstm_cellr   r   r   )return_sequencesreturn_statego_backwardsstatefulunrollactivity_regularizerrQ   )ndim)autoTFzlInvalid valid received for argument `use_cudnn`. Expected one of {'auto', True, False}. Received: use_cudnn=
tensorflow)Tr   F)r   r    r   r   r   r   
input_specr   	use_cudnnr   cudnn_okr!   r"   r   r#   supports_jit)r8   r   r!   r"   r#   r$   r%   r&   r5   r'   r(   r)   r   r*   r+   r,   r/   r0   r   r   r   r   r   r   r   r9   cellr:   s                              r;   r   zLSTM.__init__  s   8 
!
 "6
 	

  2
 .
 #8
 .
  2
 #8
 .
 0
 "6
 ,
 
  0!
" **Wd+#
$ jjd3%
& '
( )
* "::&6:+
. 			
-%%!5		
 		
 $+11''0k3 
 #OO-  ))	 ^+ %D , .r<   c                 p   t        j                  |      r|d   }| j                  dv rR| j                  sE	 |rw| j                  rk| j
                  j                  |d d dd d f         }t        j                  |d      }t        j                  |t        j                  |            }||z  }n|}t        j                  ||d   |d   || j
                  j                  | j
                  j                  | j
                  j                  | j
                  j                   | j
                  j"                  | j$                  | j&                  | j(                        }t        j                         dk(  rd| _        |S | j                  du rt/        d	      t0        | e  ||||
      S # t,        $ r Y 7w xY w)Nr   )r   Tr   rk   )r?   rE   rK   r!   r"   r   r   r   r   FTzuse_cudnn=True was specified, but cuDNN is not supported for this layer configuration with this backend. Pass use_cudnn='auto' to fallback to a non-cuDNN implementation.)maskrr   )r	   	is_nestedr   r0   r/   r   rm   r   expand_dimsbroadcast_tor@   r   lstmr?   rE   rK   r!   r"   r   r   r   r   NotImplementedErrorr   r   
inner_loop)	r8   	sequencesinitial_stater   rr   rs   dp_sequencesoutr:   s	           r;   r   zLSTM.inner_loop  s   >>$7D>>^+))#DLL"&))"<"<Yq!Qw=O"P"%//'"B"%"2"2#SYYy%9# (17':'0 ",,$%a(%a(#yy//)-)C)C!YY^^#'99#7#7-1YY-K-K)-)>)>%)%6%6#{{C  (L8,1)J >>T!1  w!}4( " 
 	
 + s   EF) )	F54F5c                 *    t         |   ||||      S )N)r   rr   r   )r   r   )r8   r   r   r   rr   r:   s        r;   r   z	LSTM.callF  s"    w|D8=  
 	
r<   c                 .    | j                   j                  S r   )r   r   r8   s    r;   r   z
LSTM.unitsK  s    yyr<   c                 .    | j                   j                  S r   )r   r!   r   s    r;   r!   zLSTM.activationO  s    yy###r<   c                 .    | j                   j                  S r   )r   r"   r   s    r;   r"   zLSTM.recurrent_activationS      yy---r<   c                 .    | j                   j                  S r   )r   r#   r   s    r;   r#   zLSTM.use_biasW  s    yy!!!r<   c                 .    | j                   j                  S r   )r   r5   r   s    r;   r5   zLSTM.unit_forget_bias[      yy)))r<   c                 .    | j                   j                  S r   )r   r$   r   s    r;   r$   zLSTM.kernel_initializer_      yy+++r<   c                 .    | j                   j                  S r   )r   r%   r   s    r;   r%   zLSTM.recurrent_initializerc      yy...r<   c                 .    | j                   j                  S r   )r   r&   r   s    r;   r&   zLSTM.bias_initializerg  r   r<   c                 .    | j                   j                  S r   )r   r'   r   s    r;   r'   zLSTM.kernel_regularizerk  r   r<   c                 .    | j                   j                  S r   )r   r(   r   s    r;   r(   zLSTM.recurrent_regularizero  r   r<   c                 .    | j                   j                  S r   )r   r)   r   s    r;   r)   zLSTM.bias_regularizers  r   r<   c                 .    | j                   j                  S r   )r   r*   r   s    r;   r*   zLSTM.kernel_constraintw      yy***r<   c                 .    | j                   j                  S r   )r   r+   r   s    r;   r+   zLSTM.recurrent_constraint{  r   r<   c                 .    | j                   j                  S r   )r   r,   r   s    r;   r,   zLSTM.bias_constraint  s    yy(((r<   c                 .    | j                   j                  S r   )r   r/   r   s    r;   r/   zLSTM.dropout  s    yy   r<   c                 .    | j                   j                  S r   )r   r0   r   s    r;   r0   zLSTM.recurrent_dropout  r   r<   c                    i 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                        dt!        j                  | j"                        dt!        j                  | j$                        dt!        j                  | j&                        d| j(                  d| j*                  d| j,                  j.                  i}t0        | e         }|d= i ||S )Nr   r!   r"   r#   r$   r%   r&   r5   r'   r(   r)   r   r*   r+   r,   r/   r0   r   r   )r   r   r   r!   r"   r#   r   r$   r%   r&   r5   r   r'   r(   r)   r   r   r*   r+   r,   r/   r0   r   r   r   r   r   s      r;   r   zLSTM.get_config  s   !
TZZ!
+//@!
 #K$9$9))%!
 !
 !,"8"8''#!
 $\%;%;**&!
  6 6t7L7L M!
  5 5!
 !,"8"8''#!
$ $\%;%;**&%!
*  6 6t7L7L M+!
, #L$:$:))%-!
2  !6!6t7M7M!N3!
4 #K$9$9))%5!
: {44T5I5IJ;!
< t||=!
>  !7!7?!
@ DIINNA!
D g(*(+(((r<   c                      | di |S )Nr   r   )clsr   s     r;   from_configzLSTM.from_config  s    }V}r<   )r   r   Tr   r   r   TNNNNNNNr   r   NFFFFFr   r   )NNF)r   r   r   r   r   r   r   propertyr   r!   r"   r#   r5   r$   r%   r&   r'   r(   r)   r*   r+   r,   r/   r0   r   classmethodr   r   r   s   @r;   r   r   K  s   ql &+* "!!3O&b3
j

   $ $ . . " " * * , , / / * * , , / / * * + + . . ) ) ! ! + +%)N  r<   r   N)	keras.srcr   r   r   r   r   r   r	   keras.src.api_exportr
   keras.src.layers.input_specr   keras.src.layers.layerr   %keras.src.layers.rnn.dropout_rnn_cellr   keras.src.layers.rnn.rnnr   r   r   r   r<   r;   <module>r      st    !  ! "  "  - 1 ( @ ( %&x
un x
 'x
v	 !"h3 h #hr<   