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 GRU layer backed by cuDNN.    N)constraints)initializers)regularizers)gru_lstm_utils)	_CuDNNRNN)keras_exportzkeras.layers.CuDNNGRU)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 )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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_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   kwargs	cell_spec	__class__ V/var/www/html/django/DPS/env/lib/python3.9/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nameinitializerregularizer
constraintrecurrent_kernel   biasT)r   build
isinstancelistint
add_weightr   r   r   r!   r-   r   r   r"   r3   r   r   r#   r5   built)r$   input_shape	input_dimr'   r)   r*   r6   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f i |\}}}}}| js| jr|d }| jr| j r|}	ntj|dd}	n|d }	|	|gfS )N)   r      )permr   )axisr?   r,         )weightsbiasesr.   Tgru)inputinput_hinput_cparamsis_trainingrnn_moder+   )
time_majortf	transposeexpand_dimsr   canonical_to_paramsr-   r   r3   r5   _vector_shaperaw_ops
CudnnRNNV2r   r   r   )
r$   inputsinitial_staterH   rJ   args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dictr8   items)r$   configbase_configr'   r)   r*   r^      s4    




zCuDNNGRU.get_config)r
   r   r   NNNNNNNFFFF)__name__
__module____qualname____doc__r   propertyr   r6   r\   r^   __classcell__r)   r)   r'   r*   r	       s*   '              +
 0r	   )rf   r   tensorflow.compat.v2compatv2rN   kerasr   r   r   keras.layers.rnnr   Zkeras.layers.rnn.base_cudnn_rnnr    tensorflow.python.util.tf_exportr   r	   r)   r)   r)   r*   <module>   s   
