tensorflow: Inconsistent behavior for tf.variable_scope
Problem:
Tensorflow doesn’t place ops (e.g. mul) in pre-existing variable scopes (and automatically creates a new scope instead).
Minimal Reproducible Example
with tf.variable_scope('layer123'):
v = tf.get_variable('v', [], initializer=tf.constant_initializer(42., tf.float32))
w = v * 2
print(w.name) # Prints 'layer123/mul:0'
However,
with tf.variable_scope('layer123'):
v = tf.get_variable('v', [], initializer=tf.constant_initializer(42., tf.float32))
with tf.variable_scope('layer123'):
w = v * 2
print(w.name) # Prints 'layer123_1/mul:0'
Observe that for the latter, the op w is placed in a different variable scope, auto-named layer123_1.
I’ve tried the following, to the same effect:
with tf.variable_scope('layer123') as scope:
v = tf.get_variable('v', [], initializer=tf.constant_initializer(42., tf.float32))
with tf.variable_scope(scope):
w = v * 2
print(w.name) # Prints 'layer123_1/mul:0'
with tf.variable_scope('layer123'):
v = tf.get_variable('v', [], initializer=tf.constant_initializer(42., tf.float32))
with tf.variable_scope('layer123', reuse=True):
w = v * 2
print(w.name) # Prints 'layer123_1/mul:0'
VersionSpec
Tensorflow version: 0.11.0 (GPU) OS: Ubuntu 14.04 (w/ CUDA 8)
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 10
- Comments: 17 (7 by maintainers)
@eamartin My bad earlier. I just found you can do something like this: (from #6007)
Maybe this will help?
@eamartin Yes that’s a bit tricky. I hope tensorflow can change the default behavior, i.e. when you reenter a variable scope, you should expect to automatically reenter its name scope as well instead of creating a new one.