如何在 TensorFlow 中为卷积层正确创建批量归一化层?

How to correctly create a batch normalization layer for a convolutional layer in TensorFlow?

我正在查看 TensorFlow 中的 official batch normalization layer (BN),但它并没有真正解释如何将其用于卷积层。有人知道怎么做吗?特别重要的是,它应用和学习 每个特征图 相同的参数(而不是每次激活)。以其他顺序应用并学习每个过滤器的 BN。

在一个具体的玩具示例中,我想在 MNIST 上使用 BN 进行 conv2d(本质上是 2D 数据)。因此可以这样做:

W_conv1 = weight_variable([5, 5, 1, 32]) # 5x5 filters with 32 filters
x_image = tf.reshape(x, [-1,28,28,1]) # MNIST image
conv = tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], padding='VALID') #[?,24,24,1]
z = conv # [?,24,24,32]
z = BN(z) # [?,24,24,32], essentially only 32 different scales and shift parameters to learn, per filer application
a = tf.nn.relu(z) # [?,24,24,32]

其中 z = BN(z) 将 BN 应用于每个过滤器创建的每个特征。在伪代码中:

x_patch = x[h:h+5,w:w+h,1] # patch to do convolution
z[h,w,f] = x_patch * W[:,:,f] = tf.matmul(x_patch, W[:,:,f]) # actual matrix multiplication for the convolution

我们应用了一个适当的批量规范层(在伪代码中省略了重要细节):

z[h,w,f] = BN(z[h,w,f]) = scale[f] * (z[h,w,f]  - mu / sigma) + shift[f]

即对于每个过滤器 f,我们应用 BN。

重要提示:我在此处提供的链接会影响 tf.contrib.layers.batch_norm 模块,而不是通常的 tf.nn(请参阅下面的评论和 post)

我没有测试它,但 TF 希望您使用它的方式似乎记录在 convolution2d docstring:

def convolution2d(inputs,
              num_outputs,
              kernel_size,
              stride=1,
              padding='SAME',
              activation_fn=nn.relu,
              normalizer_fn=None,
              normalizer_params=None,
              weights_initializer=initializers.xavier_initializer(),
              weights_regularizer=None,
              biases_initializer=init_ops.zeros_initializer,
              biases_regularizer=None,
              reuse=None,
              variables_collections=None,
              outputs_collections=None,
              trainable=True,
              scope=None):
  """Adds a 2D convolution followed by an optional batch_norm layer.
  `convolution2d` creates a variable called `weights`, representing the
  convolutional kernel, that is convolved with the `inputs` to produce a
  `Tensor` of activations. If a `normalizer_fn` is provided (such as
  `batch_norm`), it is then applied. Otherwise, if `normalizer_fn` is
  None and a `biases_initializer` is provided then a `biases` variable would be
  created and added the activations.

按照这个建议,您应该将 normalizer_fn='batch_norm' 作为参数添加到您的 conv2d 方法调用中。

关于特征映射与激活问题,我的猜测是 TF 在构建图形时会将规范化层作为新的 "node" 添加到 conv2d 层的顶部,并且它们都将修改相同的权重变量(在你的例子中, W_conv1 对象)。无论如何,我不会将规范层的任务描述为 "learning",但我不太确定我是否理解你的观点(如果你详细说明,也许我可以尝试进一步提供帮助)

编辑: 仔细查看函数体证实了我的猜测,也解释了如何使用 normalized_params 参数。从 line 354 读取:

outputs = nn.conv2d(inputs, weights, [1, stride_h, stride_w, 1],
padding=padding)
if normalizer_fn:
  normalizer_params = normalizer_params or {}
  outputs = normalizer_fn(outputs, **normalizer_params)
else:
  ...etc...

我们看到 outputs 变量,保存着每一层各自的输出,被顺序覆盖。因此,如果在构建图形时给出 normalizer_fnnn.conv2d 的输出将被额外层 normalizer_fn 覆盖。这是 **normalizer_params 开始发挥作用的地方,作为 kwarg iterable to the given normalizer_fn. You can find the default parameters to batch_norm here 传递,因此将字典传递给 normalizer_params 以及您希望更改的字典应该可以解决问题,如下所示:

normalizer_params = {"epsilon" : 0.314592, "center" : False}

希望对您有所帮助!

看来下面的例子对我有用:

import numpy as np

import tensorflow as tf


normalizer_fn = None
normalizer_fn = tf.contrib.layers.batch_norm

D = 5
kernel_height = 1
kernel_width = 3
F = 4
x = tf.placeholder(tf.float32, shape=[None,1,D,1], name='x-input') #[M, 1, D, 1]
conv = tf.contrib.layers.convolution2d(inputs=x,
    num_outputs=F, # 4
    kernel_size=[kernel_height, kernel_width], # [1,3]
    stride=[1,1],
    padding='VALID',
    rate=1,
    activation_fn=tf.nn.relu,
    normalizer_fn=normalizer_fn,
    normalizer_params=None,
    weights_initializer=tf.contrib.layers.xavier_initializer(dtype=tf.float32),
    biases_initializer=tf.zeros_initializer,
    trainable=True,
    scope='cnn'
)

# syntheitc data
M = 2
X_data = np.array( [np.arange(0,5),np.arange(5,10)] )
print(X_data)
X_data = X_data.reshape(M,1,D,1)
with tf.Session() as sess:
    sess.run( tf.initialize_all_variables() )
    print( sess.run(fetches=conv, feed_dict={x:X_data}) )

控制台输出:

$ python single_convolution.py
[[0 1 2 3 4]
 [5 6 7 8 9]]
[[[[ 1.33058071  1.33073258  1.30027914  0.        ]
   [ 0.95041472  0.95052338  0.92877126  0.        ]
   [ 0.57024884  0.57031405  0.55726254  0.        ]]]


 [[[ 0.          0.          0.          0.56916821]
   [ 0.          0.          0.          0.94861376]
   [ 0.          0.          0.          1.32805932]]]]