关于张量板 name_scope

About tensorboard name_scope

我用name_scope来管理变量的namepsace,所以在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: Variable w already exists, disallowed.

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

上工作

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

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

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

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

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

此外,是的,tf.variable_scope 在 tensorboard 中创建的图形与 tf.named_scope

完全一样