ó
¾÷Xc           @   sú   d  d l  m Z d  d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d d „ Z e d „ Z d S(   i   (   t   Layer(   t   backendt   _Mergec           B   sG   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z	 RS(   s¬   Generic merge layer for elementwise merge functions.

    Used to implement `Sum`, `Average`, etc.

    # Arguments
        **kwargs: standard layer keyword arguments.
    c         K   s#   t  t |  ƒ j |   t |  _ d  S(   N(   t   superR   t   __init__t   Truet   supports_masking(   t   selft   kwargs(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR      s    c         C   s
   t  ‚ d  S(   N(   t   NotImplementedError(   R   t   inputs(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyt   _merge_function   s    c         C   sØ   t  | t ƒ s t d ƒ ‚ n  t | ƒ d k  rS t d t t | ƒ ƒ d ƒ ‚ n  t g  | D] } | d  k ^ q] ƒ r| d  St | ƒ } d  | k r¤ | j d  ƒ n  t | ƒ d k rÔ t d |  j	 d | ƒ ‚ n  d  S(   Ns3   A merge layer should be called on a list of inputs.i   sC   A merge layer should be called on a list of at least 2 inputs. Got s    inputs.i   s1   Only tensors of same shape can be merged by layers    Got input shapes: %s(
   t
   isinstancet   listt
   ValueErrort   lent   strt   allt   Nonet   sett   removet   name(   R   t   input_shapet   shapet   input_shapes_set(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyt   build   s     %
c         C   s   |  j  | ƒ S(   N(   R   (   R   R
   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyt   call)   s    c         C   s   | d S(   Ni    (    (   R   R   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyt   compute_output_shape,   s    c         C   sõ   | d  k r d  St | t ƒ s. t d ƒ ‚ n  t | t ƒ sL t d ƒ ‚ n  t | ƒ t | ƒ k rs t d ƒ ‚ n  t g  | D] } | d  k ^ q} ƒ rœ d  Sg  | D]$ } | d  k	 r£ t j | d ƒ ^ q£ } t j t j | d d ƒd d d t	 ƒS(   Ns   `mask` should be a list.s   `inputs` should be a list.s:   The lists `inputs` and `mask` should have the same length.i    t   axist   keepdims(
   R   R   R   R   R   R   t   Kt   expand_dimst   concatenatet   False(   R   R
   t   maskt   mt   masks(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyt   compute_mask3   s    %1N(
   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R%   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR      s   					t   Addc           B   s   e  Z d  Z d „  Z RS(   s¥   Layer that adds a list of inputs.

    It takes as input a list of tensors,
    all of the same shape, and returns
    a single tensor (also of the same shape).
    c         C   s<   | d } x+ t  d t | ƒ ƒ D] } | | | 7} q  W| S(   Ni    i   (   t   rangeR   (   R   R
   t   outputt   i(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   K   s    
(   R&   R'   R(   R   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR)   C   s   t   Multiplyc           B   s   e  Z d  Z d „  Z RS(   sº   Layer that multiplies (element-wise) a list of inputs.

    It takes as input a list of tensors,
    all of the same shape, and returns
    a single tensor (also of the same shape).
    c         C   s<   | d } x+ t  d t | ƒ ƒ D] } | | | 9} q  W| S(   Ni    i   (   R*   R   (   R   R
   R+   R,   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   Z   s    
(   R&   R'   R(   R   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR-   R   s   t   Averagec           B   s   e  Z d  Z d „  Z RS(   s©   Layer that averages a list of inputs.

    It takes as input a list of tensors,
    all of the same shape, and returns
    a single tensor (also of the same shape).
    c         C   sF   | d } x+ t  d t | ƒ ƒ D] } | | | 7} q  W| t | ƒ S(   Ni    i   (   R*   R   (   R   R
   R+   R,   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   i   s    
(   R&   R'   R(   R   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR.   a   s   t   Maximumc           B   s   e  Z d  Z d „  Z RS(   sÄ   Layer that computes the maximum (element-wise) a list of inputs.

    It takes as input a list of tensors,
    all of the same shape, and returns
    a single tensor (also of the same shape).
    c         C   sD   | d } x3 t  d t | ƒ ƒ D] } t j | | | ƒ } q  W| S(   Ni    i   (   R*   R   R   t   maximum(   R   R
   R+   R,   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   x   s    
(   R&   R'   R(   R   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR/   p   s   t   Concatenatec           B   sJ   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d d „ Z d „  Z	 RS(	   sK  Layer that concatenates a list of inputs.

    It takes as input a list of tensors,
    all of the same shape expect for the concatenation axis,
    and returns a single tensor, the concatenation of all inputs.

    # Arguments
        axis: Axis along which to concatenate.
        **kwargs: standard layer keyword arguments.
    iÿÿÿÿc         K   s,   t  t |  ƒ j |   | |  _ t |  _ d  S(   N(   R   R1   R   R   R   R   (   R   R   R   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   ‹   s    	c         C   sÚ   t  | t ƒ s t d ƒ ‚ n  t g  | D] } | d  k ^ q( ƒ rG d  Sg  | D] } t | ƒ ^ qN } t ƒ  } x? t t | ƒ ƒ D]+ } | | |  j =| j	 t
 | | ƒ ƒ q‚ Wt | ƒ d k rÖ t d | ƒ ‚ n  d  S(   Ns8   `Concatenate` layer should be called on a list of inputsi   sj   `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: %s(   R   R   R   R   R   R   R*   R   R   t   addt   tuple(   R   R   R   t   reduced_inputs_shapest	   shape_setR,   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR      s    %	c         C   s4   t  | t ƒ s t d ƒ ‚ n  t j | d |  j ƒS(   Ns;   A `Concatenate` layer should be called on a list of inputs.R   (   R   R   R   R   R    R   (   R   R
   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   ¢   s    c         C   s¤   t  | t ƒ s t d ƒ ‚ n  | } t | d ƒ } xc | d D]W } | |  j d  k sk | |  j d  k r| d  | |  j <Pn  | |  j c | |  j 7<q? Wt | ƒ S(   Ns;   A `Concatenate` layer should be called on a list of inputs.i    i   (   R   R   R   R   R   R3   (   R   R   t   input_shapest   output_shapeR   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   ¨   s    &c         C   sh  | d  k r d  St | t ƒ s. t d ƒ ‚ n  t | t ƒ sL t d ƒ ‚ n  t | ƒ t | ƒ k rs t d ƒ ‚ n  t g  | D] } | d  k ^ q} ƒ rœ d  Sg  } x’ t | | ƒ D] \ } } | d  k rï | j t j	 t j
 | ƒ d ƒ ƒ q² t j | ƒ t j | ƒ k  r&| j t j | ƒ ƒ q² | j | ƒ q² Wt j | d |  j ƒ} t j | d d d t ƒS(   Ns   `mask` should be a list.s   `inputs` should be a list.s:   The lists `inputs` and `mask` should have the same length.t   boolR   iÿÿÿÿR   (   R   R   R   R   R   R   t   zipt   appendR   t   castt	   ones_liket   ndimR   R    R   R!   (   R   R
   R"   R#   R$   t   input_it   mask_it   concatenated(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR%   µ   s&    %%c         C   sK   i |  j  d 6} t t |  ƒ j ƒ  } t t | j ƒ  ƒ t | j ƒ  ƒ ƒ S(   NR   (   R   R   R1   t
   get_configt   dictR   t   items(   R   t   configt   base_config(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyRA   Ò   s    N(
   R&   R'   R(   R   R   R   R   R   R%   RA   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR1      s   
			t   Dotc           B   sJ   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d d „ Z	 d „  Z
 RS(   sÁ  Layer that computes a dot product between samples in two tensors.

    E.g. if applied to two tensors `a` and `b` of shape `(batch_size, n)`,
    the output will be a tensor of shape `(batch_size, 1)`
    where each entry `i` will be the dot product between
    `a[i]` and `b[i]`.

    # Arguments
        axes: Integer or tuple of integers,
            axis or axes along which to take the dot product.
        normalize: Whether to L2-normalize samples along the
            dot product axis before taking the dot product.
            If set to True, then the output of the dot product
            is the cosine proximity between the two samples.
        **kwargs: Standard layer keyword arguments.
    c         K   sÃ   t  t |  ƒ j |   t | t ƒ s¤ t | t t f ƒ sI t d ƒ ‚ n  t | ƒ d k rj t	 d ƒ ‚ n  t | d t ƒ s’ t | d t ƒ r¤ t	 d ƒ ‚ q¤ n  | |  _
 | |  _ t |  _ d  S(   Ns5   Invalid type for `axes` - should be a list or an int.i   s8   Invalid format for `axes` - should contain two elements.i    i   s:   Invalid format for `axes` - list elements should be "int".(   R   RF   R   R   t   intR   R3   t	   TypeErrorR   R   t   axest	   normalizeR   R   (   R   RI   RJ   R   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   ì   s    (		c         C   s  t  | t ƒ s" t | ƒ d k r1 t d ƒ ‚ n  | d } | d } | d  k s] | d  k ra d  St  |  j t ƒ r¾ |  j d k  r« |  j t | ƒ |  j t | ƒ g } qÇ |  j g d } n	 |  j } | | d | | d k rt d | | d | | d f d | | f ƒ ‚ n  d  S(   Ni   s5   A `Dot` layer should be called on a list of 2 inputs.i    i   s$   Dimension incompatibility %s != %s. s   Layer shapes: %s, %s(   R   R   R   R   R   RI   RG   (   R   R   t   shape1t   shape2RI   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   ü   s     "

)	c         C   sC  | d } | d } t  |  j t ƒ rw |  j d k  rd |  j t j | ƒ |  j t j | ƒ g } qì |  j g d } nu g  } xl t t |  j ƒ ƒ D]U } |  j | d k  rÔ | j |  j | t j | | ƒ ƒ q“ | j |  j | ƒ q“ W|  j r*t j	 | d | d ƒ} t j	 | d | d ƒ} n  t j
 | | | ƒ } | S(   Ni    i   i   R   (   R   RI   RG   R   R=   R*   R   R:   RJ   t   l2_normalizet	   batch_dot(   R   R
   t   x1t   x2RI   R,   R+   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR     s     

/(	c         C   s  t  | t ƒ s" t | ƒ d k r1 t d ƒ ‚ n  t | d ƒ } t | d ƒ } t  |  j t ƒ r® |  j d k  r› |  j t | ƒ |  j t | ƒ g } q· |  j g d } n	 |  j } | j | d ƒ | j | d ƒ | j d ƒ | | } t | ƒ d k r| d g 7} n  t | ƒ S(   Ni   s5   A `Dot` layer should be called on a list of 2 inputs.i    i   (   R   R   R   R   RI   RG   t   popR3   (   R   R   RK   RL   RI   R7   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR   '  s     ")	
c         C   s   d  S(   N(   R   (   R   R
   R"   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR%   <  s    c         C   sU   i |  j  d 6|  j d 6} t t |  ƒ j ƒ  } t t | j ƒ  ƒ t | j ƒ  ƒ ƒ S(   NRI   RJ   (   RI   RJ   R   RF   RA   RB   R   RC   (   R   RD   RE   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyRA   ?  s
    
N(   R&   R'   R(   R!   R   R   R   R   R   R%   RA   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyRF   Ú   s   			c         K   s   t  |   |  ƒ S(   sà   Functional interface to the `Add` layer.

    # Arguments
        inputs: A list of input tensors (at least 2).
        **kwargs: Standard layer keyword arguments.

    # Returns
        A tensor, the sum of the inputs.
    (   R)   (   R
   R   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR2   H  s    
c         K   s   t  |   |  ƒ S(   sö   Functional interface to the `Multiply` layer.

    # Arguments
        inputs: A list of input tensors (at least 2).
        **kwargs: Standard layer keyword arguments.

    # Returns
        A tensor, the element-wise product of the inputs.
    (   R-   (   R
   R   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyt   multiplyU  s    
c         K   s   t  |   |  ƒ S(   sè   Functional interface to the `Average` layer.

    # Arguments
        inputs: A list of input tensors (at least 2).
        **kwargs: Standard layer keyword arguments.

    # Returns
        A tensor, the average of the inputs.
    (   R.   (   R
   R   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyt   averageb  s    
c         K   s   t  |   |  ƒ S(   sõ   Functional interface to the `Maximum` layer.

    # Arguments
        inputs: A list of input tensors (at least 2).
        **kwargs: Standard layer keyword arguments.

    # Returns
        A tensor, the element-wise maximum of the inputs.
    (   R/   (   R
   R   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR0   o  s    
iÿÿÿÿc         K   s   t  d | |  |  ƒ S(   s*  Functional interface to the `Concatenate` layer.

    # Arguments
        inputs: A list of input tensors (at least 2).
        axis: Concatenation axis.
        **kwargs: Standard layer keyword arguments.

    # Returns
        A tensor, the concatenation of the inputs alongside axis `axis`.
    R   (   R1   (   R
   R   R   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyR    |  s    c         K   s   t  d | d | |  |  ƒ S(   sX  Functional interface to the `Dot` layer.

    # Arguments
        inputs: A list of input tensors (at least 2).
        axes: Integer or tuple of integers,
            axis or axes along which to take the dot product.
        normalize: Whether to L2-normalize samples along the
            dot product axis before taking the dot product.
            If set to True, then the output of the dot product
            is the cosine proximity between the two samples.
        **kwargs: Standard layer keyword arguments.

    # Returns
        A tensor, the dot product of the samples from the inputs.
    RI   RJ   (   RF   (   R
   RI   RJ   R   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyt   dotŠ  s    N(   t   engine.topologyR    t    R   R   R   R)   R-   R.   R/   R1   RF   R2   RR   RS   R0   R    R!   RT   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/merge.pyt   <module>   s   >[n				