a
    SicaM                     @   s   d Z ddlm  mZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ 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 deejZedG dd deZdS )zFully connected RNN layer.    N)activations)backend)constraints)initializers)regularizers)
base_layer)	InputSpec)	rnn_utils)RNN)DropoutRNNCellMixin)tf_utils)
tf_logging)keras_exportzkeras.layers.SimpleRNNCellc                       sT   e Zd ZdZd fd	d
	Zej fddZdddZdddZ	 fddZ
  ZS )SimpleRNNCella  Cell class for SimpleRNN.

    See [the Keras RNN API guide](https://www.tensorflow.org/guide/keras/rnn)
    for details about the usage of RNN API.

    This class processes one step within the whole time sequence input, whereas
    `tf.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 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`.
      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.

    Call arguments:
      inputs: A 2D tensor, with shape of `[batch, feature]`.
      states: A 2D tensor with shape of `[batch, units]`, which is the state
        from the previous time step. For timestep 0, the initial state provided
        by user will be feed to cell.
      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.

    Examples:

    ```python
    inputs = np.random.random([32, 10, 8]).astype(np.float32)
    rnn = tf.keras.layers.RNN(tf.keras.layers.SimpleRNNCell(4))

    output = rnn(inputs)  # The output has shape `[32, 4]`.

    rnn = tf.keras.layers.RNN(
        tf.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                    s  |dk rt d| dtjj r4|dd| _n|dd| _t jf i | || _	t
|| _|| _t|| _t|| _t|| _t|| _t|| _t|	| _t|
| _t|| _t|| _tdtd|| _tdtd|| _| j	| _| j	| _d S )	Nr   zQReceived an invalid value for argument `units`, expected a positive integer, got .enable_caching_deviceTFg      ?r   ) 
ValueErrortfcompatv1#executing_eagerly_outside_functionspop_enable_caching_devicesuper__init__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/   r0   kwargs	__class__ W/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/layers/rnn/simple_rnn.pyr   j   s<    zSimpleRNNCell.__init__c                    s   t  | t| }| j|d | jfd| j| j| j|d| _	| j| j| jfd| j
| j| j|d| _| jr| j| jfd| j| j| j|d| _nd | _d| _d S )Nkernel)shapenameinitializerregularizer
constraintcaching_devicerecurrent_kernelbiasT)r   buildr	   r@   
add_weightr    r$   r'   r*   r:   r%   r(   r+   rA   r#   r&   r)   r,   rB   built)r3   input_shapedefault_caching_devicer5   r7   r8   rC      s:    


	zSimpleRNNCell.buildc           
      C   s   t j|r|d n|}| ||}| ||}|d urLt|| | j}nt|| j}| jd urrt	|| j}|d ur|| }|t|| j
 }| jd ur| |}t j|r|gn|}	||	fS )Nr   )r   nest	is_nestedget_dropout_mask_for_cell#get_recurrent_dropout_mask_for_cellr   dotr:   rB   bias_addrA   r"   )
r3   inputsstatestrainingprev_outputdp_maskrec_dp_maskhoutput	new_stater7   r7   r8   call   s"    


zSimpleRNNCell.callc                 C   s   t | |||S N)r	   #generate_zero_filled_state_for_cell)r3   rN   
batch_sizedtyper7   r7   r8   get_initial_state   s    zSimpleRNNCell.get_initial_statec                    s   | j t| j| jt| jt| jt| jt	| j
t	| jt	| jt| jt| jt| j| j| jd}|t|  t  }tt| t|  S )N)r    r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r/   r0   )r    r   	serializer"   r#   r   r$   r%   r&   r   r'   r(   r)   r   r*   r+   r,   r/   r0   updater	    config_for_enable_caching_devicer   
get_configdictlistitemsr3   configbase_configr5   r7   r8   r`      s8    





zSimpleRNNCell.get_config)r   Tr   r   r   NNNNNNr   r   )N)NNN)__name__
__module____qualname____doc__r   r   shape_type_conversionrC   rW   r\   r`   __classcell__r7   r7   r5   r8   r   %   s(   F             6 

r   zkeras.layers.SimpleRNNc                       s   e Zd ZdZd. fd
d	Zd/ fdd	Zedd Zedd Zedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Z fd*d+Zed,d- Z  ZS )0	SimpleRNNa6  Fully-connected RNN where the output is to be fed back to input.

    See [the Keras RNN API guide](https://www.tensorflow.org/guide/keras/rnn)
    for details about the usage of RNN API.

    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:
      inputs: 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.

    Examples:

    ```python
    inputs = np.random.random([32, 10, 8]).astype(np.float32)
    simple_rnn = tf.keras.layers.SimpleRNN(4)

    output = simple_rnn(inputs)  # The output has shape `[32, 4]`.

    simple_rnn = tf.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                    s   d|v r| d td d|v r4d| di}ni }t|f||||||||	||||||d|ddd|}t j|f|||||d| t|
| _t	d	d
g| _
d S )NimplementationzhThe `implementation` argument in `SimpleRNN` has been deprecated. Please remove it from your layer call.r   r[   	trainableT)r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r/   r0   r[   ro   )return_sequencesreturn_statego_backwardsstatefulunroll   )ndim)r   loggingwarningr   r!   r   r   r   activity_regularizerr   
input_spec)r3   r    r"   r#   r$   r%   r&   r'   r(   r)   ry   r*   r+   r,   r/   r0   rp   rq   rr   rs   rt   r4   cell_kwargscellr5   r7   r8   r   U  sV    


	zSimpleRNN.__init__c                    s   t  j||||dS )N)maskrP   initial_state)r   rW   )r3   rN   r}   rP   r~   r5   r7   r8   rW     s    zSimpleRNN.callc                 C   s   | j jS rX   )r|   r    r3   r7   r7   r8   r      s    zSimpleRNN.unitsc                 C   s   | j jS rX   )r|   r"   r   r7   r7   r8   r"     s    zSimpleRNN.activationc                 C   s   | j jS rX   )r|   r#   r   r7   r7   r8   r#     s    zSimpleRNN.use_biasc                 C   s   | j jS rX   )r|   r$   r   r7   r7   r8   r$     s    zSimpleRNN.kernel_initializerc                 C   s   | j jS rX   )r|   r%   r   r7   r7   r8   r%     s    zSimpleRNN.recurrent_initializerc                 C   s   | j jS rX   )r|   r&   r   r7   r7   r8   r&     s    zSimpleRNN.bias_initializerc                 C   s   | j jS rX   )r|   r'   r   r7   r7   r8   r'     s    zSimpleRNN.kernel_regularizerc                 C   s   | j jS rX   )r|   r(   r   r7   r7   r8   r(     s    zSimpleRNN.recurrent_regularizerc                 C   s   | j jS rX   )r|   r)   r   r7   r7   r8   r)     s    zSimpleRNN.bias_regularizerc                 C   s   | j jS rX   )r|   r*   r   r7   r7   r8   r*     s    zSimpleRNN.kernel_constraintc                 C   s   | j jS rX   )r|   r+   r   r7   r7   r8   r+     s    zSimpleRNN.recurrent_constraintc                 C   s   | j jS rX   )r|   r,   r   r7   r7   r8   r,     s    zSimpleRNN.bias_constraintc                 C   s   | j jS rX   )r|   r/   r   r7   r7   r8   r/     s    zSimpleRNN.dropoutc                 C   s   | j jS rX   )r|   r0   r   r7   r7   r8   r0     s    zSimpleRNN.recurrent_dropoutc                    s   | j t| j| jt| jt| jt| jt	| j
t	| jt	| jt	| jt| jt| jt| j| j| jd}t  }|t| j |d= tt| t|  S )N)r    r"   r#   r$   r%   r&   r'   r(   r)   ry   r*   r+   r,   r/   r0   r|   )r    r   r]   r"   r#   r   r$   r%   r&   r   r'   r(   r)   ry   r   r*   r+   r,   r/   r0   r   r`   r^   r	   r_   r|   ra   rb   rc   rd   r5   r7   r8   r`     s@    





zSimpleRNN.get_configc                 C   s    d|v r| d | f i |S )Nrn   )r   )clsre   r7   r7   r8   from_config  s    
zSimpleRNN.from_config)r   Tr   r   r   NNNNNNNr   r   FFFFF)NNN)rg   rh   ri   rj   r   rW   propertyr    r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r/   r0   r`   classmethodr   rl   r7   r7   r5   r8   rm      sj   Y                   D













#rm   )rj   tensorflow.compat.v2r   v2r   kerasr   r   r   r   r   keras.enginer   keras.engine.input_specr   Zkeras.layers.rnnr	   keras.layers.rnn.base_rnnr
   'keras.layers.rnn.dropout_rnn_cell_mixinr   keras.utilsr   tensorflow.python.platformr   rw    tensorflow.python.util.tf_exportr   BaseRandomLayerr   rm   r7   r7   r7   r8   <module>   s&    X