如何为tensorflow多GPU代码实现batch normalization层

How to implement batch normalization layer for tensorflow multi-GPU code

我创建了一个多 GPU 网络 Cifar10_multigpu

在推理实现中,他们说:

We instantiate all variables using tf.get_variable() instead of tf.Variable() in order to share variables across multiple GPU training runs. If we only ran this model on a single GPU, we could simplify this function by replacing all instances of tf.get_variable() with tf.Variable().

所以我以我所有的 conv2d 层为例,但是 batchnorm 层呢?如何自己实现?

在这种情况下我可以使用 tensorflow.contrib.slim.batch_norm 吗? 该示例不包含任何有关批量规范层的建议。

只需使用tf.layers.batch_normalization。它还通过 tf.get_variable() 创建变量,因此它们也可以共享。

此外,它可以与 tf.layers.conv* 功能无缝协作。

更新: tf.nn.batch_normalization 也可以。这是一个更底层的函数,需要您自己管理 meanvariance 张量。事实上,tf.layers.batch_normalizationtf.nn.* 函数的包装器,其中还包括 tf.nn.fused_batch_norm(更快的融合版本)。