Why is tf.name_scope with the same name different?

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) 

Output signal

 y/a:0 y_1/b:0 

Why is the scope_name of variable a2 equal to y_1?

+7
tensorflow
source share
2 answers

There is an interesting discussion on this topic on github .

What you can do is add '/' to 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) 

What gives:

 y/a:0 y/b:0 

The same goes for tf.variable_scope () .

At the bottom of your question, probably Tensorflow cannot know if you want to add something to the scope or if someone else created another area somewhere and wants to protect you from inadvertent reuse. By adding '/' at the end, you turn the name into an absolute identifier.

+4
source share

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.

+2
source share

All Articles