• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15
16"""Classes and functions used to construct graphs."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21from tensorflow.python.framework import ops
22
23
24__all__ = ['get_graph_from_inputs',
25           'get_name_scope']
26
27
28def get_graph_from_inputs(op_input_list, graph=None):
29  """Returns the appropriate graph to use for the given inputs.
30
31  1. If `graph` is provided, we validate that all inputs in `op_input_list` are
32     from the same graph.
33  2. Otherwise, we attempt to select a graph from the first Operation- or
34     Tensor-valued input in `op_input_list`, and validate that all other
35     such inputs are in the same graph.
36  3. If the graph was not specified and it could not be inferred from
37     `op_input_list`, we attempt to use the default graph.
38
39  Args:
40    op_input_list: A list of inputs to an operation, which may include `Tensor`,
41      `Operation`, and other objects that may be converted to a graph element.
42    graph: (Optional) The explicit graph to use.
43
44  Raises:
45    TypeError: If `op_input_list` is not a list or tuple, or if graph is not a
46      Graph.
47    ValueError: If a graph is explicitly passed and not all inputs are from it,
48      or if the inputs are from multiple graphs, or we could not find a graph
49      and there was no default graph.
50
51  Returns:
52    The appropriate graph to use for the given inputs.
53  """
54  # pylint: disable=protected-access
55  return ops._get_graph_from_inputs(op_input_list, graph)
56
57
58def get_name_scope():
59  """Returns the current name scope of the default graph.
60
61  For example:
62
63    ```python
64    with tf.name_scope('scope1'):
65      with tf.name_scope('scope2'):
66        print(tf.contrib.framework.get_name_scope())
67    ```
68    would print the string `scope1/scope2`.
69
70  Returns:
71    A string representing the current name scope.
72  """
73  return ops.get_default_graph().get_name_scope()
74