
Xc           @   s  d  d l  Z  d  d l Z d  d l Z d d l m Z m Z d d l m Z	 d d l
 m Z m Z d d l m Z d d l m Z d d l m Z d d	 l m Z d
 e f d     YZ d d  d  e e e e d  Z d e f d     YZ d e f d     YZ d   Z d   Z d S(   iNi   (   t   Layert	   InputSpec(   t   backend(   t	   func_dumpt	   func_load(   t   regularizers(   t   constraints(   t   activations(   t   initializerst   Mergec           B   sw   e  Z d  Z d
 d d d d
 d
 d
 d
 d
 d
 d 
 Z d   Z d
 d  Z d   Z d
 d  Z d   Z	 e
 d	    Z RS(   s  A `Merge` layer can be used to merge a list of tensors
    into a single tensor, following some merge `mode`.
    # Example
    ```python
    model1 = Sequential()
    model1.add(Dense(32, input_dim=32))
    model2 = Sequential()
    model2.add(Dense(32, input_dim=32))
    merged_model = Sequential()
    merged_model.add(Merge([model1, model2], mode='concat', concat_axis=1))
    ```
    # Arguments
        layers: Can be a list of Keras tensors or
            a list of layer instances. Must be more
            than one layer/tensor.
        mode: String or lambda/function. If string, must be one
            of: 'sum', 'mul', 'concat', 'ave', 'cos', 'dot', 'max'.
            If lambda/function, it should take as input a list of tensors
            and return a single tensor.
        concat_axis: Integer, axis to use in mode `concat`.
        dot_axes: Integer or tuple of integers,
            axes to use in mode `dot` or `cos`.
        output_shape: Either a shape tuple (tuple of integers),
            or a lambda/function
            to compute `output_shape`
            (only if merge mode is a lambda/function).
            If the argument is a tuple,
            it should be expected output shape, *not* including the batch size
            (same convention as the `input_shape` argument in layers).
            If the argument is callable,
            it should take as input a list of shape tuples
            (1:1 mapping to input tensors)
            and return a single shape tuple, including the
            batch size (same convention as the
            `compute_output_shape` method of layers).
        node_indices: Optional list of integers containing
            the output node index for each input layer
            (in case some input layers have multiple output nodes).
            will default to an array of 0s if not provided.
        tensor_indices: Optional list of indices of output tensors
            to consider for merging
            (in case some input layer node returns multiple tensors).
        output_mask: Mask or lambda/function to compute the output mask (only
            if merge mode is a lambda/function). If the latter case, it should
            take as input a list of masks and return a single mask.
    t   sumic         C   s  t  j d d d | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | r^ | n i  |  _	 d  |  _ g  |  _ g  |  _ i  |  _ g  |  _ g  |  _ t |  _ t |  _ d  |  _ |
 s |  j j j   } | d t t j |   }
 n  |
 |  _ | r| s-g  t t |   D] } d ^ q} n  |	 s[g  t t |   D] } d ^ qF}	 n  |  j | | | | | |	  t |  _  g  } g  } xc t! |  D]U \ } } | | } |	 | } | j | } | j" | j# |  | j" | j$ |  qW|  | d | n	 t |  _  d  S(   Ns   The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.t
   stackleveli   t   _i    t   mask(%   t   warningst   warnt   layerst   modet   concat_axist   dot_axest   _output_shapet   node_indicest   _output_maskt	   argumentst   Nonet   _initial_weightst   inbound_nodest   outbound_nodesR   t   _trainable_weightst   _non_trainable_weightst   Truet   supports_maskingt   Falset   uses_learning_phaset
   input_spect	   __class__t   __name__t   lowert   strt   Kt   get_uidt   namet   ranget   lent   _arguments_validationt   builtt	   enumeratet   appendt   output_tensorst   output_masks(   t   selfR   R   R   R   t   output_shapet   output_maskR   R   t   tensor_indicesR)   t   prefixR   t   input_tensorst   input_maskst   it   layert
   node_indext   tensor_indext   inbound_node(    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt   __init__>   sT    																 	((
	

c         C   s  t  |  sI | d d d d d d d h k rI t d t |    qI n  t | t t f  sq t |  d	 k  r t d
 t |    n  | d k r g  t	 t |   D] } d ^ q } n  g  } xZ t
 |  D]L \ }	 }
 |
 j | |	  } t | t  r| | |	 } n  | j |  q W| d d d d d h k r|t |  } t |  d k r|t d | d d |   q|n  | d d h k rt |  d	 k rt | d   n  | d } | d } t |  } t |  } t | t  r'| d k  r| | | | g |  _ q'| g d	 |  _ n  t |  j t t f  sNt d   n  t |  j  d	 k rrt d   n  t |  j d t  st |  j d t  rt d   n  | |  j d | |  j d k rt d | |  j d | |  j d f d | | f   qn | d k rg  | D] } t |  ^ q$} t   } x? t	 t |   D]+ }	 | |	 |  j =| j t | |	   qXWt |  d k rt d |   qn  d S(   sV   Validates user-passed arguments and raises exceptions
        as appropriate.
        R
   t   mult   concatt   avet   cost   dott   maxs   Invalid merge mode: i   sT   A Merge should only be applied to a list of layers with at least 2 elements. Found: i   s5   Only layers of same output shape can be merged using s    mode. s   Layer shapes: %ss    merge takes exactly 2 layersi    s-   Invalid type for dot_axes - should be a list.s:   Invalid format for dot_axes - should contain two elements.s<   Invalid format for dot_axes - list elements should be "int".s4   Dimension incompatibility using dot mode: %s != %s. s   Layer shapes: %s, %ssl   "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: %sN(   t   callablet
   ValueErrorR&   t
   isinstancet   listt   tupleR+   t	   TypeErrorR   R*   R.   t   get_output_shape_atR/   t   sett   intR   R   t   add(   R2   R   R   R   R   R   R5   R   t   input_shapesR9   R:   t   layer_output_shapet   input_shapes_sett   shape1t   shape2t   n1t   n2t   shapet   reduced_inputs_shapest	   shape_set(    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR,   y   sd    !((

." 	c         C   s  t  | t  s" t |  d k r; t d t |    n  t |  j  r |  j } t j	 |  j  } d | j
 k r | | d <n  |  j | |  S|  j d k s |  j d k r| d } x+ t d t |   D] } | | | 7} q W|  j d k r	| t |  :} n  | S|  j d k r2t j | d |  j S|  j d	 k r}| d } x+ t d t |   D] } | | | 9} qaW| S|  j d
 k r| d } x3 t d t |   D] } t j | | |  } qW| S|  j d k r| d } | d } t j | | |  j  }	 |	 S|  j d k r| d } | d } t j t j | | |  j  t j | | |  j   }
 t j |
 t j    }
 t j | | |  j  |
 }	 t j |	 d  }	 |	 St d   d  S(   Ni   s=   Merge must be called on a list of tensors (at least 2). Got: R   R
   RA   i    R@   t   axisR?   RD   RC   RB   s   Unknown merge mode.(   RG   RH   R+   RJ   R&   RE   R   R   t   inspectt
   getargspect   argsR*   R'   t   concatenateR   t   maximumt	   batch_dotR   t   sqrtt   epsilont   expand_dimsRF   (   R2   t   inputsR   R   t   arg_spect   sR9   t   l1t   l2t   outputt   denominator(    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt   call   sV    "	






c         C   s  t  | t  s t  t |  j  r t |  j  rF |  j |  } | S|  j d  k	 rq | d d f t |  j  St d |  j	 d   n  | } |  j d k r | d S|  j d k r7t | d  } xc | d	 D]W } | |  j
 d  k s | |  j
 d  k rd  | |  j
 <Pn  | |  j
 c | |  j
 7<q Wt |  S|  j d k rt | d  } t | d	  } | j |  j d  | j |  j d	  | j d  | | } t |  d	 k r| d	 g 7} n  t |  Sd  S(   Ni    s   The Merge layer s    has a callable `mode` argument, and we cannot infer its output shape because no `output_shape` argument was provided. Make sure to pass a shape tuple (or callable) `output_shape` to Merge.R
   R?   RA   RD   R@   i   RC   RB   (   s   sums   muls   aves   max(   s   dots   cos(   RG   RH   t   AssertionErrorRE   R   R   R   RI   RF   R)   R   t   popR   R+   (   R2   t   input_shapeR3   RO   RV   RR   RS   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt   compute_output_shape   s<    &

c         C   s	  | d  k s1 t g  | D] } | d  k ^ q  r5 d  St | d  r\ t |  t |  k sb t  |  j d k r g  | D]$ } | d  k	 rx t j | d  ^ qx } t j t j | d d d d d t	 S|  j d	 k rg  } x t
 | |  D] \ } } | d  k r,| j t j t j |  d
   q t j |  t j |  k  rc| j t j |   q | j |  q Wt j | d |  j } t j | d d d t	 S|  j d k rd  St |  j  rt |  j  r|  j |  S|  j Sn t d j |  j    d  S(   Nt   __len__R
   R?   RA   RD   i    RY   t   keepdimsR@   t   booliRB   RC   s   Invalid merge mode: {}(   s   sums   muls   aves   max(   s   coss   dot(   R   t   allt   hasattrR+   Rk   R   R'   Rb   R]   R    t   zipR/   t   castt	   ones_liket   ndimR   RE   R   RF   t   format(   R2   Rc   R   t   mt   maskst   input_it   mask_it   concatenated(    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt   compute_mask  s.    1-1(%
c         C   sv  t  |  j t j  r- t |  j  } d } n3 t |  j  rQ |  j j } d } n |  j } d } t  |  j t j  r t |  j  } d } n3 t |  j  r |  j j } d } n |  j } d } t  |  j t j  r t |  j  } d } n3 t |  j  r|  j j } d } n |  j } d } i
 |  j	 d 6| d 6| d 6|  j
 d 6|  j d 6| d	 6| d
 6| d 6| d 6|  j d 6S(   Nt   lambdat   functiont   rawR)   R   t	   mode_typeR   R   R3   t   output_shape_typeR4   t   output_mask_typeR   (   RG   R   t   python_typest
   LambdaTypeR   RE   R$   R   R   R)   R   R   R   (   R2   R   R   R3   R   R4   R   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt
   get_config@  sD    									

c         C   ss  | j    } | j d  } | d k r; t   | d } n2 | d k rc t | d d t   } n
 | d } | j d d   } | d k r t   | d } n7 | d k r t | d d t   } n | j d  } | j d d   } | d k rt   | d	 } n7 | d k r0t | d	 d t   } n | j d	  } | | d <| | d <| | d	 <t t |   j |  S(
   NR   R   R   R   t   globsR   R3   R   R4   (	   t   copyRl   t   globalsR   R   t   gett   superR	   t   from_config(   t   clst   configR   R   R   R3   R   R4   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR   j  s2    



N(   R$   t
   __module__t   __doc__R   R>   R,   Rj   Rn   R~   R   t   classmethodR   (    (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR	      s   .			8	D3	+%	*R
   c         C   s:  t  j d d d t } x' |  D] }	 t |	 d  s  t } Pq  q  W| r g  }
 g  } g  } xG |  D]? }	 |	 j \ } } } |
 j |  | j |  | j |  qb Wt |
 d | d | d | d | d	 | d
 | d | d | d | 	} | j d j	 d St d | d | d | d | d	 | d
 | d |  } | |   Sd S(   s  Functional merge, to apply to Keras tensors (NOT layers).
    Returns a Keras tensor.
    # Example
    ```python
    tensor_a = Input(shape=(32,))
    tensor_b = Input(shape=(32,))
    merged_tensor = merge([tensor_a, tensor_b], mode='concat', concat_axis=1)
    ```
    # Arguments
        mode: String or lambda/function. If string, must be one
            of: 'sum', 'mul', 'concat', 'ave', 'cos', 'dot', 'max'.
            If lambda/function, it should take as input a list of tensors
            and return a single tensor.
        concat_axis: Integer, axis to use in mode `concat`.
        dot_axes: Integer or tuple of integers,
            axes to use in mode `dot` or `cos`.
        output_shape: Shape tuple (tuple of integers), or lambda/function
            to compute output_shape (only if merge mode is a lambda/function).
            If the latter case, it should take as input a list of shape tuples
            (1:1 mapping to input tensors) and return a single shape tuple,
            including the batch size
            (same convention as the `compute_output_shape` method of layers).
        node_indices: Optional list of integers containing
            the output node index for each input layer
            (in case some input layers have multiple output nodes).
            will default to an array of 0s if not provided.
        tensor_indices: Optional list of indices of output tensors
            to consider for merging
            (in case some input layer node returns multiple tensors).
    s   The `merge` function is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.R   i   t   _keras_historyR   R   R   R3   R4   R   R   R5   R)   i    N(
   R   R   R   Rs   R    R   R/   R	   R   R0   (   Rc   R   R   R   R3   R4   R   R)   t   all_keras_tensorst   xt   input_layersR   R5   t   input_layerR;   R<   t   merge_layer(    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt   merge  sD    !		t   MaxoutDensec           B   sY   e  Z d  Z d d d d d d d d e d d 
 Z d   Z d   Z d   Z d   Z	 RS(	   s  A dense maxout layer.
    A `MaxoutDense` layer takes the element-wise maximum of
    `nb_feature` `Dense(input_dim, output_dim)` linear layers.
    This allows the layer to learn a convex,
    piecewise linear activation function over the inputs.
    Note that this is a *linear* layer;
    if you wish to apply activation function
    (you shouldn't need to --they are universal function approximators),
    an `Activation` layer must be added after.
    # Arguments
        output_dim: int > 0.
        nb_feature: number of Dense layers to use internally.
        init: name of initialization function for the weights of the layer
            (see [initializations](../initializations.md)),
            or alternatively, Theano function to use for weights
            initialization. This parameter is only relevant
            if you don't pass a `weights` argument.
        weights: list of Numpy arrays to set as initial weights.
            The list should have 2 elements, of shape `(input_dim, output_dim)`
            and (output_dim,) for weights and biases respectively.
        W_regularizer: instance of [WeightRegularizer](../regularizers.md)
            (eg. L1 or L2 regularization), applied to the main weights matrix.
        b_regularizer: instance of [WeightRegularizer](../regularizers.md),
            applied to the bias.
        activity_regularizer: instance of [ActivityRegularizer](../regularizers.md),
            applied to the network output.
        W_constraint: instance of the [constraints](../constraints.md) module
            (eg. maxnorm, nonneg), applied to the main weights matrix.
        b_constraint: instance of the [constraints](../constraints.md) module,
            applied to the bias.
        bias: whether to include a bias
            (i.e. make the layer affine rather than linear).
        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 shape
        2D tensor with shape: `(nb_samples, input_dim)`.
    # Output shape
        2D tensor with shape: `(nb_samples, output_dim)`.
    # References
        - [Maxout Networks](http://arxiv.org/abs/1302.4389)
    i   t   glorot_uniformc         K   s   t  j d  | |  _ | |  _ t j |  |  _ t j |  |  _ t j |  |  _	 t j |  |  _
 t j |  |  _ t j |	  |  _ |
 |  _ | |  _ t d d  |  _ | |  _ |  j r |  j f | d <n  t t |   j |   d  S(   NsH   The `MaxoutDense` layer is deprecated and will be removed after 06/2017.Rw   i   Rm   (   R   R   t
   output_dimt
   nb_featureR   R   t   initR   t   W_regularizert   b_regularizert   activity_regularizerR   t   W_constraintt   b_constraintt   biast   initial_weightsR   R"   t	   input_dimR   R   R>   (   R2   R   R   R   t   weightsR   R   R   R   R   R   R   t   kwargs(    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR>     s     						c      
   C   s   | d } t  d t j   d d  | f  |  _ |  j |  j | |  j f d |  j d d d |  j	 d |  j
 |  _ |  j r |  j |  j |  j f d d	 d d
 d |  j d |  j |  _ n	 d  |  _ |  j d  k	 r |  j |  j  |  ` n  t |  _ d  S(   Ni   t   dtypeRV   t   initializerR)   t   Wt   regularizert
   constraintt   zerot   b(   R   R'   t   floatxR   R"   t
   add_weightR   R   R   R   R   R   R   R   R   R   R   t   set_weightsR   R-   (   R2   Rm   R   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt   build#  s&    
						c         C   s/   | r t  |  d k s t  | d |  j f S(   Ni   i    (   R+   Rk   R   (   R2   Rm   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyRn   ;  s    c         C   sG   t  j | |  j  } |  j r. | |  j 7} n  t  j | d d } | S(   NRY   i   (   R'   RC   R   R   R   RD   (   R2   R   Rh   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyRj   ?  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 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	   serializeR   R   R   R   R   R   R   R   R   R   R   R   R   R   t   dictRH   t   items(   R2   R   t   base_config(    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR   G  s    

N(
   R$   R   R   R   R   R>   R   Rn   Rj   R   (    (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR     s   *			t   Highwayc           B   sP   e  Z d  Z d d d d d d d d e d d 
 Z d   Z d   Z d   Z RS(   s\  Densely connected highway network.
    Highway layers are a natural extension of LSTMs to feedforward networks.
    # Arguments
        init: name of initialization function for the weights of the layer
            (see [initializations](../initializations.md)),
            or alternatively, Theano function to use for weights
            initialization. This parameter is only relevant
            if you don't pass a `weights` argument.
        activation: name of activation function to use
            (see [activations](../activations.md)),
            or alternatively, elementwise Theano function.
            If you don't specify anything, no activation is applied
            (ie. "linear" activation: a(x) = x).
        weights: list of Numpy arrays to set as initial weights.
            The list should have 2 elements, of shape `(input_dim, output_dim)`
            and (output_dim,) for weights and biases respectively.
        W_regularizer: instance of [WeightRegularizer](../regularizers.md)
            (eg. L1 or L2 regularization), applied to the main weights matrix.
        b_regularizer: instance of [WeightRegularizer](../regularizers.md),
            applied to the bias.
        activity_regularizer: instance of [ActivityRegularizer](../regularizers.md),
            applied to the network output.
        W_constraint: instance of the [constraints](../constraints.md) module
            (eg. maxnorm, nonneg), applied to the main weights matrix.
        b_constraint: instance of the [constraints](../constraints.md) module,
            applied to the bias.
        bias: whether to include a bias
            (i.e. make the layer affine rather than linear).
        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 shape
        2D tensor with shape: `(nb_samples, input_dim)`.
    # Output shape
        2D tensor with shape: `(nb_samples, input_dim)`.
    # References
        - [Highway Networks](http://arxiv.org/abs/1505.00387v2)
    R   c         K   s  t  j d  d | k r6 | j d  t  j d  n  t j |  |  _ t j |  |  _ t j |  |  _	 t j |  |  _
 t j |  |  _ t j |  |  _ t j |  |  _ |	 |  _ | |  _ t d d  |  _ |
 |  _ |  j r |  j f | d <n  t t |   j |   d  S(   NsD   The `Highway` layer is deprecated and will be removed after 06/2017.t   transform_biass=   `transform_bias` argument is deprecated and has been removed.Rw   i   Rm   (   R   R   Rl   R   R   R   R   t
   activationR   R   R   R   R   R   R   R   R   R   R"   R   R   R   R>   (   R2   R   R   R   R   R   R   R   R   R   R   R   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR>   ~  s$    				c      
   C   s,  | d } t  d t j   d d  | f  |  _ |  j | | f d |  j d d d |  j d |  j |  _	 |  j | | f d |  j d d	 |  _
 |  j r |  j | f d d
 d d d |  j d |  j |  _ |  j | f d d d d |  _ n	 d  |  _ |  j d  k	 r|  j |  j  |  ` n  t |  _ d  S(   Ni   R   RV   R   R)   R   R   R   t   W_carryR   R   t   onet   b_carry(   R   R'   R   R   R"   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R-   (   R2   Rm   R   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR     s2    
							c         C   s   t  j | |  j  } |  j r. | |  j 7} n  t j |  } t  j | |  j  } |  j rk | |  j 7} n  |  j	 |  } | | 9} | d | | } | S(   Ni   (
   R'   RC   R   R   R   R   t   sigmoidR   R   R   (   R2   R   t   yt   transform_weightt   actRh   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyRj     s    		
c         C   s   i	 t  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(
   NR   R   R   R   R   R   R   R   R   (   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   RH   R   (   R2   R   R   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR     s    
N(	   R$   R   R   R   R   R>   R   Rj   R   (    (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyR   V  s   &		c          O   sX   d d l  m } d | k r. | j d  } n d } | | d <t j d  | |  |   S(   Ni   (   t   Conv1Dt   atrous_ratei   t   dilation_ratesw   The `AtrousConvolution1D` layer  has been deprecated. Use instead the `Conv1D` layer with the `dilation_rate` argument.(   R   R   Rl   R   R   (   R\   R   R   t   rate(    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt   AtrousConvolution1D  s    
c          O   sX   d d l  m } d | k r. | j d  } n d } | | d <t j d  | |  |   S(   Ni   (   t   Conv2DR   i   R   sw   The `AtrousConvolution2D` layer  has been deprecated. Use instead the `Conv2D` layer with the `dilation_rate` argument.(   R   R   Rl   R   R   (   R\   R   R   R   (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt   AtrousConvolution2D  s    
(   RZ   t   typesR   R   t   engine.topologyR    R   t    R   R'   t   utils.generic_utilsR   R   R   R   R   R   R	   R   R   R   R   R   R   (    (    (    s2   /tmp/pip-build-isqEY4/keras/keras/legacy/layers.pyt   <module>   s$    	F	