o
    NeaM                     @   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                       sn   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 r|dd| _n|dd| _t jd	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v1Z#executing_eagerly_outside_functionspopZ_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_dropoutZ
state_sizeZoutput_size)selfr   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r.   r/   kwargs	__class__r   KD:\Projects\ConvertPro\env\Lib\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rG| j| jfd| j| j| j|d| _nd | _d| _d S )Nkernel)shapenameZinitializerZregularizer
constraintcaching_devicerecurrent_kernelbiasT)r   buildr	   r:   Z
add_weightr   r#   r&   r)   r6   r$   r'   r*   r;   r"   r%   r(   r+   r<   Zbuilt)r0   Zinput_shapeZdefault_caching_devicer2   r   r4   r=      s:   


	
zSimpleRNNCell.buildc           
      C   s   t j|r
|d n|}| ||}| ||}|d ur&t|| | j}nt|| j}| jd ur9t	|| j}|d urA|| }|t|| j
 }| jd urT| |}t j|r]|gn|}	||	fS )Nr   )r   nestZ	is_nestedZget_dropout_mask_for_cellZ#get_recurrent_dropout_mask_for_cellr   dotr6   r<   Zbias_addr;   r!   )
r0   inputsZstatestrainingZprev_outputZdp_maskZrec_dp_maskhoutput	new_stater   r   r4   call   s"   


zSimpleRNNCell.callc                 C   s   t | |||S N)r	   Z#generate_zero_filled_state_for_cell)r0   r@   Z
batch_sizedtyper   r   r4   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.   r/   )r   r   	serializer!   r"   r   r#   r$   r%   r   r&   r'   r(   r   r)   r*   r+   r.   r/   updater	    config_for_enable_caching_devicer   
get_configdictlistitemsr0   configZbase_configr2   r   r4   rL      s8   





zSimpleRNNCell.get_config)r   Tr   r   r   NNNNNNr   r   rF   NNN)__name__
__module____qualname____doc__r   r   Zshape_type_conversionr=   rE   rH   rL   __classcell__r   r   r2   r4   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 rd| 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   rG   	trainableT)r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r.   r/   rG   rZ   )return_sequencesreturn_statego_backwardsstatefulunroll   )ndim)r   loggingwarningr   r    r   r   r   activity_regularizerr   Z
input_spec)r0   r   r!   r"   r#   r$   r%   r&   r'   r(   rd   r)   r*   r+   r.   r/   r[   r\   r]   r^   r_   r1   Zcell_kwargscellr2   r   r4   r   U  sV   


	zSimpleRNN.__init__c                    s   t  j||||dS )N)maskrA   initial_state)r   rE   )r0   r@   rf   rA   rg   r2   r   r4   rE     s   zSimpleRNN.callc                 C      | j jS rF   )re   r   r0   r   r   r4   r        zSimpleRNN.unitsc                 C   rh   rF   )re   r!   ri   r   r   r4   r!     rj   zSimpleRNN.activationc                 C   rh   rF   )re   r"   ri   r   r   r4   r"     rj   zSimpleRNN.use_biasc                 C   rh   rF   )re   r#   ri   r   r   r4   r#     rj   zSimpleRNN.kernel_initializerc                 C   rh   rF   )re   r$   ri   r   r   r4   r$     rj   zSimpleRNN.recurrent_initializerc                 C   rh   rF   )re   r%   ri   r   r   r4   r%     rj   zSimpleRNN.bias_initializerc                 C   rh   rF   )re   r&   ri   r   r   r4   r&     rj   zSimpleRNN.kernel_regularizerc                 C   rh   rF   )re   r'   ri   r   r   r4   r'     rj   zSimpleRNN.recurrent_regularizerc                 C   rh   rF   )re   r(   ri   r   r   r4   r(     rj   zSimpleRNN.bias_regularizerc                 C   rh   rF   )re   r)   ri   r   r   r4   r)     rj   zSimpleRNN.kernel_constraintc                 C   rh   rF   )re   r*   ri   r   r   r4   r*     rj   zSimpleRNN.recurrent_constraintc                 C   rh   rF   )re   r+   ri   r   r   r4   r+     rj   zSimpleRNN.bias_constraintc                 C   rh   rF   )re   r.   ri   r   r   r4   r.     rj   zSimpleRNN.dropoutc                 C   rh   rF   )re   r/   ri   r   r   r4   r/     rj   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(   rd   r)   r*   r+   r.   r/   re   )r   r   rI   r!   r"   r   r#   r$   r%   r   r&   r'   r(   rd   r   r)   r*   r+   r.   r/   r   rL   rJ   r	   rK   re   rM   rN   rO   rP   r2   r   r4   rL     s@   





zSimpleRNN.get_configc                 C   s    d|v r	| d | di |S )NrY   r   )r   )clsrQ   r   r   r4   from_config  s   
zSimpleRNN.from_config)r   Tr   r   r   NNNNNNNr   r   FFFFFrR   )rS   rT   rU   rV   r   rE   propertyr   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r.   r/   rL   classmethodrl   rW   r   r   r2   r4   rX      sl    YD













#rX   )rV   Ztensorflow.compat.v2r   v2r   Zkerasr   r   r   r   r   Zkeras.enginer   Zkeras.engine.input_specr   Zkeras.layers.rnnr	   Zkeras.layers.rnn.base_rnnr
   Z'keras.layers.rnn.dropout_rnn_cell_mixinr   Zkeras.utilsr   Ztensorflow.python.platformr   rb   Z tensorflow.python.util.tf_exportr   ZBaseRandomLayerr   rX   r   r   r   r4   <module>   s(    X