machine-learning - TensorFlow 中的批量标准化

标签 machine-learning tensorflow deep-learning conv-neural-network

在 TensorFlow 中执行批量标准化的正确方法是什么? (即我不想计算运行平均值和方差)。我当前的实现基于 tf.nn.batch_normalization ,其中 x 是形状为 [batch_size, width, height, num_channels] 的卷积层的输出。我想明智地执行批量归一化 channel 。

batch_mean, batch_var = tf.nn.moments(x, axes=[0, 1, 2])
x = tf.nn.batch_normalization(x, batch_mean, batch_var, offset=0, scale=0, variance_epsilon=1e-6)

但是这个实现的结果非常糟糕。与tensorflow.contrib.slim.batch_norm的比较表明它较差(同样糟糕的训练性能)。

enter image description here

我做错了什么,如何解释这种糟糕的表现?

最佳答案

您可以考虑tf.contrib.layers.layer_norm。您可能需要将 x reshape 为 [batch、channel、width、height] 并设置 begin_norm_axis=2 进行 channel 标准化(每个批处理和每个 channel 将独立标准化) .

以下示例如何将原始订单 reshape 为[批处理、 channel 、宽度、高度]:

import tensorflow as tf

sess = tf.InteractiveSession()

batch = 2
height = 2
width = 2
channel = 3

tot_size = batch * height * channel * width

ts_4D_bhwc = tf.reshape(tf.range(tot_size), [batch, height, width, channel])
ts_4D_bchw = tf.transpose(ts_4D_bhwc, perm=[0,3,1,2])

print("Original tensor w/ order bhwc\n")
print(ts_4D_bhwc.eval())

print("\nTransormed tensor w/ order bchw\n")
print(ts_4D_bchw.eval())

输出:

Original tensor w/ order bhwc

[[[[ 0  1  2]
   [ 3  4  5]]

  [[ 6  7  8]
   [ 9 10 11]]]


 [[[12 13 14]
   [15 16 17]]

  [[18 19 20]
   [21 22 23]]]]

Transormed tensor w/ order bchw

[[[[ 0  3]
   [ 6  9]]

  [[ 1  4]
   [ 7 10]]

  [[ 2  5]
   [ 8 11]]]


 [[[12 15]
   [18 21]]

  [[13 16]
   [19 22]]

  [[14 17]
   [20 23]]]]

关于machine-learning - TensorFlow 中的批量标准化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46989256/

相关文章:

tensorflow - 无法加载已保存的策略(TF 代理)

python - 在 Keras 模型中删除然后插入一个新的中间层

Tensorflow-GPU 与 conda : where is CUDA_HOME specified?

tensorflow - 使用模型中生成的目标数据进行训练

python - 为什么使用Anaconda环境在Windows上安装tensorflow?

python - 选择插补方法

python - pycharm 中 OpenCV EAST 文本检测器中的 net.forward() 错误

machine-learning - 使用无监督降维的模糊聚类

python - 有没有既可以在单词级别也可以在句子级别工作的分类器?

machine-learning - 二进制交叉熵损失在自动编码器上如何工作?