Return the unique graph used by the all the elements in tops. Args: tops: list of elements to check (usually a list of tf.Operation and/or tf.Tensor). Or a tf.Graph. check_types: check that the element in tops are of given type(s). If None, the types (tf.Operation, tf.Tensor)
(tops, check_types=None, none_if_empty=False)
| 78 | |
| 79 | |
| 80 | def get_unique_graph(tops, check_types=None, none_if_empty=False): |
| 81 | """Return the unique graph used by the all the elements in tops. |
| 82 | |
| 83 | Args: |
| 84 | tops: list of elements to check (usually a list of tf.Operation and/or |
| 85 | tf.Tensor). Or a tf.Graph. |
| 86 | check_types: check that the element in tops are of given type(s). If None, |
| 87 | the types (tf.Operation, tf.Tensor) are used. |
| 88 | none_if_empty: don't raise an error if tops is an empty list, just return |
| 89 | None. |
| 90 | Returns: |
| 91 | The unique graph used by all the tops. |
| 92 | Raises: |
| 93 | TypeError: if tops is not a iterable of tf.Operation. |
| 94 | ValueError: if the graph is not unique. |
| 95 | """ |
| 96 | if isinstance(tops, ops.Graph): |
| 97 | return tops |
| 98 | if not is_iterable(tops): |
| 99 | raise TypeError("{} is not iterable".format(type(tops))) |
| 100 | if check_types is None: |
| 101 | check_types = (ops.Operation, ops.Tensor) |
| 102 | elif not is_iterable(check_types): |
| 103 | check_types = (check_types,) |
| 104 | g = None |
| 105 | for op in tops: |
| 106 | if not isinstance(op, check_types): |
| 107 | raise TypeError("Expected a type in ({}), got: {}".format(", ".join([str( |
| 108 | t) for t in check_types]), type(op))) |
| 109 | if g is None: |
| 110 | g = op.graph |
| 111 | elif g._graph_key != op.graph._graph_key: # pylint: disable=protected-access |
| 112 | raise ValueError("Operation {} does not belong to given graph".format(op)) |
| 113 | if g is None and not none_if_empty: |
| 114 | raise ValueError("Can't find the unique graph of an empty list") |
| 115 | return g |
| 116 | |
| 117 | |
| 118 | def check_graphs(*args): |
no test coverage detected