BatchNorm2D

class paddle.nn. BatchNorm2D ( num_features, momentum=0.9, epsilon=1e-05, weight_attr=None, bias_attr=None, data_format='NCHW', use_global_stats=None, name=None ) [source]

Applies Batch Normalization over a 4D input (a mini-batch of 2D inputs with additional channel dimension) as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift .

When use_global_stats = False, the \(\mu_{\beta}\) and \(\sigma_{\beta}^{2}\) are the statistics of one mini-batch. Calculated as follows:

\[\begin{split}\mu_{\beta} &\gets \frac{1}{m} \sum_{i=1}^{m} x_i \qquad &// \ mini-batch\ mean \\ \sigma_{\beta}^{2} &\gets \frac{1}{m} \sum_{i=1}^{m}(x_i - \mu_{\beta})^2 \qquad &//\ mini-batch\ variance \\\end{split}\]

When use_global_stats = True, the \(\mu_{\beta}\) and \(\sigma_{\beta}^{2}\) are not the statistics of one mini-batch. They are global or running statistics (moving_mean and moving_variance). It usually got from the pre-trained model. Calculated as follows:

\[\begin{split}moving\_mean = moving\_mean * momentum + \mu_{\beta} * (1. - momentum) \quad &// global \ mean \\ moving\_variance = moving\_variance * momentum + \sigma_{\beta}^{2} * (1. - momentum) \quad &// global \ variance \\\end{split}\]

The normalization function formula is as follows:

\[\begin{split}\hat{x_i} &\gets \frac{x_i - \mu_\beta} {\sqrt{\sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift\end{split}\]
  • \(\epsilon\) : add a smaller value to the variance to prevent division by zero

  • \(\gamma\) : trainable proportional parameter

  • \(\beta\) : trainable deviation parameter

Parameters
  • num_features (int) – Indicate the number of channels of the input Tensor.

  • epsilon (float, optional) – The small value added to the variance to prevent division by zero. Default: 1e-5.

  • momentum (float, optional) – The value used for the moving_mean and moving_var computation. Default: 0.9.

  • weight_attr (ParamAttr|bool, optional) – The parameter attribute for Parameter scale of batch_norm. If it is set to None or one attribute of ParamAttr, batch_norm will create ParamAttr as weight_attr. If it is set to False, the weight is not learnable. If the Initializer of the weight_attr is not set, the parameter is initialized with ones. Default: None.

  • bias_attr (ParamAttr|bool, optional) – The parameter attribute for the bias of batch_norm. If it is set to None or one attribute of ParamAttr, batch_norm will create ParamAttr as bias_attr. If it is set to False, the weight is not learnable. If the Initializer of the bias_attr is not set, the bias is initialized zero. Default: None.

  • data_format (str, optional) – Specify the input data format, the data format can be “NCHW” or “NHWC”, where N is batch size, C is the number of the feature map, H is the height of the feature map, W is the width of the feature map. Default: NCHW.

  • use_global_stats (bool|None, optional) – Whether to use global mean and variance. If set to False, use the statistics of one mini-batch, if set to True, use the global statistics, if set to None, use global statistics in the test phase and use the statistics of one mini-batch in the training phase. Default: None.

  • name (str, optional) – Name for the BatchNorm, default is None. For more information, please refer to Name..

Shape:
  • x: 4-D tensor with shape: (batch, num_features, height, weight) when data_format is “NCHW”,

    or (batch, height, weight, num_features) when data_format is “NHWC”.

  • output: 4-D tensor with same shape as input x.

Returns

None

Examples

>>> import paddle
>>> paddle.seed(100)
>>> x = paddle.rand((2, 1, 2, 3))
>>> batch_norm = paddle.nn.BatchNorm2D(1)
>>> batch_norm_out = batch_norm(x)

>>> print(batch_norm_out)
Tensor(shape=[2, 1, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=False,
[[[[ 0.60520101, -0.67670590, -1.40020895],
   [ 0.46540475, -0.09736633, -0.47771257]]],
 [[[-0.74365312,  0.63718963, -1.41333187],
   [ 1.44764757, -0.25489068,  1.90842628]]]])