Batch Norm - 在 TensorFlow 中提取 运行 均值和 运行 方差

Batch Norm - Extract Running Mean & Running Variance in TensorFlow

我正在尝试查看通过 GCMLE(saved_model.pbassets/*variables/*).这些值保存在图中的什么位置?我可以从 tf.GraphKeys.TRAINABLE_VARIABLES 访问 gamma/beta 值,但我无法在任何 tf.GraphKeys.MODEL_VARIABLES 中找到 运行 均值和 运行 方差。 运行 均值和 运行 方差是否存储在其他地方?

我知道在测试时(即Modes.EVAL),运行均值和运行方差用于对传入数据进行归一化,然后对归一化数据进行缩放和使用 gamma 和 beta 移动。我试图查看推理时需要的所有变量,但找不到 运行 均值和 运行 方差。这些是否仅在测试时使用而不在推理时使用 (Modes.PREDICT)?如果是这样,那就可以解释为什么我在导出的模型中找不到它们,但我希望它们在那里。

基于tf.GraphKeys,我尝试了其他的东西,比如tf.GraphKeys.MOVING_AVERAGE_VARIABLES,但它们也是空的。我还在 batch_normalization 文档 "Note: when training, the moving_mean and moving_variance need to be updated. By default the update ops are placed in tf.GraphKeys.UPDATE_OPS, so they need to be added as a dependency to the train_op." 中看到了这一行,所以我尝试从我保存的模型中查看 tf.GraphKeys.UPDATE_OPS 并且它们包含一个分配操作 batch_normalization/AssignMovingAvg:0 但仍然不清楚我在哪里将从中获取价值。

移动均值和移动方差似乎存储在 tf.GraphKeys.GLOBAL_VARIABLES 中,看起来 MODEL_VARIABLES 中没有任何内容的原因似乎是因为您需要使用 tf.contrib.framework.local_variable

除了#reese0106 的回答之外,
如果您想为 BatchNorm 取出 moving_mean、moving_variance,
你可以用下面的名字索引它们。

vars = tf.global_variables() # shows every variable being used.
vars_moving_mean_variance = []
for var in vars:
    if ("moving_mean" in var.name) or ("moving_variance" in var.name):
        vars_moving_mean_variance.append(var)

print(vars_moving_mean_variance)


p.s。感谢您的问题和答案。我也解决了自己的问题。