
    &VfD                        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)keras_export)	InputSpec)Layer)DropoutRNNCell)RNNzkeras.layers.SimpleRNNCellc                   `     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd		Zd
 ZddZddZ fdZ xZ	S )SimpleRNNCellav  Cell class for SimpleRNN.

    This class processes one step within the whole time sequence input, whereas
    `keras.layer.SimpleRNN` 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`).
        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"`.
        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:
        sequence: 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:

    ```python
    inputs = np.random.random([32, 10, 8]).astype(np.float32)
    rnn = keras.layers.RNN(keras.layers.SimpleRNNCell(4))
    output = rnn(inputs)  # The output has shape `(32, 4)`.
    rnn = keras.layers.RNN(
        keras.layers.SimpleRNNCell(4),
        return_sequences=True,
        return_state=True
    )
    # whole_sequence_output has shape `(32, 10, 4)`.
    # final_state has shape `(32, 4)`.
    whole_sequence_output, final_state = rnn(inputs)
    ```
    tanhTglorot_uniform
orthogonalzerosN        c                    |dk    rt          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
        |          | _        t3          dt5          d|                    | _        t3          dt5          d|                    | _        | j        | _        | j        | _        d S )Nr   zQReceived an invalid value for argument `units`, expected a positive integer, got .g      ?r    )
ValueErrorsuper__init__seedr   randomSeedGeneratorseed_generatorunitsr   get
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
state_sizeoutput_size)selfr   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r.   r/   r   kwargs	__class__s                    \/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/layers/rnn/simple_rnn.pyr   zSimpleRNNCell.__init__O   s   & A::=49= = =   	""6"""	%n::4@@
%/*55 "."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*:    c                    |                      |d         | j        fd| j        | j        | j                  | _        |                      | j        | j        fd| j        | j        | j                  | _	        | j
        r5|                      | j        fd| j        | j        | j                  | _        nd | _        d| _        d S )Nkernel)shapenameinitializerregularizer
constraintrecurrent_kernelbiasT)
add_weightr   r#   r&   r)   r9   r$   r'   r*   r?   r"   r%   r(   r+   r@   built)r2   input_shapes     r5   buildzSimpleRNNCell.build   s    oor?DJ///- & 
 
 !%:tz*#220 !0 !
 !
 = 		zm 1 1/ (  DII DI


r6   Fc                    t          |t          t          f          r|d         n|}|                     |          }|                     |          }|r|||z  }t          j        || j                  }| j        
|| j        z  }|r|||z  }|t          j        || j	                  z   }| j
        | 
                    |          }t          |t          t          f          r|gn|}	||	fS )Nr   )
isinstancelisttupleget_dropout_maskget_recurrent_dropout_maskr   matmulr9   r@   r?   r!   )
r2   sequencestatestrainingprev_outputdp_maskrec_dp_maskhoutput	new_states
             r5   callzSimpleRNNCell.call   s    #-ftUm#D#DPfQii&''1155kBB 	*+')HJx--9 NA 	4/%3KSZT-BCCC?&__V,,F *6D%= A AMVHHv	y  r6   c                 H    t          j        || j        f| j                  gS )N)dtype)r   r   r0   compute_dtype)r2   
batch_sizes     r5   get_initial_statezSimpleRNNCell.get_initial_state   s(    Iz4?34;MNNN
 	
r6   c                 r   | j         t          j        | j                  | j        t          j        | j                  t          j        | j                  t          j        | j                  t          j        | j
                  t          j        | j                  t          j        | j                  t          j        | j                  t          j        | j                  t          j        | j                  | j        | j        | j        d}t)                                                      }i ||S )N)r   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r.   r/   r   )r   r   	serializer!   r"   r   r#   r$   r%   r   r&   r'   r(   r   r)   r*   r+   r.   r/   r   r   
get_configr2   configbase_configr4   s      r5   r]   zSimpleRNNCell.get_config   s   Z%/@@"."8'# # &2%;*& & !- 6t7L M M"."8'# # &2%;*& & !- 6t7L M M!,!6t7M!N!N$/$9)% %  +4T5IJJ|!%!7I3
 
6 gg((**(+(((r6   )r   Tr   r   r   NNNNNNr   r   N)FN)
__name__
__module____qualname____doc__r   rD   rU   rZ   r]   __classcell__r4   s   @r5   r   r      s        = =D +* "!!/& /& /& /& /& /&b  6! ! ! !(
 
 
 

) ) ) ) ) ) ) ) )r6   r   zkeras.layers.SimpleRNNc                       e Zd Z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 fdZed             Z xZS )	SimpleRNNa  Fully-connected RNN where the output is to be fed back as the new input.

    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`).
        use_bias: Boolean, (default `True`), whether the layer uses
            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"`.
        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.
        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.

    Call arguments:
        sequence: A 3D tensor, with shape `[batch, timesteps, feature]`.
        mask: Binary tensor of shape `[batch, timesteps]` indicating whether
            a given timestep should be masked. An individual `True` entry
            indicates that the corresponding timestep should be utilized,
            while a `False` entry indicates that the corresponding timestep
            should be ignored.
        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.
        initial_state: List of initial state tensors to be passed to the first
            call of the cell.

    Example:

    ```python
    inputs = np.random.random((32, 10, 8))
    simple_rnn = keras.layers.SimpleRNN(4)
    output = simple_rnn(inputs)  # The output has shape `(32, 4)`.
    simple_rnn = keras.layers.SimpleRNN(
        4, return_sequences=True, return_state=True
    )
    # whole_sequence_output has shape `(32, 10, 4)`.
    # final_state has shape `(32, 4)`.
    whole_sequence_output, final_state = simple_rnn(inputs)
    ```
    r   Tr   r   r   Nr   Fc           	      @   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} t                      j        |f|||||d| t	          d          g| _        d S )Nr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r.   r/   r   rW   	trainableTr;   simple_rnn_cell)return_sequencesreturn_statego_backwardsstatefulunroll   )ndim)r   r    r   r   r
   
input_spec)r2   r   r!   r"   r#   r$   r%   r&   r'   r(   activity_regularizerr)   r*   r+   r.   r/   rm   rn   ro   rp   rq   r   r3   cellr4   s                           r5   r   zSimpleRNN.__init__)  sl   2 
 
 
!z
 X
  21	

 #8"7
 .-
  21
 #8"7
 .-
 0/
 "6!5
 ,O
 G
 0/
 
  **Wd+++!
" jjd333#
$ #"%
( 		
-%%	
 	
 	
 	
 	
 %!,,,-r6   c                 N    t                                          ||||          S )N)maskrN   initial_state)r   rU   )r2   	sequencesry   rx   rN   r4   s        r5   rU   zSimpleRNN.calla  s+    ww||D8=  
 
 	
r6   c                     | j         j        S ra   )rv   r   r2   s    r5   r   zSimpleRNN.unitsf  s    yr6   c                     | j         j        S ra   )rv   r!   r|   s    r5   r!   zSimpleRNN.activationj  s    y##r6   c                     | j         j        S ra   )rv   r"   r|   s    r5   r"   zSimpleRNN.use_biasn  s    y!!r6   c                     | j         j        S ra   )rv   r#   r|   s    r5   r#   zSimpleRNN.kernel_initializerr      y++r6   c                     | j         j        S ra   )rv   r$   r|   s    r5   r$   zSimpleRNN.recurrent_initializerv      y..r6   c                     | j         j        S ra   )rv   r%   r|   s    r5   r%   zSimpleRNN.bias_initializerz      y))r6   c                     | j         j        S ra   )rv   r&   r|   s    r5   r&   zSimpleRNN.kernel_regularizer~  r   r6   c                     | j         j        S ra   )rv   r'   r|   s    r5   r'   zSimpleRNN.recurrent_regularizer  r   r6   c                     | j         j        S ra   )rv   r(   r|   s    r5   r(   zSimpleRNN.bias_regularizer  r   r6   c                     | j         j        S ra   )rv   r)   r|   s    r5   r)   zSimpleRNN.kernel_constraint      y**r6   c                     | j         j        S ra   )rv   r*   r|   s    r5   r*   zSimpleRNN.recurrent_constraint  s    y--r6   c                     | j         j        S ra   )rv   r+   r|   s    r5   r+   zSimpleRNN.bias_constraint  s    y((r6   c                     | j         j        S ra   )rv   r.   r|   s    r5   r.   zSimpleRNN.dropout  s    y  r6   c                     | j         j        S ra   )rv   r/   r|   s    r5   r/   zSimpleRNN.recurrent_dropout  r   r6   c                    | j         t          j        | j                  | j        t          j        | j                  t          j        | j                  t          j        | j                  t          j        | j
                  t          j        | j                  t          j        | j                  t          j        | j                  t          j        | j                  t          j        | j                  t          j        | j                  | j        | j        d}t)                                                      }|d= i ||S )N)r   r!   r"   r#   r$   r%   r&   r'   r(   ru   r)   r*   r+   r.   r/   rv   )r   r   r\   r!   r"   r   r#   r$   r%   r   r&   r'   r(   ru   r   r)   r*   r+   r.   r/   r   r]   r^   s      r5   r]   zSimpleRNN.get_config  s7   Z%/@@"."8'# # &2%;*& & !- 6t7L M M"."8'# # &2%;*& & !- 6t7L M M$0$:)% % "-!6t7M!N!N$/$9)% %  +4T5IJJ|!%!77
 
: gg((**(+(((r6   c                      | di |S )Nr   r   )clsr_   s     r5   from_configzSimpleRNN.from_config  s    s}}V}}r6   )r   Tr   r   r   NNNNNNNr   r   FFFFFN)NNF)rb   rc   rd   re   r   rU   propertyr   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r.   r/   r]   classmethodr   rf   rg   s   @r5   ri   ri      sV       Q Ql +* "!!-6. 6. 6. 6. 6. 6.p
 
 
 
 
 

   X $ $ X$ " " X" , , X, / / X/ * * X* , , X, / / X/ * * X* + + X+ . . X. ) ) X) ! ! X! + + X+ )  )  )  )  )D   [    r6   ri   N)	keras.srcr   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   ri   r   r6   r5   <module>r      sn   ! ! ! ! ! !       ! ! ! ! ! ! " " " " " "       " " " " " " - - - - - - 1 1 1 1 1 1 ( ( ( ( ( ( @ @ @ @ @ @ ( ( ( ( ( ( *++B) B) B) B) B)E> B) B) ,+B)J &''m m m m m m m ('m m mr6   