ó
V¾÷Xc           @` sß   d  Z  d d l m Z d d l m Z d d l m 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 d g Z d „  Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d S(   s7   SubGraphView: a subgraph view on an existing tf.Graph.
i    (   t   absolute_import(   t   division(   t   print_functionN(   t	   iteritems(   t   StringIO(   t   select(   t   util(   t   opst   SubGraphViewt	   make_viewt   make_view_from_scopec         C` s‰   xE |  D]= } d | k o$ | k  n s t  d j | | ƒ ƒ ‚ q q W| r… t t |  ƒ ƒ t |  ƒ k r… t  d j |  ƒ ƒ ‚ n  d S(   s	  Check is the mapping is valid.

  Args:
    mapping: an iterable of integer.
    n: define the input domain as [0, n-1]. Note that the mapping can be
      under-complete, that is, it can only contain a subset of the integers on
      [0, n-1].
    repetition: if True repetition are allowed (the function is surjective)
      otherwise repetition are not allowed (the function is injective).
  Raises:
    ValueError: if the mapping is out of range ot if repetition is False and
      the mapping has some repetition.
  i    s   Out of [0, {}[ range: {}s   Found repetition in mapping: {}N(   t
   ValueErrort   formatt   lent   set(   t   mappingt   nt
   repetitiont   i(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   _check_within_range&   s
    %c           B` sŽ  e  Z d  Z d# d$ d „ Z d „  Z d „  Z d „  Z e e d „ Z e e d „ Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z e d „ Z e d „ Z d „  Z d „  Z d% d% d „ Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d „  Z  e  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d  „  Z& d! „  Z' d" „  Z( RS(&   sÙ  A subgraph view on an existing `tf.Graph`.

  An instance of this class is a subgraph view on an existing `tf.Graph`.
  "subgraph" means that it can represent part of the whole `tf.Graph`.
  "view" means that it only provides a passive observation and do not to act
  on the `tf.Graph`. Note that in this documentation, the term "subgraph" is
  often used as substitute to "subgraph view".

  A subgraph contains:

  * a list of input tensors, accessible via the `inputs` property.
  * a list of output tensors, accessible via the `outputs` property.
  * and the operations in between, accessible via the "ops" property.

  An subgraph can be seen as a function F(i0, i1, ...) -> o0, o1, ... It is a
  function which takes as input some input tensors and returns as output some
  output tensors. The computation that the function performs is encoded in the
  operations of the subgraph.

  The tensors (input or output) can be of two kinds:

  - connected: a connected tensor connects to at least one operation contained
  in the subgraph. One example is a subgraph representing a single operation
  and its inputs and outputs: all the input and output tensors of the op
  are "connected".
  - passthrough: a passthrough tensor does not connect to any operation
  contained in the subgraph. One example is a subgraph representing a
  single tensor: this tensor is passthrough. By default a passthrough tensor is
  present both in the input and output tensors of the subgraph. It can however
  be remapped to only appear as an input (or output) only.

  The input and output tensors can be remapped. For instance, some input tensor
  can be omitted. For instance, a subgraph representing an operation with two
  inputs can be remapped to only take one input. Note that this does not change
  at all the underlying `tf.Graph` (remember, it is a view). It means that
  the other input is being ignored, or is being treated as "given".
  The analogy with functions can be extended like this: F(x,y) is the original
  function. Remapping the inputs from [x, y] to just [x] means that the subgraph
  now represent the function F_y(x) (y is "given").

  The output tensors can also be remapped. For instance, some output tensor can
  be omitted. Other output tensor can be duplicated as well. As mentioned
  before, this does not change at all the underlying `tf.Graph`.
  The analogy with functions can be extended like this: F(...)->x,y is the
  original function. Remapping the outputs from [x, y] to just [y,y] means that
  the subgraph now represent the function M(F(...)) where M is the function
  M(a,b)->b,b.

  It is useful to describe three other kind of tensors:

  * internal: an internal tensor is a tensor connecting operations contained
    in the subgraph. One example in the subgraph representing the two
    operations A and B connected sequentially: -> A -> B ->. The middle arrow
    is an internal tensor.
  * actual input: an input tensor of the subgraph, regardless of whether it is
    listed in "inputs" or not (masked-out).
  * actual output: an output tensor of the subgraph, regardless of whether it is
    listed in "outputs" or not (masked-out).
  * hidden input: an actual input which has been masked-out using an
    input remapping. In other word, a hidden input is a non-internal tensor
    not listed as a input tensor and one of whose consumers belongs to
    the subgraph.
  * hidden output: a actual output which has been masked-out using an output
    remapping. In other word, a hidden output is a non-internal tensor
    not listed as an output and one of whose generating operations belongs to
    the subgraph.

  Here are some useful guarantees about an instance of a SubGraphView:

  * the input (or output) tensors are not internal.
  * the input (or output) tensors are either "connected" or "passthrough".
  * the passthrough tensors are not connected to any of the operation of
  the subgraph.

  Note that there is no guarantee that an operation in a subgraph contributes
  at all to its inputs or outputs. For instance, remapping both the inputs and
  outputs to empty lists will produce a subgraph which still contains all the
  original operations. However, the remove_unused_ops function can be used to
  make a new subgraph view whose operations are connected to at least one of
  the input or output tensors.

  An instance of this class is meant to be a lightweight object which is not
  modified in-place by the user. Rather, the user can create new modified
  instances of a given subgraph. In that sense, the class SubGraphView is meant
  to be used like an immutable python object.

  A common problem when using views is that they can get out-of-sync with the
  data they observe (in this case, a `tf.Graph`). This is up to the user to
  ensure that this doesn't happen. To keep on the safe side, it is recommended
  that the life time of subgraph views are kept very short. One way to achieve
  this is to use subgraphs within a "with make_sgv(...) as sgv:" Python context.

  To alleviate the out-of-sync problem, some functions are granted the right to
  modified subgraph in place. This is typically the case of graph manipulation
  functions which, given some subgraphs as arguments, can modify the underlying
  `tf.Graph`. Since this modification is likely to render the subgraph view
  invalid, those functions can modify the argument in place to reflect the
  change. For instance, calling the function swap_inputs(svg0, svg1) will modify
  svg0 and svg1 in place to reflect the fact that their inputs have now being
  swapped.
  c   	      C` s÷   t  j | ƒ } t  j | ƒ } | | } | rÆ t  j | ƒ |  _ | |  _ t j | ƒ \ } } } t | | t	 | ƒ ƒ } g  | D] } | | k r‚ | ^ q‚ |  _
 | |  j
 |  _ | |  j
 |  _ n- d |  _ g  |  _
 g  |  _ g  |  _ g  |  _ d S(   s×  Create a subgraph containing the given ops and the "passthrough" tensors.

    Args:
      inside_ops: an object convertible to a list of `tf.Operation`. This list
        defines all the operations in the subgraph.
      passthrough_ts: an object convertible to a list of `tf.Tensor`. This list
        define all the "passthrough" tensors. A passthrough tensor is a tensor
        which goes directly from the input of the subgraph to it output, without
        any intermediate operations. All the non passthrough tensors are
        silently ignored.
    Raises:
      TypeError: if inside_ops cannot be converted to a list of `tf.Operation`
        or if `passthrough_ts` cannot be converted to a list of `tf.Tensor`.
    N(   R   t   make_list_of_opt   make_list_of_tt   get_unique_grapht   _grapht   _opsR   t   compute_boundary_tst	   frozensett   listt   _passthrough_tst	   _input_tst
   _output_tst   None(	   t   selft
   inside_opst   passthrough_tst
   ops_and_tst   inputst   outputst   insidest   all_tensorst   t(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   __init__¢   s     
	(				c         C` sq   |  j  } | j | ƒ } xR t |  j ƒ D]A \ } } | d k rS t | | | ƒ q( t | | t | ƒ ƒ q( W| S(   sø   Create a copy of this subgraph.

    Note that this class is a "view", copying it only create another view and
    does not copy the underlying part of the `tf.Graph`.

    Returns:
      A new identical instance of the original subgraph view.
    R   (   t	   __class__t   __new__R   t   __dict__t   setattrR   (   R    t   clst   resultt   kt   v(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   __copy__Ê   s    		c         C` s…   t  | t ƒ s- t d j t | ƒ ƒ ƒ ‚ n  | j |  _ t | j ƒ |  _ t | j ƒ |  _ t | j	 ƒ |  _	 t | j
 ƒ |  _
 d S(   sÊ   Assign other to itself.

    Args:
      other: another subgraph-view.
    Returns:
      A new instance identical to the original one.
    Raises:
      TypeError: if other is not an SubGraphView.
    s   Expected SubGraphView, got: {}N(   t
   isinstanceR   t	   TypeErrorR   t   typeR   R   R   R   R   R   (   R    t   other(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   _assign_fromÜ   s    
c         C` s   t  j  |  ƒ S(   så   Return a copy of itself.

    Note that this class is a "view", copying it only create another view and
    does not copy the underlying part of the tf.Graph.

    Returns:
      A new instance identical to the original one.
    (   t   copy(   R    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyR8   ð   s    	c         C` so   | r | r d St  j |  j ƒ \ } } } | rL t | ƒ |  j |  _ n  | rk t | ƒ |  j |  _ n  d S(   sì   Remap in the place the inputs and/or outputs to the default mapping.

    Args:
      remove_input_map: if True the input map is reset to the default one.
      remove_output_map: if True the output map is reset to the default one.
    N(   R   R   R   R   R   R   R   (   R    t   remove_input_mapt   remove_output_mapR$   R%   t   _(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   _remap_defaultû   s    c         C` s    |  j  ƒ  } | j | | ƒ | S(   so  Remap the inputs and/or outputs to the default mapping.

    Args:
      remove_input_map: if True the input map is reset to the default one.
      remove_output_map: if True the output map is reset to the default one.
    Returns:
      A new modified instance of the original subgraph view with its
        input and/or output mapping reset to the default one.
    (   R8   R<   (   R    R9   R:   t   res(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   remap_default  s    
c         C` sC   t  | t |  j ƒ d t ƒg  | D] } |  j | ^ q# |  _ d S(   s*   Remap the inputs of the subgraph in-place.R   N(   R   R   R   t   False(   R    t   new_input_indicesR   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   _remap_inputs  s    c         C` sC   t  | t |  j ƒ d t ƒg  | D] } |  j | ^ q# |  _ d S(   s+   Remap the outputs of the subgraph in-place.R   N(   R   R   R   t   True(   R    t   new_output_indicesR   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   _remap_outputs   s    c         C` s/   t  |  j ƒ } g  |  _ t j |  j | ƒ d S(   sE   Remap the outputs in place so that all the tensors appears only once.N(   R   R   R   t   concatenate_unique(   R    t	   output_ts(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   _remap_outputs_make_unique&  s    	c         C` sY   |  j  ƒ  t |  j ƒ } g  |  _ x0 | D]( } |  j | g t | j ƒ  ƒ 7_ q) Wd S(   s<   Remap the outputs in place to match the number of consumers.N(   RG   R   R   R   t	   consumers(   R    RF   R(   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   _remap_outputs_to_consumers,  s
    
	c         C` s   t  j  |  ƒ } | j ƒ  | S(   s<   Remap the outputs so that all the tensors appears only once.(   R8   RG   (   R    R=   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   remap_outputs_make_unique4  s    
c         C` s   t  j  |  ƒ } | j ƒ  | S(   s3   Remap the outputs to match the number of consumers.(   R8   RI   (   R    R=   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   remap_outputs_to_consumers:  s    
c         C` sV   t  j |  j |  j d |  j d | ƒ} g  |  j D] } | | k r1 | ^ q1 |  _ d S(   sÂ   Remove unused ops in place.

    Args:
      control_inputs: if True, control inputs are used to detect used ops.
    Returns:
      A new subgraph view which only contains used operations.
    t
   within_opst   control_inputsN(   R   t   get_walks_union_opst   connected_inputst   connected_outputsR   (   R    RM   R   t   op(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   _remove_unused_ops@  s    			c         C` s    t  j  |  ƒ } | j | ƒ | S(   s¹   Remove unused ops.

    Args:
      control_inputs: if True, control inputs are used to detect used ops.
    Returns:
      A new subgraph view which only contains used operations.
    (   R8   RR   (   R    RM   R=   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   remove_unused_opsO  s    c         C` s   |  j  ƒ  } | j | ƒ | S(   sF  Remap the inputs of the subgraph.

    If the inputs of the original subgraph are [t0, t1, t2], remapping to [2,0]
    will create a new instance whose inputs is [t2, t0].

    Note that this is only modifying the view: the underlying `tf.Graph` is not
    affected.

    Args:
      new_input_indices: an iterable of integers representing a mapping between
        the old inputs and the new ones. This mapping can be under-complete and
        must be without repetitions.
    Returns:
      A new modified instance of the original subgraph view with remapped
        inputs.
    (   R8   RA   (   R    R@   R=   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   remap_inputs[  s    c         C` s    t  j  |  ƒ } | j | ƒ | S(   sG  Remap the output of the subgraph.

    If the output of the original subgraph are [t0, t1, t2], remapping to
    [1,1,0] will create a new instance whose outputs is [t1, t1, t0].

    Note that this is only modifying the view: the underlying tf.Graph is not
    affected.

    Args:
      new_output_indices: an iterable of integers representing a mapping between
        the old outputs and the new ones. This mapping can be under-complete and
        can have repetitions.
    Returns:
      A new modified instance of the original subgraph view with remapped
        outputs.
    (   R8   RD   (   R    RC   R=   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   remap_outputsp  s    c         C` sK   t  j  |  ƒ } | d k	 r+ | j | ƒ n  | d k	 rG | j | ƒ n  | S(   s’  Remap the inputs and outputs of the subgraph.

    Note that this is only modifying the view: the underlying tf.Graph is not
    affected.

    Args:
      new_input_indices: an iterable of integers representing a mapping between
        the old inputs and the new ones. This mapping can be under-complete and
        must be without repetitions.
      new_output_indices: an iterable of integers representing a mapping between
        the old outputs and the new ones. This mapping can be under-complete and
        can have repetitions.
    Returns:
      A new modified instance of the original subgraph view with remapped
        inputs and outputs.
    N(   R8   R   RA   RD   (   R    R@   RC   R=   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   remap…  s    c         C` s{   g  |  j  D] } | j | k r
 | ^ q
 } | sI t d j | ƒ ƒ ‚ n  t | ƒ d k rs t d j | ƒ ƒ ‚ n  | d S(   s÷   Return the op named op_name.

    Args:
      op_name: the name to search for
    Returns:
      The op named op_name.
    Raises:
      ValueError: if the op_name could not be found.
      AssertionError: if the name was found multiple time.
    s   {} not in subgraph.i   s   More than 1 op named: {}!i    (   R   t   nameR   R   R   t   AssertionError(   R    t   op_nameRQ   R=   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   find_op_by_name  s    +c         ` sŸ   ˆ s
 d Sd „  } ‡ f d †  } ‡  f d †  } t  ƒ  ‰  t d j t ˆ j ƒ ƒ d ˆ  ƒ| d ˆ j | ƒ | d ˆ j | ƒ | d	 ˆ j | ƒ ˆ  j ƒ  S(
   Ns   SubGraphView: emptyc         S` s   |  j  S(   N(   RW   (   RQ   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyRY   ³  s    c         ` s*   |  ˆ  j  k r d j |  j ƒ S|  j Sd  S(   Ns   {} *(   R   R   RW   (   R(   (   R    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   tensor_name¶  s    c         ` sƒ   | rf t  d j |  t | ƒ ƒ d ˆ  ƒt  d j g  | D] } d j | | ƒ ƒ ^ q8 ƒ d ˆ  ƒn t  d j |  ƒ d ˆ  ƒd  S(   Ns
   ** {}[{}]:t   files   
s     {}s   ** {}: empty(   t   printR   R   t   join(   RW   t   iterablet   get_namet   elem(   R=   (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt
   print_list¼  s
    "4
s   SubGraphView (graphid={}):R\   R   R$   R%   (	   R   R]   R   t   idt   graphR   R   R   t   getvalue(   R    RY   R[   Rb   (    (   R=   R    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   __str__¯  s    		"c         C` s   |  j  S(   s   The underlying `tf.Graph`.(   R   (   R    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyRd   Ë  s    c         C` s   |  j  S(   s%   The operations in this subgraph view.(   R   (   R    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyR   Ð  s    c         C` s   t  j |  j ƒ S(   s(   The input tensors of this subgraph view.(   R   t   ListViewR   (   R    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyR$   Õ  s    c         C` s)   g  |  j  D] } | |  j k r
 | ^ q
 S(   s2   The connected input tensors of this subgraph view.(   R   R   (   R    R(   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyRO   Ú  s    c         C` s   t  j |  j ƒ S(   s)   The output tensors of this subgraph view.(   R   Rg   R   (   R    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyR%   ß  s    c         C` s)   g  |  j  D] } | |  j k r
 | ^ q
 S(   s3   The connected output tensors of this subgraph view.(   R   R   (   R    R(   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyRP   ä  s    c         C` s   t  j |  j ƒ S(   s=   The passthrough tensors, going straight from input to output.(   R   Rg   R   (   R    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   passthroughsé  s    c         C` s   |  j  d k	 S(   s'   Allows for implicit boolean conversion.N(   R   R   (   R    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   __bool__î  s    c         C` s   |  j  | S(   s   Get an op by its index.(   R   (   R    t   op_id(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyRQ   õ  s    c         C` s   | |  j  k S(   s&   Check whether a tensor is passthrough.(   R   (   R    R(   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   is_passthroughù  s    c         C` s   |  S(   sÇ  Allow Python context to minimize the life time of a subgraph view.

    A subgraph view is meant to be a lightweight and transient object. A short
    lifetime will alleviate the "out-of-sync" issue mentioned earlier. For that
    reason, a SubGraphView instance can be used within a Python context. For
    example:

    from tensorflow.contrib import graph_editor as ge
    with ge.make_sgv(...) as sgv:
      print(sgv)

    Returns:
      Itself.
    (    (   R    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt	   __enter__ý  s    c         C` s   d  S(   N(    (   R    t   exc_typet	   exc_valuet	   traceback(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   __exit__  s    c         C` sB   y |  j  j | ƒ } Wn% t d j | j |  j ƒ ƒ ‚ n X| S(   sì   Find the input index corresponding to the given input tensor t.

    Args:
      t: the input tensor of this subgraph view.
    Returns:
      The index in the self.inputs list.
    Raises:
      Error: if t in not an input tensor.
    s'   Can't find {} in inputs of subgraph {}.(   R   t   indexR   R   RW   (   R    R(   t   subgraph_id(    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   input_index  s    
	c         C` sB   y |  j  j | ƒ } Wn% t d j | j |  j ƒ ƒ ‚ n X| S(   sí   Find the output index corresponding to given output tensor t.

    Args:
      t: the output tensor of this subgraph view.
    Returns:
      The index in the self.outputs list.
    Raises:
      Error: if t in not an output tensor.
    s(   Can't find {} in outputs of subgraph {}.(   R   Rq   R   R   RW   (   R    R(   Rr   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   output_index"  s    
	c         C` sh   t  |  j ƒ } g  } xL |  j D]A } g  | j ƒ  D] } | | k r2 | ^ q2 } t j | | ƒ q W| S(   s&  Return a Python set of all the consumers of this subgraph view.

    A consumer of a subgraph view is a tf.Operation which is a consumer
    of one of the output tensors and is not in the subgraph.

    Returns:
      A list of `tf.Operation` which are the consumers of this subgraph view.
    (   R   R   R   RH   R   RE   (   R    t   ops_setR=   t   outputRQ   RH   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyRH   3  s    	+(    (    N()   t   __name__t
   __module__t   __doc__R)   R2   R7   R8   RB   R<   R>   RA   RD   RG   RI   RJ   RK   RR   RS   RT   RU   R   RV   RZ   Rf   t   propertyRd   R   R$   RO   R%   RP   Rh   Ri   t   __nonzero__RQ   Rk   Rl   Rp   Rs   Rt   RH   (    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyR   ;   sH   e(																				c         C` s™   t  |  t ƒ s- t d j t | ƒ ƒ ƒ ‚ n  | d k sC |  j rG |  St  | t j ƒ sw t d j t | ƒ ƒ ƒ ‚ n  |  j | k	 r• t	 d ƒ ‚ n  |  S(   sP  Check if sgv belongs to the given graph.

  Args:
    sgv: a SubGraphView.
    graph: a graph or None.
  Returns:
    The SubGraphView sgv.
  Raises:
    TypeError: if sgv is not a SubGraphView or if graph is not None and not
      a tf.Graph.
    ValueError: if the graph of sgv and the given graph are not None and
      different.
  s    Expected a SubGraphView, got: {}s   Expected a tf.Graph, got: {}s   Graph mismatch.N(
   R3   R   R4   R   R5   R   Rd   t   tf_opst   GraphR   (   t   sgvRd   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   _check_graphD  s    c          O` s†   d | k r | d n d } t |  ƒ d k rR t |  d t ƒ rR t |  d | ƒ St j |  | Ž  \ } } t | | ƒ } t | | ƒ S(   s  Create a SubGraphView from selected operations and passthrough tensors.

  Args:
    *args: list of 1) regular expressions (compiled or not) or  2) (array of)
      `tf.Operation` 3) (array of) `tf.Tensor`. Those objects will be converted
      into a list of operations and a list of candidate for passthrough tensors.
    **kwargs: keyword graph is used 1) to check that the ops and ts are from
      the correct graph 2) for regular expression query
  Returns:
    A subgraph view.
  Raises:
    TypeError: if the optional keyword argument graph is not a `tf.Graph`
      or if an argument in args is not an (array of) `tf.Tensor`
      or an (array of) `tf.Operation` or a string or a regular expression.
    ValueError: if one of the keyword arguments is unexpected.
  Rd   i   i    N(   R   R   R3   R   R   R   t   select_ops_and_ts(   t   argst   kwargsRd   R   t   tsR~   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyR	   ]  s    %c         C` s   t  j | |  ƒ } t | ƒ S(   s¨   Make a subgraph from a name scope.

  Args:
    scope: the name of the scope.
    graph: the `tf.Graph`.
  Returns:
    A subgraph view representing the given scope.
  (   R   t   get_name_scope_opsR   (   t   scopeRd   R   (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyR
   z  s    	(   Ry   t
   __future__R    R   R   R8   t   sixR   R   t   tensorflow.contrib.graph_editorR   R   t   tensorflow.python.frameworkR   R|   t   __all__R   t   objectR   R   R	   R
   (    (    (    sj   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/subgraph.pyt   <module>   s&   		ÿ ÿ 		