B
    d^'                 @   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   kwargsZ	cell_spec)	__class__ M/var/www/html/venv/lib/python3.7/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nameZinitializerZregularizer
constraintrecurrent_kernelc                sX   t j j jd ff||t jj  jff|| j jd ff||gddS )N      r   )axis)tfconcatr   r   compatr   Zones_initializer)_argsr&   )r%   r(   r)   r      s    
z)CuDNNLSTM.build.<locals>.bias_initializer   biasT)r   build
isinstancelistintZ
add_weightr   r   r   r"   r,   r   r   r#   r0   r   r   r    r$   r:   Zbuilt)r%   Zinput_shapeZ	input_dimr   )r'   )r%   r)   r;   {   s4    

zCuDNNLSTM.buildc             C   sX  | 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 |\}}}	}
}
| js| jr|d }|	d }	| jrD| j r4|}ntj|dd}n|d }|||	gfS )N)   r   r2   )Zpermr   r?   )r3   r2      r+   r1         )weightsZbiasesr-   T)inputinput_hinput_cparamsZis_trainingr*   )Z
time_majorr4   Z	transposeZexpand_dimsr   Zcanonical_to_paramsr,   r   r0   r:   Z_vector_shapeZraw_opsZ
CudnnRNNV2r   r   r   )r%   inputsZinitial_staterE   rF   rG   r8   outputshcr7   outputr(   r(   r)   _process_batch   sN      
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dictr=   items)r%   configZbase_config)r'   r(   r)   rO      s(    



zCuDNNLSTM.get_config)r
   r   r   TNNNNNNNFFFF)__name__
__module____qualname____doc__r   propertyr   r;   rM   rO   __classcell__r(   r(   )r'   r)   r	       s(   (              46r	   )rV   r   Ztensorflow.compat.v2r6   Zv2r4   Zkerasr   r   r   Zkeras.layers.rnnr   Zkeras.layers.rnn.base_cudnn_rnnr   Z tensorflow.python.util.tf_exportr   r	   r(   r(   r(   r)   <module>   s   