【问题标题】:How to get variables of batch_normalization's mean and variance in Tensorflow如何在 Tensorflow 中获取 batch_normalization 的均值和方差的变量
【发布时间】:2018-03-20 05:54:08
【问题描述】:

伙计们。

如果要在Tensorflow中实现Batch normalization,应该是下面的程序。

def batchnorm_forward(X, gamma, beta):
mu = np.mean(X, axis=0)
var = np.var(X, axis=0)

X_norm = (X - mu) / np.sqrt(var + 1e-8)
out = gamma * X_norm + beta

cache = (X, X_norm, mu, var, gamma, beta)

return out, cache, mu, var

此时,

为了只在推理阶段进行推理,它应该如下保存变量(均值、方差)。

# BatchNorm training forward propagation
h2, bn2_cache, mu, var = batchnorm_forward(h2, gamma2, beta2)
bn_params['bn2_mean'] = .9 * bn_params['bn2_mean'] + .1 * mu
bn_params['bn2_var'] = .9 * bn_params['bn2_var'] + .1 * var

仅在此推理阶段,它使用以下程序。

# BatchNorm inference forward propagation
h2 = (h2 - bn_params['bn2_mean']) / np.sqrt(bn_params['bn2_var'] + 1e-8)
h2 = gamma2 * h2 + beta2

在Tensorflow中,如何获取“bn_params['bn2_mean']”和“bn_params['bn2_var']”的变量(原始值)?

with tf.name_scope('fc1'):
  w1  = weight_variable([7 * 7 * 16, 32])
  h1 = tf.matmul(pool_flat2,  w1)
  fc1_bn = tf.contrib.layers.batch_norm(inputs = h1, is_training = phase_train)
  fc1_bn_relu = tf.nn.relu(fc1_bn)
  ...
  ...
...
...
# ????? how to get variables ?????
# Image in my head
mean, var = fc1_bn.eval()

帮帮我:

参考:https://wiseodd.github.io/techblog/2016/07/04/batchnorm/

【问题讨论】:

    标签: tensorflow batch-normalization


    【解决方案1】:

    我会使用层的面向对象版本。然后就像访问对象的属性一样简单:

    >>> import tensorflow as tf
    >>> bn = tf.layers.BatchNormalization()
    >>> bn(tf.ones([1, 3]))
    <tf.Tensor 'batch_normalization/batchnorm/add_1:0' shape=(1, 3) dtype=float32>
    >>> bn.variables
    [<tf.Variable 'batch_normalization/gamma:0' shape=(3,) dtype=float32_ref>, <tf.Variable 'batch_normalization/beta:0' shape=(3,) dtype=float32_ref>, <tf.Variable 'batch_normalization/moving_mean:0' shape=(3,) dtype=float32_ref>, <tf.Variable 'batch_normalization/moving_variance:0' shape=(3,) dtype=float32_ref>]
    >>> bn.moving_mean
    <tf.Variable 'batch_normalization/moving_mean:0' shape=(3,) dtype=float32_ref>
    >>> bn.moving_variance
    <tf.Variable 'batch_normalization/moving_variance:0' shape=(3,) dtype=float32_ref>
    >>> 
    

    【讨论】:

    • 如何才能知道tf.layers.BarchNormalization()等属性?
    • 我好像Tensorflow中的文字没有写出来
    • Layer.variables is documented 至少。我通过skimming the code 获得了这两个属性,但您也可以在构建/调用Layer 之后使用Python 的内置dir()
    【解决方案2】:

    面向对象的版本 tf.layers.BatchNormalization() 是危险的,因为它没有初始参数 training=True/False。你必须像这样应用它

    bn_instance = tf.layers.BatchNormalization(trainable=True)
    batch_layer = bn_instance.apply(input,training=True)
    

    否则它将使用初始的moving_mean(=0)/moving_variance(=1) 在训练期间对数据进行归一化。 (应该只用于推理)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-24
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 2014-01-07
      • 1970-01-01
      • 2017-07-17
      相关资源
      最近更新 更多