From 4fd90971d44d570086383b0fd0822932d5934b3f Mon Sep 17 00:00:00 2001 From: David Nicolas <37790151+liyongchao911@users.noreply.github.com> Date: Tue, 23 Aug 2022 14:05:44 +0800 Subject: [PATCH] update loss.py in nn.functional and nn.layer module;test=document_fix (#45068) * update loss.py in nn.functional and nn.layer module * update nn.Pad1D and 2D,3D class in common.py * add code copy from function anchor for cn docs code alignemnt * update pad class in common.py * update common.py in functional and layer model * fix format issue * update format issue * update pad;test=document_fix * update NLLLoss;test=document_fix Co-authored-by: Ligoml <39876205+Ligoml@users.noreply.github.com> --- python/paddle/nn/functional/common.py | 35 ++++---- python/paddle/nn/functional/loss.py | 4 +- python/paddle/nn/layer/common.py | 114 +++++++++----------------- python/paddle/nn/layer/loss.py | 15 ++-- 4 files changed, 66 insertions(+), 102 deletions(-) diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index e003ac44379..d52a4b225ca 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -1367,30 +1367,30 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None): Parameters: x (Tensor): The input tensor with data type float32/double/int32/int64_t. - pad (Tensor | List[int] | Tuple[int]): The padding size with data type int. + pad (Tensor|list[int]|tuple[int]): The padding size with data type int. If mode is 'constant' and length of pad is twice as length of x dimension, then x will be padded from the first dimension to the last dimension. Else: 1. If input dimension is 3, then the pad has the form (pad_left, pad_right). 2. If the input dimension is 4, then the pad has the form (pad_left, pad_right, pad_top, pad_bottom). 3. If the input dimension is 5, then the pad has the form (pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_back). - mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. - When in 'constant' mode, this op uses a constant value to pad the input tensor. - When in 'reflect' mode, uses reflection of the input boundaries to pad the input tensor. - When in 'replicate' mode, uses input boundaries to pad the input tensor. - When in 'circular' mode, uses circular input to pad the input tensor. - Default is 'constant' - value (float32, optional): The value to fill the padded areas in 'constant' mode . Default is 0.0 + mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. Default is 'constant' + + - 'constant' mode, uses a constant value to pad the input tensor. + - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor. + - 'replicate' mode, uses input boundaries to pad the input tensor. + - 'circular' mode, uses circular input to pad the input tensor. + + value (float, optional): The value to fill the padded areas in 'constant' mode . Default is :math:`0.0`, data_format (str, optional): An string from: "NCL", "NLC", NHWC", "NCHW", "NCDHW", "NDHWC". Specify the data format of - the input data. - Default is "NCHW" - name (str, optional) : The default value is None. Normally there is no need for - user to set this property. For more information, please refer to :ref:`api_guide_Name`. + the input data. Default is "NCHW", + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor, a Tensor padded according to pad and mode and data type is same as input. - Examples: + Example: + .. code-block:: text x = [[[[[1., 2., 3.], @@ -1438,30 +1438,29 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None): [5. 6. 4. 5. 6. 4. 5.] [2. 3. 1. 2. 3. 1. 2.]]]]] - Code Examples: + Examples: .. code-block:: python - import numpy as np import paddle import paddle.nn.functional as F # example 1 x_shape = (1, 1, 3) - x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1 + x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 y = F.pad(x, [0, 0, 0, 0, 2, 3], value=1, mode='constant', data_format="NCL") print(y) # [[[1. 1. 1. 2. 3. 1. 1. 1.]]] # example 2 x_shape = (1, 1, 3) - x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1 + x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL") print(y) # [[[1. 1. 1. 2. 3. 1. 1. 1.]]] # example 3 x_shape = (1, 1, 2, 3) - x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1 + x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 y = F.pad(x, [1, 2, 1, 1], value=1, mode='circular') print(y) # [[[[6. 4. 5. 6. 4. 5.] diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index 4cee06e91a7..119eb1bfbee 100755 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -1331,8 +1331,8 @@ def nll_loss(input, to each class. If given, it has to be a 1D Tensor whose size is `[C, ]`. Otherwise, it treated as if having all ones. the data type is float32, float64, Default is ``'None'``. - ignore_index (int64, optional): Specifies a target value that is ignored - and does not contribute to the input gradient. + ignore_index (int, optional): Specifies a target value that is ignored + and does not contribute to the input gradient. Default is -100. reduction (str, optional): Indicate how to average the loss, the candicates are ``'none'`` | ``'mean'`` | ``'sum'``. If `reduction` is ``'mean'``, the reduced mean loss is returned; diff --git a/python/paddle/nn/layer/common.py b/python/paddle/nn/layer/common.py index 7c034d37ba6..05230c19c48 100644 --- a/python/paddle/nn/layer/common.py +++ b/python/paddle/nn/layer/common.py @@ -945,46 +945,34 @@ class Pad1D(Layer): If mode is 'reflect', pad[0] and pad[1] must be no greater than width-1. Parameters: - padding (Tensor | List[int] | int): The padding size with data type int. If is int, use the + padding (Tensor|list[int]|int): The padding size with data type int. If is int, use the same padding in both dimensions. Else [len(padding)/2] dimensions of input will be padded. The pad has the form (pad_left, pad_right). - mode (str): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. - When in 'constant' mode, this op uses a constant value to pad the input tensor. - When in 'reflect' mode, uses reflection of the input boundaries to pad the input tensor. - When in 'replicate' mode, uses input boundaries to pad the input tensor. - When in 'circular' mode, uses circular input to pad the input tensor. - Default is 'constant'. - value (float32): The value to fill the padded areas. Default is 0.0 - data_format (str): An string from: "NCL", "NLC". Specify the data format of the input data. + mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. Default is 'constant'. + + - 'constant' mode, uses a constant value to pad the input tensor. + - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor. + - 'replicate' mode, uses input boundaries to pad the input tensor. + - 'circular' mode, uses circular input to pad the input tensor. + + value (float, optional): The value to fill the padded areas. Default is :math:`0.0`。 + data_format (str, optional): An string from: "NCL", "NLC". Specify the data format of the input data. Default is "NCL" - name (str, optional) : The default value is None. Normally there is no need for - user to set this property. For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: None Examples: - .. code-block:: text - - x = [[[1., 2., 3.], - [4., 5., 6.]]] - padding = [1, 2], - mode = "constant" - value = 0.0 - Out = [[[0. 1. 2. 3. 0. 0.] - [0. 4. 5. 6. 0. 0.]]] - - Code Examples: .. code-block:: python import paddle import paddle.nn as nn - import numpy as np input_shape = (1, 2, 3) pad = [1, 2] mode = "constant" - data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1 + data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1 my_pad = nn.Pad1D(padding=pad, mode=mode) result = my_pad(data) print(result) @@ -1027,45 +1015,34 @@ class Pad2D(Layer): than width-1. The height dimension has the same condition. Parameters: - padding (Tensor | List[int] | int): The padding size with data type int. If is int, use the + padding (Tensor|list[int]|int): The padding size with data type int. If is int, use the same padding in all dimensions. Else [len(padding)/2] dimensions of input will be padded. The pad has the form (pad_left, pad_right, pad_top, pad_bottom). - mode (str): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. - When in 'constant' mode, this op uses a constant value to pad the input tensor. - When in 'reflect' mode, uses reflection of the input boundaries to pad the input tensor. - When in 'replicate' mode, uses input boundaries to pad the input tensor. - When in 'circular' mode, uses circular input to pad the input tensor. - Default is 'constant'. - value (float32): The value to fill the padded areas. Default is 0.0 - data_format (str): An string from: "NCHW", "NHWC". Specify the data format of the input data. - Default is "NCHW" - name (str, optional) : The default value is None. Normally there is no need for - user to set this property. For more information, please refer to :ref:`api_guide_Name`. + mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. Default is 'constant'. + + - 'constant' mode, uses a constant value to pad the input tensor. + - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor. + - 'replicate' mode, uses input boundaries to pad the input tensor. + - 'circular' mode, uses circular input to pad the input tensor. + + value (float, optional): The value to fill the padded areas. Default is :math:`0.0`。 + data_format (str, optional): An string from: "NCHW", "NHWC". Specify the data format of the input data. + Default is "NCHW"。 + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: None Examples: - .. code-block:: text - - x = [[[[1., 2., 3.], - [4., 5., 6.]]]] - padding = [1, 1, 0, 0] - mode = "constant" - value = 0.0 - Out = [[[[0. 1. 2. 3. 0.] - [0. 4. 5. 6. 0.]]]] - - Code Examples: .. code-block:: python import paddle import paddle.nn as nn - import numpy as np + input_shape = (1, 1, 2, 3) pad = [1, 0, 1, 2] mode = "constant" - data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1 + data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1 my_pad = nn.Pad2D(padding=pad, mode=mode) result = my_pad(data) print(result) @@ -1178,45 +1155,34 @@ class Pad3D(Layer): than width-1. The height and depth dimension has the same condition. Parameters: - padding (Tensor | List[int] | int): The padding size with data type int. If is int, use the + padding (Tensor|list[int]|int): The padding size with data type int. If is int, use the same padding in all dimensions. Else [len(padding)/2] dimensions of input will be padded. The pad has the form (pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_back). - mode (str): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. - When in 'constant' mode, this op uses a constant value to pad the input tensor. - When in 'reflect' mode, uses reflection of the input boundaries to pad the input tensor. - When in 'replicate' mode, uses input boundaries to pad the input tensor. - When in 'circular' mode, uses circular input to pad the input tensor. - Default is 'constant'. - value (float32): The value to fill the padded areas. Default is 0.0 - data_format (str): An string from: "NCDHW", "NDHWC". Specify the data format of the input data. - Default is "NCDHW" - name (str, optional) : The default value is None. Normally there is no need for - user to set this property. For more information, please refer to :ref:`api_guide_Name`. + mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. Default is 'constant'. + + - 'constant' mode, uses a constant value to pad the input tensor. + - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor. + - 'replicate' mode, uses input boundaries to pad the input tensor. + - 'circular' mode, uses circular input to pad the input tensor. + + value (float, optional): The value to fill the padded areas. Default is :math:`0.0`。 + data_format (str, optional): An string from: "NCDHW", "NDHWC". Specify the data format of the input data. + Default is "NCDHW"。 + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: None Examples: - .. code-block:: text - - x = [[[[[1., 2., 3.], - [4., 5., 6.]]]]] - padding = [1, 2, 0, 0, 0, 0] - mode = "constant" - value = 0.0 - Out = [[[[[0. 1. 2. 3. 0. 0.] - [0. 4. 5. 6. 0. 0.]]]]] - - Code Examples: .. code-block:: python import paddle import paddle.nn as nn - import numpy as np + input_shape = (1, 1, 1, 2, 3) pad = [1, 0, 1, 2, 0, 0] mode = "constant" - data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1 + data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1 my_pad = nn.Pad3D(padding=pad, mode=mode) result = my_pad(data) print(result) diff --git a/python/paddle/nn/layer/loss.py b/python/paddle/nn/layer/loss.py index 635315b0027..6612c0e75ce 100644 --- a/python/paddle/nn/layer/loss.py +++ b/python/paddle/nn/layer/loss.py @@ -795,7 +795,7 @@ class NLLLoss(Layer): This class accepts input and target label and returns negative log likelihood cross error. It is useful to train a classification problem with C classes. - The input for the loss is epected to contain log-probabilities of + The input for the loss is expected to contain log-probabilities of each classes. It has to be a Tensor of size either (batch_size, C) or (batch_size, C, d1, d2, ..., dK) with K >= 1 for the K-dimensional case. The label for the loss should be a class index in the range [0, C-1] @@ -813,7 +813,7 @@ class NLLLoss(Layer): \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_{y_n} x_{n,y_n}, \quad - w_{c} = \text{weight}[c] \cdot \mathbb{1}\{c \not= \text{ignore\_index}\}, + w_{c} = \text{weight}[c] \cdot \mathbb{1}\{c \not= \text{ignore_index}\}, where :math:`N` is the batch size. If :attr:`reduction` is not ``'none'`` (default ``'mean'``), then @@ -835,7 +835,7 @@ class NLLLoss(Layer): to each class. If given, it has to be a 1D Tensor whose size is `[C, ]`. Otherwise, it treated as if having all ones. the data type is float32, float64, Default is ``'None'``. - ignore_index (int64, optional): Specifies a target value that is ignored + ignore_index (int, optional): Specifies a target value that is ignored and does not contribute to the input gradient. reduction (str, optional): Indicate how to average the loss, the candicates are ``'none'`` | ``'mean'`` | ``'sum'``. @@ -843,16 +843,15 @@ class NLLLoss(Layer): if `reduction` is ``'sum'``, the reduced sum loss is returned; if `reduction` is ``'none'``, no reduction will be apllied. Default is ``'mean'``. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Shape: - input (Tensor): Input tensor, the shape is :math:`[N, C]`, `C` is the number of classes. + - input (Tensor): Input tensor, the shape is :math:`[N, C]`, `C` is the number of classes. But in K-dimension situation, the shape is :math:`[N, C, d_1, d_2, ..., d_K]`. The data type is float32, float64. - label (Tensor): Label tensor, the shape is :math:`[N,]` or :math:`[N, d_1, d_2, ..., d_K]`. + - label (Tensor): Label tensor, the shape is :math:`[N,]` or :math:`[N, d_1, d_2, ..., d_K]`. The data type is int64. - output (Tensor): the `negative log likelihood loss` between input `x` and `label`. + - output (Tensor): the `negative log likelihood loss` between input `x` and `label`. If `reduction` is `'none'`, the shape is `[N, *]`. If `reduction` is `'sum'` or `'mean'`, the shape is `[1]`. -- GitLab