o
    Ne!                     @   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 GRU layer backed by cuDNN.    N)constraints)initializers)regularizers)gru_lstm_utils)	_CuDNNRNN)keras_exportzkeras.layers.CuDNNGRU)v1c                       sj   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 )CuDNNGRUaP  Fast GRU 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.
        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zerosNFc                    s   || _ tdd}|| j d| _t jd||||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_initializerr   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   kwargsZ	cell_spec	__class__r   JD:\Projects\ConvertPro\env\Lib\site-packages\keras/layers/rnn/cudnn_gru.pyr   F   s*   zCuDNNGRU.__init__c                 C   s   | j S )N)r   )r%   r   r   r)   r   q   s   zCuDNNGRU.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| jd fd| j| j| jd| _d	| _d S )
Nr      kernel)shapenameZinitializerZregularizer
constraintrecurrent_kernel   biasT)r   build
isinstancelistintZ
add_weightr   r   r   r"   r,   r   r   r#   r0   r   r    r$   r2   Zbuilt)r%   Zinput_shapeZ	input_dimr'   r   r)   r3   u   s4   


zCuDNNGRU.buildc           
   
   C   s  | j s
tj|dd}|d }tj|dd}tj| jd d | j| jd f | jd d d | jf | jd d | jd d f | jd d | j| jd f | jd d d | jf | jd d | jd d f g| j	| j| jd  | j	d | j | j	| jd | jd  | j	| jd | jd  | j	| jd | jd  | j	| jd d  g| j
d	}||d|d
dd}tjjdi |\}}}}}| js| jr|d }| jr| j r|}	ntj|dd}	n|d }	|	|gfS )N)   r      )permr   )Zaxisr8   r+         )weightsZbiasesr-   TZgru)inputinput_hZinput_cparamsZis_trainingZrnn_moder*   r   )Z
time_majortfZ	transposeZexpand_dimsr   Zcanonical_to_paramsr,   r   r0   r2   Z_vector_shapeZraw_opsZ
CudnnRNNV2r   r   r   )
r%   ZinputsZinitial_stater>   r?   argsZoutputsh_outputr   r   r)   _process_batch   sJ   		
zCuDNNGRU._process_batchc                    s   | j t| jt| jt| 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   	serializer   r   r   r   r   r   r    r!   r   r"   r#   r$   r   
get_configdictr5   items)r%   configZbase_configr'   r   r)   rG      s4   




zCuDNNGRU.get_config)r
   r   r   NNNNNNNFFFF)__name__
__module____qualname____doc__r   propertyr   r3   rE   rG   __classcell__r   r   r'   r)   r	       s,    '+
 0r	   )rN   r   Ztensorflow.compat.v2compatv2r@   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   
