tensorflow中的批量标准化是否在培训期间使用运行平均值?

2024-05-10 19:30:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个tensorflow神经网络来计算批处理规范化是如何工作的,并将其复制到我自己的库中。我遇到了一个奇怪的问题:

当你初始化一个神经网络层时,所有的偏差(或者在batchnorm-betas的情况下)都被设置为0,所以这个层只需要将输入值乘以权重,就这样。现在,根据我对batchnorm的了解,在培训期间,它根据输入的minibatch计算层输入的平均值和方差,然后对输入值进行处理:output=(input-mean)/sqrt(variance+eps)。在

所以,如果你的minibatch的所有输入值都相同,那么在训练过程中,batchnorm会从输入值中减去平均值(等于每个值),那么网络应该输出0,不管输入是什么,对吧?在

事实上,在计算过程中所有的平均值都是0,而方差是1,就好像它在使用这些值的运行平均值一样。 所以,要么我不明白batchnorm是如何工作的,要么就是batchnorm被错误地使用了。下面是如何在我使用的代码中初始化它:

    layer= tflearn.fully_connected(layer, 10, weights_init=w_init)
    layer= tflearn.layers.normalization.batch_normalization(layer)
    layer= tflearn.activations.leaky_relu(layer)

另一种选择是在训练中使用不当,但我想先排除其他可能的解释。在


Tags: layerinit过程tensorflow神经网络规范化网络层神经
1条回答
网友
1楼 · 发布于 2024-05-10 19:30:49

TensorFlow批处理规范实现有一些默认情况下不包含在训练操作依赖项中的更新操作。必须显式地添加依赖项。引用docs:

[W]hen 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. Also, be sure to add any batch_normalization ops before getting the update_ops collection. Otherwise, update_ops will be empty, and training/inference will not work properly. For example:

  x_norm = tf.layers.batch_normalization(x, training=training)

  # ...

  update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
  with tf.control_dependencies(update_ops):
    train_op = optimizer.minimize(loss)

相关问题 更多 >