未验证 提交 4fd90971 编写于 作者: D David Nicolas 提交者: GitHub

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: NLigoml <39876205+Ligoml@users.noreply.github.com>
上级 7e0bada3
...@@ -1367,30 +1367,30 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None): ...@@ -1367,30 +1367,30 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
Parameters: Parameters:
x (Tensor): The input tensor with data type float32/double/int32/int64_t. 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 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. 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, 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_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_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). (pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_back).
mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. Default is 'constant'
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. - 'constant' mode, uses a constant value to pad the input tensor.
When in 'replicate' mode, uses input boundaries to pad the input tensor. - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
When in 'circular' mode, uses circular input to pad the input tensor. - 'replicate' mode, uses input boundaries to pad the input tensor.
Default is 'constant' - 'circular' mode, uses circular input to pad the input tensor.
value (float32, optional): The value to fill the padded areas in 'constant' mode . Default is 0.0
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 data_format (str, optional): An string from: "NCL", "NLC", NHWC", "NCHW", "NCDHW", "NDHWC". Specify the data format of
the input data. the input data. Default is "NCHW",
Default is "NCHW" name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
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`.
Returns: Returns:
Tensor, a Tensor padded according to pad and mode and data type is same as input. Tensor, a Tensor padded according to pad and mode and data type is same as input.
Examples: Example:
.. code-block:: text .. code-block:: text
x = [[[[[1., 2., 3.], x = [[[[[1., 2., 3.],
...@@ -1438,30 +1438,29 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None): ...@@ -1438,30 +1438,29 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
[5. 6. 4. 5. 6. 4. 5.] [5. 6. 4. 5. 6. 4. 5.]
[2. 3. 1. 2. 3. 1. 2.]]]]] [2. 3. 1. 2. 3. 1. 2.]]]]]
Code Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
import paddle.nn.functional as F import paddle.nn.functional as F
# example 1 # example 1
x_shape = (1, 1, 3) 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") y = F.pad(x, [0, 0, 0, 0, 2, 3], value=1, mode='constant', data_format="NCL")
print(y) print(y)
# [[[1. 1. 1. 2. 3. 1. 1. 1.]]] # [[[1. 1. 1. 2. 3. 1. 1. 1.]]]
# example 2 # example 2
x_shape = (1, 1, 3) 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") y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL")
print(y) print(y)
# [[[1. 1. 1. 2. 3. 1. 1. 1.]]] # [[[1. 1. 1. 2. 3. 1. 1. 1.]]]
# example 3 # example 3
x_shape = (1, 1, 2, 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') y = F.pad(x, [1, 2, 1, 1], value=1, mode='circular')
print(y) print(y)
# [[[[6. 4. 5. 6. 4. 5.] # [[[[6. 4. 5. 6. 4. 5.]
......
...@@ -1331,8 +1331,8 @@ def nll_loss(input, ...@@ -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, 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 it treated as if having all ones. the data type is
float32, float64, Default is ``'None'``. 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. and does not contribute to the input gradient. Default is -100.
reduction (str, optional): Indicate how to average the loss, reduction (str, optional): Indicate how to average the loss,
the candicates are ``'none'`` | ``'mean'`` | ``'sum'``. the candicates are ``'none'`` | ``'mean'`` | ``'sum'``.
If `reduction` is ``'mean'``, the reduced mean loss is returned; If `reduction` is ``'mean'``, the reduced mean loss is returned;
......
...@@ -945,46 +945,34 @@ class Pad1D(Layer): ...@@ -945,46 +945,34 @@ class Pad1D(Layer):
If mode is 'reflect', pad[0] and pad[1] must be no greater than width-1. If mode is 'reflect', pad[0] and pad[1] must be no greater than width-1.
Parameters: 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 same padding in both dimensions. Else [len(padding)/2] dimensions
of input will be padded. The pad has the form (pad_left, pad_right). of input will be padded. The pad has the form (pad_left, pad_right).
mode (str): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. Default is 'constant'.
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. - 'constant' mode, uses a constant value to pad the input tensor.
When in 'replicate' mode, uses input boundaries to pad the input tensor. - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
When in 'circular' mode, uses circular input to pad the input tensor. - 'replicate' mode, uses input boundaries to pad the input tensor.
Default is 'constant'. - 'circular' mode, uses circular input to pad the input tensor.
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. 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" Default is "NCL"
name (str, optional) : The default value is None. Normally there is no need for name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
user to set this property. For more information, please refer to :ref:`api_guide_Name`.
Returns: Returns:
None None
Examples: 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 .. code-block:: python
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import numpy as np
input_shape = (1, 2, 3) input_shape = (1, 2, 3)
pad = [1, 2] pad = [1, 2]
mode = "constant" 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) my_pad = nn.Pad1D(padding=pad, mode=mode)
result = my_pad(data) result = my_pad(data)
print(result) print(result)
...@@ -1027,45 +1015,34 @@ class Pad2D(Layer): ...@@ -1027,45 +1015,34 @@ class Pad2D(Layer):
than width-1. The height dimension has the same condition. than width-1. The height dimension has the same condition.
Parameters: 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. 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). The pad has the form (pad_left, pad_right, pad_top, pad_bottom).
mode (str): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. Default is 'constant'.
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. - 'constant' mode, uses a constant value to pad the input tensor.
When in 'replicate' mode, uses input boundaries to pad the input tensor. - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
When in 'circular' mode, uses circular input to pad the input tensor. - 'replicate' mode, uses input boundaries to pad the input tensor.
Default is 'constant'. - 'circular' mode, uses circular input to pad the input tensor.
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. value (float, optional): The value to fill the padded areas. Default is :math:`0.0`。
Default is "NCHW" data_format (str, optional): An string from: "NCHW", "NHWC". Specify the data format of the input data.
name (str, optional) : The default value is None. Normally there is no need for Default is "NCHW"。
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: Returns:
None None
Examples: 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 .. code-block:: python
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import numpy as np
input_shape = (1, 1, 2, 3) input_shape = (1, 1, 2, 3)
pad = [1, 0, 1, 2] pad = [1, 0, 1, 2]
mode = "constant" 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) my_pad = nn.Pad2D(padding=pad, mode=mode)
result = my_pad(data) result = my_pad(data)
print(result) print(result)
...@@ -1178,45 +1155,34 @@ class Pad3D(Layer): ...@@ -1178,45 +1155,34 @@ class Pad3D(Layer):
than width-1. The height and depth dimension has the same condition. than width-1. The height and depth dimension has the same condition.
Parameters: 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 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). 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'. mode (str, optional): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'. Default is 'constant'.
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. - 'constant' mode, uses a constant value to pad the input tensor.
When in 'replicate' mode, uses input boundaries to pad the input tensor. - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
When in 'circular' mode, uses circular input to pad the input tensor. - 'replicate' mode, uses input boundaries to pad the input tensor.
Default is 'constant'. - 'circular' mode, uses circular input to pad the input tensor.
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. value (float, optional): The value to fill the padded areas. Default is :math:`0.0`。
Default is "NCDHW" data_format (str, optional): An string from: "NCDHW", "NDHWC". Specify the data format of the input data.
name (str, optional) : The default value is None. Normally there is no need for Default is "NCDHW"。
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: Returns:
None None
Examples: 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 .. code-block:: python
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import numpy as np
input_shape = (1, 1, 1, 2, 3) input_shape = (1, 1, 1, 2, 3)
pad = [1, 0, 1, 2, 0, 0] pad = [1, 0, 1, 2, 0, 0]
mode = "constant" 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) my_pad = nn.Pad3D(padding=pad, mode=mode)
result = my_pad(data) result = my_pad(data)
print(result) print(result)
......
...@@ -795,7 +795,7 @@ class NLLLoss(Layer): ...@@ -795,7 +795,7 @@ class NLLLoss(Layer):
This class accepts input and target label and returns negative log likelihood 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. 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 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. (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] The label for the loss should be a class index in the range [0, C-1]
...@@ -813,7 +813,7 @@ class NLLLoss(Layer): ...@@ -813,7 +813,7 @@ class NLLLoss(Layer):
\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad
l_n = - w_{y_n} x_{n,y_n}, \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'`` where :math:`N` is the batch size. If :attr:`reduction` is not ``'none'``
(default ``'mean'``), then (default ``'mean'``), then
...@@ -835,7 +835,7 @@ class NLLLoss(Layer): ...@@ -835,7 +835,7 @@ class NLLLoss(Layer):
to each class. If given, it has to be a 1D Tensor whose size is `[C, ]`. Otherwise, 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 it treated as if having all ones. the data type is
float32, float64, Default is ``'None'``. 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. and does not contribute to the input gradient.
reduction (str, optional): Indicate how to average the loss, reduction (str, optional): Indicate how to average the loss,
the candicates are ``'none'`` | ``'mean'`` | ``'sum'``. the candicates are ``'none'`` | ``'mean'`` | ``'sum'``.
...@@ -843,16 +843,15 @@ class NLLLoss(Layer): ...@@ -843,16 +843,15 @@ class NLLLoss(Layer):
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). name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
For more information, please refer to :ref:`api_guide_Name`.
Shape: 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]`. But in K-dimension situation, the shape is :math:`[N, C, d_1, d_2, ..., d_K]`.
The data type is float32, float64. 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. 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 `'none'`, the shape is `[N, *]`.
If `reduction` is `'sum'` or `'mean'`, the shape is `[1]`. If `reduction` is `'sum'` or `'mean'`, the shape is `[1]`.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册