
    &Vfg                        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dS )    )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)
    tanhsigmoidTglorot_uniform
orthogonalzerosN        c                    |dk    rt          d| d          |                    dd          } t                      j        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t/          d|                    | _        t-          dt/          d|                    | _        || _        t6          j                            |          | _        || _        | j        | j        g| _         | j        | _!        || _"        d S )
Nr   zQReceived an invalid value for argument `units`, expected a positive integer, got .implementation         ?r   )seed )#
ValueErrorpopsuper__init__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_dropoutr   r   randomSeedGeneratorseed_generatorunit_forget_bias
state_sizeoutput_sizer   )selfr"   r$   r%   r&   r'   r(   r)   r7   r*   r+   r,   r-   r.   r/   r2   r3   r   kwargsr   	__class__s                       V/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/layers/rnn/lstm.pyr!   zLSTMCell.__init__X   s   * A::=49= = =    $4a88""6"""
%/*55$/O4H$I$I! "."23E"F"F%1%56K%L%L" , 01A B B"."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	%n:::EE 0:tz2:,    c                     t                                          |           |d         }                     | j        dz  fd j         j         j                   _                              j         j        dz  fd j         j	         j
                   _         j        rG j        r fd}n j        }                      j        dz  fd| j         j                   _        nd  _        d _        d S )	N   kernel)shapenameinitializer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   r#   )_argsr;   r:   s      r=   r)   z(LSTMCell.build.<locals>.bias_initializer   s    ?1D1!%04  8>  5L,V44!%04  8>  2D1!%a 148  <B 
  r>   biasT)r    build
add_weightr"   r'   r*   r-   rB   r(   r+   r.   rH   r&   r7   r)   r,   r/   rN   built)r:   input_shape	input_dimr)   r<   s   `   r=   rO   zLSTMCell.build   s0   k"""O	oodj1n-//- & 
 
 !%:tzA~.#220 !0 !
 !
 = 	$ 9       $(#8 zA~', 1/ (  DII DI


r>   c                 l   |\  }}}}|\  }}	}
}|                      |t          j        || j        ddd| j        f                   z             }|                      |t          j        |	| j        dd| j        | j        dz  f                   z             }||z  ||                     |t          j        |
| j        dd| j        dz  | j        dz  f                   z             z  z   }|                      |t          j        || j        dd| j        dz  df                   z             }||fS )z.Computes carry and output using split kernels.Nr      )r%   r   matmulrH   r"   r$   )r:   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   sk   S#s-2*'7%%#*Wd&;AAA||O&LMMM
 
 %%j.qqq$*tzA~2M/MN 
 
 IDOOj%aaaa$*q.)H&HI 
 
 
 
 %%j$"74:>;K;K8K"LMMN
 
 !tr>   c                     |\  }}}}|                      |          }|                      |          }||z  ||                     |          z  z   }	|                      |          }
|	|
fS )z.Computes carry and output using fused kernels.)r%   r$   )r:   zrY   z0z1z2z3rb   rc   rd   re   s              r=   _compute_carry_and_output_fusedz(LSTMCell._compute_carry_and_output_fused   sp    BB%%b))%%b))IDOOB////%%b))!tr>   Fc                    |d         }|d         }|                      |          }|                     |          }|rd| j        cxk     rdk     rn n||z  }|rd| j        cxk     rdk     rn n||z  }| j        dk    r|}|}	|}
|}t          j        | j        dd          \  }}}}t          j        ||          }t          j        |	|          }t          j        |
|          }t          j        ||          }| j	        r5t          j        | j
        dd          \  }}}}||z  }||z  }||z  }||z  }|}|}|}|}||||f}||||f}|                     |||          \  }}nxt          j        || j                  }|t          j        || j                  z  }| j	        r
|| j
        z  }t          j        |dd          }|                     ||          \  }}||                     |          z  } | | |gfS )Nr      r   r   rA   )axis)get_dropout_maskget_recurrent_dropout_maskr2   r3   r   r   splitrB   rV   r&   rN   rf   rH   rm   r$   )!r:   inputsstatestrainingrX   rY   dp_maskrec_dp_maskinputs_iinputs_finputs_cinputs_ok_ik_fk_ck_orZ   r[   r\   r]   b_ib_fb_cb_or^   r_   r`   ra   rW   rd   re   rh   hs!                                    r=   callzLSTMCell.call   s\   q	q	''//55e<< 	&dl0000S00000g%F 	(d4::::s:::::K'E!##HHHH!$4;!B!B!BCc3*Xs++C*Xs++C*Xs++C*Xs++C} %(Yty!!%D%D%D"S#ss
s
s
s
GGGGc3$Agw8E11!UEBBDAqq
64;//AE4#8999A} TY	!QQ'''A775A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&   r7   r'   r(   r)   r*   r+   r,   r-   r.   r/   r2   r3   r   )r"   r   	serializer$   r%   r&   r7   r   r'   r(   r)   r   r*   r+   r,   r   r-   r.   r/   r2   r3   r   r    
get_configr:   configbase_configr<   s      r=   r   zLSTMCell.get_config  s   
TZ
+/@@
 #K$9)% %
 
  5
 !,"8'# #
 $\%;*& &
  6t7L M M
 !,"8'# #
$ $\%;*& &%
*  6t7L M M+
,  !6t7M!N!N-
. #K$9)% %/
4 {4T5IJJ5
6 t|7
8  !79
: DI;
> gg((**(+(((r>   c                 .      fd j         D             S )Nc                 J    g | ]}t          j        |fj                    S ))dtype)r   r   compute_dtype).0d
batch_sizer:   s     r=   
<listcomp>z.LSTMCell.get_initial_state.<locals>.<listcomp>8  s?     
 
 
 Iz1oT-?@@@
 
 
r>   )r8   )r:   r   s   ``r=   get_initial_statezLSTMCell.get_initial_state7  s6    
 
 
 
 
_
 
 
 	
r>   )r   r   Tr   r   r   TNNNNNNr   r   NFN)__name__
__module____qualname____doc__r!   rO   rf   rm   r   r   r   __classcell__r<   s   @r=   r   r      s        E ET &+* "!%5- 5- 5- 5- 5- 5-n/ / / / /b  4  / / / /b!) !) !) !) !)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 )$LSTMaS  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. `dropout` == 0 and `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`.
    r   r   Tr   r   r   Nr   Fautoc           
      Z   t          |f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vrt          d|           || _        t          j	                    dk    r8t          j
        |j        |j        | j        |j                  r|dv rd| _        d S d S d S d S ) Nr$   r%   r&   r'   r7   r(   r)   r*   r+   r,   r-   r.   r/   r2   r3   r   	trainableTrD   	lstm_cellr   r   r   )return_sequencesreturn_statego_backwardsstatefulunrollactivity_regularizerrU   )ndim)r   TFzlInvalid 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)r:   r"   r$   r%   r&   r'   r(   r)   r7   r*   r+   r,   r   r-   r.   r/   r2   r3   r   r   r   r   r   r   r   r;   cellr<   s                              r=   r!   zLSTM.__init__  sD   8 
 
 
!z
 "6!5
 X	

  21
 .-
 #8"7
 .-
  21
 #8"7
 .-
 0/
 "6!5
 ,O
 G
  0/!
" **Wd+++#
$ jjd333%
& '
( )
* "::&6:::+
. 			
-%%!5		
 		
 		
 		
 		
 $+++1113'03 3  
 #O-- )	  . ^++ %D .--- ,+r>   c                     t          j        |          r|d         }| j        dv r| j        s| j        s	 t          j        ||d         |d         || j        j        | j        j	        | j        j
        | j        j        | j        j        | j        | j        | j                  }t          j                    dk    rd| _        |S # t"          $ r Y nw xY w| j        du rt%          d          t'                                          ||||	          S )
Nr   )r   Tro   )rB   rH   rN   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.)maskrv   )r	   	is_nestedr   r2   r3   r   lstmr   rB   rH   rN   r$   r%   r   r   r   r   NotImplementedErrorr   r    
inner_loop)r:   	sequencesinitial_stater   rv   outr<   s         r=   r   zLSTM.inner_loop  s<   >$ 	7D>^++< (> 
 ",!%a(%a(#y/)-)C!Y^#'9#7-1Y-K)-)>%)%6#{  C  ((L88,1)J*   D>T!!1   ww!!}4( " 
 
 	
s   BC 
CCc                 N    t                                          ||||          S )N)r   rv   r   )r    r   )r:   r   r   r   rv   r<   s        r=   r   z	LSTM.call/  s+    ww||D8=  
 
 	
r>   c                     | j         j        S r   )r   r"   r:   s    r=   r"   z
LSTM.units4  s    yr>   c                     | j         j        S r   )r   r$   r   s    r=   r$   zLSTM.activation8  s    y##r>   c                     | j         j        S r   )r   r%   r   s    r=   r%   zLSTM.recurrent_activation<      y--r>   c                     | j         j        S r   )r   r&   r   s    r=   r&   zLSTM.use_bias@  s    y!!r>   c                     | j         j        S r   )r   r7   r   s    r=   r7   zLSTM.unit_forget_biasD      y))r>   c                     | j         j        S r   )r   r'   r   s    r=   r'   zLSTM.kernel_initializerH      y++r>   c                     | j         j        S r   )r   r(   r   s    r=   r(   zLSTM.recurrent_initializerL      y..r>   c                     | j         j        S r   )r   r)   r   s    r=   r)   zLSTM.bias_initializerP  r   r>   c                     | j         j        S r   )r   r*   r   s    r=   r*   zLSTM.kernel_regularizerT  r   r>   c                     | j         j        S r   )r   r+   r   s    r=   r+   zLSTM.recurrent_regularizerX  r   r>   c                     | j         j        S r   )r   r,   r   s    r=   r,   zLSTM.bias_regularizer\  r   r>   c                     | j         j        S r   )r   r-   r   s    r=   r-   zLSTM.kernel_constraint`      y**r>   c                     | j         j        S r   )r   r.   r   s    r=   r.   zLSTM.recurrent_constraintd  r   r>   c                     | j         j        S r   )r   r/   r   s    r=   r/   zLSTM.bias_constrainth  s    y((r>   c                     | j         j        S r   )r   r2   r   s    r=   r2   zLSTM.dropoutl  s    y  r>   c                     | j         j        S r   )r   r3   r   s    r=   r3   zLSTM.recurrent_dropoutp  r   r>   c                 6   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}t1                                                      }|d= i ||S )Nr"   r$   r%   r&   r'   r(   r)   r7   r*   r+   r,   r   r-   r.   r/   r2   r3   r   r   )r"   r   r   r$   r%   r&   r   r'   r(   r)   r7   r   r*   r+   r,   r   r   r-   r.   r/   r2   r3   r   r   r    r   r   s      r=   r   zLSTM.get_configt  s   !
TZ!
+/@@!
 #K$9)% %!
 !
 !,"8'# #!
 $\%;*& &!
  6t7L M M!
  5!
 !,"8'# #!
$ $\%;*& &%!
*  6t7L M M+!
, #L$:)% %-!
2  !6t7M!N!N3!
4 #K$9)% %5!
: {4T5IJJ;!
< t|=!
>  !7?!
@ DINA!
 !
D gg((**(+(((r>   c                      | di |S )Nr   r   )clsr   s     r=   from_configzLSTM.from_config  s    s}}V}}r>   )r   r   Tr   r   r   TNNNNNNNr   r   NFFFFFr   r   )NNF)r   r   r   r   r!   r   r   propertyr"   r$   r%   r&   r7   r'   r(   r)   r*   r+   r,   r-   r.   r/   r2   r3   r   classmethodr   r   r   s   @r=   r   r   >  s       q ql &+* "!!3O& O& O& O& O& O&b)
 )
 )
 )
 )
 )
V
 
 
 
 
 

   X $ $ X$ . . X. " " X" * * X* , , X, / / X/ * * X* , , X, / / X/ * * X* + + X+ . . X. ) ) X) ! ! X! + + X+%) %) %) %) %)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      s   ! ! ! ! ! !       ! ! ! ! ! ! " " " " " "       " " " " " "       - - - - - - 1 1 1 1 1 1 ( ( ( ( ( ( @ @ @ @ @ @ ( ( ( ( ( ( %&&k
 k
 k
 k
 k
un k
 k
 '&k
\	 !""^ ^ ^ ^ ^3 ^ ^ #"^ ^ ^r>   