# BatchNorm1D¶

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

Applies Batch Normalization over a 2D or 3D input (a mini-batch of 1D inputswith 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 Fasle, the weight is not learnable. If the Initializer of the weight_attr is not set, the parameter is initialized with Xavier. 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 Fasle, 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, may be “NC”, “NCL” or “NLC”. Defalut “NCL”.

• 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: 2-D or 3-D tensor with shape: (batch, num_features) or (batch, num_features, length) when data_format is “NC” or “NCL”,

(batch, length, num_features) when data_format is “NLC”.

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

Returns

None.

Examples

import paddle
import numpy as np

np.random.seed(123)
x_data = np.random.random(size=(2, 1, 3)).astype('float32')