ó
¾÷Xc           @  sp   d  d l  m Z d d l m Z d d l m Z d  d l Z d d d „ Z
 d d „ Z d	 „  Z d
 d „ Z d S(   iÿÿÿÿ(   t   print_functioni   (   t   convert_kerneli   (   t   backendNc           s›  |  j  j d k r t } n; t } x2 |  j j ƒ  D]! } t | ƒ d k r1 t } q1 q1 W| rÅ | pe d } ˆ  pz d d d g ‰  ˆ  d d k r³ g  ˆ  D] } t | | ƒ ^ q” ‰  n  d d	 d
 g } n– | pÎ d } ˆ  pæ d d d d g ‰  ˆ  d d k rg  ˆ  D] } t | | ƒ ^ q ‰  n  d d	 d
 d g } g  ‰ x! |  j j ƒ  D] } ˆ | 7‰ qGWd „  ‰ t d | ƒ ˆ | ˆ  ƒ t d | ƒ ‡  ‡ f d †  } ‡  ‡ ‡ f d †  } |  j	 }	 xt t
 t |	 ƒ ƒ D]` }
 | rí| |	 |
 ƒ n | |	 |
 ƒ |
 t |	 ƒ d k r"t d | ƒ qÐt d | ƒ qÐWt |	 d d ƒ\ } } t d j | | ƒ ƒ t d j | ƒ ƒ t d j | ƒ ƒ t d | ƒ d S(   s  Prints a summary of a model.

    # Arguments
        model: Keras model instance.
        line_length: total length of printed lines
        positions: relative or absolute positions of log elements in each line.
            If not provided, defaults to `[.33, .55, .67, 1.]`.
    t
   Sequentiali   iA   gÍÌÌÌÌÌÜ?g333333ë?g      ð?iÿÿÿÿs   Layer (type)s   Output Shapes   Param #id   g…ëQ¸Õ?gš™™™™™á?gq=
×£på?s   Connected toc         S  sŒ   d } xu t  t |  ƒ ƒ D]a } | d k r< | d  d } n  | t |  | ƒ 7} | | |  } | d | | t | ƒ 7} q Wt | ƒ d  S(   Nt    i    iÿÿÿÿt    (   t   ranget   lent   strt   print(   t   fieldst	   positionst   linet   i(    (    s6   /tmp/pip-build-isqEY4/keras/keras/utils/layer_utils.pyt	   print_row+   s     t   _t   =c           sn   y |  j  } Wn t k
 r& d } n X|  j } |  j j } | d | d | |  j ƒ  g } ˆ | ˆ  ƒ d  S(   Nt   multiples    (t   )(   t   output_shapet   AttributeErrort   namet	   __class__t   __name__t   count_params(   t   layerR   R   t   cls_nameR
   (   R   R   (    s6   /tmp/pip-build-isqEY4/keras/keras/utils/layer_utils.pyt   print_layer_summary9   s    
	!c           s±  y |  j  } Wn t k
 r& d } n Xg  } xÆ t |  j ƒ D]µ \ } } ˆ r{ |  j d t | ƒ } | ˆ k r{ q= q{ n  xt t t | j ƒ ƒ D]] } | j | j } | j	 | } | j
 | }	 | j | d t | ƒ d t |	 ƒ d ƒ q‘ Wq= W|  j }
 |  j j } | sd } n
 | d } |
 d | d	 | |  j ƒ  | g } ˆ | ˆ  ƒ t | ƒ d
 k r­xC t d
 t | ƒ ƒ D]) } d d d | | g } ˆ | ˆ  ƒ q}Wn  d S(   sc   Prints a summary for a single layer.

        # Arguments
            layer: target layer.
        R   s   _ib-t   [s   ][t   ]R   i    s    (R   i   N(   R   R   t	   enumeratet   inbound_nodesR   R   R   R   t   inbound_layerst   node_indicest   tensor_indicest   appendR   R   R   (   R   R   t   connectionst
   node_indext   nodet   node_keyR   t   inbound_layert   inbound_node_indext   inbound_tensor_indexR   R   t   first_connectionR
   (   R   R   t   relevant_nodes(    s6   /tmp/pip-build-isqEY4/keras/keras/utils/layer_utils.pyt$   print_layer_summary_with_connectionsC   s4    
	5		
$t	   layer_sets   Total params: {:,}s   Trainable params: {:,}s   Non-trainable params: {:,}N(   R   R   t   Truet   nodes_by_deptht   valuesR   t   Falset   intR	   t   layersR   t   count_total_paramst   Nonet   format(   t   modelt   line_lengthR   t   sequential_liket   vt   pt
   to_displayR   R-   R4   R   t   trainable_countt   non_trainable_count(    (   R   R   R,   s6   /tmp/pip-build-isqEY4/keras/keras/utils/layer_utils.pyt   print_summary   sN    		&&	

$	c         C  s   | d k r t ƒ  } n  d } d } xÏ |  D]Ç } | | k rC q+ n  | j | ƒ t | d ƒ rŽ t | j | ƒ \ } } | | 7} | | 7} q+ | t j g  | j D] } t	 j
 | ƒ ^ q¡ ƒ 7} | t j g  | j D] } t	 j
 | ƒ ^ qÓ ƒ 7} q+ W| | f S(   s/  Counts the number of parameters in a list of layers.

    # Arguments
        layers: list of layers.
        layer_set: set of layers already seen
            (so that we don't count their weights twice).

    # Returns
        A tuple (count of trainable weights, count of non-trainable weights.)
    i    R4   N(   R6   t   sett   addt   hasattrR5   R4   t   npt   sumt   trainable_weightst   KR   t   non_trainable_weights(   R4   R.   R>   R?   R   t   tt   ntR<   (    (    s6   /tmp/pip-build-isqEY4/keras/keras/utils/layer_utils.pyR5   z   s    
26c         C  s†   d d d d h } g  } xZ |  j  D]O } | j j | k r" t j | j ƒ } t | ƒ } | j | j | f ƒ q" q" Wt j | ƒ d S(   s·   Converts all convolution kernels in a model from Theano to TensorFlow.

    Also works from TensorFlow to Theano.

    # Arguments
        model: target model for the conversion.
    t   Conv1Dt   Conv2Dt   Conv3Dt   Conv2DTransposeN(	   R4   R   R   RG   t	   get_valuet   kernelR   R#   t   batch_set_value(   R8   t   conv_classest	   to_assignR   t   original_kernelt   converted_kernel(    (    s6   /tmp/pip-build-isqEY4/keras/keras/utils/layer_utils.pyt   convert_all_kernels_in_model—   s    	t   channels_firstc         C  s:  | d d h k s t  ‚ |  j ƒ  \ } } xö t | j d ƒ D]á } | d k r¢ | \ } } } | | | f }	 | d d … | f j |	 ƒ }
 t j |
 d ƒ }
 nO | \ } } } | | | f }	 | d d … | f j |	 ƒ }
 t j |
 d ƒ }
 t j |
 t j | ƒ f ƒ | d d … | f <q> W|  j | | g ƒ d S(	   sb  Utility useful when changing a convnet's `data_format`.

    When porting the weights of a convnet from one data format to the other,
    if the convnet includes a `Flatten` layer
    (applied to the last convolutional feature map)
    followed by a `Dense` layer, the weights of that `Dense` layer
    should be updated to reflect the new dimension ordering.

    # Arguments
        dense: The target `Dense` layer.
        previous_feature_map_shape: A shape tuple of 3 integers,
            e.g. `(512, 7, 7)`. The shape of the convolutional
            feature map right before the `Flatten` layer that
            came before the target `Dense` layer.
        target_data_format: One of "channels_last", "channels_first".
            Set it "channels_last"
            if converting a "chnnels_first" model to "channels_last",
            or reciprocally.
    t   channels_lastRW   i   Ni   i    (   i   i    i   (   i   i   i    (	   t   AssertionErrort   get_weightsR   t   shapet   reshapeRD   t	   transposet   prodt   set_weights(   t   denset   previous_feature_map_shapet   target_data_formatRP   t   biasR   t   ct   ht   wt   original_fm_shapet   ki(    (    s6   /tmp/pip-build-isqEY4/keras/keras/utils/layer_utils.pyt!   convert_dense_weights_data_format°   s    2(   t
   __future__R    t
   conv_utilsR   R   R   RG   t   numpyRD   R6   R@   R5   RV   Ri   (    (    (    s6   /tmp/pip-build-isqEY4/keras/keras/utils/layer_utils.pyt   <module>   s   r	