简体   繁体   English

使用张量流实现批量标准化

[英]Implementing batch normalization with tensorflow

I am trying to implement a batch normalization layer in tensor-flow . 我试图在tensor-flow实现批量规范化层。 I am having no problem running the train step of this using tf.moments to get the mean and variance . 我使用tf.moments运行火车步骤没有问题,以获得均值方差

For test time, I'd like to set up an exponential moving average to track the mean and variance. 对于测试时间,我想建立一个指数移动平均线来跟踪均值和方差。 I am trying to do it like this: 我想这样做:

def batch_normalized_linear_layer(state_below, scope_name, n_inputs, n_outputs, stddev, wd, eps=.0001):
  with tf.variable_scope(scope_name) as scope:
    weight = _variable_with_weight_decay(
      "weights", shape=[n_inputs, n_outputs],
      stddev=stddev, wd=wd
    )
    act = tf.matmul(state_below, weight)
    # get moments
    act_mean, act_variance = tf.nn.moments(act, [0])
    # get mean and variance variables
    mean = _variable_on_cpu('bn_mean', [n_outputs], tf.constant_initializer(0.0))
    variance = _variable_on_cpu('bn_variance', [n_outputs], tf.constant_initializer(1.0))
    # assign the moments
    assign_mean = mean.assign(act_mean)
    assign_variance = variance.assign(act_variance)

    act_bn = tf.mul((act - mean), tf.rsqrt(variance + eps), name=scope.name+"_bn")

    beta = _variable_on_cpu("beta", [n_outputs], tf.constant_initializer(0.0))
    gamma = _variable_on_cpu("gamma", [n_outputs], tf.constant_initializer(1.0))
    bn = tf.add(tf.mul(act_bn, gamma), beta)
    output = tf.nn.relu(bn, name=scope.name)
    _activation_summary(output)
  return output, mean, variance

Where _variable_on_cpu is defined as: 其中_variable_on_cpu定义为:

def _variable_on_cpu(name, shape, initializer):
  """Helper to create a Variable stored on CPU memory.

  Args:
    name: name of the variable
    shape: list of ints
    initializer: initializer for Variable

  Returns:
    Variable Tensor
  """
  with tf.device('/cpu:0'):
    var = tf.get_variable(name, shape, initializer=initializer)
  return var

I believe that I am setting 我相信我正在设定

assign_mean = mean.assign(act_mean)
assign_variance = variance.assign(act_variance)

Incorrectly, but I am not sure how. 不正确,但我不知道如何。 When I use tensorboard to track these mean and variance variables, they are just flat that their initialized values. 当我使用tensorboard来跟踪这些均值和方差变量时,它们只是它们的初始化值是平的。

Rafal's comment gets at the core of the problem: You're not running the assign nodes. 拉法尔的评论是问题的核心:你没有运行分配节点。 You might try using the batchnorm helper I posted in another answer - How could I use Batch Normalization in TensorFlow? 您可以尝试使用我在另一个答案中发布的batchnorm助手 - 如何在TensorFlow中使用批量标准化? - or you can force the assign to happen by adding with_dependencies, as he suggests. - 或者您可以通过添加with_dependencies强制分配,正如他所建议的那样。

The general principle is that you should only count on a node being run if data or control dependencies flow "through" it. 一般原则是,如果数据或控制依赖关系“流过”它,您应该只依赖于正在运行的节点。 with_dependencies ensures that before the output op is used, the specified dependencies will have completed. with_dependencies确保在使用输出op之前,指定的依赖项将已完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM