2017-04-11 28 views
0

我使用name_scope来管理变量的namepsace,所以它可以很好地显示在张量板上。但是我发现一些奇怪的东西,name_scope不会为由tf.get_variable创建的变量添加前缀。 因此,代码提出一个错误:关于tensorboard name_scope

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.

我可以用它代替name_scope variable_scope? tensorboard可以在variable_scope上工作吗?

回答

6

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

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

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

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

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

此外,是的,tf.variable_scope完全可以在张量板上创建好看的图形tf.named_scope