I have a question regarding sub-scopes when reusing variables. This
import tensorflow as tf
def make_bar():
with tf.variable_scope('bar'):
tf.get_variable('baz', ())
with tf.variable_scope('foo') as scope:
make_bar()
scope.reuse_variables()
make_bar()
works perfectly fine, only a single variable foo/bar/baz is created.
However, if I change make_bar to
def make_bar(scope=None):
with tf.variable_scope(scope, 'bar'):
tf.get_variable('baz', ())
the code now fails with a
ValueError: Variable foo/bar_1/baz does not exist
Question: why does variable scope reuse fail when using default names? If it is on purpose, what is the rationale behind this choice?
EDIT
Some precisions on the default_name argument of tf.variable_scope. From the documentation,
default_name: The default name to use if thename_or_scopeargument isNone, this name will be uniquified. Ifname_or_scopeis provided it won't be used and therefore it is not required and can beNone.
So as it name underlies, it is a way to provide a default scope name.
In the first version of make_bar, the scope name is forced to be bar -- the function has no parameter to change it.
In the second version of make_bar, I enhance this function to make it parameterizable. So bar is still the default scope name (provided this time as the default_name argument of tf.variable_scope), but this time the caller has the possibility to change it by setting the default argument scope of make_bar to anything else than None.`
When this second version of make_bar is used without an argument, it should, I think, fall back to the behavior of the first version -- which it does not.
Note that in my example, bar is intended to be a subscope of foo. The variable to be reused is meant to be foo/bar/baz.