a
    Sic^'                     @   s   d 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 ed	gd
G dd deZdS )z Fast LSTM layer backed by cuDNN.    N)constraints)initializers)regularizers)gru_lstm_utils)	_CuDNNRNN)keras_exportzkeras.layers.CuDNNLSTM)v1c                       sN   e Zd ZdZd fdd		Zed
d Z fddZdd Z fddZ	  Z
S )	CuDNNLSTMa{  Fast LSTM implementation backed by cuDNN.

    More information about cuDNN can be found on the [NVIDIA
    developer website](https://developer.nvidia.com/cudnn).
    Can only be run on GPU.

    Args:
        units: Positive integer, dimensionality of the output space.
        kernel_initializer: Initializer for the `kernel` weights matrix, used
          for the linear transformation of the inputs.
        unit_forget_bias: Boolean. 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.](http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf)
        recurrent_initializer: Initializer for the `recurrent_kernel` weights
          matrix, used for the linear transformation of the recurrent state.
        bias_initializer: Initializer for the bias vector.
        kernel_regularizer: Regularizer function applied to the `kernel` weights
          matrix.
        recurrent_regularizer: Regularizer function applied to the
          `recurrent_kernel` weights matrix.
        bias_regularizer: Regularizer function applied to the bias vector.
        activity_regularizer: Regularizer function applied to the output of the
          layer (its "activation").
        kernel_constraint: Constraint function applied to the `kernel` weights
          matrix.
        recurrent_constraint: Constraint function applied to the
          `recurrent_kernel` weights matrix.
        bias_constraint: Constraint function applied to the bias vector.
        return_sequences: Boolean. Whether to return the last output. in the
          output sequence, or the full sequence.
        return_state: Boolean. 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.
    glorot_uniform
orthogonalzerosTNFc                    s   || _ tdd}|| j | j fd| _t jf ||||d| t|| _t|| _	t|| _
|| _t|| _t|| _t|| _t|	| _t|
| _t|| _t|| _d S )Ncell
state_size)r   )return_sequencesreturn_statego_backwardsstateful)unitscollections
namedtuple_cellsuper__init__r   getkernel_initializerrecurrent_initializerbias_initializerunit_forget_biasr   kernel_regularizerrecurrent_regularizerbias_regularizeractivity_regularizerr   kernel_constraintrecurrent_constraintbias_constraint)selfr   r   r   r   r   r   r   r    r!   r"   r#   r$   r   r   r   r   kwargs	cell_spec	__class__ W/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/layers/rnn/cudnn_lstm.pyr   J   s,    zCuDNNLSTM.__init__c                 C   s   | j S )N)r   r%   r*   r*   r+   r   w   s    zCuDNNLSTM.cellc                    s   t  | t|tr|d }t|d } j| jd fd j j j	d _
 j j jd fd j j jd _ jr fdd}n j} j jd	 fd
| j jd _d _d S )Nr      kernel)shapenameinitializerregularizer
constraintrecurrent_kernelc                    sj   t j j jd fg|R i |t jj  jfg|R i | j jd fg|R i |gddS )N      r   axis)tfconcatr   r   compatr   ones_initializer)_argsr&   r,   r*   r+   r      s2    


z)CuDNNLSTM.build.<locals>.bias_initializer   biasT)r   build
isinstancelistint
add_weightr   r   r   r"   r/   r   r   r#   r5   r   r   r    r$   rA   built)r%   input_shape	input_dimr   r(   r,   r+   rB   {   s:    

zCuDNNLSTM.buildc                 C   s\  | j stj|dd}|d }|d }tj|dd}tj|dd}tj| jd d d | jf | jd d | j| jd f | jd d | jd | jd f | jd d | jd d f | jd d d | jf | jd d | j| jd f | jd d | jd | jd f | jd d | jd d f g| j	d | j | j	| j| jd  | j	| jd | jd  | j	| jd | jd  | j	| jd | jd	  | j	| jd	 | jd
  | j	| jd
 | jd  | j	| jd d  g| j
d}||||dd}tjjf i |\}}}	}
}
| js| jr"|d }|	d }	| jrH| j r8|}ntj|dd}n|d }|||	gfS )N)   r   r7   )permr   rJ   r8   r7      r.   r6         )weightsbiasesr0   T)inputinput_hinput_cparamsis_trainingr-   )
time_majorr:   	transposeexpand_dimsr   canonical_to_paramsr/   r   r5   rA   _vector_shaperaw_ops
CudnnRNNV2r   r   r   )r%   inputsinitial_staterR   rS   rT   r?   outputshcr>   outputr*   r*   r+   _process_batch   sV      
zCuDNNLSTM._process_batchc                    s   | j t| jt| jt| j| jt| jt| j	t| j
t| jt| jt| jt| jd}t  }tt| t|  S )N)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   
get_configdictrD   items)r%   configbase_configr(   r*   r+   re      s6    




zCuDNNLSTM.get_config)r
   r   r   TNNNNNNNFFFF)__name__
__module____qualname____doc__r   propertyr   rB   rc   re   __classcell__r*   r*   r(   r+   r	       s,   +               -
46r	   )rm   r   tensorflow.compat.v2r<   v2r:   kerasr   r   r   keras.layers.rnnr   keras.layers.rnn.base_cudnn_rnnr    tensorflow.python.util.tf_exportr   r	   r*   r*   r*   r+   <module>   s   
