未验证 提交 b9207054 编写于 作者: A Ayuan 提交者: GitHub

InstanceNorm1D、InstanceNorm2D、InstanceNorm3D (#48940)

* modified:   python/paddle/nn/layer/norm.py

* modified:   python/paddle/nn/layer/norm.py

* modified:   python/paddle/nn/layer/norm.py

* modified:   python/paddle/nn/layer/norm.py

* modified:   python/paddle/nn/layer/norm.py

* modified:   python/paddle/nn/layer/norm.py

* test=docs_preview

* InstanceNorm2D中文档格式修改

* test=docs_preview

* modified:   python/paddle/nn/functional/loss.py
	modified:   python/paddle/nn/functional/norm.py
	modified:   python/paddle/nn/layer/loss.py
	modified:   python/paddle/nn/layer/norm.py

* test=docs_preview

* test=docs_preview
上级 f6915d42
...@@ -1546,33 +1546,27 @@ def kl_div(input, label, reduction='mean', name=None): ...@@ -1546,33 +1546,27 @@ def kl_div(input, label, reduction='mean', name=None):
$$l(x, y) = y * (\log(y) - x)$$ $$l(x, y) = y * (\log(y) - x)$$
While :math:`x` is input and :math:`y` is label. Here :math:`x` is input and :math:`y` is label.
While :attr:`reduction` is :attr:`none`, output loss is in If `reduction` is ``'none'``, the output loss is the same shape as the input, and the loss at each point is calculated separately. There is no reduction to the result.
the same shape as input, loss in each point is calculated
separately and no reduction is applied.
While :attr:`reduction` is :attr:`mean`, output loss is in If `reduction` is ``'mean'``, the output loss is the shape of [1], and the output is the average of all losses.
shape of [1] and loss value is the mean value of all losses.
While :attr:`reduction` is :attr:`sum`, output loss is in If `reduction` is ``'sum'``, the output loss is the shape of [1], and the output is the sum of all losses.
shape of [1] and loss value is the sum value of all losses.
While :attr:`reduction` is :attr:`batchmean`, output loss is If `reduction` is ``'batchmean'``, the output loss is the shape of [N], N is the batch size, and the output is the sum of all losses divided by the batch size.
in shape of [1] and loss value is the sum value of all losses
divided by batch size.
Args: Args:
input (Tensor): The input tensor. The shapes is [N, *], where N is batch size and `*` means input (Tensor): The input tensor. The shapes is [N, *], where N is batch size and `*` means
any number of additional dimensions. It's data type should be float32, float64. any number of additional dimensions. It's data type should be float32, float64.
label (Tensor): label. The shapes is [N, *], same shape as ``input`` . It's data type should be float32, float64. label (Tensor): label. The shapes is [N, *], same shape as ``input`` . It's data type should be float32, float64.
reduction (Tensor): Indicate how to average the loss, reduction (str, optional): Indicate how to average the loss,
the candicates are ``'none'`` | ``'batchmean'`` | ``'mean'`` | ``'sum'``. the candicates are ``'none'`` | ``'batchmean'`` | ``'mean'`` | ``'sum'``.
If `reduction` is ``'mean'``, the reduced mean loss is returned; If `reduction` is ``'mean'``, the reduced mean loss is returned;
If `reduction` is ``'batchmean'``, the sum loss divided by batch size is returned; If `reduction` is ``'batchmean'``, the sum loss divided by batch size is returned;
if `reduction` is ``'sum'``, the reduced sum loss is returned; if `reduction` is ``'sum'``, the reduced sum loss is returned;
if `reduction` is ``'none'``, no reduction will be apllied. if `reduction` is ``'none'``, no reduction will be apllied.
Default is ``'mean'``. Default is ``'mean'``.
name(str, optional): Name for the operation (optional, default is None). For more information, name(str, optional): Name for the operation (optional, default is None). For more information,
please refer to :ref:`api_guide_Name`. please refer to :ref:`api_guide_Name`.
......
...@@ -327,7 +327,8 @@ def layer_norm( ...@@ -327,7 +327,8 @@ def layer_norm(
x, normalized_shape, weight=None, bias=None, epsilon=1e-05, name=None x, normalized_shape, weight=None, bias=None, epsilon=1e-05, name=None
): ):
""" """
see more detail in paddle.nn.LayerNorm nn.LayerNorm is recommended.
For more information, please refer to :ref:`api_paddle_nn_LayerNorm` .
Parameters: Parameters:
x(Tensor): Input Tensor. It's data type should be float32, float64. x(Tensor): Input Tensor. It's data type should be float32, float64.
...@@ -335,11 +336,11 @@ def layer_norm( ...@@ -335,11 +336,11 @@ def layer_norm(
size :math:`[*, normalized_shape[0], normalized_shape[1], ..., normalized_shape[-1]]`. size :math:`[*, normalized_shape[0], normalized_shape[1], ..., normalized_shape[-1]]`.
If it is a single integer, this module will normalize over the last dimension If it is a single integer, this module will normalize over the last dimension
which is expected to be of that specific size. which is expected to be of that specific size.
epsilon(float, optional): The small value added to the variance to prevent
division by zero. Default: 1e-05.
weight(Tensor, optional): The weight tensor of batch_norm. Default: None. weight(Tensor, optional): The weight tensor of batch_norm. Default: None.
bias(Tensor, optional): The bias tensor of batch_norm. Default: None. bias(Tensor, optional): The bias tensor of batch_norm. Default: None.
name(str, optional): Name for the LayerNorm, default is None. For more information, please refer to :ref:`api_guide_Name`.. epsilon(float, optional): The small value added to the variance to prevent
division by zero. Default: 1e-05.
name(str, optional): Name for the LayerNorm, default is None. For more information, please refer to :ref:`api_guide_Name` .
Returns: Returns:
None None
...@@ -448,7 +449,7 @@ def instance_norm( ...@@ -448,7 +449,7 @@ def instance_norm(
name=None, name=None,
): ):
""" """
See more detail in nn.layer.InstanceNorm2D. It is recommended to use :ref:`api_paddle_nn_InstanceNorm1D` , :ref:`api_paddle_nn_InstanceNorm2D` , :ref:`api_paddle_nn_InstanceNorm3D` to call this method internally.
Parameters: Parameters:
x(Tensor): Input Tensor. It's data type should be float32, float64. x(Tensor): Input Tensor. It's data type should be float32, float64.
......
...@@ -891,19 +891,32 @@ class KLDivLoss(Layer): ...@@ -891,19 +891,32 @@ class KLDivLoss(Layer):
$$l(x, y) = y * (\log(y) - x)$$ $$l(x, y) = y * (\log(y) - x)$$
Here :math:`x` is input and :math:`y` is label.
If `reduction` is ``'none'``, the output loss is the same shape as the input, and the loss at each point is calculated separately. There is no reduction to the result.
If `reduction` is ``'mean'``, the output loss is the shape of [1], and the output is the average of all losses.
If `reduction` is ``'sum'``, the output loss is the shape of [1], and the output is the sum of all losses.
If `reduction` is ``'batchmean'``, the output loss is the shape of [N], N is the batch size, and the output is the sum of all losses divided by the batch size.
Parameters: Parameters:
reduction (Tensor): Indicate how to average the loss, reduction (str, optional): Indicate how to average the loss,
the candicates are ``'none'`` | ``'batchmean'`` | ``'mean'`` | ``'sum'``. the candicates are ``'none'`` | ``'batchmean'`` | ``'mean'`` | ``'sum'``.
If `reduction` is ``'mean'``, the reduced mean loss is returned; If `reduction` is ``'mean'``, the reduced mean loss is returned;
If `reduction` is ``'batchmean'``, the sum loss divided by batch size is returned; If `reduction` is ``'batchmean'``, the sum loss divided by batch size is returned;
if `reduction` is ``'sum'``, the reduced sum loss is returned; if `reduction` is ``'sum'``, the reduced sum loss is returned;
if `reduction` is ``'none'``, no reduction will be apllied. if `reduction` is ``'none'``, no reduction will be apllied.
Default is ``'mean'``. Default is ``'mean'``.
Shape: Shape:
- input (Tensor): ``(N, *)``, where ``*`` means, any number of additional dimensions.
- label (Tensor): ``(N, *)``, same shape as input. input (Tensor): ``(N, *)``, where ``*`` means, any number of additional dimensions.
- output (Tensor): tensor with shape: [1] by default.
label (Tensor): ``(N, *)``, same shape as input.
output (Tensor): tensor with shape: [1] by default.
Examples: Examples:
.. code-block:: python .. code-block:: python
......
...@@ -132,25 +132,25 @@ class InstanceNorm1D(_InstanceNormBase): ...@@ -132,25 +132,25 @@ class InstanceNorm1D(_InstanceNormBase):
\sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\
y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift
Where `H` means height of feature map, `W` means width of feature map. Where `H` means height of feature map, `W` means width of feature map.
Parameters: Parameters:
num_features(int): Indicate the number of channels of the input ``Tensor``. num_features(int): Indicate the number of channels of the input ``Tensor``.
epsilon(float, optional): A value added to the denominator for epsilon(float, optional): A value added to the denominator for
numerical stability. Default is 1e-5. numerical stability. Default is 1e-5.
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.
weight_attr(ParamAttr|bool, optional): The parameter attribute for Parameter `scale` weight_attr(ParamAttr|bool, optional): The parameter attribute for Parameter `scale` of instance_norm.
of instance_norm. If it is set to None or one attribute of ParamAttr, instance_norm If it is set to None or one attribute of ParamAttr, instance_norm
will create ParamAttr as weight_attr, the name of scale can be set in ParamAttr. will create ParamAttr as weight_attr, the name of scale can be set in ParamAttr.
If the Initializer of the weight_attr is not set, the parameter is initialized If the Initializer of the weight_attr is not set, the parameter is initialized
one. If it is set to False, will not create weight_attr. Default: None. one. If it is set to False, will not create weight_attr. Default: None. For more information, please refer to :ref:`api_paddle_ParamAttr` .
bias_attr(ParamAttr|bool, optional): The parameter attribute for the bias of instance_norm. bias_attr(ParamAttr|bool, optional): The parameter attribute for the bias of instance_norm.
If it is set to None or one attribute of ParamAttr, instance_norm If it is set to None or one attribute of ParamAttr, instance_norm
will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr. will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr.
If the Initializer of the bias_attr is not set, the bias is initialized zero. If the Initializer of the bias_attr is not set, the bias is initialized zero.
If it is set to False, will not create bias_attr. Default: None. If it is set to False, will not create bias_attr. Default: None. For more information, please refer to :ref:`api_paddle_ParamAttr` .
data_format(str, optional): Specify the input data format, may be "NC", "NCL". Default "NCL". data_format(str, optional): Specify the input data format, may be "NC", "NCL". Default "NCL".
name(str, optional): Name for the InstanceNorm, default is None. For more information, please refer to :ref:`api_guide_Name`.. name(str, optional): Name for the InstanceNorm, default is None. For more information, please refer to :ref:`api_guide_Name` .
Shape: Shape:
...@@ -175,6 +175,26 @@ Where `H` means height of feature map, `W` means width of feature map. ...@@ -175,6 +175,26 @@ Where `H` means height of feature map, `W` means width of feature map.
""" """
def __init__(
self,
num_features,
epsilon=0.00001,
momentum=0.9,
weight_attr=None,
bias_attr=None,
data_format="NCL",
name=None,
):
super().__init__(
num_features,
epsilon,
momentum,
weight_attr,
bias_attr,
data_format,
name,
)
def _check_input_dim(self, input): def _check_input_dim(self, input):
if len(input.shape) != 2 and len(input.shape) != 3: if len(input.shape) != 2 and len(input.shape) != 3:
raise ValueError( raise ValueError(
...@@ -203,7 +223,7 @@ class InstanceNorm2D(_InstanceNormBase): ...@@ -203,7 +223,7 @@ class InstanceNorm2D(_InstanceNormBase):
\sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\
y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift
Where `H` means height of feature map, `W` means width of feature map. Where `H` means height of feature map, `W` means width of feature map.
Parameters: Parameters:
num_features(int): Indicate the number of channels of the input ``Tensor``. num_features(int): Indicate the number of channels of the input ``Tensor``.
...@@ -214,14 +234,14 @@ Where `H` means height of feature map, `W` means width of feature map. ...@@ -214,14 +234,14 @@ Where `H` means height of feature map, `W` means width of feature map.
of instance_norm. If it is set to None or one attribute of ParamAttr, instance_norm of instance_norm. If it is set to None or one attribute of ParamAttr, instance_norm
will create ParamAttr as weight_attr, the name of scale can be set in ParamAttr. will create ParamAttr as weight_attr, the name of scale can be set in ParamAttr.
If the Initializer of the weight_attr is not set, the parameter is initialized If the Initializer of the weight_attr is not set, the parameter is initialized
one. If it is set to False, will not create weight_attr. Default: None. one. If it is set to False, will not create weight_attr. Default: None. For more information, please refer to :ref:`api_paddle_ParamAttr` .
bias_attr(ParamAttr|bool, optional): The parameter attribute for the bias of instance_norm. bias_attr(ParamAttr|bool, optional): The parameter attribute for the bias of instance_norm.
If it is set to None or one attribute of ParamAttr, instance_norm If it is set to None or one attribute of ParamAttr, instance_norm
will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr. will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr.
If the Initializer of the bias_attr is not set, the bias is initialized zero. If the Initializer of the bias_attr is not set, the bias is initialized zero.
` If it is set to False, will not create bias_attr. Default: None. If it is set to False, will not create bias_attr. Default: None. For more information, please refer to :ref:`api_paddle_ParamAttr` .
data_format(str, optional): Specify the input data format, could be "NCHW". Default: NCHW. data_format(str, optional): Specify the input data format, could be "NCHW". Default: NCHW.
name(str, optional): Name for the InstanceNorm, default is None. For more information, please refer to :ref:`api_guide_Name`.. name(str, optional): Name for the InstanceNorm, default is None. For more information, please refer to :ref:`api_guide_Name` .
Shape: Shape:
- x: 4-D tensor with shape: (batch, num_features, height, weight). - x: 4-D tensor with shape: (batch, num_features, height, weight).
...@@ -244,6 +264,26 @@ Where `H` means height of feature map, `W` means width of feature map. ...@@ -244,6 +264,26 @@ Where `H` means height of feature map, `W` means width of feature map.
print(instance_norm_out) print(instance_norm_out)
""" """
def __init__(
self,
num_features,
epsilon=0.00001,
momentum=0.9,
weight_attr=None,
bias_attr=None,
data_format="NCHW",
name=None,
):
super().__init__(
num_features,
epsilon,
momentum,
weight_attr,
bias_attr,
data_format,
name,
)
def _check_input_dim(self, input): def _check_input_dim(self, input):
if len(input.shape) != 4: if len(input.shape) != 4:
raise ValueError( raise ValueError(
...@@ -255,7 +295,7 @@ class InstanceNorm3D(_InstanceNormBase): ...@@ -255,7 +295,7 @@ class InstanceNorm3D(_InstanceNormBase):
r""" r"""
Create a callable object of `InstanceNorm3D`. Applies Instance Normalization over a 5D input (a mini-batch of 3D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization . Create a callable object of `InstanceNorm3D`. Applies Instance Normalization over a 5D input (a mini-batch of 3D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization .
DataLayout: NCHW `[batch, in_channels, D, in_height, in_width]` DataLayout: NCDHW `[batch, in_channels, D, in_height, in_width]`
:math:`input` is the input features over a mini-batch. :math:`input` is the input features over a mini-batch.
...@@ -270,7 +310,7 @@ class InstanceNorm3D(_InstanceNormBase): ...@@ -270,7 +310,7 @@ class InstanceNorm3D(_InstanceNormBase):
\sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\
y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift
Where `H` means height of feature map, `W` means width of feature map. Where `H` means height of feature map, `W` means width of feature map.
Parameters: Parameters:
num_features(int): Indicate the number of channels of the input ``Tensor``. num_features(int): Indicate the number of channels of the input ``Tensor``.
...@@ -281,14 +321,14 @@ Where `H` means height of feature map, `W` means width of feature map. ...@@ -281,14 +321,14 @@ Where `H` means height of feature map, `W` means width of feature map.
of instance_norm. If it is set to None or one attribute of ParamAttr, instance_norm of instance_norm. If it is set to None or one attribute of ParamAttr, instance_norm
will create ParamAttr as weight_attr, the name of scale can be set in ParamAttr. will create ParamAttr as weight_attr, the name of scale can be set in ParamAttr.
If the Initializer of the weight_attr is not set, the parameter is initialized If the Initializer of the weight_attr is not set, the parameter is initialized
one. If it is set to False, will not create weight_attr. Default: None. one. If it is set to False, will not create weight_attr. Default: None. For more information, please refer to :ref:`api_paddle_ParamAttr` .
bias_attr(ParamAttr|bool, optional): The parameter attribute for the bias of instance_norm. bias_attr(ParamAttr|bool, optional): The parameter attribute for the bias of instance_norm.
If it is set to None or one attribute of ParamAttr, instance_norm If it is set to None or one attribute of ParamAttr, instance_norm
will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr. will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr.
If the Initializer of the bias_attr is not set, the bias is initialized zero. If the Initializer of the bias_attr is not set, the bias is initialized zero.
If it is set to False, will not create bias_attr. Default: None. If it is set to False, will not create bias_attr. Default: None. For more information, please refer to :ref:`api_paddle_ParamAttr` .
data_format(str, optional): Specify the input data format, could be "NCDHW". Default: NCDHW. data_format(str, optional): Specify the input data format, could be "NCDHW". Default: NCDHW.
name(str, optional): Name for the InstanceNorm, default is None. For more information, please refer to :ref:`api_guide_Name`.. name(str, optional): Name for the InstanceNorm, default is None. For more information, please refer to :ref:`api_guide_Name` .
Shape: Shape:
- x: 5-D tensor with shape: (batch, num_features, dims, height, weight). - x: 5-D tensor with shape: (batch, num_features, dims, height, weight).
...@@ -311,6 +351,26 @@ Where `H` means height of feature map, `W` means width of feature map. ...@@ -311,6 +351,26 @@ Where `H` means height of feature map, `W` means width of feature map.
print(instance_norm_out.numpy) print(instance_norm_out.numpy)
""" """
def __init__(
self,
num_features,
epsilon=0.00001,
momentum=0.9,
weight_attr=None,
bias_attr=None,
data_format="NCDHW",
name=None,
):
super().__init__(
num_features,
epsilon,
momentum,
weight_attr,
bias_attr,
data_format,
name,
)
def _check_input_dim(self, input): def _check_input_dim(self, input):
if len(input.shape) != 5: if len(input.shape) != 5:
raise ValueError( raise ValueError(
...@@ -508,11 +568,11 @@ class LayerNorm(Layer): ...@@ -508,11 +568,11 @@ class LayerNorm(Layer):
division by zero. Default: 1e-05. division by zero. Default: 1e-05.
weight_attr(ParamAttr|bool, optional): The parameter attribute for the learnable weight_attr(ParamAttr|bool, optional): The parameter attribute for the learnable
gain :math:`g`. If False, weight is None. If is None, a default :code:`ParamAttr` would be added as scale. The gain :math:`g`. If False, weight is None. If is None, a default :code:`ParamAttr` would be added as scale. The
:attr:`param_attr` is initialized as 1 if it is added. Default: None. :attr:`param_attr` is initialized as 1 if it is added. Default: None. For more information, please refer to :ref:`api_paddle_ParamAttr` .
bias_attr(ParamAttr|bool, optional): The parameter attribute for the learnable bias_attr(ParamAttr|bool, optional): The parameter attribute for the learnable
bias :math:`b`. If is False, bias is None. If is None, a default :code:`ParamAttr` would be added as bias. The bias :math:`b`. If is False, bias is None. If is None, a default :code:`ParamAttr` would be added as bias. The
:attr:`bias_attr` is initialized as 0 if it is added. Default: None. :attr:`bias_attr` is initialized as 0 if it is added. Default: None. For more information, please refer to :ref:`api_paddle_ParamAttr` .
name(str, optional): Name for the LayerNorm, default is None. For more information, please refer to :ref:`api_guide_Name`.. name(str, optional): Name for the LayerNorm, default is None. For more information, please refer to :ref:`api_guide_Name` .
Shape: Shape:
- x: 2-D, 3-D, 4-D or 5-D tensor. - x: 2-D, 3-D, 4-D or 5-D tensor.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册