-
Notifications
You must be signed in to change notification settings - Fork 826
新增MultiLabelMarginLoss #7331
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
Open
Qin-sx
wants to merge
1
commit into
PaddlePaddle:develop
Choose a base branch
from
Qin-sx:multilabelmarginloss
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
新增MultiLabelMarginLoss #7331
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
.. _cn_api_paddle_nn_MultiLabelMarginLoss: | ||
|
||
MultiLabelMarginLoss | ||
------------------------------- | ||
|
||
.. py:class:: paddle.nn.MultiLabelMarginLoss(reduction='mean', name=None) | ||
|
||
创建一个 MultiLabelMarginLoss 的可调用类。通过计算输入 `input` 和 `label` 间的多类别多分类问题的 `hinge loss (margin-based loss)` 损失。 | ||
|
||
损失函数计算每一个 mini-batch 的 loss 按照下列公式计算 | ||
|
||
.. math:: | ||
\text{loss}(input_i, label_i) = \frac{\sum_{j \in \text{valid_labels}} \sum_{k \neq \text{valid_labels}} \max(0, 1 - (input_i[\text{valid_labels}[j]] - input_i[k]))}{C} | ||
|
||
其中 :math:`C` 是类别数量, :math:`\text{valid_labels}` 包含样本 :math:`i` 所有非负的标签索引(遇到第一个 -1 时停止),:math:`k` 遍历除了 :math:`\text{valid_labels}` 之外的所有类别索引。 | ||
|
||
该损失函数只考虑前面的非负标签值,允许不同样本具有不同数量的目标类别。 | ||
|
||
参数 | ||
::::::::: | ||
- **reduction** (str,可选) - 指定应用于输出结果的计算方式,可选值有:``'none'``、``'mean'``、``'sum'``。默认为 ``'mean'``,计算 Loss 的均值;设置为 ``'sum'`` 时,计算 Loss 的总和;设置为 ``'none'`` 时,则返回原始 Loss。 | ||
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。 | ||
|
||
调用参数 | ||
::::::::: | ||
- **input** (Tensor) - 数据类型是 float32、float64。 | ||
- **label** (Tensor) - 标签的数据类型为 int32、int64。标签值应该是类别索引(非负值)和 -1 值。-1 值会被忽略并停止处理每个样本。 | ||
|
||
形状 | ||
::::::::: | ||
- **input** (Tensor) - :math:`[N, C]`,其中 N 是 batch_size, C 是类别数量。 | ||
- **label** (Tensor) - :math:`[N, C]`,与 input 形状相同。 | ||
- **output** (Tensor) - 输出的 Tensor。如果 :attr:`reduction` 是 ``'none'``,则输出的维度为 :math:`[N]`。如果 :attr:`reduction` 是 ``'mean'`` 或 ``'sum'``,则输出的维度为 :math:`[]` 。 | ||
|
||
返回 | ||
::::::::: | ||
返回计算 MultiLabelMarginLoss 的可调用对象。 | ||
|
||
代码示例 | ||
::::::::: | ||
COPY-FROM: paddle.nn.MultiLabelMarginLoss |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
docs/api/paddle/nn/functional/multi_label_margin_loss_cn.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.. _cn_api_paddle_nn_functional_multi_label_margin_loss: | ||
|
||
multi_label_margin_loss | ||
------------------------------- | ||
|
||
.. py:function:: paddle.nn.functional.multi_label_margin_loss(input, label, reduction='mean', name=None) | ||
|
||
计算输入 `input` 和 `label` 间的多类别多分类问题的 `hinge loss` 损失。 | ||
|
||
损失函数计算每一个 mini-batch 的 loss 按照下列公式计算 | ||
|
||
.. math:: | ||
\text{loss}(input_i, label_i) = \frac{\sum_{j \in \text{valid_labels}} \sum_{k \neq \text{valid_labels}} \max(0, 1 - (input_i[\text{valid_labels}[j]] - input_i[k]))}{C} | ||
|
||
其中 :math:`C` 是类别数量, :math:`\text{valid_labels}` 包含样本 :math:`i` 所有非负的标签索引(遇到第一个 -1 时停止),:math:`k` 遍历除了 :math:`\text{valid_labels}` 之外的所有类别索引。 | ||
|
||
该损失函数只考虑前面的非负标签值,允许不同样本具有不同数量的目标类别。 | ||
|
||
参数 | ||
::::::::: | ||
- **input** (Tensor) - :math:`[N, C]`,其中 N 是 batch_size, `C` 是类别数量。数据类型是 float32、float64。 | ||
- **label** (Tensor) - :math:`[N, C]`,与 input 形状相同。标签 ``label`` 的数据类型为 int32、int64。标签值应该是类别索引(非负值)和 -1 值。-1 值会被忽略并停止处理每个样本。 | ||
- **reduction** (str,可选) - 指定应用于输出结果的计算方式,可选值有:``'none'``, ``'mean'``, ``'sum'``。默认为 ``'mean'``,计算 Loss 的均值;设置为 ``'sum'`` 时,计算 Loss 的总和;设置为 ``'none'`` 时,则返回原始 Loss。 | ||
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。 | ||
|
||
形状 | ||
::::::::: | ||
- **input** (Tensor) - :math:`[N, C]`,其中 N 是 batch_size,`C` 是类别数量。数据类型是 float32、float64。 | ||
- **label** (Tensor) - :math:`[N, C]`,与 input 形状相同,标签 ``label`` 的数据类型为 int32、int64。 | ||
- **output** (Tensor) - 输出的 Tensor。如果 :attr:`reduction` 是 ``'none'``,则输出的维度为 :math:`[N]`,与 batch_size 相同。如果 :attr:`reduction` 是 ``'mean'`` 或 ``'sum'``,则输出的维度为 :math:`[]` 。 | ||
|
||
返回 | ||
::::::::: | ||
返回计算的 Loss。 | ||
|
||
代码示例 | ||
::::::::: | ||
COPY-FROM: paddle.nn.functional.multi_label_margin_loss |
44 changes: 44 additions & 0 deletions
44
...pytorch/api_difference/functional/torch.nn.functional.multilabel_margin_loss.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
## [ torch 参数更多 ]torch.nn.functional.multilabel_margin_loss | ||
|
||
### [torch.nn.functional.multilabel\_margin\_loss](https://pytorch.org/docs/stable/generated/torch.nn.functional.multilabel_margin_loss.html) | ||
|
||
```python | ||
torch.nn.functional.multilabel_margin_loss(input, target, size_average=None, reduce=None, reduction='mean') | ||
``` | ||
|
||
### [paddle.nn.functional.multi\_label\_margin\_loss](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/multi_label_margin_loss_cn.html#multi-label-margin-loss) | ||
|
||
```python | ||
paddle.nn.functional.multi_label_margin_loss(input, label, reduction='mean', name=None) | ||
``` | ||
|
||
PyTorch 相比 Paddle 支持更多其他参数,具体如下: | ||
|
||
### 参数映射 | ||
|
||
| PyTorch | PaddlePaddle | 备注 | | ||
| ------------ | ------------ | -- | | ||
| input | input | 输入 Tensor。 | | ||
| target | label | 标签 Tensor,仅参数名不一致。 | | ||
| size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | | ||
| reduce | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | | ||
| reduction | reduction | 指定应用于输出结果的计算方式。 | | ||
| - | name | Paddle 支持的操作名称,PyTorch 无此参数。 | | ||
|
||
### 转写示例 | ||
|
||
#### size_average、reduce | ||
```python | ||
# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 | ||
if size_average is None: | ||
size_average = True | ||
if reduce is None: | ||
reduce = True | ||
|
||
if size_average and reduce: | ||
reduction = 'mean' | ||
elif reduce: | ||
reduction = 'sum' | ||
else: | ||
reduction = 'none' | ||
``` |
41 changes: 41 additions & 0 deletions
41
...convert/convert_from_pytorch/api_difference/nn/torch.nn.MultiLabelMarginLoss.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
## [torch 参数更多]torch.nn.MultiLabelMarginLoss | ||
|
||
### [torch.nn.MultiLabelMarginLoss](https://pytorch.org/docs/stable/generated/torch.nn.MultiLabelMarginLoss.html#torch.nn.MultiLabelMarginLoss) | ||
|
||
```python | ||
torch.nn.MultiLabelMarginLoss(size_average=None, reduce=None, reduction='mean') | ||
``` | ||
|
||
### [paddle.nn.MultiLabelMarginLoss](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/MultiLabelMarginLoss_cn.html) | ||
|
||
```python | ||
paddle.nn.MultiLabelMarginLoss(reduction='mean', name=None) | ||
``` | ||
|
||
PyTorch 相比 Paddle 支持更多其他参数,具体如下: | ||
|
||
### 参数映射 | ||
|
||
| PyTorch | PaddlePaddle | 备注 | | ||
| ------------ | ------------ | ---------------------------------------------- | | ||
| size_average | - | 已废弃,和 reduce 组合决定损失计算方式。 | | ||
| reduce | - | 已废弃,和 size_average 组合决定损失计算方式。 | | ||
| reduction | reduction | 指定应用于输出结果的计算方式。 | | ||
| - | name | Paddle 支持的操作名称,PyTorch 无此参数。 | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个 name 参数统一不用管 |
||
|
||
### 转写示例 | ||
|
||
```python | ||
# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 | ||
if size_average is None: | ||
size_average = True | ||
if reduce is None: | ||
reduce = True | ||
|
||
if size_average and reduce: | ||
reduction = 'mean' | ||
elif reduce: | ||
reduction = 'sum' | ||
else: | ||
reduction = 'none' | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 name 参数统一不用管