Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hackathon No.14 #4651

Merged
merged 15 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/api/paddle/nn/MultiLabelSoftMarginLoss_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.. _cn_api_paddle_nn_MultiLabelSoftMarginLoss:

MultiLabelSoftMarginLoss
-------------------------------

.. py:class:: paddle.nn.MultiLabelSoftMarginLoss(weight:Optional=None, reduction: str = 'mean', name:str=None)

该接口用于创建一个 MultiLabelSoftMarginLoss 的可调用类,MultiLabelSoftMarginLoss 计算输入 `input` 和 `label` 间的 `margin-based loss` 损失。


损失函数按照下列公式计算

.. math::
\text{loss}(x, y) = \sum_{ij}\frac{\max(0, 1 - (x[y[j]] - x[i]))}{\text{x.size}(0)}

如果添加权重则再乘以对应的权重值


最后,会添加 `reduce` 操作到前面的输出Out上。当 `reduction` 为 `none` 时,直接返回最原始的 `Out` 结果。当 `reduction` 为 `mean` 时,返回输出的均值 :math:`Out = MEAN(Out)` 。当 `reduction` 为 `sum` 时,返回输出的求和 :math:`Out = SUM(Out)` 。


参数
:::::::::
- **weight** (Tensor,可选) - 手动设定权重,默认为None
- **reduction** (str,可选) - 指定应用于输出结果的计算方式,可选值有: ``'none'``, ``'mean'``, ``'sum'`` 。默认为 ``'mean'``,计算 Loss 的均值;设置为 ``'sum'`` 时,计算 Loss 的总和;设置为 ``'none'`` 时,则返回原始Loss。
- **name** (str,可选) - 操作的名称(可选,默认值为None)。更多信息请参见 :ref:`api_guide_Name` 。

输入
:::::::::
- **input** (Tensor): - 输入 Tensor,维度是 [N, *], 其中 N 是 batch size, `*` 是任意数量的额外维度。数据类型为:float32、float64。
- **label** (Tensor): - 标签,维度是 [N, *], 与 ``input`` 相同,Tensor 中的值应该只包含 1 和 -1。数据类型为:float32、float64。

形状
:::::::::
- **input** (Tensor) - :math:`[N, *]` , 其中N是batch_size, `*` 是任意其他维度。数据类型是float32、float64。
- **label** (Tensor) - :math:`[N, *]` ,标签 ``label`` 的维度、数据类型与输入 ``input`` 相同。
- **output** (Tensor) - 输出的Tensor。如果 :attr:`reduction` 是 ``'none'``, 则输出的维度为 :math:`[N, *]` , 与输入 ``input`` 的形状相同。如果 :attr:`reduction` 是 ``'mean'`` 或 ``'sum'``, 则输出的维度为 :math:`[1]` 。


返回
:::::::::
返回计算 MultiLabelSoftMarginLoss 的可调用类。


代码示例
:::::::::
COPY-FROM: Paddle.nn.MultiLabelSoftMarginLoss
4 changes: 3 additions & 1 deletion docs/api/paddle/nn/Overview_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ Loss层
" :ref:`paddle.nn.SmoothL1Loss <cn_api_paddle_nn_SmoothL1Loss>` ", "平滑L1损失层"
" :ref:`paddle.nn.TripletMarginLoss <cn_api_paddle_nn_TripletMarginLoss>` ", "TripletMarginLoss层"
" :ref:`paddle.nn.TripletMarginWithDistanceLoss <cn_api_paddle_nn_TripletMarginWithDistanceLoss>` ", "TripletMarginWithDistanceLoss层"

" :ref:`paddle.nn.MultiLabelSoftMarginLoss <cn_api_paddle_nn_MultiLabelSoftMarginLoss>` ", "多标签Hinge损失层"

.. _vision_layers:

Vision层
Expand Down Expand Up @@ -483,6 +484,7 @@ Embedding相关函数
" :ref:`paddle.nn.functional.margin_cross_entropy <cn_api_paddle_nn_functional_margin_cross_entropy>` ", "支持 ``Arcface``,``Cosface``,``Sphereface`` 的结合 Margin 损失函数"
" :ref:`paddle.nn.functional.triplet_margin_loss <cn_api_paddle_nn_functional_triplet_margin_loss>` ", "用于计算TripletMarginLoss"
" :ref:`paddle.nn.functional.triplet_margin_with_distance_loss <cn_api_paddle_nn_functional_triplet_margin_with_distance_loss>` ", "用户自定义距离函数用于计算triplet margin loss 损失"
" :ref:`paddle.nn.functional.multi_label_soft_margin_loss <cn_api_nn_functional_multi_label_soft_margin_loss>` ", "用于计算多分类的hinge loss损失函数"

.. _common_functional:

Expand Down
43 changes: 43 additions & 0 deletions docs/api/paddle/nn/functional/multi_label_soft_margin_loss_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.. _cn_api_paddle_nn_functional_multi_label_soft_margin_loss:

multi_label_soft_margin_loss
-------------------------------

.. py:function:: paddle.nn.functional.multi_label_soft_margin_loss(input, label, weight:Optional=None, reduction: str = 'mean', name:str=None)

该 APIs 计算输入 `input` 和 `label` 间的 `margin-based loss` 损失。


损失函数按照下列公式计算

.. math::
\text{loss}(x, y) = \sum_{ij}\frac{\max(0, 1 - (x[y[j]] - x[i]))}{\text{x.size}(0)}

如果添加权重则再乘以对应的权重值


最后,添加 `reduce` 操作到前面的输出Out上。当 `reduction` 为 `none` 时,直接返回最原始的 `Out` 结果。当 `reduction` 为 `mean` 时,返回输出的均值 :math:`Out = MEAN(Out)` 。当 `reduction` 为 `sum` 时,返回输出的求和 :math:`Out = SUM(Out)` 。


参数
:::::::::
- **input** (Tensor) - :math:`[N, *]` , 其中N是batch_size, `*` 是任意其他维度。数据类型是float32、float64。
- **label** (Tensor) - :math:`[N, *]` , 标签 ``label`` 的维度、数据类型与输入 ``input`` 相同。
- **weight** (Tensor,可选) - 手动设定权重,默认为None
- **reduction** (str,可选) - 指定应用于输出结果的计算方式,可选值有: ``'none'``, ``'mean'``, ``'sum'`` 。默认为 ``'mean'``,计算 Loss 的均值;设置为 ``'sum'`` 时,计算 Loss 的总和;设置为 ``'none'`` 时,则返回原始 Loss。
- **name** (str,可选) - 操作的名称(可选,默认值为None)。更多信息请参见 :ref:`api_guide_Name` 。

形状
:::::::::
- **input** (Tensor) - :math:`[N, *]` , 其中N是batch_size, `*` 是任意其他维度。数据类型是float32、float64。
- **label** (Tensor) - :math:`[N, *]` ,标签 ``label`` 的维度、数据类型与输入 ``input`` 相同。
- **output** (Tensor) - 输出的Tensor。如果 :attr:`reduction` 是 ``'none'``, 则输出的维度为 :math:`[N, *]` , 与输入 ``input`` 的形状相同。如果 :attr:`reduction` 是 ``'mean'`` 或 ``'sum'``, 则输出的维度为 :math:`[1]` 。

返回
:::::::::
输出的Tensor。如果 :attr:`reduction` 是 ``'none'``, 则输出的维度为 :math:`[N, *]` , 与输入 ``input`` 的形状相同。如果 :attr:`reduction` 是 ``'mean'`` 或 ``'sum'``, 则输出的维度为 :math:`[1]` 。


代码示例
:::::::::
COPY-FROM: Paddle.nn.functional.multi_label_soft_margin_loss