ó
¾÷Xc           @@  s  d  d l  m 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 e e e e e e d „ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   i    (   t   absolute_importNi   (   t   backend(   t   activations(   t   initializers(   t   regularizers(   t   constraints(   t   Layer(   t	   InputSpec(   t
   interfacesc      	   C@  s²  | s t  j |  ƒ d } n  | s8 t  j |  ƒ d } n  | sT t  j | ƒ d } n  | d k	 rù d | k  ow d k  n rù t  j t  j |  d d … d d d … f d | f ƒ ƒ } t  j | | ƒ }	 t  j |	 | ƒ }
 t  j |  |
 |  d | ƒ}  n  t  j |  d | f ƒ }  t  j |  | ƒ }  | d k	 rDt  j	 |  | ƒ }  n  t  j
 ƒ  d	 k r“t  j |  t  j d | | g ƒ ƒ }  |  j d d | g ƒ n t  j |  d | | f ƒ }  |  S(
   s&  Apply `y . w + b` for every temporal slice y of x.

    # Arguments
        x: input tensor.
        w: weight matrix.
        b: optional bias vector.
        dropout: wether to apply dropout (same dropout mask
            for every temporal slice of the input).
        input_dim: integer; optional dimensionality of the input.
        output_dim: integer; optional dimensionality of the output.
        timesteps: integer; optional number of timesteps.
        training: training phase tensor or boolean.

    # Returns
        Output tensor.
    i   i   g        g      ð?Ni    iÿÿÿÿt   trainingt
   tensorflow(   t   Kt   shapet   Nonet	   ones_liket   reshapet   dropoutt   repeatt   in_train_phaset   dott   bias_addR   t   stackt	   set_shape(   t   xt   wt   bR   t	   input_dimt
   output_dimt	   timestepsR	   t   onest   dropout_matrixt   expanded_dropout_matrix(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   _time_distributed_dense   s(    (:$t	   Recurrentc           B@  s•   e  Z d  Z e e e e d d „ Z d „  Z d „  Z d „  Z d d „ Z	 d „  Z
 d d „ Z d d	 „ Z d d d d
 „ Z d d „ Z d „  Z RS(   sž  Abstract base class for recurrent layers.

    Do not use in a model -- it's not a valid layer!
    Use its children classes `LSTM`, `GRU` and `SimpleRNN` instead.

    All recurrent layers (`LSTM`, `GRU`, `SimpleRNN`) also
    follow the specifications of this class and accept
    the keyword arguments listed below.

    # Example

    ```python
        # as the first layer in a Sequential model
        model = Sequential()
        model.add(LSTM(32, input_shape=(10, 64)))
        # now model.output_shape == (None, 32)
        # note: `None` is the batch dimension.

        # the following is identical:
        model = Sequential()
        model.add(LSTM(32, input_dim=64, input_length=10))

        # for subsequent layers, not need to specify the input size:
        model.add(LSTM(16))
    ```

    # Arguments
        weights: list of Numpy arrays to set as initial weights.
            The list should have 3 elements, of shapes:
            `[(input_dim, output_dim), (output_dim, output_dim), (output_dim,)]`.
        return_sequences: Boolean. Whether to return the last output
            in the output sequence, or the full sequence.
        go_backwards: Boolean (default False).
            If True, process the input sequence backwards.
        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.
        unroll: Boolean (default False).
            If True, the network will be unrolled,
            else a symbolic loop will be used.
            Unrolling can speed-up a RNN,
            although it tends to be more memory-intensive.
            Unrolling is only suitable for short sequences.
        implementation: one of {0, 1, or 2}.
            If set to 0, the RNN will use
            an implementation that uses fewer, larger matrix products,
            thus running faster on CPU but consuming more memory.
            If set to 1, the RNN will use more matrix products,
            but smaller ones, thus running slower
            (may actually be faster on GPU) while consuming less memory.
            If set to 2 (LSTM/GRU only),
            the RNN will combine the input gate,
            the forget gate and the output gate into a single matrix,
            enabling more time-efficient parallelization on the GPU.
            Note: RNN dropout must be shared for all gates,
            resulting in a slightly reduced regularization.
        input_dim: dimensionality of the input (integer).
            This argument (or alternatively, the keyword argument `input_shape`)
            is required when using this layer as the first layer in a model.
        input_length: Length of input sequences, to be specified
            when it is constant.
            This argument is required if you are going to connect
            `Flatten` then `Dense` layers upstream
            (without it, the shape of the dense outputs cannot be computed).
            Note that if the recurrent layer is not the first layer
            in your model, you would need to specify the input length
            at the level of the first layer
            (e.g. via the `input_shape` argument)

    # Input shapes
        3D tensor with shape `(batch_size, timesteps, input_dim)`,
        (Optional) 2D tensors with shape `(batch_size, output_dim)`.

    # Output shape
        - if `return_sequences`: 3D tensor with shape
            `(batch_size, timesteps, units)`.
        - else, 2D tensor with shape `(batch_size, units)`.

    # Masking
        This layer supports masking for input data with a variable number
        of timesteps. To introduce masks to your data,
        use an [Embedding](embeddings.md) layer with the `mask_zero` parameter
        set to `True`.

    # Note on using statefulness in RNNs
        You can set RNN layers to be 'stateful', which means that the states
        computed for the samples in one batch will be reused as initial states
        for the samples in the next batch. This assumes a one-to-one mapping
        between samples in different successive batches.

        To enable statefulness:
            - specify `stateful=True` in the layer constructor.
            - specify a fixed batch size for your model, by passing
                if sequential model:
                  `batch_input_shape=(...)` to the first layer in your model.
                else for functional model with 1 or more Input layers:
                  `batch_shape=(...)` to all the first layers in your model.
                This is the expected shape of your inputs
                *including the batch size*.
                It should be a tuple of integers, e.g. `(32, 10, 100)`.
            - specify `shuffle=False` when calling fit().

        To reset the states of your model, call `.reset_states()` on either
        a specific layer, or on your entire model.

    # Note on specifying initial states in RNNs
        You can specify the initial state of RNN layers by calling them with
        the keyword argument `initial_state`. The value of `initial_state`
        should be a tensor or list of tensors representing the initial state
        of the RNN layer.
    i    c         K@  s}   t  t |  ƒ j |   | |  _ | |  _ | |  _ | |  _ | |  _ t |  _	 t
 d d ƒ |  _ d  |  _ d |  _ d |  _ d  S(   Nt   ndimi   i    (   t   superR!   t   __init__t   return_sequencest   go_backwardst   statefult   unrollt   implementationt   Truet   supports_maskingR   t
   input_specR   t
   state_specR   t   recurrent_dropout(   t   selfR%   R&   R'   R(   R)   t   kwargs(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR$   ¯   s    								c         C@  sR   t  | t ƒ r | d } n  |  j r= | d | d |  j f S| d |  j f Sd  S(   Ni    i   (   t
   isinstancet   listR%   t   units(   R/   t   input_shape(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   compute_output_shapeÁ   s
    	c         C@  s   |  j  r | Sd  Sd  S(   N(   R%   R   (   R/   t   inputst   mask(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   compute_maskÉ   s    	c         C@  s
   t  ‚ d  S(   N(   t   NotImplementedError(   R/   R6   t   states(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   stepÏ   s    c         C@  s   g  S(   N(    (   R/   R6   R	   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   get_constantsÒ   s    c         C@  sz   t  j | ƒ } t  j | d d ƒ} t  j | ƒ } t  j | d |  j g ƒ } g  t t |  j ƒ ƒ D] } | ^ qd } | S(   Nt   axisi   i   (   i   i   (	   R   t
   zeros_liket   sumt   expand_dimst   tileR3   t   ranget   lenR:   (   R/   R6   t   initial_statet   _t   initial_states(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   get_initial_statesÕ   s    (c         C@  s   | S(   N(    (   R/   R6   R	   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   preprocess_inputÞ   s    c         K@  sÚ   | d  k	 rÁ t | d ƒ r´ |  j } |  j } t | t ƒ sH | g } n  | g | |  _ t | t t f ƒ sy | g } n  | g t | ƒ } t t |  ƒ j	 | |  } | |  _ | S| | d <n  t t |  ƒ j	 | |  S(   Nt   _keras_historyRD   (
   R   t   hasattrR,   R-   R1   R2   t   tupleR#   R!   t   __call__(   R/   R6   RD   R0   R,   R-   t   output(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyRL   á   s    			c         C@  s"  | d  k	 r< t | t t f ƒ s- | g } q< t | ƒ } n  t | t ƒ rb | d } | d } n$ |  j rw |  j } n |  j | ƒ } t | ƒ t |  j ƒ k rÛ t d t	 t |  j ƒ ƒ d t	 t | ƒ ƒ d ƒ ‚ n  t
 j | ƒ } |  j r| d d  k rt d ƒ ‚ n  |  j | d d  ƒ} |  j | d d  ƒ} t
 j |  j | | d |  j d	 | d
 | d |  j d | d ƒ\ }	 }
 } |  j râg  } x8 t t | ƒ ƒ D]$ } | j |  j | | | f ƒ q§W|  j | | ƒ n  d |  j |  j k  rt |	 _ t |
 _ n  |  j r|
 S|	 Sd  S(   Ni   i    s
   Layer has s    states but was passed s    initial states.s”  Cannot unroll a RNN if the time dimension is undefined. 
- If using a Sequential model, specify the time dimension by passing an `input_shape` or `batch_input_shape` argument to your first layer. If your first layer is an Embedding, you can also use the `input_length` argument.
- If using the functional API, specify the time dimension by passing a `shape` or `batch_shape` argument to your Input layer.R	   R&   R7   t	   constantsR(   t   input_length(   R   R1   R2   RK   R'   R:   RG   RC   t
   ValueErrort   strR   t	   int_shapeR(   R<   RH   t   rnnR;   R&   RB   t   appendt
   add_updateR   R.   R*   t   _uses_learning_phaseR%   (   R/   R6   R7   RD   R	   RF   R4   RN   t   preprocessed_inputt   last_outputt   outputsR:   t   updatest   i(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   callþ   sL    
	*			"		c         C@  sã  |  j  s t d ƒ ‚ n  |  j s0 t d ƒ ‚ n  |  j j d } | sU t d ƒ ‚ n  | d  k	 rÚ t | t t	 f ƒ s‚ | g } n  t
 | ƒ t
 |  j ƒ k rÚ t d t t
 |  j ƒ ƒ d t t
 | ƒ ƒ d ƒ ‚ qÚ n  |  j d d  k r+g  |  j D] } t j | |  j f ƒ ^ q÷ |  _ | s+d  Sn  x± t |  j ƒ D]  \ } } | r³| | } | j | |  j f k rËt d t | ƒ d	 t | |  j f ƒ d
 t | j ƒ ƒ ‚ qËn t j | |  j f ƒ } t j | | ƒ q;Wd  S(   Ns   Layer must be stateful.s3   Layer has never been called and thus has no states.i    sM  If a RNN is stateful, it needs to know its batch size. Specify the batch size of your input tensors: 
- If using a Sequential model, specify the batch size by passing a `batch_input_shape` argument to your first layer.
- If using the functional API, specify the time dimension by passing a `batch_shape` argument to your Input layer.s   The layer has s9    states, but the `states_value` argument passed only has s    entriess   Expected state #s    to have shape s    but got array with shape (   R'   t   AttributeErrorR,   t   RuntimeErrorR   RP   R   R1   R2   RK   RC   R:   RQ   R   t   zerosR3   t	   enumeratet   npt	   set_value(   R/   t   states_valuet
   batch_sizeRE   R[   t   statet   value(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   reset_states;  s6    		
*.
Ac         C@  ss   i |  j  d 6|  j d 6|  j d 6|  j d 6|  j d 6} t t |  ƒ j ƒ  } t t	 | j
 ƒ  ƒ t	 | j
 ƒ  ƒ ƒ S(   NR%   R&   R'   R(   R)   (   R%   R&   R'   R(   R)   R#   R!   t
   get_configt   dictR2   t   items(   R/   t   configt   base_config(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyRh   g  s    


N(   t   __name__t
   __module__t   __doc__t   FalseR$   R5   R8   R;   R   R<   RG   RH   RL   R\   Rg   Rh   (    (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR!   >   s    o					=,t	   SimpleRNNc           B@  s}   e  Z d  Z e j d e d d d d d d d d d d d d d „ ƒ Z d „  Z d d „ Z	 d	 „  Z
 d d
 „ Z d „  Z RS(   s•	  Fully-connected RNN where the output is to be fed back to input.

    # Arguments
        units: Positive integer, dimensionality of the output space.
        activation: Activation function to use
            (see [activations](../activations.md)).
            If you don't specify anything, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        use_bias: Boolean, whether the layer uses a bias vector.
        kernel_initializer: Initializer for the `kernel` weights matrix,
            used for the linear transformation of the inputs.
            (see [initializers](../initializers.md)).
        recurrent_initializer: Initializer for the `recurrent_kernel`
            weights matrix,
            used for the linear transformation of the recurrent state.
            (see [initializers](../initializers.md)).
        bias_initializer: Initializer for the bias vector
            (see [initializers](../initializers.md)).
        kernel_regularizer: Regularizer function applied to
            the `kernel` weights matrix
            (see [regularizer](../regularizers.md)).
        recurrent_regularizer: Regularizer function applied to
            the `recurrent_kernel` weights matrix
            (see [regularizer](../regularizers.md)).
        bias_regularizer: Regularizer function applied to the bias vector
            (see [regularizer](../regularizers.md)).
        activity_regularizer: Regularizer function applied to
            the output of the layer (its "activation").
            (see [regularizer](../regularizers.md)).
        kernel_constraint: Constraint function applied to
            the `kernel` weights matrix
            (see [constraints](../constraints.md)).
        recurrent_constraint: Constraint function applied to
            the `recurrent_kernel` weights matrix
            (see [constraints](../constraints.md)).
        bias_constraint: Constraint function applied to the bias vector
            (see [constraints](../constraints.md)).
        dropout: Float between 0 and 1.
            Fraction of the units to drop for
            the linear transformation of the inputs.
        recurrent_dropout: Float between 0 and 1.
            Fraction of the units to drop for
            the linear transformation of the recurrent state.

    # References
        - [A Theoretically Grounded Application of Dropout in Recurrent Neural Networks](http://arxiv.org/abs/1512.05287)
    t   tanht   glorot_uniformt
   orthogonalR_   g        c         K@  s(  t  t |  ƒ j |   | |  _ t j | ƒ |  _ | |  _ t j | ƒ |  _	 t j | ƒ |  _
 t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j |	 ƒ |  _ t j |
 ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t d t d | ƒ ƒ |  _ t d t d | ƒ ƒ |  _ d  S(   Ng      ð?g        (   R#   Rq   R$   R3   R   t   gett
   activationt   use_biasR   t   kernel_initializert   recurrent_initializert   bias_initializerR   t   kernel_regularizert   recurrent_regularizert   bias_regularizert   activity_regularizerR   t   kernel_constraintt   recurrent_constraintt   bias_constraintt   mint   maxR   R.   (   R/   R3   Rv   Rw   Rx   Ry   Rz   R{   R|   R}   R~   R   R€   R   R   R.   R0   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR$   ¢  s     		c      
   C@  sv  t  | t ƒ r | d } n  |  j r/ | d n d  } | d |  _ t d | d  |  j f ƒ |  _ t d | |  j f ƒ |  _ d  g |  _	 |  j r |  j
 ƒ  n  |  j |  j |  j f d d d |  j d |  j d |  j ƒ|  _ |  j |  j |  j f d d	 d |  j d |  j d |  j ƒ|  _ |  j r`|  j |  j f d d
 d |  j d |  j d |  j ƒ|  _ n	 d  |  _ t |  _ d  S(   Ni    i   R   t   namet   kernelt   initializert   regularizert
   constraintt   recurrent_kernelt   bias(   R1   R2   R'   R   R   R   R,   R3   R-   R:   Rg   t
   add_weightRx   R{   R   R…   Ry   R|   R€   R‰   Rw   Rz   R}   R   RŠ   R*   t   built(   R/   R4   Rd   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   buildÈ  s8    									c      
   C@  sh   |  j  d k r | St j | ƒ } | d } | d } t | |  j |  j |  j | |  j | d | ƒSd  S(   Ni    i   i   R	   (   R)   R   RR   R    R…   RŠ   R   R3   (   R/   R6   R	   R4   R   R   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyRH   ê  s    

c         C@  s6  |  j  d k r | } n{ d |  j k  o2 d k  n rW t j | | d |  j ƒ } n t j | |  j ƒ } |  j d  k	 r“ t j | |  j ƒ } n  | d } d |  j k  o· d k  n rÍ | | d 9} n  | t j | |  j	 ƒ } |  j
 d  k	 r|  j
 | ƒ } n  d |  j |  j k  r)t | _ n  | | g f S(   Ni    i   i   (   R)   R   R   R   R…   RŠ   R   R   R.   R‰   Rv   R*   RV   (   R/   R6   R:   t   ht   prev_outputRM   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR;   ú  s     	 
c   	   	   @  s¥  g  } ˆ j  d k rÓ d ˆ j k  o/ d k  n rÓ t j | ƒ } | d } t j t j | d  d  … d d f d ƒ ƒ ‰  t j ˆ  d t | ƒ f ƒ ‰  ‡  ‡ f d †  } t j | ˆ  d | ƒ} | j	 | ƒ n | j	 t j
 d ƒ ƒ d ˆ j k  od k  n r‹t j t j | d  d  … d d f d	 ƒ ƒ ‰  t j ˆ  d ˆ j f ƒ ‰  ‡  ‡ f d †  } t j | ˆ  d | ƒ} | j	 | ƒ n | j	 t j
 d ƒ ƒ | S(
   Ni    i   iÿÿÿÿc           @  s   t  j ˆ  ˆ j ƒ S(   N(   R   R   (    (   R   R/   (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   dropped_inputs  s    R	   g      ð?c           @  s   t  j ˆ  ˆ j ƒ S(   N(   R   R   R.   (    (   R   R/   (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR   '  s    (   iÿÿÿÿi   (   iÿÿÿÿi   (   R)   R   R   RR   R   R   RA   t   intR   RT   t   cast_to_floatxR.   R3   (	   R/   R6   R	   RN   R4   R   R   t   dp_maskt   rec_dp_mask(    (   R   R/   s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR<     s,    .
.		.		c         C@  s:  i |  j  d 6t j |  j ƒ d 6|  j d 6t j |  j ƒ d 6t j |  j ƒ d 6t j |  j ƒ d 6t	 j |  j
 ƒ d 6t	 j |  j ƒ d 6t	 j |  j ƒ d	 6t	 j |  j ƒ d
 6t j |  j ƒ d 6t j |  j ƒ d 6t j |  j ƒ d 6|  j d 6|  j d 6} t t |  ƒ j ƒ  } t t | j ƒ  ƒ t | j ƒ  ƒ ƒ S(   NR3   Rv   Rw   Rx   Ry   Rz   R{   R|   R}   R~   R   R€   R   R   R.   (   R3   R   t	   serializeRv   Rw   R   Rx   Ry   Rz   R   R{   R|   R}   R~   R   R   R€   R   R   R.   R#   Rq   Rh   Ri   R2   Rj   (   R/   Rk   Rl   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyRh   1  s"    

N(   Rm   Rn   Ro   R   t   legacy_recurrent_supportR*   R   R$   R   RH   R;   R<   Rh   (    (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyRq   q  s*   /	"	 t   GRUc           B@  s€   e  Z d  Z e j d d e d d d d d d d d d d d d d „ ƒ Z d „  Z d d	 „ Z	 d d
 „ Z
 d „  Z d „  Z RS(   s   Gated Recurrent Unit - Cho et al. 2014.

    # Arguments
        units: Positive integer, dimensionality of the output space.
        activation: Activation function to use
            (see [activations](../activations.md)).
            If you don't specify anything, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        recurrent_activation: Activation function to use
            for the recurrent step
            (see [activations](../activations.md)).
        use_bias: Boolean, whether the layer uses a bias vector.
        kernel_initializer: Initializer for the `kernel` weights matrix,
            used for the linear transformation of the inputs.
            (see [initializers](../initializers.md)).
        recurrent_initializer: Initializer for the `recurrent_kernel`
            weights matrix,
            used for the linear transformation of the recurrent state.
            (see [initializers](../initializers.md)).
        bias_initializer: Initializer for the bias vector
            (see [initializers](../initializers.md)).
        kernel_regularizer: Regularizer function applied to
            the `kernel` weights matrix
            (see [regularizer](../regularizers.md)).
        recurrent_regularizer: Regularizer function applied to
            the `recurrent_kernel` weights matrix
            (see [regularizer](../regularizers.md)).
        bias_regularizer: Regularizer function applied to the bias vector
            (see [regularizer](../regularizers.md)).
        activity_regularizer: Regularizer function applied to
            the output of the layer (its "activation").
            (see [regularizer](../regularizers.md)).
        kernel_constraint: Constraint function applied to
            the `kernel` weights matrix
            (see [constraints](../constraints.md)).
        recurrent_constraint: Constraint function applied to
            the `recurrent_kernel` weights matrix
            (see [constraints](../constraints.md)).
        bias_constraint: Constraint function applied to the bias vector
            (see [constraints](../constraints.md)).
        dropout: Float between 0 and 1.
            Fraction of the units to drop for
            the linear transformation of the inputs.
        recurrent_dropout: Float between 0 and 1.
            Fraction of the units to drop for
            the linear transformation of the recurrent state.

    # References
        - [On the Properties of Neural Machine Translation: Encoder-Decoder Approaches](https://arxiv.org/abs/1409.1259)
        - [Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling](http://arxiv.org/abs/1412.3555v1)
        - [A Theoretically Grounded Application of Dropout in Recurrent Neural Networks](http://arxiv.org/abs/1512.05287)
    Rr   t   hard_sigmoidRs   Rt   R_   g        c         K@  s:  t  t |  ƒ j |   | |  _ t j | ƒ |  _ t j | ƒ |  _ | |  _ t	 j | ƒ |  _
 t	 j | ƒ |  _ t	 j | ƒ |  _ t j | ƒ |  _ t j |	 ƒ |  _ t j |
 ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t d t d | ƒ ƒ |  _ t d t d | ƒ ƒ |  _ d  S(   Ng      ð?g        (   R#   R—   R$   R3   R   Ru   Rv   t   recurrent_activationRw   R   Rx   Ry   Rz   R   R{   R|   R}   R~   R   R   R€   R   R‚   Rƒ   R   R.   (   R/   R3   Rv   R™   Rw   Rx   Ry   Rz   R{   R|   R}   R~   R   R€   R   R   R.   R0   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR$   {  s"    		c      
   C@  sá  t  | t ƒ r | d } n  |  j r/ | d n d  } | d |  _ t d | d  |  j f ƒ |  _ t d | |  j f ƒ |  _ d  g |  _	 |  j r |  j
 ƒ  n  |  j |  j |  j d f d d d |  j d |  j d	 |  j ƒ|  _ |  j |  j |  j d f d d
 d |  j d |  j d	 |  j ƒ|  _ |  j ri|  j |  j d f d d d d d |  j d	 |  j ƒ|  _ n	 d  |  _ |  j d  d  … d  |  j … f |  _ |  j d  d  … d  |  j … f |  _ |  j d  d  … |  j |  j d … f |  _ |  j d  d  … |  j |  j d … f |  _ |  j d  d  … |  j d d  … f |  _ |  j d  d  … |  j d d  … f |  _ |  j r¹|  j |  j  |  _ |  j |  j |  j d !|  _ |  j |  j d |  _  n d  |  _ d  |  _ d  |  _  t! |  _" d  S(   Ni    i   R   i   R„   R…   R†   R‡   Rˆ   R‰   RŠ   t   zero(#   R1   R2   R'   R   R   R   R,   R3   R-   R:   Rg   R‹   Rx   R{   R   R…   Ry   R|   R€   R‰   Rw   R}   R   RŠ   t   kernel_zt   recurrent_kernel_zt   kernel_rt   recurrent_kernel_rt   kernel_ht   recurrent_kernel_ht   bias_zt   bias_rt   bias_hR*   RŒ   (   R/   R4   Rd   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR   £  sV    								%%,))				c   	   
   C@  sæ   |  j  d k rÞ t j | ƒ } | d } | d } t | |  j |  j |  j | |  j | d | ƒ} t | |  j |  j	 |  j | |  j | d | ƒ} t | |  j
 |  j |  j | |  j | d | ƒ} t j | | | g d d ƒS| Sd  S(   Ni    i   i   R	   R=   (   R)   R   RR   R    R›   R¡   R   R3   R   R¢   RŸ   R£   t   concatenate(	   R/   R6   R	   R4   R   R   t   x_zt   x_rt   x_h(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyRH   Ø  s    

c   
   
   @  s	  g  } ˆ j  d k rì d ˆ j k  o/ d k  n rì t j | ƒ } | d } t j t j | d  d  … d d f d	 ƒ ƒ ‰  t j ˆ  d t | ƒ f ƒ ‰  ‡  ‡ f d †  } g  t d ƒ D] } t j	 | ˆ  d | ƒ^ q¸ } | j
 | ƒ n/ | j
 g  t d ƒ D] } t j d ƒ ^ qÿ ƒ d ˆ j k  o5d k  n rÖt j t j | d  d  … d d f d
 ƒ ƒ ‰  t j ˆ  d ˆ j f ƒ ‰  ‡  ‡ f d †  } g  t d ƒ D] } t j	 | ˆ  d | ƒ^ q¢}	 | j
 |	 ƒ n/ | j
 g  t d ƒ D] } t j d ƒ ^ qéƒ | S(   Ni    i   iÿÿÿÿc           @  s   t  j ˆ  ˆ j ƒ S(   N(   R   R   (    (   R   R/   (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR   ó  s    i   R	   g      ð?c           @  s   t  j ˆ  ˆ j ƒ S(   N(   R   R   R.   (    (   R   R/   (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR     s    (   iÿÿÿÿi   (   iÿÿÿÿi   (   R)   R   R   RR   R   R   RA   R‘   RB   R   RT   R’   R.   R3   (
   R/   R6   R	   RN   R4   R   R   RE   R“   R”   (    (   R   R/   s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR<   ë  s(    .
../../c         C@  sÍ  | d } | d } | d } |  j  d k rÌt j | | d |  j ƒ } |  j rk t j | |  j ƒ } n  t j | | d |  j d  d  … d  d |  j … f ƒ } | d  d  … d  |  j … f } | d  d  … |  j d |  j … f }	 | d  d  … d  |  j … f }
 | d  d  … |  j d |  j … f } |  j	 | |
 ƒ } |  j	 |	 | ƒ } | d  d  … d |  j d  … f } t j | | | d |  j d  d  … d |  j d  … f ƒ } |  j
 | | ƒ } n¼|  j  d k rF| d  d  … d  |  j … f } | d  d  … |  j d |  j … f }	 | d  d  … d |  j d  … f } nÀ |  j  d k rút j | | d |  j ƒ } t j | | d |  j ƒ }	 t j | | d |  j ƒ } |  j rt j | |  j ƒ } t j |	 |  j ƒ }	 t j | |  j ƒ } qn t d ƒ ‚ |  j	 | t j | | d |  j ƒ ƒ } |  j	 |	 t j | | d |  j ƒ ƒ } |  j
 | t j | | | d |  j ƒ ƒ } | | d | | } d |  j |  j k  rÀt | _ n  | | g f S(   Ni    i   i   s   Unknown `implementation` mode.(   R)   R   R   R…   Rw   R   RŠ   R‰   R3   R™   Rv   R›   R   RŸ   R¡   R¢   R£   RP   Rœ   Rž   R    R   R.   R*   RV   (   R/   R6   R:   t   h_tm1R“   R”   t   matrix_xt   matrix_innerR¥   R¦   t   recurrent_zt   recurrent_rt   zt   rR§   t   recurrent_ht   hhRŽ   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR;     sT    


	)&&#)&&	c         C@  sM  i |  j  d 6t j |  j ƒ d 6t j |  j ƒ d 6|  j d 6t j |  j ƒ d 6t j |  j ƒ d 6t j |  j	 ƒ d 6t
 j |  j ƒ d 6t
 j |  j ƒ d	 6t
 j |  j ƒ d
 6t
 j |  j ƒ d 6t j |  j ƒ d 6t j |  j ƒ d 6t j |  j ƒ d 6|  j d 6|  j d 6} t t |  ƒ j ƒ  } t t | j ƒ  ƒ t | j ƒ  ƒ ƒ S(   NR3   Rv   R™   Rw   Rx   Ry   Rz   R{   R|   R}   R~   R   R€   R   R   R.   (   R3   R   R•   Rv   R™   Rw   R   Rx   Ry   Rz   R   R{   R|   R}   R~   R   R   R€   R   R   R.   R#   R—   Rh   Ri   R2   Rj   (   R/   Rk   Rl   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyRh   >  s$    

N(   Rm   Rn   Ro   R   R–   R*   R   R$   R   RH   R<   R;   Rh   (    (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR—   E  s,   4	5 	3t   LSTMc           B@  sƒ   e  Z d  Z e j d d e d d d e d d d d d d d d d d „ ƒ Z d „  Z d d	 „ Z	 d d
 „ Z
 d „  Z d „  Z RS(   s,  Long-Short Term Memory unit - Hochreiter 1997.

    For a step-by-step description of the algorithm, see
    [this tutorial](http://deeplearning.net/tutorial/lstm.html).

    # Arguments
        units: Positive integer, dimensionality of the output space.
        activation: Activation function to use
            (see [activations](../activations.md)).
            If you don't specify anything, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        recurrent_activation: Activation function to use
            for the recurrent step
            (see [activations](../activations.md)).
        use_bias: Boolean, whether the layer uses a bias vector.
        kernel_initializer: Initializer for the `kernel` weights matrix,
            used for the linear transformation of the inputs.
            (see [initializers](../initializers.md)).
        recurrent_initializer: Initializer for the `recurrent_kernel`
            weights matrix,
            used for the linear transformation of the recurrent state.
            (see [initializers](../initializers.md)).
        bias_initializer: Initializer for the bias vector
            (see [initializers](../initializers.md)).
        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)
        kernel_regularizer: Regularizer function applied to
            the `kernel` weights matrix
            (see [regularizer](../regularizers.md)).
        recurrent_regularizer: Regularizer function applied to
            the `recurrent_kernel` weights matrix
            (see [regularizer](../regularizers.md)).
        bias_regularizer: Regularizer function applied to the bias vector
            (see [regularizer](../regularizers.md)).
        activity_regularizer: Regularizer function applied to
            the output of the layer (its "activation").
            (see [regularizer](../regularizers.md)).
        kernel_constraint: Constraint function applied to
            the `kernel` weights matrix
            (see [constraints](../constraints.md)).
        recurrent_constraint: Constraint function applied to
            the `recurrent_kernel` weights matrix
            (see [constraints](../constraints.md)).
        bias_constraint: Constraint function applied to the bias vector
            (see [constraints](../constraints.md)).
        dropout: Float between 0 and 1.
            Fraction of the units to drop for
            the linear transformation of the inputs.
        recurrent_dropout: Float between 0 and 1.
            Fraction of the units to drop for
            the linear transformation of the recurrent state.

    # References
        - [Long short-term memory](http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf) (original 1997 paper)
        - [Learning to forget: Continual prediction with LSTM](http://www.mitpressjournals.org/doi/pdf/10.1162/089976600300015015)
        - [Supervised sequence labeling with recurrent neural networks](http://www.cs.toronto.edu/~graves/preprint.pdf)
        - [A Theoretically Grounded Application of Dropout in Recurrent Neural Networks](http://arxiv.org/abs/1512.05287)
    Rr   R˜   Rs   Rt   R_   g        c         K@  sC  t  t |  ƒ j |   | |  _ t j | ƒ |  _ t j | ƒ |  _ | |  _ t	 j | ƒ |  _
 t	 j | ƒ |  _ t	 j | ƒ |  _ | |  _ t j |	 ƒ |  _ t j |
 ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t d t d | ƒ ƒ |  _ t d t d | ƒ ƒ |  _ d  S(   Ng      ð?g        (   R#   R±   R$   R3   R   Ru   Rv   R™   Rw   R   Rx   Ry   Rz   t   unit_forget_biasR   R{   R|   R}   R~   R   R   R€   R   R‚   Rƒ   R   R.   (   R/   R3   Rv   R™   Rw   Rx   Ry   Rz   R²   R{   R|   R}   R~   R   R€   R   R   R.   R0   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR$     s$    			c      
   C@  sØ  t  | t ƒ r | d } n  |  j r/ | d n d  } | d |  _ t d | d  |  j f ƒ |  _ t d | |  j f ƒ t d | |  j f ƒ g |  _ d  d  g |  _	 |  j r¸ |  j
 ƒ  n  |  j |  j |  j d f d d d |  j d |  j d	 |  j ƒ|  _ |  j |  j |  j d f d d
 d |  j d |  j d	 |  j ƒ|  _ |  j rÖ|  j |  j d f d d d |  j d |  j d	 |  j ƒ|  _ |  j rßt j |  j d f ƒ } d | |  j |  j d +t j |  j | ƒ qßn	 d  |  _ |  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 |  _% |  j r§|  j |  j  |  _& |  j |  j |  j d !|  _' |  j |  j d |  j d !|  _( |  j |  j d |  _) n$ d  |  _& d  |  _' d  |  _( d  |  _) t* |  _+ d  S(   Ni    i   R   i   R„   R…   R†   R‡   Rˆ   R‰   RŠ   g      ð?i   (,   R1   R2   R'   R   R   R   R,   R3   R-   R:   Rg   R‹   Rx   R{   R   R…   Ry   R|   R€   R‰   Rw   Rz   R}   R   RŠ   R²   Ra   R_   R   Rb   t   kernel_it   kernel_ft   kernel_ct   kernel_ot   recurrent_kernel_it   recurrent_kernel_ft   recurrent_kernel_ct   recurrent_kernel_ot   bias_it   bias_ft   bias_ct   bias_oR*   RŒ   (   R/   R4   Rd   t
   bias_value(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR   º  sd    										%,0)%,0)	!				c   
   
   C@  s  |  j  d k rt j | ƒ } | d } | d } t | |  j |  j |  j | |  j | d | ƒ} t | |  j |  j	 |  j | |  j | d | ƒ} t | |  j
 |  j |  j | |  j | d | ƒ} t | |  j |  j |  j | |  j | d | ƒ}	 t j | | | |	 g d d ƒS| Sd  S(   Ni    i   i   R	   R=   (   R)   R   RR   R    R³   R»   R   R3   R´   R¼   Rµ   R½   R¶   R¾   R¤   (
   R/   R6   R	   R4   R   R   t   x_it   x_ft   x_ct   x_o(    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyRH   ÷  s$    

c   
   
   @  s	  g  } ˆ j  d k rì d ˆ j k  o/ d k  n rì t j | ƒ } | d } t j t j | d  d  … d d f d	 ƒ ƒ ‰  t j ˆ  d t | ƒ f ƒ ‰  ‡  ‡ f d †  } g  t d ƒ D] } t j	 | ˆ  d | ƒ^ q¸ } | j
 | ƒ n/ | j
 g  t d ƒ D] } t j d ƒ ^ qÿ ƒ d ˆ j k  o5d k  n rÖt j t j | d  d  … d d f d
 ƒ ƒ ‰  t j ˆ  d ˆ j f ƒ ‰  ‡  ‡ f d †  } g  t d ƒ D] } t j	 | ˆ  d | ƒ^ q¢}	 | j
 |	 ƒ n/ | j
 g  t d ƒ D] } t j d ƒ ^ qéƒ | S(   Ni    i   iÿÿÿÿc           @  s   t  j ˆ  ˆ j ƒ S(   N(   R   R   (    (   R   R/   (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR     s    i   R	   g      ð?c           @  s   t  j ˆ  ˆ j ƒ S(   N(   R   R   R.   (    (   R   R/   (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR   #  s    (   iÿÿÿÿi   (   iÿÿÿÿi   (   R)   R   R   RR   R   R   RA   R‘   RB   R   RT   R’   R.   R3   (
   R/   R6   R	   RN   R4   R   R   RE   R“   R”   (    (   R   R/   s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR<     s(    .
../../c         C@  s¾  | d } | d } | d } | d } |  j  d k rst j | | d |  j ƒ } | t j | | d |  j ƒ 7} |  j r– t j | |  j ƒ } n  | d  d  … d  |  j … f } | d  d  … |  j d |  j … f }	 | d  d  … d |  j d |  j … f }
 | d  d  … d |  j d  … f } |  j	 | ƒ } |  j	 |	 ƒ } | | | |  j
 |
 ƒ } |  j	 | ƒ } n|  j  d k r| d  d  … d  |  j … f } | d  d  … |  j d |  j … f } | d  d  … d |  j d |  j … f } | d  d  … d |  j d  … f } n® |  j  d k r¹t j | | d |  j ƒ |  j } t j | | d |  j ƒ |  j } t j | | d |  j ƒ |  j } t j | | d |  j ƒ |  j } n t d ƒ ‚ |  j	 | t j | | d |  j ƒ ƒ } |  j	 | t j | | d |  j ƒ ƒ } | | | |  j
 | t j | | d |  j ƒ ƒ } |  j	 | t j | | d |  j ƒ ƒ } | |  j
 | ƒ } d |  j |  j k  r®t | _ n  | | | g f S(   Ni    i   i   i   s   Unknown `implementation` mode.(   R)   R   R   R…   R‰   Rw   R   RŠ   R3   R™   Rv   R³   R»   R´   R¼   Rµ   R½   R¶   R¾   RP   R·   R¸   R¹   Rº   R   R.   R*   RV   (   R/   R6   R:   R¨   t   c_tm1R“   R”   R­   t   z0t   z1t   z2t   z3R[   t   ft   ct   oRÀ   RÁ   RÂ   RÃ   RŽ   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR;   -  sP    



!	&*#&*&$$$'$c         C@  sW  i |  j  d 6t j |  j ƒ d 6t j |  j ƒ d 6|  j d 6t j |  j ƒ d 6t j |  j ƒ d 6t j |  j	 ƒ d 6|  j
 d 6t j |  j ƒ d	 6t j |  j ƒ d
 6t j |  j ƒ d 6t j |  j ƒ d 6t j |  j ƒ d 6t j |  j ƒ d 6t j |  j ƒ d 6|  j d 6|  j d 6} t t |  ƒ j ƒ  } t t | j ƒ  ƒ t | j ƒ  ƒ ƒ S(   NR3   Rv   R™   Rw   Rx   Ry   Rz   R²   R{   R|   R}   R~   R   R€   R   R   R.   (   R3   R   R•   Rv   R™   Rw   R   Rx   Ry   Rz   R²   R   R{   R|   R}   R~   R   R   R€   R   R   R.   R#   R±   Rh   Ri   R2   Rj   (   R/   Rk   Rl   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyRh   ]  s&    


N(   Rm   Rn   Ro   R   R–   R*   R   R$   R   RH   R<   R;   Rh   (    (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyR±   S  s.   <	= 	0(   t
   __future__R    t   numpyRa   t    R   R   R   R   R   R   t   engineR   R   t   legacyR   R   R    R!   Rq   R—   R±   (    (    (    s5   /tmp/pip-build-isqEY4/keras/keras/layers/recurrent.pyt   <module>   s$   -ÿ 4Ôÿ 