Look at the code snippet:
import tensorflow as tf
with tf.name_scope('y'):
a1 = tf.Variable(1,name='a')
with tf.name_scope('y'):
a2 = tf.Variable(1,name='b')
print(a1.name)
print(a2.name)
The output is
y/a:0
y_1/b:0
Why the the name_scope of variable a2 is y_1?
In code above, we have created two tensorflow variables with name ‘ w1 ‘ and ‘ w2 ‘ in the name scope ‘ weights ‘. From result we find: The way of tf.name_scope () to manage tensorflow variables is to add a scope name before the name of variables. For example, we have created a variable w1 with name ‘ w1 ‘ in name scope ‘ weights ‘.
So as the name suggests, the scope functions create a scope for the names of the ops you create inside. This has an effect on how you refer to tensors, on reuse, on how the graph shows in TensorBoard and so on. Thank you for the reply.
They are not the same thing. import tensorflow as tf c1 = tf.constant (42) with tf.name_scope ('s1'): c2 = tf.constant (42) print (c1.name) print (c2.name) So as the name suggests, the scope functions create a scope for the names of the ops you create inside.
think about this as a drawer. Compared with name space, this is of more "physical" meaning, because such drawer truly exists; in the contrary, name space just helps understand which variables are included.
On github there is an interesting discussion regarding this topic.
What you could do it append a '/' at the end, making it an absolute identifier:
import tensorflow as tf
with tf.name_scope('y'):
a1 = tf.Variable(1,name='a')
with tf.name_scope('y/'):
a2 = tf.Variable(1,name='b')
print(a1.name)
print(a2.name)
Which yields:
y/a:0
y/b:0
The same also applies to tf.variable_scope().
The bottomline to your question is likely that Tensorflow can't know whether you explicitely want to append something to the scope or whether somewhere else somebody created a different scope and wants to protect you against unintentional re-use. By appending a '/' at the end you turn the name into an absolute identifier.
The tf.name_scope
returns a new context manager everytime when it's called with a string as the name
parameter, it doesn't matter if you supply a name that has been seen before, in that case, the name of the scope will simply be made unique by calling unique_name(name)
.
If you want to re-entry the same name scope, you have to capture it somewhere, and use that scope as the 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 it to be unique_name
d.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With