python - 如何将 tf.layers 变量放入 tf.name_scope/tf.variable_scope 中?

标签 python tensorflow scope layer tensorboard

我在使用 Tensorflow 时遇到问题:

以下代码为卷积 block 生成正确的图:

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 View ,您可以明白我的意思。

Tensorboard

是否有办法让这些变量进入作用域?当涉及到图形可视化时,这是一个大问题,因为我计划将这个网络变得更大。 (如右图所示,这已经是一个大问题了,每次启动 Tensorboard 时我都必须手动从主图中删除它们。)

最佳答案

您可以尝试使用tf.variable_scope代替。 tf.name_scope 会被通过 tf.get_variable() 创建的变量忽略,该变量通常由 tf.layers 函数使用。这与通过 tf.Variable 创建的变量形成对比。

参见this question有关差异的解释(尽管有些过时)。

关于python - 如何将 tf.layers 变量放入 tf.name_scope/tf.variable_scope 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51732039/

相关文章:

tensorflow - 如何检查Bazel版本?

python - 如何使用具有多个输入的 keras 生成序列数据?

javascript - 设置 onclick 以在循环中使用变量的当前值

javascript - var在NodeJs模块中属于哪个对象?

python - 在提交对象之前从 SqlAlchemy 获取主键

python - 导入错误 : No module named azure. storage.blob

tensorflow - 从预训练的 NASnet 网络迁移学习。如何知道要卡住的层数?

scope - Kotlin - 限制扩展方法范围

python - 没有扭曲的扭曲应用程序

python - 交换嵌套列表中的列 (Python 3)