tf.name_scope returns a new context manager every time it is called with a string as the name parameter, it does not matter if you specify the name that was noticed earlier, in this case the name of the region will simply be made unique by calling unique_name(name)
If you want to re-enter a region with the same name, you need to capture it somewhere and use this region as a parameter for name_scope .
Example taken from ops.py :
# Creates a scope called "nested" with g.name_scope("nested") as scope: nested_c = tf.constant(10.0, name="c") assert nested_c.op.name == "nested/c" # Creates a nested scope called "inner". with g.name_scope("inner"): nested_inner_c = tf.constant(20.0, name="c") assert nested_inner_c.op.name == "nested/inner/c" # Create a nested scope called "inner_1". with g.name_scope("inner"): nested_inner_1_c = tf.constant(30.0, name="c") assert nested_inner_1_c.op.name == "nested/inner_1/c" # Treats `scope` as an absolute name scope, and # switches to the "nested/" scope. with g.name_scope(scope): nested_d = tf.constant(40.0, name="d") assert nested_d.op.name == "nested/d" with g.name_scope(""): e = tf.constant(50.0, name="e") assert e.op.name == "e"
And apparently, you can put a slash ('/') at the end of the name to avoid its unique_name d.
Metaphox
source share