Skip to main content
 首页 » 编程设计

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

2024年09月07日43dudu

我创建了一个多 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也很好。这是一个更底层的函数,需要您自己管理 mean 和 variance 张量。事实上,tf.layers.batch_normalization 是 tf.nn.* 函数的包装器,它还包括 tf.nn.fused_batch_norm(一个更快的融合版本)。