ó
¾÷Xc           @@  sÐ   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 e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d S(   i    (   t   absolute_importi   (   t   backend(   t   activations(   t   initializers(   t   regularizers(   t   constraints(   t   Layer(   t	   InputSpec(   t
   conv_utils(   t
   interfacest   LocallyConnected1Dc           B@  sh   e  Z d  Z e j d d d
 d
 e d d d
 d
 d
 d
 d
 d „ ƒ Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(   s³
  Locally-connected layer for 1D inputs.

    The `LocallyConnected1D` layer works similarly to
    the `Conv1D` layer, except that weights are unshared,
    that is, a different set of filters is applied at each different patch
    of the input.

    # Example
    ```python
        # apply a unshared weight convolution 1d of length 3 to a sequence with
        # 10 timesteps, with 64 output filters
        model = Sequential()
        model.add(LocallyConnected1D(64, 3, input_shape=(10, 32)))
        # now model.output_shape == (None, 8, 64)
        # add a new conv1d on top
        model.add(LocallyConnected1D(32, 3))
        # now model.output_shape == (None, 6, 32)
    ```

    # Arguments
        filters: Integer, the dimensionality of the output space
            (i.e. the number output of filters in the convolution).
        kernel_size: An integer or tuple/list of a single integer,
            specifying the length of the 1D convolution window.
        strides: An integer or tuple/list of a single integer,
            specifying the stride length of the convolution.
            Specifying any stride value != 1 is incompatible with specifying
            any `dilation_rate` value != 1.
        padding: One of `"valid"` or `"same"` (case-insensitive).
        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
            (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)).
        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 matrix
            (see [constraints](../constraints.md)).
        bias_constraint: Constraint function applied to the bias vector
            (see [constraints](../constraints.md)).

    # Input shape
        3D tensor with shape: `(batch_size, steps, input_dim)`

    # Output shape
        3D tensor with shape: `(batch_size, new_steps, filters)`
        `steps` value might have changed due to padding or strides.
    i   t   validt   glorot_uniformt   zerosc         K@  sD  t  t |  ƒ j |   | |  _ t j | d d ƒ |  _ t j | d d ƒ |  _ t j | ƒ |  _	 |  j	 d k rƒ t
 d | ƒ ‚ n  t j | ƒ |  _ t j | ƒ |  _ | |  _ t j | ƒ |  _ t j |	 ƒ |  _ t j |
 ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t d d ƒ |  _ d  S(   Ni   t   kernel_sizet   stridesR   sH   Invalid border mode for LocallyConnected1D (only "valid" is supported): t   ndimi   (   t   superR
   t   __init__t   filtersR   t   normalize_tupleR   R   t   normalize_paddingt   paddingt
   ValueErrort   normalize_data_formatt   data_formatR   t   gett
   activationt   use_biasR   t   kernel_initializert   bias_initializerR   t   kernel_regularizert   bias_regularizert   activity_regularizerR   t   kernel_constraintt   bias_constraintR   t
   input_spec(   t   selfR   R   R   R   R   R   R   R   R   R   R    R!   R"   R#   t   kwargs(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyR   K   s&    		c      
   C@  s(  | d } | d  k r( t d | ƒ ‚ n  t j | d |  j d |  j |  j d ƒ } | |  j d | |  j f |  _ |  j	 |  j d |  j
 d d d |  j d	 |  j ƒ|  _ |  j ró |  j	 | |  j f d |  j d d
 d |  j d	 |  j ƒ|  _ n	 d  |  _ t d d d i | d 6ƒ |  _ t |  _ d  S(   Ni   s5   Axis 2 of input should be fully-defined. Found shape:i   i    t   initializert   namet   kernelt   regularizert
   constraintt   biasR   i   t   axes(   t   NoneR   R   t   conv_output_lengthR   R   R   R   t   kernel_shapet
   add_weightR   R   R"   R)   R   R   R    R#   R,   R   R$   t   Truet   built(   R%   t   input_shapet	   input_dimt   output_length(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyt   buildo   s6    

							c         C@  sA   t  j | d |  j d |  j |  j d ƒ } | d | |  j f S(   Ni   i    (   R   R/   R   R   R   R   (   R%   R4   t   length(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyt   compute_output_shape   s
    
c         C@  s(  |  j  d } |  j \ } } } g  } xq t | ƒ D]c } t | | | | |  j d ƒ } | j t j | d  d  … | d  d  … f d d | f ƒ ƒ q2 Wt j | d d ƒ}	 t j	 |	 |  j
 ƒ }
 t j |
 d ƒ }
 |  j r|
 t j |  j d | | f ƒ 7}
 n  |  j d  k	 r$|  j |
 ƒ }
 n  |
 S(   Ni    i   iÿÿÿÿt   axisi   (   i   i    i   (   R   R0   t   ranget   sliceR   t   appendt   Kt   reshapet   concatenatet	   batch_dotR)   t   permute_dimensionsR   R,   R   R.   (   R%   t   inputst   strideR6   t   feature_dimR   t   xst   it   slice_lengtht   x_aggregatet   output(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyt   call”   s     
(	%c         C@  s  i |  j  d 6|  j d 6|  j d 6|  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} 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   t	   serializeR   R   R   R   R   R   R   R    R!   R   R"   R#   R   R
   t
   get_configt   dictt   listt   items(   R%   t   configt   base_config(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyRM   ©   s     




N(   t   __name__t
   __module__t   __doc__R	   t   legacy_conv1d_supportR.   R2   R   R7   R9   RK   RM   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyR
      s$   :			t   LocallyConnected2Dc           B@  sh   e  Z d  Z e j d
 d d d e d d d d d d d d „ ƒ Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(   s­  Locally-connected layer for 2D inputs.

    The `LocallyConnected2D` layer works similarly
    to the `Conv2D` layer, except that weights are unshared,
    that is, a different set of filters is applied at each
    different patch of the input.

    # Examples
    ```python
        # apply a 3x3 unshared weights convolution with 64 output filters on a 32x32 image
        # with `data_format="channels_last"`:
        model = Sequential()
        model.add(LocallyConnected2D(64, (3, 3), input_shape=(32, 32, 3)))
        # now model.output_shape == (None, 30, 30, 64)
        # notice that this layer will consume (30*30)*(3*3*3*64) + (30*30)*64 parameters

        # add a 3x3 unshared weights convolution on top, with 32 output filters:
        model.add(LocallyConnected2D(32, (3, 3)))
        # now model.output_shape == (None, 28, 28, 32)
    ```

    # Arguments
        filters: Integer, the dimensionality of the output space
            (i.e. the number output of filters in the convolution).
        kernel_size: An integer or tuple/list of 2 integers, specifying the
            width and height of the 2D convolution window.
            Can be a single integer to specify the same value for
            all spatial dimensions.
        strides: An integer or tuple/list of 2 integers,
            specifying the strides of the convolution along the width and height.
            Can be a single integer to specify the same value for
            all spatial dimensions.
            Specifying any stride value != 1 is incompatible with specifying
            any `dilation_rate` value != 1.
        padding: one of `"valid"` or `"same"` (case-insensitive).
        data_format: A string,
            one of `channels_last` (default) or `channels_first`.
            The ordering of the dimensions in the inputs.
            `channels_last` corresponds to inputs with shape
            `(batch, height, width, channels)` while `channels_first`
            corresponds to inputs with shape
            `(batch, channels, height, width)`.
            It defaults to the `image_data_format` value found in your
            Keras config file at `~/.keras/keras.json`.
            If you never set it, then it will be "channels_last".
        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
            (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)).
        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 matrix
            (see [constraints](../constraints.md)).
        bias_constraint: Constraint function applied to the bias vector
            (see [constraints](../constraints.md)).

    # Input shape
        4D tensor with shape:
        `(samples, channels, rows, cols)` if data_format='channels_first'
        or 4D tensor with shape:
        `(samples, rows, cols, channels)` if data_format='channels_last'.

    # Output shape
        4D tensor with shape:
        `(samples, filters, new_rows, new_cols)` if data_format='channels_first'
        or 4D tensor with shape:
        `(samples, new_rows, new_cols, filters)` if data_format='channels_last'.
        `rows` and `cols` values might have changed due to padding.
    i   R   R   R   c         K@  sD  t  t |  ƒ j |   | |  _ t j | d d ƒ |  _ t j | d d ƒ |  _ t j | ƒ |  _	 |  j	 d k rƒ t
 d | ƒ ‚ n  t j | ƒ |  _ t j | ƒ |  _ | |  _ t j | ƒ |  _ t j |	 ƒ |  _ t j |
 ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t j | ƒ |  _ t d d ƒ |  _ d  S(   Ni   R   R   R   sH   Invalid border mode for LocallyConnected2D (only "valid" is supported): R   i   (   R   RW   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   R$   (   R%   R   R   R   R   R   R   R   R   R   R   R    R!   R"   R#   R&   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyR     s&    		c         C@  sô  |  j  d k r/ | d d !\ } } | d } n | d \ } } | d } | d  k sa | d  k rz t d t | ƒ ƒ ‚ n  t j | |  j d |  j |  j d ƒ } t j | |  j d |  j |  j d ƒ } | |  _	 | |  _
 | | |  j d |  j d | |  j f |  _ |  j |  j d |  j d	 d
 d |  j d |  j ƒ|  _ |  j rŽ|  j | | |  j f d |  j d	 d d |  j d |  j ƒ|  _ n	 d  |  _ |  j  d k rÈt d d d i | d 6ƒ |  _ n t d d d i | d 6ƒ |  _ t |  _ d  S(   Nt   channels_lasti   iÿÿÿÿi   i   s   The spatial dimensions of the inputs to  a LocallyConnected2D layer should be fully-defined, but layer received the inputs shape i    R'   R(   R)   R*   R+   R,   t   channels_firstR   i   R-   (   R   R.   R   t   strR   R/   R   R   R   t
   output_rowt
   output_colR   R0   R1   R   R   R"   R)   R   R   R    R#   R,   R   R$   R2   R3   (   R%   R4   t	   input_rowt	   input_colt   input_filterR[   R\   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyR7   3  sB    
								"c         C@  sî   |  j  d k r& | d } | d } n& |  j  d k rL | d } | d } n  t j | |  j d |  j |  j d ƒ } t j | |  j d |  j |  j d ƒ } |  j  d k rÄ | d |  j | | f S|  j  d k rê | d | | |  j f Sd  S(   NRY   i   i   RX   i   i    (   R   R   R/   R   R   R   R   (   R%   R4   t   rowst   cols(    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyR9   [  s    

c      
   C@  s5  |  j  \ } } |  j \ } } } |  j d k rnt j ƒ  d k rOg  } xì t |  j ƒ D]Û } xÒ t |  j ƒ D]Á }	 t | | | | |  j	 d ƒ }
 t |	 | |	 | |  j	 d ƒ } t j
 | d  d  … d  d  … |
 | f d d | f ƒ } | j t j | |  j | |  j |	 d  d  … d  d  … f ƒ ƒ qn WqX Wt j | d d ƒ} næ g  } x³ t |  j ƒ D]¢ } x™ t |  j ƒ D]ˆ }	 t | | | | |  j	 d ƒ }
 t |	 | |	 | |  j	 d ƒ } | j t j
 | d  d  … d  d  … |
 | f d d | f ƒ ƒ q{WqeWt j | d d ƒ} t j | |  j ƒ } t j
 | |  j |  j d | f ƒ } t j | d
 ƒ } n.|  j d	 k rœg  } x³ t |  j ƒ D]¢ } x™ t |  j ƒ D]ˆ }	 t | | | | |  j	 d ƒ }
 t |	 | |	 | |  j	 d ƒ } | j t j
 | d  d  … |
 | d  d  … f d d | f ƒ ƒ q©Wq“Wt j | d d ƒ} t j | |  j ƒ } t j
 | |  j |  j d | f ƒ } t j | d ƒ } n  |  j r"|  j d k râ| t j
 |  j d | |  j |  j f ƒ 7} q"|  j d	 k r"| t j
 |  j d |  j |  j | f ƒ 7} q"n  |  j | ƒ } | S(   NRY   t   theanoi    i   iÿÿÿÿR:   i   i   RX   (   i   i   i    i   (   i   i    i   i   (   R   R0   R   R>   R   R;   R[   R\   R<   R   R?   R=   t   dotR)   R@   RA   RB   R   R,   R   (   R%   RC   t
   stride_rowt
   stride_colt   _RE   R   RJ   RG   t   jt	   slice_rowt	   slice_colt	   x_flattenRF   RI   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyRK   m  sl    

%9

+	

+		"c         C@  s  i |  j  d 6|  j d 6|  j d 6|  j d 6|  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} 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   RL   R   R   R   R   R   R   R   R    R!   R   R"   R#   R   RW   RM   RN   RO   RP   (   R%   RQ   RR   (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyRM   ©  s"    





(   i   i   N(   RS   RT   RU   R	   t   legacy_conv2d_supportR.   R2   R   R7   R9   RK   RM   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyRW   ½   s$   P	(		<N(   t
   __future__R    t    R   R>   R   R   R   R   t   engineR   R   t   utilsR   t   legacyR	   R
   RW   (    (    (    s1   /tmp/pip-build-isqEY4/keras/keras/layers/local.pyt   <module>   s   ®