2017-04-11 26 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