Applies Batch Normalization as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift .
Applies Batch Normalization as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift .
...
@@ -139,6 +140,7 @@ def batch_norm(x,
...
@@ -139,6 +140,7 @@ def batch_norm(x,
momentum(float, optional): The value used for the moving_mean and moving_var computation. Default: 0.9.
momentum(float, optional): The value used for the moving_mean and moving_var computation. Default: 0.9.
training(bool, optional): True means train mode which compute by batch data and track global mean and var during train period. False means inference mode which compute by global mean and var which calculated by train period. Defalut False.
training(bool, optional): True means train mode which compute by batch data and track global mean and var during train period. False means inference mode which compute by global mean and var which calculated by train period. Defalut False.
data_format(str, optional): Specify the input data format, may be "NC", "NCL", "NCHW", "NCDHW", "NLC", "NHWC" or "NDHWC". Defalut "NCHW".
data_format(str, optional): Specify the input data format, may be "NC", "NCL", "NCHW", "NCDHW", "NLC", "NHWC" or "NDHWC". Defalut "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 :ref:`api_guide_Name`..
name(str, optional): Name for the BatchNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..
Returns:
Returns:
...
@@ -167,8 +169,6 @@ def batch_norm(x,
...
@@ -167,8 +169,6 @@ def batch_norm(x,
assertlen(x.shape)>=2,"input dim must be larger than 1"
assertlen(x.shape)>=2,"input dim must be larger than 1"
# we use not training means use_global_status, more details see nn._BatchNormBase
@@ -550,11 +550,13 @@ class _BatchNormBase(layers.Layer):
...
@@ -550,11 +550,13 @@ class _BatchNormBase(layers.Layer):
weight_attr=None,
weight_attr=None,
bias_attr=None,
bias_attr=None,
data_format='NCHW',
data_format='NCHW',
use_global_stats=None,
name=None):
name=None):
super(_BatchNormBase,self).__init__()
super(_BatchNormBase,self).__init__()
self._num_features=num_features
self._num_features=num_features
self._weight_attr=weight_attr
self._weight_attr=weight_attr
self._bias_attr=bias_attr
self._bias_attr=bias_attr
self._use_global_stats=use_global_stats
ifget_default_dtype()=='float16':
ifget_default_dtype()=='float16':
set_default_dtype('float32')
set_default_dtype('float32')
...
@@ -642,7 +644,8 @@ class _BatchNormBase(layers.Layer):
...
@@ -642,7 +644,8 @@ class _BatchNormBase(layers.Layer):
training=self.training,
training=self.training,
momentum=self._momentum,
momentum=self._momentum,
epsilon=self._epsilon,
epsilon=self._epsilon,
data_format=self._data_format)
data_format=self._data_format,
use_global_stats=self._use_global_stats)
classBatchNorm1D(_BatchNormBase):
classBatchNorm1D(_BatchNormBase):
...
@@ -694,6 +697,7 @@ class BatchNorm1D(_BatchNormBase):
...
@@ -694,6 +697,7 @@ class BatchNorm1D(_BatchNormBase):
will create ParamAttr as bias_attr. If it is set to Fasle, the weight is not learnable.
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.
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".
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 :ref:`api_guide_Name`..
name(str, optional): Name for the BatchNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..
Shape:
Shape:
...
@@ -784,6 +788,7 @@ class BatchNorm2D(_BatchNormBase):
...
@@ -784,6 +788,7 @@ class BatchNorm2D(_BatchNormBase):
will create ParamAttr as bias_attr. If it is set to Fasle, the weight is not learnable.
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.
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". Default: NCHW.
data_format(str, optional): Specify the input data format, the data format can be "NCHW" or "NHWC". 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 :ref:`api_guide_Name`..
name(str, optional): Name for the BatchNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..
Shape:
Shape:
...
@@ -872,6 +877,7 @@ class BatchNorm3D(_BatchNormBase):
...
@@ -872,6 +877,7 @@ class BatchNorm3D(_BatchNormBase):
will create ParamAttr as bias_attr. If it is set to Fasle, the weight is not learnable.
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.
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 "NCDHW" or "NDHWC. Default: NCDHW.
data_format(str, optional): Specify the input data format, the data format can be "NCDHW" or "NDHWC. Default: NCDHW.
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 :ref:`api_guide_Name`..
name(str, optional): Name for the BatchNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..