Keras BatchNormalization,究竟什么是样本标准化?

Keras BatchNormalization, What exactly is sample wise normalization?

我想弄清楚 Keras 中的批量归一化究竟做了什么。现在我有以下代码。

for i in range(8):
    c = Convolution2D(128, 3, 3, border_mode = 'same', init = 'he_normal')(c)
    c = LeakyReLU()(c)
    c = Convolution2D(128, 3, 3, border_mode = 'same', init = 'he_normal')(c)
    c = LeakyReLU()(c)
    c = Convolution2D(128, 3, 3, border_mode = 'same', init = 'he_normal')(c)
    c = LeakyReLU()(c)
    c = merge([c, x], mode = 'sum')
    c = BatchNormalization(mode = 1)(c)
    x = c

我根据 Keras 文档将批量归一化模式设置为 1 1: sample-wise normalization. This mode assumes a 2D input.

我认为这应该做的只是独立于每个其他样本对批次中的每个样本进行归一化。但是,当我查看调用函数的源代码时,我看到了以下内容。

    elif self.mode == 1:
        # sample-wise normalization
        m = K.mean(x, axis=-1, keepdims=True)
        std = K.std(x, axis=-1, keepdims=True)
        x_normed = (x - m) / (std + self.epsilon)
        out = self.gamma * x_normed + self.beta

在这里它只是计算所有 x 的平均值,我认为在我的例子中是 (BATCH_SIZE, 128, 56, 56)。我认为它应该在模式 1 下独立于批次中的其他样本进行归一化。那么 axis = 1 不应该吗?另外 "assumes a 2D input" 在文档中是什么意思?

In this it is just computing the mean over all of x which in my case is (BATCH_SIZE, 128, 56, 56) I think.

这样做你已经违反了该层的合同。那不是 2 维而是 4 维输入。

I thought it was supposed to normalize independent of the other samples in the batch when in mode 1

确实如此。 K.mean(..., axis=-1) 减少轴 -1,它与输入的最后一个轴同义。因此,假设输入形状为 (batchsz, features),轴 -1 将是 features 轴。

由于 K.meannumpy.mean 非常相似,您可以自己测试一下:

>>> x = [[1,2,3],[4,5,6]]
>>> x
array([[1, 2, 3],
       [4, 5, 6]])
>>> np.mean(x, axis=-1)
array([ 2.,  5.])

您可以看到批次中每个样本的特征都减少了。