@tf_contextlib.contextmanager
def name_scope(self, name):
"""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 (not ending with '/') will create a new name scope, in which
`name` is appended to the prefix of all operations created in the
context. If `name` has been used before, it will be made unique by
calling `self.unique_name(name)`.
* A scope previously captured from a `with g.name_scope(...) as
scope:` statement will be treated as an "absolute" name scope, which
makes it possible to re-enter existing scopes.
* A value of `None` or the empty string will reset the current name scope
to the top-level (empty) name scope.
For example:
with tf.Graph().as_default() as g:
c = tf.constant(5.0, name="c")
assert c.op.name == "c"
c_1 = tf.constant(6.0, name="c")
assert c_1.op.name == "c_1" """
if name:
if isinstance(name, compat.bytes_or_text_types):
name = compat.as_str(name)
if self._name_stack:
# Scopes created in a nested scope may have initial characters
# that are illegal as the initial character of an op name
# (viz. '-', '\\', '/', and '_').
if not _VALID_SCOPE_NAME_REGEX.match(name):
raise ValueError(
f"'{name}' is not a valid scope name. A scope name has to match "
f"the following pattern: {_VALID_SCOPE_NAME_REGEX.pattern}")
else:
# Scopes created in the root must match the more restrictive
# op name regex, which constrains the initial character.
if not _VALID_OP_NAME_REGEX.match(name):
raise ValueError(
f"'{name}' is not a valid root scope name. A root scope name has "
f"to match the following pattern: {_VALID_OP_NAME_REGEX.pattern}")
old_stack = self._name_stack
if not name: # Both for name=None and name="" we re-set to empty scope.
new_stack = ""
returned_scope = ""
elif name[-1] == "/":
new_stack = name_from_scope_name(name)
returned_scope = name
else:
new_stack = self.unique_name(name)
returned_scope = new_stack + "/"
self._name_stack = new_stack
try:
yield returned_scope
finally:
self._name_stack = old_stack