a
    Sic                     @   s   d Z ddlZddl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 edG dd dejZdS )z Base class for recurrent layers.    N)backend)
base_layer)	InputSpec)	rnn_utils)DropoutRNNCellMixin)StackedRNNCells)layer_serialization)generic_utils)keras_export)doc_controlszkeras.layers.RNNc                       s   e Zd ZdZd' fdd	Ze fddZedd Zeje	j
jjd	d Zd
d Zdd Z fddZedd Zejdd Zd( fdd	Zd)ddZdd Zdd Zdd Zd*dd Z fd!d"Zed+d#d$Zed%d& Z  ZS ),RNNa6%  Base class for recurrent layers.

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

    Args:
      cell: A RNN cell instance or a list of RNN cell instances.
        A RNN cell is a class that has:
        - A `call(input_at_t, states_at_t)` method, returning
          `(output_at_t, states_at_t_plus_1)`. The call method of the
          cell can also take the optional argument `constants`, see
          section "Note on passing external constants" below.
        - A `state_size` attribute. This can be a single integer
          (single state) in which case it is the size of the recurrent
          state. This can also be a list/tuple of integers (one size per state).
          The `state_size` can also be TensorShape or tuple/list of
          TensorShape, to represent high dimension state.
        - A `output_size` attribute. This can be a single integer or a
          TensorShape, which represent the shape of the output. For backward
          compatible reason, if this attribute is not available for the
          cell, the value will be inferred by the first element of the
          `state_size`.
        - A `get_initial_state(inputs=None, batch_size=None, dtype=None)`
          method that creates a tensor meant to be fed to `call()` as the
          initial state, if the user didn't specify any initial state via other
          means. The returned initial state should have a shape of
          [batch_size, cell.state_size]. The cell might choose to create a
          tensor full of zeros, or full of other values based on the cell's
          implementation.
          `inputs` is the input tensor to the RNN layer, which should
          contain the batch size as its shape[0], and also dtype. Note that
          the shape[0] might be `None` during the graph construction. Either
          the `inputs` or the pair of `batch_size` and `dtype` are provided.
          `batch_size` is a scalar tensor that represents the batch size
          of the inputs. `dtype` is `tf.DType` that represents the dtype of
          the inputs.
          For backward compatibility, if this method is not implemented
          by the cell, the RNN layer will create a zero filled tensor with the
          size of [batch_size, cell.state_size].
        In the case that `cell` is a list of RNN cell instances, the cells
        will be stacked on top of each other in the RNN, resulting in an
        efficient stacked RNN.
      return_sequences: Boolean (default `False`). Whether to return the last
        output in the output sequence, or the full sequence.
      return_state: Boolean (default `False`). Whether to return the last state
        in addition to the output.
      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.
      time_major: The shape format of the `inputs` and `outputs` tensors.
        If True, the inputs and outputs will be in shape
        `(timesteps, batch, ...)`, whereas in the False case, it will be
        `(batch, timesteps, ...)`. Using `time_major = True` is a bit more
        efficient because it avoids transposes at the beginning and end of the
        RNN calculation. However, most TensorFlow data is batch-major, so by
        default this function accepts input and emits output in batch-major
        form.
      zero_output_for_mask: Boolean (default `False`).
        Whether the output should use zeros for the masked timesteps. Note that
        this field is only used when `return_sequences` is True and mask is
        provided. It can useful if you want to reuse the raw output sequence of
        the RNN without interference from the masked timesteps, eg, merging
        bidirectional RNNs.

    Call arguments:
      inputs: Input tensor.
      mask: Binary tensor of shape `[batch_size, 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 for use with cells that use dropout.
      initial_state: List of initial state tensors to be passed to the first
        call of the cell.
      constants: List of constant tensors to be passed to the cell at each
        timestep.

    Input shape:
      N-D tensor with shape `[batch_size, timesteps, ...]` or
      `[timesteps, batch_size, ...]` when time_major is True.

    Output shape:
      - If `return_state`: a list of tensors. The first tensor is
        the output. The remaining tensors are the last states,
        each with shape `[batch_size, state_size]`, where `state_size` could
        be a high dimension tensor shape.
      - If `return_sequences`: N-D tensor with shape
        `[batch_size, timesteps, output_size]`, where `output_size` could
        be a high dimension tensor shape, or
        `[timesteps, batch_size, output_size]` when `time_major` is True.
      - Else, N-D tensor with shape `[batch_size, output_size]`, where
        `output_size` could be a high dimension tensor shape.

    Masking:
      This layer supports masking for input data with a variable number
      of timesteps. To introduce masks to your data,
      use an [tf.keras.layers.Embedding] layer with the `mask_zero` parameter
      set to `True`.

    Note on using statefulness in RNNs:
      You can set RNN layers to be 'stateful', which means that the states
      computed for the samples in one batch will be reused as initial states
      for the samples in the next batch. This assumes a one-to-one mapping
      between samples in different successive batches.

      To enable statefulness:
        - Specify `stateful=True` in the layer constructor.
        - Specify a fixed batch size for your model, by passing
          If sequential model:
            `batch_input_shape=(...)` to the first layer in your model.
          Else for functional model with 1 or more Input layers:
            `batch_shape=(...)` to all the first layers in your model.
          This is the expected shape of your inputs
          *including the batch size*.
          It should be a tuple of integers, e.g. `(32, 10, 100)`.
        - Specify `shuffle=False` when calling `fit()`.

      To reset the states of your model, call `.reset_states()` on either
      a specific layer, or on your entire model.

    Note on specifying the initial state of RNNs:
      You can specify the initial state of RNN layers symbolically by
      calling them with the keyword argument `initial_state`. The value of
      `initial_state` should be a tensor or list of tensors representing
      the initial state of the RNN layer.

      You can specify the initial state of RNN layers numerically by
      calling `reset_states` with the keyword argument `states`. The value of
      `states` should be a numpy array or list of numpy arrays representing
      the initial state of the RNN layer.

    Note on passing external constants to RNNs:
      You can pass "external" constants to the cell using the `constants`
      keyword argument of `RNN.__call__` (as well as `RNN.call`) method. This
      requires that the `cell.call` method accepts the same keyword argument
      `constants`. Such constants can be used to condition the cell
      transformation on additional static inputs (not changing over time),
      a.k.a. an attention mechanism.

    Examples:

    ```python
    # First, let's define a RNN Cell, as a layer subclass.

    class MinimalRNNCell(keras.layers.Layer):

        def __init__(self, units, **kwargs):
            self.units = units
            self.state_size = units
            super(MinimalRNNCell, self).__init__(**kwargs)

        def build(self, input_shape):
            self.kernel = self.add_weight(shape=(input_shape[-1], self.units),
                                          initializer='uniform',
                                          name='kernel')
            self.recurrent_kernel = self.add_weight(
                shape=(self.units, self.units),
                initializer='uniform',
                name='recurrent_kernel')
            self.built = True

        def call(self, inputs, states):
            prev_output = states[0]
            h = backend.dot(inputs, self.kernel)
            output = h + backend.dot(prev_output, self.recurrent_kernel)
            return output, [output]

    # Let's use this cell in a RNN layer:

    cell = MinimalRNNCell(32)
    x = keras.Input((None, 5))
    layer = RNN(cell)
    y = layer(x)

    # Here's how to use the cell to build a stacked RNN:

    cells = [MinimalRNNCell(32), MinimalRNNCell(64)]
    x = keras.Input((None, 5))
    layer = RNN(cells)
    y = layer(x)
    ```
    Fc           
         s  t |ttfrt|}dt|vr0td| dt|vrJtd| |dd| _d|vrd|v spd	|v r|d	d |dd f}	|	|d< t j	f i | || _
|| _|| _|| _|| _|| _|| _d
| _d | _d | _d | _d | _d| _|r
tj r
tdd S )NcallzFArgument `cell` should have a `call` method. The RNN was passed: cell=
state_sizezqThe RNN cell should have a `state_size` attribute (tuple of integers, one integer per RNN state). Received: cell=zero_output_for_maskFinput_shape	input_diminput_lengthTr   z_Stateful RNNs (created with `stateful=True`) are not yet supported with tf.distribute.Strategy.)
isinstancelisttupler   dir
ValueErrorpopr   super__init__cellreturn_sequencesreturn_statego_backwardsstatefulunroll
time_majorsupports_masking
input_spec
state_spec_statesconstants_spec_num_constantstf
distributehas_strategy)
selfr   r   r   r   r   r    r!   kwargsr   	__class__ U/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/layers/rnn/base_rnn.pyr      sV    

zRNN.__init__c                    s   | j r
dS t jS )NF)r    r   !_use_input_spec_as_call_signaturer+   r-   r/   r0   r1   &  s    z%RNN._use_input_spec_as_call_signaturec                 C   s@   | j d u r:tjdd | jj}tj| jjr4|S |gS | j S )Nc                 S   s   d S Nr/   _r/   r/   r0   <lambda>4      zRNN.states.<locals>.<lambda>)r%   r(   nestmap_structurer   r   	is_nested)r+   stater/   r/   r0   states1  s    
z
RNN.statesc                 C   s
   || _ d S r3   )r%   )r+   r<   r/   r/   r0   r<   8  s    c              	      s,  t |tr|d }zt|}W n& ttfyF   tj|d }Y n0 |d  |d jrh   t	
jjrjj}n
jjg} fdd}tjdd d urtjtj|jj}t|dkr|d n|}n||d }jr$ fdd}tj||}t|tj| S |S d S )Nr      c                    sV   t |  }jrBjr.t  g| }qRt  g| }nt  g| }|S r3   )r(   TensorShapeas_listr   r!   )flat_output_size
output_dimoutput_shapebatchr+   	time_stepr/   r0   _get_output_shapeU  s    

z3RNN.compute_output_shape.<locals>._get_output_shapeoutput_sizec                    s    gt |   }t |S r3   )r(   r>   r?   )
flat_statestate_shape)rD   r/   r0   _get_state_shaper  s    z2RNN.compute_output_shape.<locals>._get_state_shape)r   r   r(   r>   r   	TypeErrorr8   flattenr!   r   is_multiple_stater   r   getattrr9   rG   lenr   r	   to_list)r+   r   r   rF   rB   rJ   rI   r/   rC   r0   compute_output_shape?  s8    



zRNN.compute_output_shapec                 C   sF   t j|d }| jr|nd }| jr>dd | jD }|g| S |S d S )Nr   c                 S   s   g | ]}d qS r3   r/   ).0r5   r/   r/   r0   
<listcomp>  r7   z$RNN.compute_mask.<locals>.<listcomp>)r(   r8   rL   r   r   r<   )r+   inputsmaskoutput_mask
state_maskr/   r/   r0   compute_mask}  s    
zRNN.compute_maskc              	      s  t |tr|d } fdd} fdd}dd }zt|}W n ttfyV   Y n0 tj|s jd ur~|| jd< n||g _||}nD jd urtj	|| jd< nt
tj	|| _tj	||}t  jtjr6 jjs6t jj$  j| d j_W d    n1 s,0    Y  t jjrTt jj}n
 jjg} jd urz | j n>tj|rtj	|| _nd	d
 |D  _ | j  jrȈ   t | d S )Nr   c                    sT   t | tjr|  }nt| } jr(dnd\}} js>d||< d||< tt|dS )z!Convert input shape to InputSpec.)r=   r   )r   r=   Nshape)	r   r(   r>   r?   r   r!   r   r   r   )rZ   input_spec_shapebatch_indextime_step_indexr2   r/   r0   get_input_spec  s    
z!RNN.build.<locals>.get_input_specc                    s@   t | tjrt|  }  jr*| dd  S | d f| dd   S )Nr=   r      )r   r(   r>   r   r?   r!   rY   r2   r/   r0   get_step_input_shape  s    z'RNN.build.<locals>.get_step_input_shapec                 S   s&   t |  }d g| }tt|dS NrY   )r(   r>   r?   r   r   )rZ   Zstate_spec_shaper/   r/   r0   get_state_spec  s    
z!RNN.build.<locals>.get_state_specTc                 S   s&   g | ]}t d gt|  dqS ra   )r   r(   r>   r?   )rR   dimr/   r/   r0   rS     s   zRNN.build.<locals>.<listcomp>)r   r   r(   r>   r   rK   r8   r:   r#   r9   r	   rP   r   r   Layerbuiltr   
name_scopenamebuildr   rM   r   r$   _validate_state_specr   reset_statesr   )r+   r   r^   r`   rb   step_input_shaper   r-   r2   r0   rh     sX    
	


(
z	RNN.buildc                 C   sx   t d|| }tj| }tj|}t|t|kr<|t||D ],\}}t|jdd 	t|sF|qFdS )a  Validate the state spec between the initial_state and the state_size.

        Args:
          cell_state_sizes: list, the `state_size` attribute from the cell.
          init_state_specs: list, the `state_spec` from the initial_state that
            is passed in `call()`.

        Raises:
          ValueError: When initial state spec is not compatible with the state
            size.
        zAn `initial_state` was passed that is not compatible with `cell.state_size`. Received `state_spec`={}; however `cell.state_size` is {}r=   N)
r   formatr(   r8   rL   rO   zipr>   rZ   is_compatible_with)cell_state_sizesinit_state_specsvalidation_errorflat_cell_state_sizesflat_state_specscell_state_speccell_state_sizer/   r/   r0   ri     s$    zRNN._validate_state_specc                 C   s   t | jdd }tj|r*tj|d }t|}| jrB|d n|d }|j}|rd|d ||d}nt	
|| jj|}tj|s|g}t|S )Nget_initial_stater   r=   rT   
batch_sizedtype)rN   r   r(   r8   r:   rL   rZ   r!   ry   r   generate_zero_filled_stater   r   )r+   rT   get_initial_state_fnr   rx   ry   
init_stater/   r/   r0   rv     s     

zRNN.get_initial_stateNc                    s  t |||| j\}}}|d u r<|d u r<t j|fi |S g }g }|d urr||7 }tjdd || _|| j7 }|d ur||7 }dd |D | _	t
|| _|| j	7 }tj|}|rt|d nd}|D ]&}	t|	|krtd| d| q|rh|g| }
| jr| j| }nttjd	d || }|| _t j|
fi |}| jd t
|  | _|S |d urz||d
< |d ur||d< t j|fi |S d S )Nc                 S   s   t t| dS ra   r   r   	int_shape)sr/   r/   r0   r6   4  r7   zRNN.__call__.<locals>.<lambda>c                 S   s   g | ]}t t|d qS )rY   r}   )rR   constantr/   r/   r0   rS   9  s   z RNN.__call__.<locals>.<listcomp>r   Ta  The initial state or constants of an RNN layer cannot be specified via a mix of Keras tensors and non-Keras tensors (a "Keras tensor" is a tensor that was returned by a Keras layer  or by `Input` during Functional model construction). Received: initial_state=z, constants=c                 S   s   d S r3   r/   r4   r/   r/   r0   r6   ^  r7   initial_state	constants)r   standardize_argsr'   r   __call__r(   r8   r9   r$   r&   rO   rL   r   is_keras_tensorr   re   r#   r	   rP   )r+   rT   r   r   r,   additional_inputsadditional_specsflat_additional_inputsr   tensor
full_inputfull_input_specoutputr-   r/   r0   r   #  sj    




	


zRNN.__call__c                    sJ  t |\}}|d u}|| |||\}}}j tjtrfjjD ]}| qV|d ur~t	j
|d }t	j
|rt t	j
|d }	n
t |}	jr|	d n|	d }
jr|
d u rtdi tjjdr|d< tjdd d utjrjjnjj |rdtjjdsPtdj d|  fd	d
}n fdd
}t j||||j|j|d ur|n|
jjjd\}}}jrdd tt	j
jt	j
|D }| jrt j|||jd}n|}j rBt|t!t"fs,|g}nt!|}t#|| S |S d S )Nr   r=   a  Cannot unroll a RNN if the time dimension is undefined. 
- If using a Sequential model, specify the time dimension by passing an `input_shape` or `batch_input_shape` argument to your first layer. If your first layer is an Embedding, you can also use the `input_length` argument.
- If using the functional API, specify the time dimension by passing a `shape` or `batch_shape` argument to your Input layer.training_is_tf_rnn_cellr   z	RNN cell z1 does not support constants. Received: constants=c                    sp   |j  d  }|d j   }t|dkr8r8|d n|} | |fd|i\}}tj|sh|g}||fS )Nr=   r   r   )r'   rO   r(   r8   r:   )rT   r<   r   r   
new_statescell_call_fnis_tf_rnn_cellr,   r+   r/   r0   step  s    
zRNN.call.<locals>.stepc                    sL   t |dkrr|d n|} | |fi \}}tj|sD|g}||fS )Nr=   r   )rO   r(   r8   r:   )rT   r<   r   r   )r   r   r,   r/   r0   r     s    )r   r   rU   r    r   r!   r   return_all_outputsc              	   S   s*   g | ]"\}}t jj|t ||jqS r/   )r(   compatv1assigncastry   )rR   
self_stater;   r/   r/   r0   rS     s   zRNN.call.<locals>.<listcomp>)r   )$r   convert_inputs_if_ragged_validate_args_if_ragged_process_inputs_maybe_reset_cell_dropout_maskr   r   r   cellsr(   r8   rL   r:   r~   r!   r    r   r	   has_argr   rN   callabler   rnnr   r   r   r   rm   r<   
add_updatemaybe_convert_to_raggedr   r   r   rP   )r+   rT   rU   r   r   r   row_lengthsis_ragged_inputr   r   	timestepsr   last_outputoutputsr<   updatesr   r/   r   r0   r   q  s    



	
zRNN.callc                    s.  t |tjjrdt |tsdjs,|dd   n |dj   |j d  }t dkr\d  |d }jrڈ d urt	dd tj
jD }tjjj|dkfdd fdddd	 nj tj
fd
d  n d u r| t tjkr$tdtj dt  d  | |fS )Nr=   r   c                 S   s   g | ]}t j|qS r/   )r(   mathcount_nonzero)rR   r   r/   r/   r0   rS     s   z'RNN._process_inputs.<locals>.<listcomp>c                      s    j S r3   )r<   r/   r2   r/   r0   r6     r7   z%RNN._process_inputs.<locals>.<lambda>c                      s    S r3   r/   r/   )r   r/   r0   r6     r7   T)true_fnfalse_fnstrictc                    s   t |  jp jjS r3   )r(   r   compute_dtyper   )vr2   r/   r0   r6   $  s   z
Layer has z states but was passed z) initial states. Received: initial_state=)r   collectionsabcSequencer   r'   rO   r   r(   add_nr8   rL   r<   r   r   condr9   rv   r   )r+   rT   r   r   non_zero_countr/   )r   r+   r0   r     sN    



zRNN._process_inputsc                 C   s2   |sd S |d ur t d| d| jr.t dd S )Nz The mask that was passed in was zu, which cannot be applied to RaggedTensor inputs. Please make sure that there is no mask injected by upstream layers.zThe input received contains RaggedTensors and does not support unrolling. Disable unrolling by passing `unroll=False` in the RNN Layer constructor.)r   r    )r+   r   rU   r/   r/   r0   r   4  s    
zRNN._validate_args_if_raggedc                 C   s   t |tr|  |  d S r3   )r   r   reset_dropout_maskreset_recurrent_dropout_mask)r+   r   r/   r/   r0   r   F  s    
z"RNN._maybe_reset_cell_dropout_maskc              
   C   s:  | j stdd}| jdur4tj| jd d j}|du rBd}n| jrP|d n|d }|sdtdtj| j	d du rt
| jddrtj| jjd|| jpt d}n$tjt|| jj| jpt }tjtj|}tj| jj|| _	tj| j	s6| j	g| _	n |du rpttj| j	tj| jjD ],\}}t|t|gt|   q@ntj| j	}tj|}	t|	t|krtd| j d	t| d
t|	 d| g }
tt|	|D ]R\}\}}|j|jkrtd| d| j d||f d|j |
 ||f qt!|
 dS )a  Reset the recorded states for the stateful RNN layer.

        Can only be used when RNN layer is constructed with `stateful` = `True`.
        Args:
          states: Numpy arrays that contains the value for the initial state,
            which will be feed to cell at the first time step. When the value is
            None, zero filled numpy array will be created based on the cell
            state size.

        Raises:
          AttributeError: When the RNN layer is not stateful.
          ValueError: When the batch size of the RNN layer is unknown.
          ValueError: When the input numpy array is not compatible with the RNN
            layer state, either size wise or dtype wise.
        zLayer must be stateful.Nr   r=   aI  If a RNN is stateful, it needs to know its batch size. Specify the batch size of your input tensors: 
- If using a Sequential model, specify the batch size by passing a `batch_input_shape` argument to your first layer.
- If using the functional API, specify the batch size by passing a `batch_shape` argument to your Input layer.rv   rw   zLayer z	 expects z states, but it received z  state values. States received: zState z is incompatible with layer z: expected shape=z but found shape=)"r   AttributeErrorr#   r(   r8   rL   rZ   r!   r   r<   rN   r   rv   variable_dtyper   floatxr   rz   r   r9   variablepack_sequence_asr:   rm   	set_valuenpzerosr>   r?   rO   rg   	enumerateappendbatch_set_value)r+   r<   
spec_shaperx   flat_init_state_valuesflat_states_variablesr;   sizeflat_statesflat_input_statesset_value_tuplesivaluer/   r/   r0   rj   K  s    



zRNN.reset_statesc                    st   | j | j| j| j| j| jd}| jr.| j|d< | jr>| j|d< t	| j
|d< t  }tt| t|  S )N)r   r   r   r   r    r!   num_constantsr   r   )r   r   r   r   r    r!   r'   r   r	   serialize_keras_objectr   r   
get_configdictr   items)r+   configbase_configr-   r/   r0   r     s    


zRNN.get_configc                 C   sD   ddl m} ||d|d}|dd}| |fi |}||_|S )Nr   )deserializer   )custom_objectsr   )keras.layersr   r   r'   )clsr   r   deserialize_layerr   r   layerr/   r/   r0   from_config  s    
zRNN.from_configc                 C   s
   t | S r3   )r   RNNSavedModelSaverr2   r/   r/   r0   _trackable_saved_model_saver  s    z RNN._trackable_saved_model_saver)FFFFFF)NN)NNNN)N)N) __name__
__module____qualname____doc__r   propertyr1   r<   setterr(   __internal__tracking no_automatic_dependency_trackingrQ   rX   rh   staticmethodri   r   do_not_doc_inheritablerv   r   r   r   r   r   rj   r   classmethodr   r   __classcell__r/   r/   r-   r0   r   %   sN    C      @

>[
 
Q    
 9
er   )r   r   numpyr   tensorflow.compat.v2r   v2r(   kerasr   keras.enginer   keras.engine.input_specr   Zkeras.layers.rnnr   Z'keras.layers.rnn.dropout_rnn_cell_mixinr   "keras.layers.rnn.stacked_rnn_cellsr   keras.saving.saved_modelr   keras.utilsr	    tensorflow.python.util.tf_exportr
   tensorflow.tools.docsr   rd   r   r/   r/   r/   r0   <module>   s   