Search code examples
tensorflowtensorboard

About tensorboard name_scope


I use name_scope to manage namepsace of variables, so it can show well by tensorboard. But I find something strange, name_scope don't add prefix to variables created by tf.get_variable. So the code raise a error:

with tf.name_scope(self.networkName + '/conv1'):
    self.conv1_w = tf.get_variable(shape = [8, 8, 4, 16], name = 'w', initializer=tf.contrib.layers.xavier_initializer())
    self.conv1_b = tf.get_variable(shape = [16], name = 'b', initializer=tf.contrib.layers.xavier_initializer())
    self.conv1_o = tf.nn.relu(tf.nn.conv2d(self.states, self.conv1_w, [1, 4, 4, 1], 'SAME') + self.conv1_b)

with tf.name_scope(self.networkName + '/conv2'):
    self.conv2_w = tf.get_variable(shape = [4, 4, 16, 32], name = 'w', initializer=tf.contrib.layers.xavier_initializer())
    self.conv2_b = tf.get_variable(shape = [32], name = 'b', initializer=tf.contrib.layers.xavier_initializer())
    self.conv2_o = tf.nn.relu(tf.nn.conv2d(self.conv1_o, self.conv2_w, [1, 2, 2, 1], 'SAME') + self.conv2_b)

ValueError: Variable w already exists, disallowed.

Can I use variable_scope instead of name_scope? And tensorboard can work on variable_scope?


Solution

  • tf.name_scope defines a prefix for the operations defined within the scope.

    tf.variable_scope defines a prefix for the operations and variables defined within the scope.

    You have to use tf.variable_scope if you want to create a variable with the same name of another variable but in a different scope.

    tf.name_scope is used to define custom operations in order to well define the context.

    Personally, I use tf.variable_scope almost always.

    Moreover, yes, tf.variable_scope creates good looking graph in tensorboard exactly as tf.named_scope