Can not use both bias and batch normalization in convolution layers

后端 未结 1 1004
予麋鹿
予麋鹿 2021-02-12 17:16

I use slim framework for tensorflow, because of its simplicity. But I want to have convolutional layer with both biases and batch normalization. In vanilla tensorflow, I have:

相关标签:
1条回答
  • 2021-02-12 17:49

    Batchnormalization already includes the addition of the bias term. Recap that BatchNorm is already:

    gamma * normalized(x) + bias
    

    So there is no need (and it makes no sense) to add another bias term in the convolution layer. Simply speaking BatchNorm shifts the activation by their mean values. Hence, any constant will be canceled out.

    If you still want to do this, you need to remove the normalizer_fn argument and add BatchNorm as a single layer. Like I said, this makes no sense.

    But the solution would be something like

    net = slim.conv2d(net, normalizer_fn=None, ...)
    net = tf.nn.batch_normalization(net)
    

    Note, the BatchNorm relies on non-gradient updates. So you either need to use an optimizer which is compatible with the UPDATE_OPS collection. Or you need to manually add tf.control_dependencies.

    Long story short: Even if you implement the ConvWithBias+BatchNorm, it will behave like ConvWithoutBias+BatchNorm. It is the same as multiple fully-connected layers without activation function will behave like a single one.

    0 讨论(0)
提交回复
热议问题