Returns a context manager that creates hierarchical names for operations. A graph maintains a stack of name scopes. A `with name_scope(...):` statement pushes a new name onto the stack for the lifetime of the context. The `name` argument will be interpreted as follows: * A string
(self, name)
| 4049 | # pylint: disable=g-doc-return-or-yield,line-too-long |
| 4050 | @tf_contextlib.contextmanager |
| 4051 | def name_scope(self, name): |
| 4052 | """Returns a context manager that creates hierarchical names for operations. |
| 4053 | |
| 4054 | A graph maintains a stack of name scopes. A `with name_scope(...):` |
| 4055 | statement pushes a new name onto the stack for the lifetime of the context. |
| 4056 | |
| 4057 | The `name` argument will be interpreted as follows: |
| 4058 | |
| 4059 | * A string (not ending with '/') will create a new name scope, in which |
| 4060 | `name` is appended to the prefix of all operations created in the |
| 4061 | context. If `name` has been used before, it will be made unique by |
| 4062 | calling `self.unique_name(name)`. |
| 4063 | * A scope previously captured from a `with g.name_scope(...) as |
| 4064 | scope:` statement will be treated as an "absolute" name scope, which |
| 4065 | makes it possible to re-enter existing scopes. |
| 4066 | * A value of `None` or the empty string will reset the current name scope |
| 4067 | to the top-level (empty) name scope. |
| 4068 | |
| 4069 | For example: |
| 4070 | |
| 4071 | ```python |
| 4072 | with tf.Graph().as_default() as g: |
| 4073 | c = tf.constant(5.0, name="c") |
| 4074 | assert c.op.name == "c" |
| 4075 | c_1 = tf.constant(6.0, name="c") |
| 4076 | assert c_1.op.name == "c_1" |
| 4077 | |
| 4078 | # Creates a scope called "nested" |
| 4079 | with g.name_scope("nested") as scope: |
| 4080 | nested_c = tf.constant(10.0, name="c") |
| 4081 | assert nested_c.op.name == "nested/c" |
| 4082 | |
| 4083 | # Creates a nested scope called "inner". |
| 4084 | with g.name_scope("inner"): |
| 4085 | nested_inner_c = tf.constant(20.0, name="c") |
| 4086 | assert nested_inner_c.op.name == "nested/inner/c" |
| 4087 | |
| 4088 | # Create a nested scope called "inner_1". |
| 4089 | with g.name_scope("inner"): |
| 4090 | nested_inner_1_c = tf.constant(30.0, name="c") |
| 4091 | assert nested_inner_1_c.op.name == "nested/inner_1/c" |
| 4092 | |
| 4093 | # Treats `scope` as an absolute name scope, and |
| 4094 | # switches to the "nested/" scope. |
| 4095 | with g.name_scope(scope): |
| 4096 | nested_d = tf.constant(40.0, name="d") |
| 4097 | assert nested_d.op.name == "nested/d" |
| 4098 | |
| 4099 | with g.name_scope(""): |
| 4100 | e = tf.constant(50.0, name="e") |
| 4101 | assert e.op.name == "e" |
| 4102 | ``` |
| 4103 | |
| 4104 | The name of the scope itself can be captured by `with |
| 4105 | g.name_scope(...) as scope:`, which stores the name of the scope |
| 4106 | in the variable `scope`. This value can be used to name an |
| 4107 | operation that represents the overall result of executing the ops |
| 4108 | in a scope. For example: |