mindspore.ops.batch_norm
- mindspore.ops.batch_norm(input_x, running_mean, running_var, weight, bias, training=False, momentum=0.1, eps=1e-05)[source]
Batch Normalization for input data and updated parameters.
Batch Normalization is widely used in convolutional neural networks. This operation applies Batch Normalization over inputs to avoid internal covariate shift as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. It rescales and recenters the features using a mini-batch of data and the learned parameters can be described in the following formula,
\[y = \frac{x - mean}{\sqrt{variance + \epsilon}} * \gamma + \beta\]where \(\gamma\) is weight, \(\beta\) is bias, \(\epsilon\) is eps, \(mean\) is the mean of \(x\), \(variance\) is the variance of \(x\).
Warning
For Atlas 200/300/500 inference product, the result accuracy fails to reach 1‰ due to the square root instruction.
Note
If training is False, weight, bias, running_mean and running_var are Tensors.
If training is True, weight, bias, running_mean and running_var are Parameters.
- Parameters
input_x (Tensor) – Tensor of shape \((N, C)\), with float16 or float32 data type.
running_mean (Union[Tensor, Parameter]) – The shape \((C,)\), has the same data type with weight.
running_var (Union[Tensor, Parameter]) – The shape \((C,)\), has the same data type with weight.
weight (Union[Tensor, Parameter]) – The shape \((C,)\), with float16 or float32 data type.
bias (Union[Tensor, Parameter]) – The shape \((C,)\), has the same data type with weight.
training (bool, optional) – If training is True, mean and variance are computed during training. If training is False, they’re loaded from checkpoint during inference. Default:
False
.momentum (float, optional) – The hyper parameter to compute moving average for running_mean and running_var (e.g. \(new\_running\_mean = (1 - momentum) * running\_mean + momentum * current\_mean\)). Momentum value must be [0, 1]. Default:
0.1
.eps (float, optional) – A small value added for numerical stability. Default:
1e-5
, value must be (0, 1] .
- Returns
output_x (Tensor) - The same type and shape as the input_x. The shape is \((N, C)\).
- Raises
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> from mindspore import Tensor, ops >>> input_x = Tensor([[1.0, 2.0], [3.0, 4.0]], mindspore.float32) >>> running_mean = Tensor([0.5, 1.5], mindspore.float32) >>> running_var = Tensor([0.1, 0.2], mindspore.float32) >>> weight = Tensor([2.0, 2.0], mindspore.float32) >>> bias = Tensor([-1.0, -1.0], mindspore.float32) >>> output = ops.batch_norm(input_x, running_mean, running_var, weight, bias) >>> print(output) [[ 2.1621194 1.2360122] [14.810596 10.180061 ]]