tensorflow - creating a variable within tf.variable_scope(name), initialized from another variable's initialized_value -
hey tensorflow community,
i experiencing unexpected naming conventions when using variable_scope in following setup:
with tf.variable_scope("my_scope"): var = tf.variable(initial_value=other_var.initialized_value())
in above, holds
other_var.name = 'outer_scope/my_scope/other_var_name:0'
i therefore "reusing" same scope @ point in code. intuitively not see issue this, following happens:
var.name = 'outer_scope/my_scope_1/var_name:0'
so apparently, tf isn't happy "my_scope" , needs append "_1". "outer_scope" remains same, though.
if not initialize "other_var", behaviour not come up.
an explanation appreciated! thx
mat
you might want use tf.get_variable()
instead of 'tf.variable`.
with tf.variable_scope('var_scope', reuse=false) var_scope: var = tf.get_variable('var', [1]) var2 = tf.variable([1], name='var2') print var.name # var_scope/var:0 print var2.name # var_scope/var2:0 tf.variable_scope('var_scope', reuse=true) var_scope: var = tf.get_variable('var', [1]) var2 = tf.variable([1], name='var2') print var.name # var_scope/var:0 print var2.name # var_scope_1/var2:0
the reason behind think in example, although have "re-entered" variable_scope want, affects variable name scope named name_scope
intead of variable_scope
might guess. official document here can see that:
when tf.variable_scope("name"), implicitly opens tf.name_scope("name").
name_scope
used managing operation names(such add
, matmul
), because tf.variable
operation , operation name "inherited" variables created it, name of name_scope
rather variable_scope
used prefix.
but if want use tf.variable, can directly use name_scope
in with
statement:
with tf.name_scope('n_scope') n_scope: var = tf.variable([1], name='var') print var.name #n_scope/var_1:0 tf.name_scope(n_scope) n_scope: var = tf.variable([1], name='var') print var.name #n_scope/var_1:0
one thing pay attention should pass argument scope varible captured with
statement when want "re-enter" name scope, rather using str
scope name:
tf.name_scope('n_scope') n_scope: var = tf.variable([1], name='var') print var.name #n_scope/var_1:0 tf.name_scope('n_scope') n_scope: var = tf.variable([1], name='var') print var.name #n_scope_1/var_1:0
pay attention argument passed tf.name_scope
. behavior again described in doc string of name_scope
:
the name argument interpreted follows:
a string (not ending ‘/’) create new name scope, in name appended prefix of operations created in context. if name has been used before, made unique calling self.unique_name(name).
a scope captured g.name_scope(...) scope: statement treated “absolute” name scope, makes possible re-enter existing scopes.
a value of none or empty string reset current name scope top-level (empty) name scope.
Comments
Post a Comment