B
    ӻdvQ                 @   s   d Z ddl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 ddlmZ ddlmZ G dd deZedgdG dd deZedgdG dd deZdS )z!Recurrent layers backed by cuDNN.    N)constant_op)backend)constraints)initializers)regularizers)	InputSpec)recurrent_v2)RNN)	array_ops)gen_cudnn_rnn_ops)	state_ops)keras_exportc                   sz   e Zd ZdZd fdd	ZdddZ fdd	Zed
d Ze	dd Z
e	dd Ze	 fddZd fdd	Z  ZS )	_CuDNNRNNa-  Private base class for CuDNNGRU and CuDNNLSTM layers.

  Args:
    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.
    time_major: Boolean (default False). If true, the inputs and outputs will be
        in shape `(timesteps, batch, ...)`, whereas in the False case, it will
        be `(batch, timesteps, ...)`.
  Fc                s   t t| jf | || _|| _|| _|| _|| _d| _t	ddg| _
t| jjdr\| jj}n
| jjg}dd |D | _d | _d | _d| _tdg| _d S )	NF   )ndim__len__c             S   s   g | ]}t d |fdqS )N)shape)r   ).0dim r   `/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/keras/layers/cudnn_recurrent.py
<listcomp>I   s    z&_CuDNNRNN.__init__.<locals>.<listcomp>r   )superr	   __init__return_sequencesreturn_statego_backwardsstateful
time_majorZsupports_maskingr   Z
input_spechasattrcell
state_sizeZ
state_specZconstants_spec_statesZ_num_constantsr   Zconstant_vector_shape)selfr   r   r   r   r   kwargsr"   )	__class__r   r   r   4   s     	

z_CuDNNRNN.__init__Nc             C   s   t |tr|d }|d k	r"tdt |trB|dd  }|d }n"|d k	rLn| jrZ| j}n
| |}t|t| jkrtdtt| j d tt| d | jrt	
|d}| ||\}}| jrdd t| j|D }| | | jr|g| S |S d S )	Nr   z(Masking is not supported for CuDNN RNNs.   z
Layer has z states but was passed z initial states.c             S   s   g | ]\}}t ||qS r   )r   Zassign)r   Z
self_statestater   r   r   r   n   s   z"_CuDNNRNN.call.<locals>.<listcomp>)
isinstancelist
ValueErrorr   statesZget_initial_statelenstrr   r   reverse_process_batchzipZ
add_updater   )r%   inputsmaskZtraininginitial_stateoutputr-   Zupdatesr   r   r   callO   s4    



 

z_CuDNNRNN.callc                sD   | j | j| j| j| jd}tt|  }tt	|
 t	|
  S )N)r   r   r   r   r   )r   r   r   r   r   r   r	   
get_configdictr+   items)r%   configbase_config)r'   r   r   r8   x   s    
z_CuDNNRNN.get_configc             C   s
   | f |S )Nr   )clsr;   r   r   r   from_config   s    z_CuDNNRNN.from_configc             C   s    | j r| jr| j| j| jgS g S )N)	trainablebuiltkernelrecurrent_kernelbias)r%   r   r   r   trainable_weights   s    z_CuDNNRNN.trainable_weightsc             C   s    | j s| jr| j| j| jgS g S )N)r?   r@   rA   rB   rC   )r%   r   r   r   non_trainable_weights   s    z_CuDNNRNN.non_trainable_weightsc                s   t t| jS )N)r   r	   losses)r%   )r'   r   r   rF      s    z_CuDNNRNN.lossesc                s   t t| j|dS )N)r3   )r   r	   get_losses_for)r%   r3   )r'   r   r   rG      s    z_CuDNNRNN.get_losses_for)FFFFF)NNN)N)__name__
__module____qualname____doc__r   r7   r8   classmethodr>   propertyrD   rE   rF   rG   __classcell__r   r   )r'   r   r   !   s       
)r   zkeras.layers.CuDNNGRU)Z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 )CuDNNGRUa  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t| jf ||||d| t|| _	t|| _
t|| _t|| _t|| _t|| _t|| _t|	| _t|
| _t|| _d S )Nr!   r"   )r"   )r   r   r   r   )unitscollections
namedtuple_cellr   rO   r   r   getkernel_initializerrecurrent_initializerbias_initializerr   kernel_regularizerrecurrent_regularizerbias_regularizeractivity_regularizerr   kernel_constraintrecurrent_constraintbias_constraint)r%   rS   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   r   r   r   r   r&   	cell_spec)r'   r   r   r      s&    zCuDNNGRU.__init__c             C   s   | j S )N)rV   )r%   r   r   r   r!      s    zCuDNNGRU.cellc                s   t 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   r   r   rA   )r   nameinitializerregularizer
constraintrB      rC   T)r   rO   buildr*   r+   int
add_weightrS   rX   r[   r_   rA   rY   r\   r`   rB   rZ   r]   ra   rC   r@   )r%   input_shape	input_dim)r'   r   r   rh      s.    

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f |\}}}}}| js| jr|d }| jr| j r|}	ntj|dd}	n|d }	|	|gfS )N)r(   r      )permr   )axisrm   r         )weightsbiasesr   TZgru)inputinput_hinput_cparamsis_trainingZrnn_moder   )r   r
   	transposeexpand_dimsr   _canonical_to_paramsrA   rS   rB   rC   r$   r   
CudnnRNNV2r   r   r   )
r%   r3   r5   ru   rw   argsoutputsh_r6   r   r   r   r1     sB    
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| t|  S )N)rS   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   )rS   r   	serializerX   rY   rZ   r   r[   r\   r]   r^   r   r_   r`   ra   r   rO   r8   r9   r+   r:   )r%   r;   r<   )r'   r   r   r8   ;  s    








zCuDNNGRU.get_config)rP   rQ   rR   NNNNNNNFFFF)rH   rI   rJ   rK   r   rM   r!   rh   r1   r8   rN   r   r   )r'   r   rO      s&   $             /rO   zkeras.layers.CuDNNLSTMc                   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 )	CuDNNLSTMa3  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.
  rP   rQ   rR   TNFc                s   || _ tdd}|| j | j fd| _tt| jf ||||d| t|| _	t|| _
t|| _|| _t|| _t|| _t|| _t|	| _t|
| _t|| _t|| _d S )Nr!   r"   )r"   )r   r   r   r   )rS   rT   rU   rV   r   r   r   r   rW   rX   rY   rZ   unit_forget_biasr   r[   r\   r]   r^   r   r_   r`   ra   )r%   rS   rX   rY   rZ   r   r[   r\   r]   r^   r_   r`   ra   r   r   r   r   r&   rb   )r'   r   r   r   {  s(    zCuDNNLSTM.__init__c             C   s   | j S )N)rV   )r%   r   r   r   r!     s    zCuDNNLSTM.cellc                s   t 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   r   rp   rA   )r   rc   rd   re   rf   rB   c                sT   t j j jd ff||t  jff|| j jd ff||gddS )Nrq   rm   r   )ro   )r
   concatrZ   rS   r   ZOnes)r   r}   r&   )r%   r   r   rZ     s
    z)CuDNNLSTM.build.<locals>.bias_initializer   rC   T)r   r   rh   r*   r+   ri   rj   rS   rX   r[   r_   rA   rY   r\   r`   rB   r   rZ   r]   ra   rC   r@   )r%   rk   rl   rZ   )r'   )r%   r   rh     s4    

zCuDNNLSTM.buildc             C   sV  | 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f |\}}}	}
}
| js| jr|d }|	d }	| jrB| j r2|}ntj|dd}n|d }|||	gfS )N)r(   r   rm   )rn   r   r(   )ro   rm   r   rp   rq   rg      )rr   rs   r   T)rt   ru   rv   rw   rx   r   )r   r
   ry   rz   r   r{   rA   rS   rB   rC   r$   r   r|   r   r   r   )r%   r3   r5   ru   rv   rw   r}   r~   r   cr   r6   r   r   r   r1     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| t|  S )N)rS   rX   rY   rZ   r   r[   r\   r]   r^   r_   r`   ra   )rS   r   r   rX   rY   rZ   r   r   r[   r\   r]   r^   r   r_   r`   ra   r   r   r8   r9   r+   r:   )r%   r;   r<   )r'   r   r   r8     s    








zCuDNNLSTM.get_config)rP   rQ   rR   TNNNNNNNFFFF)rH   rI   rJ   rK   r   rM   r!   rh   r1   r8   rN   r   r   )r'   r   r   Q  s(   (              '5r   )rK   rT   Ztensorflow.python.frameworkr   Ztensorflow.python.kerasr   r   r   r   Z)tensorflow.python.keras.engine.input_specr   Ztensorflow.python.keras.layersr   Z(tensorflow.python.keras.layers.recurrentr	   Ztensorflow.python.opsr
   r   r   Z tensorflow.python.util.tf_exportr   r   rO   r   r   r   r   r   <module>   s&   | 4