1

I would like to use tf.name_scope for something like this:

my_scope = tf.name_scope('something')

if cond_1:
    foo(my_scope)

if cond_2:
    bar(my_scope)

I would like to avoid using the with tf.name_scope('something') as scope notation, as I don't know when the first time I want to use the name scope something. The simple my_scope = tf.name_scope('something') doesn't work, resulting in the error TypeError: expected string or buffer.

Right now I use:

with tf.name_scope('something') as scope:
    pass
my_scope = scope

if cond_1:
    foo(my_scope)

if cond_2:
    bar(my_scope)

which works, but is very unsatisfying.

Maxim
  • 52,561
  • 27
  • 155
  • 209
Toke Faurby
  • 5,788
  • 9
  • 41
  • 62

1 Answers1

1

I am not sure what foo and bar are supposed to do, but two my_scope in the provided above snippets are completely different.

The first my_scope is a context manager, namely contextlib.GeneratorContextManager instance. The second my_scope (just as scope) is an ordinary string, namely "something/" (see the source code). You can achieve exactly the same effect with just my_scope="something/". Based on the error TypeError: expected string or buffer, that's exactly what foo and bar expect - a string.

Also note that you can get a reference on a context and enter it whenever you like. tf.name_scope simply pushes the name on top of the stack and then pops it back on exit. But if you enter the same name scope multiple times, you'll get a suffix: _1, _2, ...

Maxim
  • 52,561
  • 27
  • 155
  • 209