好的,所以最近我一直在使用Tensorflow,并开始使用更高级别的Layers API。但这对我来说一直是个问题。
以下代码为卷积块生成正确的(ish)图:
def conv_layer(self, inputs, filter_size = 3, num_filters = 256, name = None):
scope_name = name
if name == None:
scope_name = "conv_layer"
with tf.name_scope(scope_name):
conv = tf.contrib.layers.conv2d(inputs, num_filters, filter_size, activation_fn = None)
batch_norm = tf.contrib.layers.batch_norm(conv)
act = tf.nn.leaky_relu(batch_norm)
return act
问题在于tf.layers API产生了一些丑陋的变量,这些变量实际上并没有保留在name_scope中。这是Tensorboard视图,因此您可以了解我的意思。
无论如何,有没有让这些变量进入范围?在可视化图形时,这是一个大问题,因为我计划将此网络扩大到更大。 (如右图所示,这已经是一个大问题,每次启动Tensorboard时,我都必须从主图中手动删除它们。)
感谢进阶:)
编辑-解决方案/解决方案
将name_scope
的每个实例更改为variable_scope
时,该问题已被忽略。但是,我必须为每个variable_scope
分配一个唯一的ID,并设置reuse = False
。
def conv_layer(self, inputs, filter_size = 3, num_filters = 256, name = None):
scope_name = name
if name == None:
scope_name = "conv_layer_" + str(self.conv_id)
self.conv_id += 1
with tf.variable_scope(scope_name, reuse = False):
conv = tf.contrib.layers.conv2d(inputs, num_filters, filter_size, activation_fn = None)
batch_norm = tf.contrib.layers.batch_norm(conv)
act = tf.nn.leaky_relu(batch_norm)
return act
如您所见,变量很好地隐藏在正确的块中:)
答案 0 :(得分:1)
您可以尝试使用tf.variable_scope
代替。 tf.name_scope
被通过tf.get_variable()
函数创建的tf.layers
创建的变量忽略。这与通过tf.Variable
创建的变量相反。
有关差异的解释(尽管有些过时),请参见this question。
答案 1 :(得分:0)
解决方案从问题变成了答案:
将 name_scope
的每个实例更改为 variable_scope
问题已被忽略。但是,我必须为每个 variable_scope
分配一个唯一 ID 并设置 reuse = False
。
def conv_layer(self, inputs, filter_size = 3, num_filters = 256, name = None):
scope_name = name
if name == None:
scope_name = "conv_layer_" + str(self.conv_id)
self.conv_id += 1
with tf.variable_scope(scope_name, reuse = False):
conv = tf.contrib.layers.conv2d(inputs, num_filters, filter_size, activation_fn = None)
batch_norm = tf.contrib.layers.batch_norm(conv)
act = tf.nn.leaky_relu(batch_norm)
return act
如您所见,变量很好地隐藏在正确的块中。