
Xc           @` s,  d  d l  m Z d  d l  m Z d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 d  d l 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 y d  d l Z Wn e k
 r)e Z n Xd e f d     YZ  d e f d     YZ! d e f d     YZ" d e" f d     YZ# e e e e j$   e% e d  Z& d e" f d     YZ' e e d  Z( d   Z) d   Z* d   Z+ d   Z, d   Z- d   Z. d   Z/ e e d   Z0 d!   Z1 d"   Z2 d S(#   i    (   t   print_function(   t   absolute_import(   t   divisionN(   t   zipi   (   t   backend(   t   initializers(   t   ask_to_proceed_with_overwrite(   t   print_summary(   t
   conv_utils(   t
   interfacest	   InputSpecc           B` s)   e  Z d  Z d d d d d d d  Z RS(   s  Specifies the ndim, dtype and shape of every input to a layer.

    Every layer should expose (if appropriate) an `input_spec` attribute:
    a list of instances of InputSpec (one per input tensor).

    A None entry in a shape is compatible with any dimension,
    a None shape is compatible with any shape.

    # Arguments
        dtype: Expected datatype of the input.
        shape: Shape tuple, expected shape of the input
            (may include None for unchecked axes).
        ndim: Integer, expected rank of the input.
        max_ndim: Integer, maximum rank of the input.
        min_ndim: Integer, minimum rank of the input.
        axes: Dictionary mapping integer axes to
            a specific dimension value.
    c         C` s^   | |  _  | |  _ | d  k	 r0 t |  |  _ n	 | |  _ | |  _ | |  _ | pT i  |  _ d  S(   N(   t   dtypet   shapet   Nonet   lent   ndimt   max_ndimt   min_ndimt   axes(   t   selfR   R   R   R   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   __init__1   s    					N(   t   __name__t
   __module__t   __doc__R   R   (    (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR
      s   t   Nodec           B` s#   e  Z d  Z d d  Z d   Z RS(   sr  A `Node` describes the connectivity between two layers.

    Each time a layer is connected to some new input,
    a node is added to `layer.inbound_nodes`.
    Each time the output of a layer is used by another layer,
    a node is added to `layer.outbound_nodes`.

    # Arguments
        outbound_layer: the layer that takes
            `input_tensors` and turns them into `output_tensors`
            (the node gets created when the `call`
            method of the layer was called).
        inbound_layers: a list of layers, the same length as `input_tensors`,
            the layers from where `input_tensors` originate.
        node_indices: a list of integers, the same length as `inbound_layers`.
            `node_indices[i]` is the origin node of `input_tensors[i]`
            (necessary since each inbound layer might have several nodes,
            e.g. if the layer is being shared with a different data stream).
        tensor_indices: a list of integers,
            the same length as `inbound_layers`.
            `tensor_indices[i]` is the index of `input_tensors[i]` within the
            output of the inbound layer
            (necessary since each inbound layer might
            have multiple tensor outputs, with each one being
            independently manipulable).
        input_tensors: list of input tensors.
        output_tensors: list of output tensors.
        input_masks: list of input masks (a mask can be a tensor, or None).
        output_masks: list of output masks (a mask can be a tensor, or None).
        input_shapes: list of input shape tuples.
        output_shapes: list of output shape tuples.
        arguments: dictionary of keyword arguments that were passed to the
            `call` method of the layer at the call that created the node.

    `node_indices` and `tensor_indices` are basically fine-grained coordinates
    describing the origin of the `input_tensors`, verifying the following:

    `input_tensors[i] == inbound_layers[i].inbound_nodes[node_indices[i]].output_tensors[tensor_indices[i]]`

    A node from layer A to layer B is added to:
        A.outbound_nodes
        B.inbound_nodes
    c         C` s   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ |
 |  _	 | |  _
 x- | D]% } | d  k	 rj | j j |   qj qj W| j j |   d  S(   N(   t   outbound_layert   inbound_layerst   node_indicest   tensor_indicest   input_tensorst   output_tensorst   input_maskst   output_maskst   input_shapest   output_shapest	   argumentsR   t   outbound_nodest   appendt   inbound_nodes(   R   R   R   R   R   R   R   R   R    R!   R"   R#   t   layer(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR   o   s    											c         C` s{   g  } x7 |  j  D], } | r/ | j | j  q | j d   q Wi |  j rX |  j j n d  d 6| d 6|  j d 6|  j d 6S(   NR   R   R   R   (   R   R%   t   nameR   R   R   R   (   R   t   inbound_namesR'   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt
   get_config   s    
N(   R   R   R   R   R   R*   (    (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR   B   s   +2t   Layerc           B` s  e  Z d  Z d   Z e d    Z e j d    Z e d    Z e j d    Z e d    Z e j d    Z d' e
 d' d' d  Z d	   Z d
   Z d   Z d' d  Z d   Z d' d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z d' d  Z  d' d  Z! d   Z" d    Z# e d!    Z$ d"   Z% d#   Z& d$   Z' e( d%    Z) d&   Z* RS((   s
  Abstract base layer class.

    # Properties
        name: String, must be unique within a model.
        input_spec: List of InputSpec class instances
            each entry describes one required input:
                - ndim
                - dtype
            A layer with `n` input tensors must have
            an `input_spec` of length `n`.
        trainable: Boolean, whether the layer weights
            will be updated during training.
        uses_learning_phase: Whether any operation
            of the layer uses `K.in_training_phase()`
            or `K.in_test_phase()`.
        input_shape: Shape tuple. Provided for convenience,
            but note that there may be cases in which this
            attribute is ill-defined (e.g. a shared layer
            with multiple input shapes), in which case
            requesting `input_shape` will raise an Exception.
            Prefer using `layer.get_input_shape_for(input_shape)`,
            or `layer.get_input_shape_at(node_index)`.
        output_shape: Shape tuple. See above.
        inbound_nodes: List of nodes.
        outbound_nodes: List of nodes.
        input, output: Input/output tensor(s). Note that if the layer is used
            more than once (shared layer), this is ill-defined
            and will raise an exception. In such cases, use
            `layer.get_input_at(node_index)`.
        input_mask, output_mask: Same as above, for masks.
        trainable_weights: List of variables.
        non_trainable_weights: List of variables.
        weights: The concatenation of the lists trainable_weights and
            non_trainable_weights (in this order).
        constraints: Dict mapping weights to constraints.

    # Methods
        call(x, mask=None): Where the layer's logic lives.
        __call__(x, mask=None): Wrapper around the layer logic (`call`).
            If x is a Keras tensor:
                - Connect current layer with last layer from tensor:
                    `self._add_inbound_node(last_layer)`
                - Add layer to tensor history
            If layer is not built:
                - Build from x._keras_shape
        get_weights()
        set_weights(weights)
        get_config()
        count_params()
        compute_output_shape(input_shape)
        compute_mask(x, mask)
        get_input_at(node_index)
        get_output_at(node_index)
        get_input_shape_at(node_index)
        get_output_shape_at(node_index)
        get_input_mask_at(node_index)
        get_output_mask_at(node_index)

    # Class Methods
        from_config(config)

    # Internal methods:
        build(input_shape)
        _add_inbound_node(layer, index=0)
        assert_input_compatibility()
    c   	      K` s  d  |  _ t |  _ g  |  _ g  |  _ i  |  _ t |  _ g  |  _ g  |  _	 d d d d d d d d h } x, | D]$ } | | k rm t
 d	 |   qm qm W| j d  } | s |  j j } t |  d
 t t j |   } n  | |  _ | j d t  |  _ d | k sd | k rd | k r1t | d  } nE d | k rvd | k rV| d } n d  } | f t | d  } n  | |  _ | j d  } | d  k r| j d  } n  | d  k rt j   } n  | |  _ n  d | k r| d |  _ n	 d  |  _ d  S(   Nt   input_shapet   batch_input_shapet
   batch_sizeR   R(   t	   trainablet   weightst   input_dtypes    Keyword argument not understood:t   _(   R   t
   input_spect   Falset   supports_maskingt   _trainable_weightst   _non_trainable_weightst   _constraintst   builtR&   R$   t	   TypeErrort   gett	   __class__R   t   _to_snake_caset   strt   Kt   get_uidR(   t   TrueR/   t   tupleR-   t   floatxR   t   _initial_weights(	   R   t   kwargst   allowed_kwargst   kwargR(   t   prefixR-   R.   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR      sV    									&		c         C` s   |  j  S(   N(   R8   (   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   constraints7  s    c         C` s   | |  _  d  S(   N(   R8   (   R   RI   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRI   ;  s    c         C` s'   t  |  d t  } | r |  j Sg  Sd  S(   NR/   (   t   getattrRA   R6   (   R   R/   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   trainable_weights?  s    c         C` s   | |  _  d  S(   N(   R6   (   R   R0   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRK   G  s    c         C` s1   t  |  d t  } | s& |  j |  j S|  j Sd  S(   NR/   (   RJ   RA   R6   R7   (   R   R/   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   non_trainable_weightsK  s    c         C` s   | |  _  d  S(   N(   R7   (   R   R0   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRL   S  s    c         C` s   t  j |  } t j | |  d t j   d | } | d k	 rX |  j | |   n  | d k	 rt | |  j | <n  | r |  j j	 |  n |  j
 j	 |  | S(   sL  Adds a weight variable to the layer.

        # Arguments
            shape: The shape tuple of the weight.
            initializer: An Initializer instance (callable).
            name: String, the name for the weight variable.
            trainable: A boolean, whether the weight should
                be trained via backprop or not (assuming
                that the layer itself is also trainable).
            regularizer: An optional Regularizer instance.
            constraint: An optional Constraint instance.

        # Returns
            The created weight variable.
        R   R(   N(   R   R;   R?   t   variableRC   R   t   add_lossRI   R6   R%   R7   (   R   R   t   initializerR(   R/   t   regularizert
   constraintt   weight(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt
   add_weightW  s    'c      	   C` s  |  j  s d St |  j  t t f  s7 t |  j   } n	 |  j  } t |  } t |  t |  k r t d |  j d t t |   d t t |   d t t	    n  xt
 t | |   D]\ } \ } } | d k r q n  | j d k	 r`t j |  | j k r`t d t |  d |  j d t | j  d	 t t j |     q`n  | j d k	 rt j |  } | d k	 r| | j k rt d t |  d |  j d
 t | j  d	 t t j |     qn  | j d k	 rpt j |  } | d k	 rp| | j k  rpt d t |  d |  j d t | j  d	 t t j |     qpn  | j d k	 rt j |  | j k rt d t |  d |  j d t | j  d t t j |     qn  | j ry t j |  } Wn t k
 rd } n X| d k	 rx | j j   D] \ } }	 |	 d k	 r8| t |  |	 d h k r8t d t |  d |  j d t |  d t |	  d t |    q8q8Wqn  | j d k	 r y t j |  } Wn t k
 rd } n X| d k	 rx t | j |  D]v \ }
 } |
 d k	 r| d k	 r|
 | k rt d t |  d |  j d t | j  d t |    qqqWqq q Wd S(   s  Checks compatibility between the layer and provided inputs.

        This checks that the tensor(s) `input`
        verify the input assumptions of the layer
        (if any). If not, exceptions are raised.

        # Arguments
            inputs: input tensor or list of input tensors.

        # Raises
            ValueError: in case of mismatch between
                the provided inputs and the expectations of the layer.
        Ns   Layer s	    expects s    inputs, but it received s     input tensors. Input received: s   Input s    is incompatible with layer s   : expected ndim=s   , found ndim=s   : expected max_ndim=s   : expected min_ndim=s   : expected dtype=s   , found dtype=s   : expected axis s    of input shape to have value s    but got shape s   : expected shape=s   , found shape=(   R3   t
   isinstancet   listRB   t   _to_listR   t
   ValueErrorR(   R>   t   inputt	   enumerateR   R   R   R?   R   R   R   R   t	   int_shapeR:   t   itemst   intR   (   R   t   inputsR3   t   input_indext   xt   specR   t   x_shapet   axist   valuet   spec_dimt   dim(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   assert_input_compatibilityw  sr    		6(----	
(U
-c         C` s   | S(   s   This is where the layer's logic lives.

        # Arguments
            inputs: input tensor, or list/tuple of input tensors.

        # Returns
            A tensor or list/tuple of tensors.
        (    (   R   R]   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   call  s    	c         K` s  t  j |  j  |  j s|  j |  g  } xu t |  D]g } t | d  rd | j | j  q< t t  d  r | j t  j	 |   q< t
 d |  j d   q< Wt |  d k r |  j | d  n |  j |  t |  _ |  j d k	 r|  j |  j  qn  |  j |  t |  } t |  sgd t j |  j  j k rgd | k rd| | d <qdqgn  t |  } |  j | |  } |  j | |  } t g  t |  D] }	 |	 d k	 ^ q r|  j |  }
 n1 t | t  rg  | D] } d ^ q}
 n d }
 |  j d | d	 | d
 | d | d | d |
 d |  t |  d  r|  j d k	 rg  t |  D] } |  j |  ^ qd} |  j | t |   n  Wd QX| S(   s  Wrapper around self.call(), for handling internal references.

        If a Keras tensor is passed:
            - We call self._add_inbound_node().
            - If necessary, we `build` the layer to match
                the _keras_shape of the input(s).
            - We update the _keras_shape of every input tensor with
                its new shape (obtained via self.compute_output_shape).
                This is done as part of _add_inbound_node().
            - We update the _keras_history of the output tensor(s)
                with the current layer.
                This is done as part of _add_inbound_node().

        # Arguments
            inputs: Can be a tensor or list/tuple of tensors.
            **kwargs: Additional keyword arguments to be passed to `call()`.

        # Returns
            Output of the layer's `call` method.

        # Raises
            ValueError: in case the layer is missing shape information
                for its `build` call.
        t   _keras_shapeRZ   s   You tried to call layer "s   ". This layer has no information about its expected input shape, and thus cannot be built. You can build it manually via: `layer.build(batch_input_shape)`i   i    t   maskR   R   R   R    R!   R"   R#   t   activity_regularizerN(    R?   t
   name_scopeR(   R9   Rf   RV   t   hasattrR%   Rh   RZ   RW   R   t   buildRA   RD   R   t   set_weightst   _collect_previous_maskt   _is_all_nonet   inspectt
   getargspecRg   t   argst   _collect_input_shapet   compute_maskt   allt   compute_output_shapeRT   RU   t   _add_inbound_nodeRj   RN   (   R   R]   RE   R!   t   x_elemt   previous_maskR,   t   outputt   output_maskt   st   output_shapeR2   R_   t   regularization_losses(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   __call__  sN    		+(c         C` s  t  |  } t  |  } t  |  } t  |  } t  |  } t  |  } g  } g  }	 g  }
 x | D]x } t | d  r | j \ } } } | j |  |	 j |  |
 j |  qa | j d  |	 j d  |
 j d  qa Wt |  d | d |	 d |
 d | d | d | d | d	 | d
 | d | 
x t t |   D] } | | | | _ t	 g  | D] } t
 | d t  ^ qW } t
 |  d t  p| } t
 | | d t  p| | | _ |  t |  j  d | f | | _ q6Wd S(   si  Internal method to create an inbound node for the layer.

        # Arguments
            input_tensors: list of input tensors.
            output_tensors: list of output tensors.
            input_masks: list of input masks (a mask can be a tensor, or None).
            output_masks: list of output masks (a mask can be a tensor, or None).
            input_shapes: list of input shape tuples.
            output_shapes: list of output shape tuples.
            arguments: dictionary of keyword arguments that were passed to the
                `call` method of the layer at the call that created the node.
        t   _keras_historyR   R   R   R   R   R   R    R!   R"   R#   t   _uses_learning_phaset   uses_learning_phasei   N(   RV   Rl   R   R%   R   R   t   rangeR   Rh   t   anyRJ   R4   R   R&   (   R   R   R   R   R    R!   R"   R#   R   R   R   R_   t   inbound_layert
   node_indext   tensor_indext   it   uses_lp(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRx   F  sL    +#c         C` sQ   t  |  d  rM d d } t j | j t |   j t |   j  d d n  | S(   s  Computes the output shape of the layer.

        Assumes that the layer will be built
        to match that input shape provided.

        # Arguments
            input_shape: Shape tuple (tuple of integers)
                or list of shape tuples (one per output tensor of the layer).
                Shape tuples can include None for free dimensions,
                instead of an integer.

        # Returns
            An input shape tuple.
        t   get_output_shape_fors[   Class `{}.{}` defines `get_output_shape_for` but does not override `compute_output_shape`. sW   If this is a Keras 1 layer, please implement `compute_output_shape` to support Keras 2.t
   stackleveli   (   Rl   t   warningst   warnt   formatt   typeR   R   (   R   R,   t   msg(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRw     s
    4c         C` s   |  j  s | d k	 r t | t  ra t d   | D  r t d |  j d t |    q q t d |  j d t |    n  d S| S(   s  Computes an output mask tensor.

        # Arguments
            inputs: Tensor or list of tensors.
            mask: Tensor or list of tensors.

        # Returns
            None or a tensor (or list of tensors,
                one per output tensor of the layer).
        c         s` s   |  ] } | d  k	 Vq d  S(   N(   R   (   t   .0t   m(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pys	   <genexpr>  s    s   Layer s9    does not support masking, but was passed an input_mask: N(   R5   R   RT   RU   R   R:   R(   R>   (   R   R]   Ri   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRu     s    	c         C` s   t  |  _ d S(   s  Creates the layer weights.

        Must be implemented on all layers that have weights.

        # Arguments
            input_shape: Keras tensor (future input to layer)
                or list/tuple of Keras tensors to reference
                for weight shape computations.
        N(   RA   R9   (   R   R,   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRm     s    
c         C` s   |  j  s  t d | d   n  t |  j   | k sq t d | d t |  d t t |  j    d   n  t |  j  | |  } t |  d k r | d S| Sd	 S(
   s  Retrieves an attribute (e.g. input_tensors) from a node.

        This is used to implement the methods:
            - get_input_shape_at
            - get_output_shape_at
            - get_input_at
            etc...

        # Arguments
            node_index: Integer index of the node from which
                to retrieve the attribute.
            attr: Exact node attribute name.
            attr_name: Human-readable attribute name, for error messages.

        # Returns
            The layer's attribute `attr` at the node of index `node_index`.

        # Raises
            RuntimeError: If the layer has no inbound nodes.
            ValueError: If the index is does not match any node.
        s8   The layer has never been called and thus has no defined t   .s   Asked to get s	    at node s   , but the layer has only s    inbound nodes.i   i    N(   R&   t   RuntimeErrorR   RW   R>   RJ   (   R   R   t   attrt	   attr_namet   values(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   _get_node_attribute_at_index  s    	9c         C` s   |  j  | d d  S(   s  Retrieves the input shape(s) of a layer at a given node.

        # Arguments
            node_index: Integer, index of the node
                from which to retrieve the attribute.
                E.g. `node_index=0` will correspond to the
                first time the layer was called.

        # Returns
            A shape tuple
            (or list of shape tuples if the layer has multiple inputs).
        R!   s   input shape(   R   (   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_input_shape_at  s    	c         C` s   |  j  | d d  S(   s  Retrieves the output shape(s) of a layer at a given node.

        # Arguments
            node_index: Integer, index of the node
                from which to retrieve the attribute.
                E.g. `node_index=0` will correspond to the
                first time the layer was called.

        # Returns
            A shape tuple
            (or list of shape tuples if the layer has multiple outputs).
        R"   s   output shape(   R   (   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_output_shape_at  s    	c         C` s   |  j  | d d  S(   s  Retrieves the input tensor(s) of a layer at a given node.

        # Arguments
            node_index: Integer, index of the node
                from which to retrieve the attribute.
                E.g. `node_index=0` will correspond to the
                first time the layer was called.

        # Returns
            A tensor (or list of tensors if the layer has multiple inputs).
        R   RX   (   R   (   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_input_at	  s    	c         C` s   |  j  | d d  S(   s  Retrieves the output tensor(s) of a layer at a given node.

        # Arguments
            node_index: Integer, index of the node
                from which to retrieve the attribute.
                E.g. `node_index=0` will correspond to the
                first time the layer was called.

        # Returns
            A tensor (or list of tensors if the layer has multiple outputs).
        R   R{   (   R   (   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_output_at  s    	c         C` s   |  j  | d d  S(   s  Retrieves the input mask tensor(s) of a layer at a given node.

        # Arguments
            node_index: Integer, index of the node
                from which to retrieve the attribute.
                E.g. `node_index=0` will correspond to the
                first time the layer was called.

        # Returns
            A mask tensor
            (or list of tensors if the layer has multiple inputs).
        R   s
   input mask(   R   (   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_input_mask_at)  s    	c         C` s   |  j  | d d  S(   s  Retrieves the output mask tensor(s) of a layer at a given node.

        # Arguments
            node_index: Integer, index of the node
                from which to retrieve the attribute.
                E.g. `node_index=0` will correspond to the
                first time the layer was called.

        # Returns
            A mask tensor
            (or list of tensors if the layer has multiple outputs).
        R    s   output mask(   R   (   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_output_mask_at:  s    	c         C` se   t  |  j  d k r/ t d |  j d   n# |  j sR t d |  j d   n  |  j d d d  S(   si  Retrieves the input tensor(s) of a layer.

        Only applicable if the layer has exactly one inbound node,
        i.e. if it is connected to one incoming layer.

        # Returns
            Input tensor or list of input tensors.

        # Raises
            AttributeError: if the layer is connected to
            more than one incoming layers.
        i   s   Layer sv    has multiple inbound nodes, hence the notion of "layer input" is ill-defined. Use `get_input_at(node_index)` instead.s&    is not connected, no input to return.i    R   RX   (   R   R&   t   AttributeErrorR(   R   (   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRX   K  s    	c         C` se   |  j  s# t d |  j d   n  t |  j   d k rR t d |  j d   n  |  j d d d  S(   sl  Retrieves the output tensor(s) of a layer.

        Only applicable if the layer has exactly one inbound node,
        i.e. if it is connected to one incoming layer.

        # Returns
            Output tensor or list of output tensors.

        # Raises
            AttributeError: if the layer is connected to
            more than one incoming layers.
        s   Layer s    has no inbound nodes.i   sx    has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `get_output_at(node_index)` instead.i    R   R{   (   R&   R   R(   R   R   (   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR{   e  s    	c         C` sF   t  |  j  d k r3 t d |  j d d   n  |  j d d d  S(   s  Retrieves the input mask tensor(s) of a layer.

        Only applicable if the layer has exactly one inbound node,
        i.e. if it is connected to one incoming layer.

        # Returns
            Input mask tensor (potentially None) or list of input
            mask tensors.

        # Raises
            AttributeError: if the layer is connected to
            more than one incoming layers.
        i   s   Layer s    has multiple inbound nodes, sc   hence the notion of "layer input mask" is ill-defined. Use `get_input_mask_at(node_index)` instead.i    R   s
   input mask(   R   R&   R   R(   R   (   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt
   input_mask  s    c         C` sB   t  |  j  d k r/ t d |  j d   n  |  j d d d  S(   s  Retrieves the output mask tensor(s) of a layer.

        Only applicable if the layer has exactly one inbound node,
        i.e. if it is connected to one incoming layer.

        # Returns
            Output mask tensor (potentially None) or list of output
            mask tensors.

        # Raises
            AttributeError: if the layer is connected to
            more than one incoming layers.
        i   s   Layer s    has multiple inbound nodes, hence the notion of "layer output mask" is ill-defined. Use `get_output_mask_at(node_index)` instead.i    R    s   output mask(   R   R&   R   R(   R   (   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR|     s
    c         C` s   |  j  s t d   n  t g  |  j  D] } t | j  ^ q%  } t |  d k r |  j  d j } t |  d k r | d S| Sn t d t |  j  d   d S(   s  Retrieves the input shape tuple(s) of a layer.

        Only applicable if the layer has exactly one inbound node,
        i.e. if it is connected to one incoming layer.

        # Returns
            Input shape tuple
            (or list of input shape tuples, one tuple per input tensor).

        # Raises
            AttributeError: if the layer is connected to
            more than one incoming layers.
        sD   The layer has never been called and thus has no defined input shape.i   i    s   The layer "s    has multiple inbound nodes, with different input shapes. Hence the notion of "input shape" is ill-defined for the layer. Use `get_input_shape_at(node_index)` instead.N(   R&   R   t   setR>   R!   R   R(   (   R   t   nodet   all_input_shapesR!   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR,     s    	+c         C` s   |  j  s t d   n  t g  |  j  D] } t | j  ^ q%  } t |  d k r |  j  d j } t |  d k r | d S| Sn t d t |  j  d   d S(   s  Retrieves the output shape tuple(s) of a layer.

        Only applicable if the layer has one inbound node,
        or if all inbound nodes have the same output shape.

        # Returns
            Output shape tuple
            (or list of input shape tuples, one tuple per output tensor).

        # Raises
            AttributeError: if the layer is connected to
            more than one incoming layers.
        sE   The layer has never been called and thus has no defined output shape.i   i    s   The layer "s    has multiple inbound nodes, with different output shapes. Hence the notion of "output shape" is ill-defined for the layer. Use `get_output_shape_at(node_index)` instead.N(   R&   R   R   R>   R"   R   R(   (   R   R   t   all_output_shapesR"   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR~     s    	+c         C` s   | d k r d St |  } t |  d  s7 g  |  _ n  y |  j | 7_ Wn t k
 r] n Xt |  d  sy i  |  _ n  | d k	 r t |  } n d } | |  j k r g  |  j | <n  |  j | c | 7<d S(   s|  Add losses to the layer.

        The loss may potentially be conditional on some inputs tensors,
        for instance activity losses are conditional on the layer's inputs.

        # Arguments
            losses: loss tensor or list of loss tensors
                to add to the layer.
            inputs: input tensor or list of inputs tensors to mark
                the losses as conditional on these inputs.
                If None is passed, the loss is assumed unconditional
                (e.g. L2 weight regularization, which only depends
                on the layer's weights variables, not on any inputs tensors).
        Nt   lossest   _per_input_losses(   R   RV   Rl   R   R   R   t   _object_list_uid(   R   R   R]   t   inputs_hash(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRN     s"    c         C` s   | d k r d St |  } t |  d  s7 g  |  _ n  y |  j | 7_ Wn t k
 r] n Xt |  d  sy i  |  _ n  | d k	 r t |  } n d } | |  j k r g  |  j | <n  |  j | c | 7<d S(   s  Add updates to the layer.

        The updates may potentially be conditional on some inputs tensors,
        for instance batch norm updates are conditional on the layer's inputs.

        # Arguments
            updates: update op or list of update ops
                to add to the layer.
            inputs: input tensor or list of inputs tensors to mark
                the updates as conditional on these inputs.
                If None is passed, the updates are assumed unconditional.
        Nt   updatest   _per_input_updates(   R   RV   Rl   R   R   R   R   (   R   R   R]   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt
   add_update  s"    c         C` sR   t  |  d  s g  S| d  k	 r. t |  } n d  } | |  j k rN |  j | Sg  S(   NR   (   Rl   R   R   R   (   R   R]   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_updates_forG  s    c         C` sR   t  |  d  s g  S| d  k	 r. t |  } n d  } | |  j k rN |  j | Sg  S(   NR   (   Rl   R   R   R   (   R   R]   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_losses_forR  s    c         C` s   |  j  |  j S(   N(   RK   RL   (   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR0   ]  s    c         C` s  |  j  } t |  t |  k ru t d |  j d t t |   d t t |   d t |  d  d   n  | s d Sg  } t j |  } xu t | | |  D]a \ } } } | j | j k r t d t | j  d	 t | j    n  | j	 | | f  q Wt j
 |  d S(
   s  Sets the weights of the layer, from Numpy arrays.

        # Arguments
            weights: a list of Numpy arrays. The number
                of arrays and their shape must match
                number of the dimensions of the weights
                of the layer (i.e. it should match the
                output of `get_weights`).

        # Raises
            ValueError: If the provided weights list does not match the
                layer's specifications.
        s,   You called `set_weights(weights)` on layer "s    " with a  weight list of length s   , but the layer was expecting s    weights. Provided weights: i2   s   ...Ns   Layer weight shape s+    not compatible with provided weight shape (   R0   R   RW   R(   R>   R?   t   batch_get_valueR   R   R%   t   batch_set_value(   R   R0   t   paramst   weight_value_tuplest   param_valuest   pvt   pt   w(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRn   a  s    	Q"*c         C` s   |  j  } t j |  S(   s{   Returns the current weights of the layer.

        # Returns
            Weights values as a list of numpy arrays.
        (   R0   R?   R   (   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_weights  s    	c         C` s\   i |  j  d 6|  j d 6} t |  d  r9 |  j | d <n  t |  d  rX |  j | d <n  | S(   s  Returns the config of the layer.

        A layer config is a Python dictionary (serializable)
        containing the configuration of a layer.
        The same layer can be reinstantiated later
        (without its trained weights) from this configuration.

        The config of a layer does not include connectivity
        information, nor the layer class name. These are handled
        by `Container` (one layer of abstraction above).

        # Returns
            Python dictionary.
        R(   R/   R-   R   (   R(   R/   Rl   R-   R   (   R   t   config(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR*     s    c         C` s
   |  |   S(   s  Creates a layer from its config.

        This method is the reverse of `get_config`,
        capable of instantiating the same layer from the config
        dictionary. It does not handle layer connectivity
        (handled by Container), nor weights (handled by `set_weights`).

        # Arguments
            config: A Python dictionary, typically the
                output of get_config.

        # Returns
            A layer instance.
        (    (   t   clsR   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   from_config  s    c         C` sv   |  j  sM |  j j d k r( |  j   qM t d |  j d |  j d   n  t g  |  j D] } t j	 |  ^ qZ  S(   s   Count the total number of scalars composing the weights.

        # Returns
            An integer count.

        # Raises
            RuntimeError: if the layer isn't yet built
                (in which case its weights aren't yet defined).
        t
   Sequentials$   You tried to call `count_params` on s=   , but the layer isn't built. You can build it manually via: `s   .build(batch_input_shape)`.(
   R9   R<   R   Rm   R   R(   t   sumR0   R?   t   count_params(   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR     s    
	"N(+   R   R   R   R   t   propertyRI   t   setterRK   RL   R   RA   RS   Rf   Rg   R   Rx   Rw   Ru   Rm   R   R   R   R   R   R   R   RX   R{   R   R|   R,   R~   RN   R   R   R   R0   Rn   R   R*   t   classmethodR   R   (    (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR+      sT   B	@	_		g<			$						""*(			%			t
   InputLayerc           B` s5   e  Z d  Z d d d d d e d d  Z d   Z RS(   s  Layer to be used as an entry point into a graph.

    It can either wrap an existing tensor (pass an `input_tensor` argument)
    or create its a placeholder tensor (pass arguments `input_shape`
    or `batch_input_shape` as well as `dtype`).

    # Arguments
        input_shape: Shape tuple, not including the batch axis.
        batch_size: Optional input batch size (integer or None).
        batch_input_shape: Shape tuple, including the batch axis.
        dtype: Datatype of the input.
        input_tensor: Optional tensor to use as layer input
            instead of creating a placeholder.
        sparse: Boolean, whether the placeholder created
            is meant to be sparse.
        name: Name of the layer (string).
    c   	      C` s  | s, d } | d t  t j |   } n  t t |   j d | d |  t |  _ t |  _	 | |  _
 | r | r t d   n  | d  k	 r y t j |  } Wq t k
 r | r | r t d   q q Xn  | s| s t d   q| f t |  } n t |  } | sD| d  k r2t j   } qDt j |  } n  | |  _ | |  _ | d  k rt |  _ t j d | d | d	 |  j
 d |  j  } n t |  _ | | _ t | _ |  d
 d
 f | _ t |  d g  d g  d g  d | g d | g d d  g d d  g d | g d | g 	d  S(   NRX   R2   R   R(   sd   Only provide the input_shape OR batch_input_shape argument to InputLayer, not both at the same time.s   InputLayer was provided an input_tensor argument, but its input shape cannot be automatically inferred. You should pass an input_shape or batch_input_shape argument.sQ   An Input layer should be passed either a `batch_input_shape` or an `input_shape`.R   t   sparsei    R   R   R   R   R   R   R    R!   R"   (   R>   R?   R@   t   superR   R   R4   R/   RA   R9   R   RW   R   RZ   R:   RB   RC   R   R-   t   is_placeholdert   placeholderR(   Rh   R   R   R   (	   R   R,   R.   R-   R   t   input_tensorR   R(   RH   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR     s\     																c         C` s2   i |  j  d 6|  j d 6|  j d 6|  j d 6} | S(   NR-   R   R   R(   (   R-   R   R   R(   (   R   R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR*   &  s
    

N(   R   R   R   R   R4   R   R*   (    (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR     s
   Dc         C` s   | r( | d	 k r( |  s( t d   n  |  rH | rH d
 t |   } n  t d | d | d | d | d |  } | j d j } t |  d k r | d S| Sd	 S(   s  `Input()` is used to instantiate a Keras tensor.

    A Keras tensor is a tensor object from the underlying backend
    (Theano or TensorFlow), which we augment with certain
    attributes that allow us to build a Keras model
    just by knowing the inputs and outputs of the model.

    For instance, if a, b and c and Keras tensors,
    it becomes possible to do:
    `model = Model(input=[a, b], output=c)`

    The added Keras attributes are:
        ._keras_shape: Integer shape tuple propagated
            via Keras-side shape inference.
        ._keras_history: Last layer applied to the tensor.
            the entire layer graph is retrievable from that layer,
            recursively.

    # Arguments
        shape: A shape tuple (integer), not including the batch size.
            For instance, `shape=(32,)` indicates that the expected input
            will be batches of 32-dimensional vectors.
        batch_shape: A shape tuple (integer), including the batch size.
            For instance, `batch_shape=(10, 32)` indicates that
            the expected input will be batches of 10 32-dimensional vectors.
            `batch_shape=(None, 32)` indicates batches of an arbitrary number
            of 32-dimensional vectors.
        name: An optional name string for the layer.
            Should be unique in a model (do not reuse the same name twice).
            It will be autogenerated if it isn't provided.
        dtype: The data type expected by the input, as a string
            (`float32`, `float64`, `int32`...)
        sparse: A boolean specifying whether the placeholder
            to be created is sparse.
        tensor: Optional existing tensor to wrap into the `Input` layer.
            If set, the layer will not create a placeholder tensor.

    # Returns
        A tensor.

    # Example

        ```python
        # this is a logistic regression in Keras
        x = Input(shape=(32,))
        y = Dense(16, activation='softmax')(x)
        model = Model(x, y)
        ```
    s}   Please provide to Input either a `shape` or a `batch_shape` argument. Note that `shape` does not include the batch dimension.R-   R(   R   R   R   i    i   N(   N(   R   t   AssertionErrorRB   R   R&   R   R   (   R   t   batch_shapeR(   R   R   t   tensort   input_layert   outputs(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   Input.  s    4	t	   Containerc           B` sg  e  Z d  Z e j e d   Z e e d  Z e d    Z	 e d    Z
 e d    Z e d    Z d   Z e d    Z e d	    Z e d
    Z e d    Z d   Z d   Z e d    Z e d  Z d   Z d   Z e d  Z d   Z e e d   Z e d  Z e d  Z e d  Z  d   Z! d   Z" d   Z# e e d  Z$ RS(   s{  A Container is a directed acyclic graph of layers.

    It is the topological form of a "model". A Model
    is simply a Container with added training routines.

    # Properties
        name
        inputs
        outputs
        input_layers
        output_layers
        input_spec (list of class instances)
            each entry describes one required input:
                - ndim
                - dtype
        trainable (boolean)
        input_shape
        output_shape
        inbound_nodes: list of nodes
        outbound_nodes: list of nodes
        trainable_weights (list of variables)
        non_trainable_weights (list of variables)
        constraints (list of tuples (weight, constraint))

    # Methods
        summary
        get_layer
        get_weights
        set_weights
        get_config
        compute_output_shape

    # Class Methods
        from_config
    c         ` s	  | s8 |  j  j j   } | d t t j |   } n  | |  _ t |  _ t	 |  _
 t | t t f  rz t |  |  _ n | g |  _ t | t t f  r t |  |  _ n | g |  _ t |  j  } t |  t |  j  k r t d t |  j    n  g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ i  |  _ i  |  _ i  |  _ x |  j D] } t | d  s|  j  j } t d | d d t |  d   n  | j \ } }	 }
 t | j   d k s| j  rc| j  d	 j! rc|  j  j } t" j# | d
 |  j d | j d t | j   qcqcWxM |  j D]B } t | d  s>|  j  j } t d | d t |    q>q>WxS |  j D]H } | j \ } }	 }
 |  j j$ |  |  j j$ |	  |  j j$ |
  qWg  } xJ |  j D]? } | j \ } }	 }
 | j  |	 } | j% |
 } | j$ |  qWd j& g  |  j D] } t t' |   ^ q= } | d d j& g  | D] } t t' |   ^ qq 7} g  } xJ |  j D]? } | j \ } }	 }
 | j  |	 } | j% |
 } | j$ |  qWt |  d k r| d	 } n | } | |  j | <xw |  j D]l } | j \ } }	 }
 |	 d	 k sMt(  |
 d	 k s_t(  |  j j$ |  |  j j$ |	  |  j j$ |
  q#Wg  |  _) g  |  _* g  |  _+ g  |  _, g  |  _- x| t. |  j  D]k \ } } |  j) j$ | j  | j/ r|  j+ j$ | j  |  j, j$ | j0  |  j- j$ |  j | j1  qqWx$ |  j D] } |  j* j$ | j  qIWg  |  j D] } | j1 ^ qp|  _2 g  |  j D] } | j1 ^ q|  _3 t    i   i   i   d    d  d	 d  d  d         f d    x- |  j D]" } t   }   | | d d	 qWi  } xD  j5   D]6 \ } } | | k rcg  | | <n  | | j$ |  q>Wi  } xD  j5   D]6 \ } } | | k rg  | | <n  | | j$ |  qWt | j6    } | j7 d t	  g  } xO | D]G } | | } | j7 d  f d    x | D] } | j$ |  q$WqW| |  _ | |  _8 t | j6    } | j7 d t	  g  } x |  j D] } | j$ |  qWg  } x | D] } x | | D] } | j9 } | rxO | j: D]D } | | k rt; d t |  d | j d t |    qqWx | j< D] } | j$ |  q)W| j$ | j  qqWqW |  _= | |  _> g  |  j D] } | j ^ qw} xQ | D]I } | j? |  d k rt; d | d t | j? |   d   qqWg  |  _@ g  |  _  tA d |  d g  d g  d g  d  |  j d! |  j d" g  |  j D] } d  ^ q,	d# g  |  j D] } d  ^ qH	d$ g  |  j D] } | j1 ^ qd	d% g  |  j D] } | j1 ^ q	 
t	 |  _B d  S(&   NR2   s`   The list of inputs passed to the model is redundant. All inputs should only appear once. Found: R   s   Input tensors to a t    s   must be Keras tensors. Found: s    (missing Keras metadata).i   i    s    inputs must come from a Keras Input layer, they cannot be the output of a previous non-Input layer. Here, a tensor specified as input to "s5   " was not an Input tensor, it was generated by layer sn   .
Note that input tensors are instantiated via `tensor = Input(shape)`.
The tensor that caused the issue was: s   Output tensors to a s    must be Keras tensors. Found: t   ,c         S` s   t  t |    d t  |  S(   Nt   -(   R>   t   id(   R   t   depth(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   make_node_marker0  s    c         ` s  | p t    } | s. | d k s. | d k rC |  j \ } } } n  | j | } | j  | |   | j d t |  }  j |   j |  } | d k r |  | <n t | |   | < j |  }	 |	 d k r | }
 n t | |	  }
 |
  | <|  k r!t	    | <n  x t
 t	 | j   D] } | j | } | j | } | j | } | j | } | j | }  | |
 d  } | | k r7  | | |
 d | | |  q7q7Wd S(   s.  Builds a map of the graph of layers.

            This recursively updates the maps `nodes_depths`,
            `layers_depths` and the set `container_nodes`.

            Does not try to detect cycles in the graph.

            # Arguments
                tensor: Some tensor in a graph.
                seen_nodes: Set of node ids ("{layer.name}_ib-{node_index}")
                    of nodes seen so far. Useful to prevent infinite loops.
                depth: Current depth in the graph (0 = last output).
                layer: Layer from which `tensor` comes from. If not provided,
                    will be obtained from `tensor._keras_history`.
                node_index: Node index from which `tensor` comes from.
                tensor_index: Tensor_index from which `tensor` comes from.
            s   _ib-i   N(   R   R   R   R&   t   addR(   R>   R;   t   maxR   R   R   R   R   R   (   R   t
   seen_nodesR   R'   R   R   R   t   node_keyt
   node_deptht   previously_seen_deptht   current_depthR   R_   t	   next_nodet   node_marker(   t   build_map_of_grapht   container_nodest   layer_indicest   layers_depthsR   t   nodes_depths(    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR   3  s8    	
R   t   reverset   keyc         ` s     |  S(   N(    (   R_   (   R   (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   <lambda>  s    s3   Graph disconnected: cannot obtain value for tensor s    at layer "s>   ". The following previous layers were accessed without issue: s
   The name "s
   " is used s6    times in the model. All layer names should be unique.R   R   R   R   R   R   R   R    R!   R"   (C   R<   R   t   lowerR>   R?   R@   R(   R4   R5   RA   R/   RT   RU   RB   R]   R   R   R   RW   t   input_layerst   input_layers_node_indicest   input_layers_tensor_indicest   output_layerst   output_layers_node_indicest   output_layers_tensor_indicest   layerst   _output_mask_cachet   _output_tensor_cachet   _output_shape_cacheRl   R:   R   R&   R   R   R   R%   R    t   joinR   R   t   input_namest   output_namest   _feed_input_namest   _feed_inputst   _feed_input_shapesRY   R   RX   Rh   t   internal_input_shapest   internal_output_shapesR   R[   t   keyst   sortt   layers_by_depthR   R   R   R   R   t   nodes_by_deptht   countR$   R   R9   (   R   R]   R   R(   RH   t
   inputs_setR_   t   cls_nameR'   R   R   t   masksR   Ri   t   mask_cache_keyR   R   R   R   R   t
   depth_keysR   t   layers_for_deptht   computable_tensorst   layers_with_complete_inputt	   all_namesR2   (    (   R   R   R   R   R   R   s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR     s:    														."16						!""		':	
									 c         C` s   | d k	 rc t |  j  | k rU t d t |  d t t |  j   d   qx |  j | Sn | sx t d   n  d } x$ |  j D] } | j | k r | Sq W| s t d |   n  d S(   s  Retrieves a layer based on either its name (unique) or index.

        Indices are based on order of horizontal graph traversal (bottom-up).

        # Arguments
            name: String, name of layer.
            index: Integer, index of layer.

        # Returns
            A layer instance.

        # Raises
            ValueError: In case of invalid layer name or index.
        s%   Was asked to retrieve layer at index s    but model only has s    layers.s+   Provide either a layer name or layer index.s   No such layer: N(   R   R   R   RW   R>   R(   (   R   R(   t   indexR'   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt	   get_layer  s    1c         C` s   g  } x |  j  D] } t | d  r t | j  d k rJ | | j 7} q xb t | j  D]Q \ } } | j d t |  } | |  j k rZ | j	 } | | j
 |  7} qZ qZ W| | j
 d  7} q q W| S(   s5  Retrieve the model's updates.

        Will only include updates that are either
        inconditional, or conditional on inputs to this model
        (e.g. will not include updates that depend on tensors
        that aren't inputs to this model).

        # Returns
            A list of update ops.
        R   i   s   _ib-N(   R   Rl   R   R&   R   RY   R(   R>   R   R   R   R   (   R   R   R'   R   R   R   R]   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR     s    	c         C` s   g  } x |  j  D] } t | d  r t | j  d k rJ | | j 7} q xb t | j  D]Q \ } } | j d t |  } | |  j k rZ | j	 } | | j
 |  7} qZ qZ W| | j
 d  7} q q Wt |  d  r | |  j j d g   7} n  | S(   s4  Retrieve the model's losses.

        Will only include losses that are either
        inconditional, or conditional on inputs to this model
        (e.g. will not include losses that depend on tensors
        that aren't inputs to this model).

        # Returns
            A list of loss tensors.
        R   i   s   _ib-R   N(   R   Rl   R   R&   R   RY   R(   R>   R   R   R   R   R   R;   (   R   R   R'   R   R   R   R]   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR     s    	c         C` s#   t  g  |  j D] } | j ^ q  S(   N(   R   R   R   (   R   R_   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR   8  s    c         C` s2   t  g  |  j D] } t | d  o( | j ^ q  S(   Nt   stateful(   R   R   Rl   R  (   R   R'   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR  <  s    c         C` sF   x? |  j  D]4 } t | d  r
 t | d t  r
 | j   q
 q
 Wd  S(   Nt   reset_statesR  (   R   Rl   RJ   R4   R	  (   R   R'   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR	  @  s    !c         C` sR   g  } xE |  j  D]: } t | d t  r t | d  rJ | | j 7} qJ q q W| S(   s  Returns the `updates` from all layers that are stateful.

        This is useful for separating training updates and
        state updates, e.g. when we need to update a layer's internal state
        during prediction.

        # Returns
            A list of update ops.
        R  R   (   R   RJ   R4   Rl   R   (   R   t   state_updatesR'   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR
  E  s    c         C` s}   i  } xp |  j  D]e } x\ | j j   D]K \ } } | | k rg | | | k rg t d t |    n  | | | <q& Wq W| S(   Ns5   Received multiple constraints for one weight tensor: (   R   RI   R[   RW   R>   (   R   t   consR'   R   Rc   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRI   W  s    c         C` s8   |  j  s g  Sg  } x |  j D] } | | j 7} q W| S(   N(   R/   R   RK   (   R   R0   R'   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRK   b  s    	c         C` sc   g  } x |  j  D] } | | j 7} q W|  j s_ g  } x |  j  D] } | | j 7} q@ W| | S| S(   N(   R   RL   R/   RK   (   R   R0   R'   RK   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRL   k  s    	c         C` s4   g  } x |  j  D] } | | j 7} q Wt j |  S(   sh   Retrieves the weights of the model.

        # Returns
            A flat list of Numpy arrays.
        (   R   R0   R?   R   (   R   R0   R'   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR   w  s    c         C` s   g  } xj |  j  D]_ } t | j  } | |  } x3 t | j |  D] \ } } | j | | f  qB W| | } q Wt j |  d S(   s   Sets the weights of the model.

        # Arguments
            weights: A list of Numpy arrays with shapes and types matching
                the output of `model.get_weights()`.
        N(   R   R   R0   R   R%   R?   R   (   R   R0   t   tuplesR'   t	   num_paramt   layer_weightst   swR   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRn     s    
c         C` s   g  } x t  |  d g   D]k } | j d k r> | j d  q t | j t  sw t d | j d t | j    n  | | j 7} q Wt	 |  d k r | d S| S(   s   Gets the model's input specs.

        # Returns
            A list of `InputSpec` instances (one per input to the model)
                or a single instance if the model has only one input.
        R   s   Layer sV    has an input_spec attribute that is not a list. We expect a list. Found input_spec = i   i    N(
   RJ   R3   R   R%   RT   RU   R:   R(   R>   R   (   R   t   specsR'   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR3     s    c         C` s   t  |  } | d k r@ g  t t |   D] } d ^ q+ } n t  |  } d j g  | D] } t t |   ^ qY  } | d d j g  | D] } t t |   ^ q  7} | |  j k r |  j | S|  j | |  \ } } } | Sd S(   s9  Call the model on new inputs.

        In this case `call` just reapplies
        all ops in the graph to the new inputs
        (e.g. build a new computational graph from the provided inputs).

        A model is callable on non-Keras tensors.

        # Arguments
            inputs: A tensor or list of tensors.
            mask: A mask or list of masks. A mask can be
                either a tensor or None (no mask).

        # Returns
            A tensor if there is a single output, or
            a list of tensors if there are more than one outputs.
        R   R2   N(	   RV   R   R   R   R   R>   R   R   t   run_internal_graph(   R   R]   Ri   R2   R   R_   t	   cache_keyR   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRg     s    (.6c         C` s   t  |  } | d  k r@ g  t t |   D] } d  ^ q+ } n t  |  } d j g  | D] } t t |   ^ qY  } | d d j g  | D] } t t |   ^ q  7} | |  j k r |  j | S|  j | |  \ } } } | Sd  S(   NR   R2   (	   RV   R   R   R   R   R>   R   R   R  (   R   R]   Ri   R2   R   R_   R  R    (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRu     s    (.6c         C` s  t  |  } t |  t |  j  k r[ t d t |  d t t |  j   d   n  d j g  | D] } t |  ^ qh  } | |  j k r |  j | } t | t  r t |  d k r | d S| Si  } xH t	 t |   D]4 } |  j | } | | } | j
 d }	 | | |	 <q Wt |  j j    }
 |
 j d t  t |
  d k rx_|
 D]T} |  j | } x>| D]6} | j } | |  j k rqun  g  } xr t	 t | j   D][ } | j | } | j | } | j | } | j
 d	 | | f }	 | |	 } | j |  qWt |  d k r<| j | d  } n | j |  } t  |  } | j j |  } x? t	 t |   D]+ } | j
 d	 | | f }	 | | | |	 <q|WquWq[Wn  g  } g  } xh t	 t |  j   D]Q } |  j | } |  j | } |  j | } | j
 d	 | | f }	 | j |	  qWx= t |  D]/ \ } } | | k sXt  | j | |  q:W| |  j | <t | t  rt |  d k r| d S| Sd  S(
   Ns   Invalid input_shape argument s   : model has s    tensor inputs.R   i   i    t   _0_0R   s   _%s_%s(   RV   R   R   RW   R>   R   R   RT   RU   R   R(   R   R   R   RA   R   R   R   R   R%   Rw   R&   R  R   R   R   RY   R   (   R   R,   R!   R_   R  R"   t   layers_to_output_shapesR   R'   t	   shape_keyR  R   t   nodesR   t   jR   R   R   R~   t   output_shape_keysR   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRw     sp    1(!
	
!c       
   C` s  | d k r4 g  t t |   D] } d ^ q } n  i  } xB t |  j | |  D]+ \ } } } | | f | t t |   <qP Wt |  j j	    } | j
 d t  x| D]}	 |  j |	 }
 x|
 D]} | j } | j } | j } g  } xF | D]> } t t |   | k r | j | t t |    q q Wt |  t |  k r t j | j  _| j ru| j } n i  } t |  d k r| d \ } } d t j | j  j k rd | k r| | d <qn  t | j | |   } t | j | |   } | g } | g } n g  | D] } | d ^ q } g  | D] } | d ^ q=} d t j | j  j k rd | k r| | d <qn  t | j | |   } t | j | |   } Wd QXg  | D] } | d ^ q} |  j | j |  |  |  j | j d  d  |  j | j |  |  |  j | j d  d  t g  | D] } t | d  ^ qK r;t |  d k rt | j  | d j!   } | d j" } nM t | j  g  | D] } | j! ^ q  } t# g  | D] } | j" ^ q } xD t | |  D]0 \ } } | | _! t$ | d t%  p.| | _" qWn  xB t | | |  D]+ \ } } } | | f | t t |   <qNWq q Wq Wg  } g  } g  } x |  j& D] } t t |   | k st' d t |    | t t |   \ } } t | d  r(| d k	 r(| j! } | j |  n d } | j |  | j |  qWd	 j( g  | D] } t t |   ^ qY } | d
 d	 j( g  | D] } t t |   ^ q 7} t |  d k r| d } | |  j) | <n | |  j) | <t |  d k r| d } | |  j* | <n | |  j* | <| d k	 rg  | D] } | j! ^ q5} d	 j( g  | D] } t |  ^ qW } t |  d k r| d } | |  j+ | <q| |  j+ | <n  | | | f S(   s  Computes output tensors for new inputs.

        # Note:
            - Expects `inputs` to be a list (potentially with 1 element).
            - Can be run on non-Keras tensors.

        # Arguments
            inputs: List of tensors
            masks: List of masks (tensors or None).

        # Returns
            Three lists: output_tensors, output_masks, output_shapes
        R   i   i    Ri   NRh   R   s   Could not compute output R   R2   (,   R   R   R   R   R]   R>   R   RU   R   R   R   RA   R   R   R   R%   R?   Rk   R(   R#   Rq   Rr   Rg   Rs   RV   Ru   R   R   RN   R   Rv   Rl   Rw   Rh   R   R   RJ   R4   R   R   R   R   R   R   (    R   R]   R   R2   t
   tensor_mapR_   t   yRi   R  R   R  R   R'   t   reference_input_tensorst   reference_output_tensorst   computed_dataRE   t   computed_tensort   computed_maskR   R    t   computed_tensorst   computed_maskst   layer_inputst   shapesR   R}   R"   R   R   R  R!   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR  %  s    (% 			$		(+"	""+.	.6

(
c         C` s  i |  j  d 6} i  } x |  j D] } t | j t  rA d } n d } xZ t | j  D]I \ } } | j  d t |  } | |  j k rW | | | <| d 7} qW qW Wq  Wg  } x|  j D]} | j j	 }	 | j
   }
 g  } xct | j  D]R\ } } | j  d t |  } | |  j k r | j ry t j | j  | j } Wqt k
 rt j d | j  d t | j  d  i  } qXn i  } | j r>g  } x t t | j   D]r } | j | } | j | } | j | } | j  d t |  } | j | d  } | j | j  | | | g  qW| j |  q>q q W| j i | j  d 6|	 d 6|
 d	 6| d
 6 q W| | d <g  } x~ t t |  j   D]g } |  j | } |  j | } | j  d t |  } | | } |  j | } | j | j  | | g  qW| | d <g  } x~ t t |  j   D]g } |  j | } |  j | } | j  d t |  } | | } |  j | } | j | j  | | g  q)W| | d <t j |  S(   NR(   i   i    s   _ib-s   Layer s0    was passed non-serializable keyword arguments: sg   . They will not be included in the serialized model (and thus will be missing at deserialization time).t
   class_nameR   R&   R   R   R   (    R(   R   t
   issubclassR<   R   RY   R&   R>   R   R   R*   R#   t   jsont   dumpsR:   R   R   R   R   R   R   R   R;   R%   R   R   R   R   R   R   t   copyt   deepcopy(   R   R   t   node_conversion_mapR'   t
   kept_nodest   original_node_indexR   R   t   layer_configst   layer_class_namet   layer_configt   filtered_inbound_nodesRE   t	   node_dataR   R   R   R   t   new_node_indext   model_inputst   model_outputs(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR*     s    	
	#		





c         ` s-  i       f d   } x | d D] } | |  q# W| j  d  } g  } g  } x^ | d D]R } | \ } }	 }
 |   k s t    | } | j |	 j } | j | |
  q] Wx^ | d D]R } | \ } }	 }
 |   k s t    | } | j |	 j } | j | |
  q W|  d | d | d |  S(   s  Instantiates a Model from its config (output of `get_config()`).

        # Arguments
            config: Model config dictionary.
            custom_objects: Optional dictionary mapping names
                (strings) to custom classes or functions to be
                considered during deserialization.

        # Returns
            A model instance.

        # Raises
            ValueError: In case of improperly formatted config dict.
        c         ` sV  |  d } d d l  m } | |  d  } |   | <|  d } x| D]} g  } x | D] } | d } | d }	 | d }
 t |  d k r i  } n+ t |  d	 k r | d } n t d
   |   k r t d |   n    | } | j |	 } | j | j |
  qZ W| rG t |  d k r>| | d |  qN| | |  qG qG Wd S(   s   Deserialize a layer, then call it on appropriate inputs.

            # Arguments
                layer_data: layer config dict.

            # Raises
                ValueError: In case of improperly formatted `layer_data` dict.
            R(   i   (   t   deserializet   custom_objectsR&   i    i   i   i   s"   Improperly formatted model config.s   Missing layer: N(   R   R5  R   RW   R&   R%   R   (   t
   layer_datat
   layer_namet   deserialize_layerR'   t   inbound_nodes_dataR1  R   t
   input_datat   inbound_layer_namet   inbound_node_indext   inbound_tensor_indexRE   R   t   inbound_node(   t   created_layersR6  (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   process_layer	  s4    	
		




	
R   R(   R   R   R]   R   (   R;   R   R&   R   R%   (   R   R   R6  RA  R7  R(   R   R   R8  R   R   R'   t   layer_output_tensors(    (   R@  R6  s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR   	  s(    ,

c         C` s$   d d l  m } | |  | |  d S(   s  Save the model to a single HDF5 file.

        The savefile includes:
            - The model architecture, allowing to re-instantiate the model.
            - The model weights.
            - The state of the optimizer, allowing to resume training
                exactly where you left off.

        This allows you to save the entirety of the state of a model
        in a single file.

        Saved models can be reinstantiated via `keras.models.load_model`.
        The model returned by `load_model`
        is a compiled model ready to be used (unless the saved model
        was never compiled in the first place).

        # Arguments
            filepath: String, path to the file to save the weights to.
            overwrite: Whether to silently overwrite any existing file at the
                target location, or provide the user with a manual prompt.

        # Example

        ```python
        from keras.models import load_model

        model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
        del model  # deletes the existing model

        # returns a compiled model
        # identical to the previous one
        model = load_model('my_model.h5')
        ```
        i   (   t
   save_modelN(   t   modelsRC  (   R   t   filepatht	   overwriteRC  (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   saveY	  s    #c         C` s   t  d k r t d   n  | rM t j j |  rM t |  } | sM d Sn  t  j | d  } t | |  j	  | j
   | j   d S(   sr  Dumps all layer weights to a HDF5 file.

        The weight file has:
            - `layer_names` (attribute), a list of strings
                (ordered names of model layers).
            - For every layer, a `group` named `layer.name`
                - For every such layer group, a group attribute `weight_names`,
                    a list of strings
                    (ordered names of weights tensor of the layer).
                - For every weight in the layer, a dataset
                    storing the weight value, named after the weight tensor.

        # Arguments
            filepath: String, path to the file to save the weights to.
            overwrite: Whether to silently overwrite any existing file at the
                target location, or provide the user with a manual prompt.

        # Raises
            ImportError: If h5py is not available.
        s   `save_weights` requires h5py.NR   (   t   h5pyR   t   ImportErrort   ost   patht   isfileR   t   Filet   save_weights_to_hdf5_groupR   t   flusht   close(   R   RE  RF  t   proceedt   f(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   save_weights	  s    
c         C` s   t  d k r t d   n  t  j | d d } d | j k rX d | k rX | d } n  | rq t | |  j  n t | |  j  t | d  r | j	   n  d S(   s  Loads all layer weights from a HDF5 save file.

        If `by_name` is False (default) weights are loaded
        based on the network's topology, meaning the architecture
        should be the same as when the weights were saved.
        Note that layers that don't have weights are not taken
        into account in the topological ordering, so adding or
        removing layers is fine as long as they don't have weights.

        If `by_name` is True, weights are loaded into layers
        only if they share the same name. This is useful
        for fine-tuning or transfer-learning models where
        some of the layers have changed.

        # Arguments
            filepath: String, path to the weights file to load.
            by_name: Boolean, whether to load weights by name
                or by topological order.

        # Raises
            ImportError: If h5py is not available.
        s   `load_weights` requires h5py.t   modet   rt   layer_namest   model_weightsRP  N(
   RH  R   RI  RM  t   attrst$   load_weights_from_hdf5_group_by_nameR   t   load_weights_from_hdf5_groupRl   RP  (   R   RE  t   by_nameRR  (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   load_weights	  s    c         C` sN   d d l  m } |  j   } i |  j j d 6| d 6| d 6t j   d 6} | S(   s   Util hared between different serialization methods.

        # Returns
            Model config with Keras version information added.
        i   (   t   __version__R$  R   t   keras_versionR   (   t    R]  R*   R<   R   R?   R   (   R   R^  R   t   model_config(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   _updated_config	  s    c         K` s+   d   } |  j    } t j | d | | S(   sh  Returns a JSON string containing the network configuration.

        To load a network from a JSON save file, use
        `keras.models.model_from_json(json_string, custom_objects={})`.

        # Arguments
            **kwargs: Additional keyword arguments
                to be passed to `json.dumps()`.

        # Returns
            A JSON string.
        c         S` sT   t  |   j t j k r" |  j   St  |   j t  j k rA |  j St d |    d  S(   Ns   Not JSON Serializable:(   R   R   t   npR   t   itemR:   (   t   obj(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   get_json_type	  s
    
t   default(   Ra  R&  R'  (   R   RE   Re  R`  (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   to_json	  s    	c         K` s   t  j |  j   |  S(   s  Returns a yaml string containing the network configuration.

        To load a network from a yaml save file, use
        `keras.models.model_from_yaml(yaml_string, custom_objects={})`.

        `custom_objects` should be a dictionary mapping
        the names of custom losses / layers / etc to the corresponding
        functions / classes.

        # Arguments
            **kwargs: Additional keyword arguments
                to be passed to `yaml.dump()`.

        # Returns
            A YAML string.
        (   t   yamlt   dumpRa  (   R   RE   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   to_yaml	  s    c         C` s   t  |  d | d | d  S(   Nt   line_lengtht	   positions(   t   print_layer_summary(   R   Rk  Rl  (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   summary
  s    	(%   R   R   R   R	   t    legacy_model_constructor_supportR   R   R  R   R   R   R   R  R	  R
  RI   RK   RL   R   Rn   R3   Rg   Ru   Rw   R  R*   R   R   RA   RG  RS  R4   R\  Ra  Rg  Rj  Rn  (    (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR   v  s>   # 9$"						N	SS&!$			c   	      C` s   t  |  d  s |  S| d k s% | r: |  j \ } } } n  | j sJ |  g S| j | } | j sg | j Sg  } x t t | j   D]l } | j | } | j | } | j | } t	 | | |  } x* | D]" } | | k r | j
 |  q q Wq W| Sd S(   s  Returns the list of input tensors necessary to compute `tensor`.

    Output will always be a list of tensors
    (potentially with 1 element).

    # Arguments
        tensor: The tensor to start from.
        layer: Origin layer of the tensor. Will be
            determined via tensor._keras_history if not provided.
        node_index: Origin node index of the tensor.

    # Returns
        List of input tensors.
    R   N(   Rl   R   R   R&   R   R   R   R   R   t   get_source_inputsR%   (	   R   R'   R   R2   R   t   source_tensorsR   R_   t   previous_sources(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRp  	
  s*    			c         C` s   t  |  t  r |  S|  g S(   s   Normalizes a list/tensor into a list.

    If a tensor is passed, we return
    a list of size 1 containing the tensor.

    # Arguments
        x: target object to be normalized.

    # Returns
        A list.
    (   RT   RU   (   R_   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRV   4
  s    c         C` s>   t  |   }  d j g  |  D] } t t t |    ^ q  S(   Ns   , (   RV   R   R>   t   absR   (   t   object_listR_   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR   E
  s    c         C` sL   t  |  t t f  s! |  g } n |  } x | D] } | d  k	 r. t Sq. Wt S(   N(   RT   RU   RB   R   R4   RA   (   t   iterable_or_elementt   iterablet   element(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRp   J
  s    c         C` s   t  |   }  g  } xf |  D]^ } t | d  rj | j \ } } } | j | } | j | } | j |  q | j d  q Wt |  d k r | d S| S(   s   Retrieves the output mask(s) of the previous node.

    # Arguments
        input_tensors: A tensor or list of tensors.

    # Returns
        A mask tensor or list of mask tensors.
    R   i   i    N(   RV   Rl   R   R&   R    R%   R   R   (   R   R   R_   R   R   R   R   Ri   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRo   U
  s    	c         C` sL   t  j d d |   } t  j d d |  j   } | d d k rD | Sd | S(   Ns   (.)([A-Z][a-z0-9]+)s   \1_\2s   ([a-z])([A-Z])i    R2   t   private(   t   ret   subR   (   R(   t   intermediatet   insecure(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyR=   m
  s
    c         C` s|   t  |   }  g  } xI |  D]A } y | j t j |   Wq t k
 rY | j d  q Xq Wt |  d k rx | d S| S(   s   Collects the output shape(s) of a list of Keras tensors.

    # Arguments
        input_tensors: list of input tensors (or single input tensor).

    # Returns
        List of shape tuples (or single tuple), one tuple per input.
    i   i    N(   RV   R%   R?   RZ   R:   R   R   (   R   R#  R_   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRt   w
  s    	c         C` s  d d l  m } g  | D] } | j j d  ^ q |  j d <t j   j d  |  j d <t |  j d  |  j d <x"| D]} |  j | j  } | j	 } t j
 |  } g  } xy t t | |   D]b \ } \ }	 }
 t |	 d  r|	 j rt |	 j  } n d t |  } | j | j d   q W| | j d	 <xX t | |  D]G \ } }
 | j | |
 j d
 |
 j } |
 j s|
 | d <qJ|
 | (qJWq{ Wd  S(   Ni   (   R]  t   utf8RV  R   R^  R(   t   param_t   weight_namesR   (    (   R_  R]  R(   t   encodeRX  R?   R   R>   t   create_groupR0   R   RY   R   Rl   R%   t   create_datasetR   R   (   RR  R   R^  R'   t   gt   symbolic_weightst   weight_valuesR  R   R   t   valR(   t
   param_dset(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRN  
  s*    ,	(	c   	   	   C` s  | d k rX|  j  j d k r | d j } | d  |  j d d f k s[ | d |  j k r | d |  j k r | d |  j d d f k s t  t j | d d  | d <n  | d d d  d d d  d d  f | d <n  |  j  j d k r"|  j d	 k r"t j | d d  | d <q"n  |  j  j d
 k r|  j d k r`t j | d d  | d <n  |  j d	 k rt j | d d  | d <qn  |  j  j d k r|  j d	 k rt j | d d  | d <qn  |  j  j d k rt	 |  d k rt j
 | d | d | d g d d } t j
 | d | d | d g d d } t j
 | d | d | d g d d } | | | g } qn  |  j  j d k rSt	 |  d k rSt j
 | d | d | d | d g d d } t j
 | d | d | d | d g d d } t j
 | d | d | d | d g d d } | | | g } qSn  |  j  j d k rXt	 |  d k rUt j
 | d | d | d | d g d d } t j
 | d | d | d | d g d d } t j
 | d | d | d | d g d d } |  j d	 k r@t j | d   } t j | d!  } n  | | | g } qUqXn  | rt j   | k rd d d d
 g } |  j  j | k rt j | d  | d <n  |  j  j d k rt j | d  | d <t j | d  | d <qn  | S("   s  Converts layers weights from Keras 1 format to Keras 2.

    # Arguments
        layer: Layer instance.
        weights: List of weights values (Numpy arrays).
        original_keras_version: Keras version for the weights, as a string.
        original_backend: Keras backend the weights were trained with,
            as a string.

    # Returns
        A list of weights values (Numpy arrays).
    t   1t   Conv1Di    i   i   i   Nt   Conv2Dt   channels_firstt   Conv2DTransposet   channels_lastt   Conv3Di   t   GRUi	   i   Rb   ii   i   i   t   LSTMi   i
   i   t
   ConvLSTM2D(   i   i   i   i    (   i   i   i   i    (   i    i   i   i   (   i   i   i    i   (   i   i   i   i   i    (   i   i   i   i    (   i   i   i   i    (   R<   R   R   t   kernel_sizet   filtersR   Rb  t	   transposet   data_formatR   t   concatenateR?   R   R   t   convert_kernel(	   R'   R0   t   original_keras_versiont   original_backendR   t   kernelt   recurrent_kernelt   biast   conv_layers(    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   preprocess_weights_for_loading
  s    063   		c         C` s  d |  j  k r( |  j  d j d  } n d } d |  j  k rV |  j  d j d  } n d } g  } x- | D]% } | j } | ri | j |  qi qi Wg  |  j  d D] } | j d  ^ q } g  }	 xW | D]O }
 |  |
 } g  | j  d D] } | j d  ^ q } | r |	 j |
  q q W|	 } t |  t |  k rpt d t t |   d t t |   d	   n  g  } x t |  D]\ } }
 |  |
 } g  | j  d D] } | j d  ^ q} g  | D] } | | ^ q} | | } | j } t	 | | | |  } t |  t |  k rt d
 t |  d | j
 d |
 d | j
 d t t |   d t t |   d   n  | t | |  7} qWt j |  d S(   s  Implements topological (order-based) weight loading.

    # Arguments
        f: A pointer to a HDF5 group.
        layers: a list of target layers.

    # Raises
        ValueError: in case of mismatch between provided layers
            and weights file.
    R^  R}  R  R   RV  R  s0   You are trying to load a weight file containing s    layers into a model with s    layers.s   Layer #s	    (named "s9   " in the current model) was found to correspond to layer s)    in the save file. However the new layer s	    expects s%    weights, but the saved weights have s
    elements.N(   RX  t   decodeR   R0   R%   R   RW   R>   RY   R  R(   R   R?   R   (   RR  R   R  R  t   filtered_layersR'   R0   t   nRV  t   filtered_layer_namesR(   R  R  R   t   kt   weight_nameR  R  (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRZ    sN    	)
)4
)
		Sc         C` s'  d |  j  k r( |  j  d j d  } n d } d |  j  k rV |  j  d j d  } n d } g  |  j  d D] } | j d  ^ qj } i  } x6 | D]. } | j r | j | j g   j |  q q Wg  } xIt |  D];\ }	 }
 |  |
 } g  | j  d D] } | j d  ^ q } g  | D] } | | ^ q} x | j |
 g   D] } | j } t	 | | | |  } t
 |  t
 |  k rt d t |	  d | j d	 t t
 |   d
 d t t
 |   d   n  x5 t t
 |   D]! } | j | | | | f  qWqFWq Wt j |  d S(   sX  Implements name-based weight loading.

    (instead of topological weight loading).

    Layers that have no matching name are skipped.

    # Arguments
        f: A pointer to a HDF5 group.
        layers: a list of target layers.

    # Raises
        ValueError: in case of mismatch between provided layers
            and weights file.
    R^  R}  R  R   RV  R  s   Layer #s	    (named "s   ") expects s!    weight(s), but the saved weightss    have s    element(s).N(   RX  R  R   R(   t
   setdefaultR%   RY   R;   R0   R  R   RW   R>   R   R?   R   (   RR  R   R  R  R  RV  R  R'   R   R  R(   R  R  R  R  R  R   (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyRY  _  s>    )	#
)		D(3   t
   __future__R    R   R   t   numpyRb  R&  Rh  R   R(  RJ  Ry  Rq   t	   six.movesR   R_  R   R?   R   t   utils.io_utilsR   t   utils.layer_utilsR   Rm  t   utilsR   t   legacyR	   RH  RI  R   t   objectR
   R   R+   R   RC   R4   R   R   Rp  RV   R   Rp   Ro   R=   Rt   RN  R  RZ  RY  (    (    (    s4   /tmp/pip-build-isqEY4/keras/keras/engine/topology.pyt   <module>   sb   
%q    aF    +					
		p	D