From f9009c6e712c3fd7a716b7725ab36535a1189113 Mon Sep 17 00:00:00 2001 From: Qin-sx Date: Mon, 23 Jun 2025 10:32:37 +0800 Subject: [PATCH] added MultiLabelMarginLoss new file: docs/api/paddle/nn/MultiLabelMarginLoss_cn modified: docs/api/paddle/nn/Overview_cn.rst new file: docs/api/paddle/nn/functional/multi_label_margin_loss_cn.rst new file: docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_margin_loss.md new file: docs/guides/model_convert/convert_from_pytorch/api_difference/nn/torch.nn.MultiLabelMarginLoss.md --- docs/api/paddle/nn/MultiLabelMarginLoss_cn | 41 +++++++++++++++++ docs/api/paddle/nn/Overview_cn.rst | 2 + .../functional/multi_label_margin_loss_cn.rst | 38 ++++++++++++++++ ...ch.nn.functional.multilabel_margin_loss.md | 44 +++++++++++++++++++ .../nn/torch.nn.MultiLabelMarginLoss.md | 41 +++++++++++++++++ 5 files changed, 166 insertions(+) create mode 100644 docs/api/paddle/nn/MultiLabelMarginLoss_cn create mode 100644 docs/api/paddle/nn/functional/multi_label_margin_loss_cn.rst create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_margin_loss.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/nn/torch.nn.MultiLabelMarginLoss.md diff --git a/docs/api/paddle/nn/MultiLabelMarginLoss_cn b/docs/api/paddle/nn/MultiLabelMarginLoss_cn new file mode 100644 index 00000000000..88ff2039138 --- /dev/null +++ b/docs/api/paddle/nn/MultiLabelMarginLoss_cn @@ -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 diff --git a/docs/api/paddle/nn/Overview_cn.rst b/docs/api/paddle/nn/Overview_cn.rst index f8927abff6a..02f7bd24aa7 100644 --- a/docs/api/paddle/nn/Overview_cn.rst +++ b/docs/api/paddle/nn/Overview_cn.rst @@ -281,6 +281,7 @@ Loss 层 " :ref:`paddle.nn.TripletMarginWithDistanceLoss ` ", "TripletMarginWithDistanceLoss 层" " :ref:`paddle.nn.MultiLabelSoftMarginLoss ` ", "多标签 Hinge 损失层" " :ref:`paddle.nn.MultiMarginLoss ` ", "MultiMarginLoss 层" + " :ref:`paddle.nn.MultiLabelMarginLoss ` ", "MultiLabelMarginLoss 层" " :ref:`paddle.nn.AdaptiveLogSoftmaxWithLoss ` ", "自适应 logsoftmax 损失类" @@ -523,6 +524,7 @@ Embedding 相关函数 " :ref:`paddle.nn.functional.hinge_embedding_loss ` ", "计算输入 input 和标签 label(包含 1 和 -1) 间的 `hinge embedding loss` 损失" " :ref:`paddle.nn.functional.rnnt_loss ` ", "计算 RNNT loss,也可以叫做 softmax with RNNT" " :ref:`paddle.nn.functional.multi_margin_loss ` ", "用于计算 multi margin loss 损失函数" + " :ref:`paddle.nn.functional.multi_label_margin_loss ` ", "用于计算 multi label margin loss 损失函数" " :ref:`paddle.nn.functional.adaptive_log_softmax_with_loss ` ", "自适应 logsoftmax 损失函数" diff --git a/docs/api/paddle/nn/functional/multi_label_margin_loss_cn.rst b/docs/api/paddle/nn/functional/multi_label_margin_loss_cn.rst new file mode 100644 index 00000000000..34218826abc --- /dev/null +++ b/docs/api/paddle/nn/functional/multi_label_margin_loss_cn.rst @@ -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 diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_margin_loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_margin_loss.md new file mode 100644 index 00000000000..dbda331b098 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_margin_loss.md @@ -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' +``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/nn/torch.nn.MultiLabelMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/nn/torch.nn.MultiLabelMarginLoss.md new file mode 100644 index 00000000000..af6b83ecdde --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/nn/torch.nn.MultiLabelMarginLoss.md @@ -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 无此参数。 | + +### 转写示例 + +```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' +```