关于tensorboard name_scope

时间:2017-04-11 10:28:17

标签: tensorflow tensorboard

我使用name_scope来管理变量的名称,因此它可以通过tensorboard很好地显示。但是我发现一些奇怪的东西,name_scope不会为tf.get_variable创建的变量添加前缀。 所以代码引发了一个错误:

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:变量w已存在,不允许。

我可以使用variable_scope而不是name_scope吗? tensorboard可以在variable_scope上工作吗?

1 个答案:

答案 0 :(得分:7)

tf.name_scope定义了范围内定义的操作的前缀。

tf.variable_scope定义了范围内定义的操作和变量的前缀。

如果要创建一个与另一个变量同名但在不同范围内的变量,则必须使用tf.variable_scope

tf.name_scope用于定义自定义操作,以便很好地定义上下文。

就个人而言,我几乎总是使用tf.variable_scope

此外,是的,tf.variable_scope在张量板中创建外观漂亮的图形,与tf.named_scope完全相同