Returns the appropriate graph to use for the given inputs. This library method provides a consistent algorithm for choosing the graph in which an Operation should be constructed: 1. If the default graph is being used to construct a function, we use the default graph. 2. If the "graph"
(op_input_list, graph=None)
| 5995 | |
| 5996 | |
| 5997 | def _get_graph_from_inputs(op_input_list, graph=None): |
| 5998 | """Returns the appropriate graph to use for the given inputs. |
| 5999 | |
| 6000 | This library method provides a consistent algorithm for choosing the graph |
| 6001 | in which an Operation should be constructed: |
| 6002 | |
| 6003 | 1. If the default graph is being used to construct a function, we |
| 6004 | use the default graph. |
| 6005 | 2. If the "graph" is specified explicitly, we validate that all of the inputs |
| 6006 | in "op_input_list" are compatible with that graph. |
| 6007 | 3. Otherwise, we attempt to select a graph from the first Operation- |
| 6008 | or Tensor-valued input in "op_input_list", and validate that all other |
| 6009 | such inputs are in the same graph. |
| 6010 | 4. If the graph was not specified and it could not be inferred from |
| 6011 | "op_input_list", we attempt to use the default graph. |
| 6012 | |
| 6013 | Args: |
| 6014 | op_input_list: A list of inputs to an operation, which may include `Tensor`, |
| 6015 | `Operation`, and other objects that may be converted to a graph element. |
| 6016 | graph: (Optional) The explicit graph to use. |
| 6017 | |
| 6018 | Raises: |
| 6019 | TypeError: If op_input_list is not a list or tuple, or if graph is not a |
| 6020 | Graph. |
| 6021 | ValueError: If a graph is explicitly passed and not all inputs are from it, |
| 6022 | or if the inputs are from multiple graphs, or we could not find a graph |
| 6023 | and there was no default graph. |
| 6024 | |
| 6025 | Returns: |
| 6026 | The appropriate graph to use for the given inputs. |
| 6027 | |
| 6028 | """ |
| 6029 | current_default_graph = get_default_graph() |
| 6030 | if current_default_graph.building_function: |
| 6031 | return current_default_graph |
| 6032 | |
| 6033 | op_input_list = tuple(op_input_list) # Handle generators correctly |
| 6034 | if graph and not isinstance(graph, Graph): |
| 6035 | raise TypeError("Input graph needs to be a Graph: %s" % graph) |
| 6036 | |
| 6037 | # 1. We validate that all of the inputs are from the same graph. This is |
| 6038 | # either the supplied graph parameter, or the first one selected from one |
| 6039 | # the graph-element-valued inputs. In the latter case, we hold onto |
| 6040 | # that input in original_graph_element so we can provide a more |
| 6041 | # informative error if a mismatch is found. |
| 6042 | original_graph_element = None |
| 6043 | for op_input in op_input_list: |
| 6044 | # Determine if this is a valid graph_element. |
| 6045 | # TODO(josh11b): Note that we exclude subclasses of Tensor. Need to clean this |
| 6046 | # up. |
| 6047 | graph_element = None |
| 6048 | if (isinstance(op_input, (Operation, _TensorLike)) and |
| 6049 | ((not isinstance(op_input, Tensor)) or type(op_input) == Tensor)): # pylint: disable=unidiomatic-typecheck |
| 6050 | graph_element = op_input |
| 6051 | else: |
| 6052 | graph_element = _as_graph_element(op_input) |
| 6053 | |
| 6054 | if graph_element is not None: |
no test coverage detected