Understanding variable_scope and name_scope in tensorflow and variable sharing -


i'd share variables between 2 subgraphs. more precisely, i'd fowolling operation : given 4 tensors a, b, c, d , weight variable w, compute w*a, w*b, w*c, w*d in different subgraph. code, have follow :

def forward(inputs):   w = tf.get_variable("weights", ...)   return tf.matmult(w, inputs)  tf.name_scope("group_1"):   = tf.placeholder(...)   b = tf.placeholder(...)   c = tf.placeholder(...)    aa = forward(a)   bb = forward(b)   cc = forward(c)  tf.name_scope("group_2):   d = tf.placeholder(...)    tf.get_variable_scope().reuse_variable()   dd = forward(d) 

this example seems run i'm not sure whether variable w reused inside group_1 when add tf.get_variable_scope.reuse_variable() got error saying there's no variable share. when visualize graph in tensorboard, have several weigths_* inside group_1 subgraph.

the following code want:

import tensorflow tf  def forward(inputs):     init = tf.random_normal_initializer()     w = tf.get_variable("weights", shape=(3,2), initializer=init)     return tf.matmul(w, inputs)  tf.name_scope("group_1"):     = tf.placeholder(tf.float32, shape=(2, 3), name="a")     b = tf.placeholder(tf.float32, shape=(2, 3), name="b")     c = tf.placeholder(tf.float32, shape=(2, 3), name="c")     tf.variable_scope("foo", reuse=false):         aa = forward(a)     tf.variable_scope("foo", reuse=true):         bb = forward(b)         cc = forward(c)  tf.name_scope("group_2"):     d = tf.placeholder(tf.float32, shape=(2, 3), name="d")     tf.variable_scope("foo", reuse=true):         dd = forward(d)  init = tf.initialize_all_variables()  tf.session() sess:     sess.run(init)     print(bb.eval(feed_dict={b: np.array([[1,2,3],[4,5,6]])}))     var in tf.all_variables():         print(var.name)         print(var.eval()) 

a few important things understand:

  • name_scope()affects ops except variables created get_variable().
  • to place variable in scope, need use variable_scope(). example, placeholders a, b, , c named "group_1/a", "group_1/b", "group_1/c" , "group_1/d", weights variable named "foo/weights". get_variable("weights") in name scope "group_1" , variable scope "foo" looks "foo/weights".

the all_variables() function useful if not sure variables exist , how named.


Comments