From cbe4988e4f3e1d80c976796db6e41684d21ea1bf Mon Sep 17 00:00:00 2001 From: zty-king <17786324919@163.com> Date: Wed, 5 Nov 2025 12:24:36 +0000 Subject: [PATCH 1/2] fix the loss docs --- .../torch_more_args/torch.nn.BCELoss.md | 9 +- .../torch.nn.BCEWithLogitsLoss.md | 9 +- .../torch.nn.CosineEmbeddingLoss.md | 84 +++++++++++++++--- .../torch.nn.CrossEntropyLoss.md | 9 +- .../torch.nn.HingeEmbeddingLoss.md | 83 +++++++++++++++--- .../torch_more_args/torch.nn.KLDivLoss.md | 81 ++++++++++++++--- .../torch_more_args/torch.nn.L1Loss.md | 81 ++++++++++++++--- .../torch_more_args/torch.nn.MSELoss.md | 9 -- .../torch.nn.MarginRankingLoss.md | 81 ++++++++++++++--- .../torch.nn.MultiLabelMarginLoss.md | 84 +++++++++++++++--- .../torch.nn.MultiLabelSoftMarginLoss.md | 84 +++++++++++++++--- .../torch.nn.MultiMarginLoss.md | 87 +++++++++++++++---- .../torch_more_args/torch.nn.NLLLoss.md | 9 -- .../torch.nn.PoissonNLLLoss.md | 85 ++++++++++++++---- .../torch_more_args/torch.nn.SmoothL1Loss.md | 16 ++-- .../torch.nn.SoftMarginLoss.md | 83 +++++++++++++++--- .../torch.nn.TripletMarginLoss.md | 84 +++++++++++++++--- 17 files changed, 802 insertions(+), 176 deletions(-) diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCELoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCELoss.md index b9805cb8922..5cd295e7960 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCELoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCELoss.md @@ -25,7 +25,8 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | reduction | reduction | 表示应用于输出结果的计算方式。 | ### 转写示例 -#### size_averagesize_average 为 True +#### size_average +size_average 为 True ```python # PyTorch 写法 torch.nn.BCELoss(weight=w, size_average=True) @@ -43,7 +44,8 @@ torch.nn.BCELoss(weight=w, size_average=False) paddle.nn.BCELoss(weight=w, reduction='sum') ``` -#### reducereduce 为 True +#### reduce +reduce 为 True ```python # PyTorch 写法 torch.nn.BCELoss(weight=w, reduce=True) @@ -61,7 +63,8 @@ torch.nn.BCELoss(weight=w, reduce=False) paddle.nn.BCELoss(weight=w, reduction='none') ``` -#### reductionreduction 为'none' +#### reduction +reduction 为'none' ```python # PyTorch 写法 torch.nn.BCELoss(weight=w, reduction='none') diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCEWithLogitsLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCEWithLogitsLoss.md index f11f2925181..a05a0bc8ffe 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCEWithLogitsLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCEWithLogitsLoss.md @@ -28,7 +28,8 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | pos_weight | pos_weight | 表示正类的权重。 | ### 转写示例 -#### size_averagesize_average 为 True +#### size_average +size_average 为 True ```python # PyTorch 写法 torch.nn.BCEWithLogitsLoss(weight=w, size_average=True) @@ -46,7 +47,8 @@ torch.nn.BCEWithLogitsLoss(weight=w, size_average=False) paddle.nn.BCEWithLogitsLoss(weight=w, reduction='sum') ``` -#### reducereduce 为 True +#### reduce +reduce 为 True ```python # PyTorch 写法 torch.nn.BCEWithLogitsLoss(weight=w, reduce=True) @@ -64,7 +66,8 @@ torch.nn.BCEWithLogitsLoss(weight=w, reduce=False) paddle.nn.BCEWithLogitsLoss(weight=w, reduction='none') ``` -#### reductionreduction 为'none' +#### reduction +reduction 为'none' ```python # PyTorch 写法 torch.nn.BCEWithLogitsLoss(weight=w, reduction='none') diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CosineEmbeddingLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CosineEmbeddingLoss.md index 186efe01e13..b095446bf9b 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CosineEmbeddingLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CosineEmbeddingLoss.md @@ -21,17 +21,77 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | reduction | reduction | 指定应用于输出结果的计算方式。 | ### 转写示例 +#### size_average +size_average 为 True + ```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' +# PyTorch 写法 +torch.nn.CosineEmbeddingLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.CosineEmbeddingLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.CosineEmbeddingLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.CosineEmbeddingLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.CosineEmbeddingLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.CosineEmbeddingLoss(weight=w, reduction='mean') +``` + +reduce 为 False + +```python +# PyTorch 写法 +torch.nn.CosineEmbeddingLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.CosineEmbeddingLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.CosineEmbeddingLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.CosineEmbeddingLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.CosineEmbeddingLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.CosineEmbeddingLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.CosineEmbeddingLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.CosineEmbeddingLoss(weight=w, reduction='sum') +``` +reduction = 'none' ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CrossEntropyLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CrossEntropyLoss.md index 4f5bd45cccd..f6b69e18ead 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CrossEntropyLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CrossEntropyLoss.md @@ -36,7 +36,8 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | - | axis | 进行 softmax 计算的维度索引,PyTorch 无此参数,Paddle 保持默认即可。 | ### 转写示例 -#### size_averagesize_average 为 True +#### size_average +size_average 为 True ```python # PyTorch 写法 torch.nn.CrossEntropyLoss(weight=w, size_average=True) @@ -54,7 +55,8 @@ torch.nn.CrossEntropyLoss(weight=w, size_average=False) paddle.nn.CrossEntropyLoss(weight=w, reduction='sum') ``` -#### reducereduce 为 True +#### reduce +reduce 为 True ```python # PyTorch 写法 torch.nn.CrossEntropyLoss(weight=w, reduce=True) @@ -72,7 +74,8 @@ torch.nn.CrossEntropyLoss(weight=w, reduce=False) paddle.nn.CrossEntropyLoss(weight=w, reduction='none') ``` -#### reductionreduction 为'none' +#### reduction +reduction 为'none' ```python # PyTorch 写法 torch.nn.CrossEntropyLoss(weight=w, reduction='none') diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.HingeEmbeddingLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.HingeEmbeddingLoss.md index bb68acdc67d..91e8bda22c4 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.HingeEmbeddingLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.HingeEmbeddingLoss.md @@ -27,17 +27,76 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 转写示例 #### size_average +size_average 为 True + ```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' +# PyTorch 写法 +torch.nn.HingeEmbeddingLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.HingeEmbeddingLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.HingeEmbeddingLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.HingeEmbeddingLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.HingeEmbeddingLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.HingeEmbeddingLoss(weight=w, reduction='mean') +``` + +reduce 为 False + +```python +# PyTorch 写法 +torch.nn.HingeEmbeddingLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.HingeEmbeddingLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.HingeEmbeddingLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.HingeEmbeddingLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.HingeEmbeddingLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.HingeEmbeddingLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.HingeEmbeddingLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.HingeEmbeddingLoss(weight=w, reduction='sum') +``` +reduction = 'none' ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.KLDivLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.KLDivLoss.md index 44b60df0963..a80b9250c6c 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.KLDivLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.KLDivLoss.md @@ -25,17 +25,74 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 转写示例 #### size_average +size_average 为 True + +```python +# PyTorch 写法 +torch.nn.KLDivLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.KLDivLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.KLDivLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.KLDivLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.KLDivLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.KLDivLoss(weight=w, reduction='mean') +``` + +reduce 为 False + ```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' +# PyTorch 写法 +torch.nn.KLDivLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.KLDivLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.KLDivLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.KLDivLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.KLDivLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.KLDivLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.KLDivLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.KLDivLoss(weight=w, reduction='sum') ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.L1Loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.L1Loss.md index 27d625ae85f..c7155ebfc1c 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.L1Loss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.L1Loss.md @@ -23,17 +23,74 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 转写示例 #### size_average +size_average 为 True + +```python +# PyTorch 写法 +torch.nn.L1Loss(size_average=True) + +# Paddle 写法 +paddle.nn.L1Loss(reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.L1Loss(size_average=False) + +# Paddle 写法 +paddle.nn.L1Loss(reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.L1Loss(reduce=True) + +# Paddle 写法 +paddle.nn.L1Loss(reduction='mean') +``` + +reduce 为 False + ```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' +# PyTorch 写法 +torch.nn.L1Loss(reduce=False) + +# Paddle 写法 +paddle.nn.L1Loss(reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.L1Loss(reduction='none') + +# Paddle 写法 +paddle.nn.L1Loss(reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.L1Loss(reduction='mean') + +# Paddle 写法 +paddle.nn.L1Loss(reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.L1Loss(reduction='sum') + +# Paddle 写法 +paddle.nn.L1Loss(reduction='sum') ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MSELoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MSELoss.md index 433eb5fbcd3..adfef1dfc58 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MSELoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MSELoss.md @@ -21,15 +21,6 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | reduction | reduction | 表示对输出结果的计算方式。 | ### 转写示例 -#### size_average -```python -# Paddle 写法 -torch.nn.MSELoss(size_average=True) - -# Paddle 写法 -paddle.nn.MSELoss(reduction='mean') -``` - #### size_average size_average 为 True diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MarginRankingLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MarginRankingLoss.md index 98eb855c012..e6e92148b17 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MarginRankingLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MarginRankingLoss.md @@ -27,17 +27,74 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 转写示例 #### size_average +size_average 为 True + +```python +# PyTorch 写法 +torch.nn.MarginRankingLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.MarginRankingLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.MarginRankingLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.MarginRankingLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.MarginRankingLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.MarginRankingLoss(weight=w, reduction='mean') +``` + +reduce 为 False + ```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' +# PyTorch 写法 +torch.nn.MarginRankingLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.MarginRankingLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.MarginRankingLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.MarginRankingLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.MarginRankingLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.MarginRankingLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.MarginRankingLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.MarginRankingLoss(weight=w, reduction='sum') ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelMarginLoss.md index 936dafc0c66..b733ab856c8 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelMarginLoss.md @@ -20,17 +20,77 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | reduction | reduction | 指定应用于输出结果的计算方式。 | ### 转写示例 +#### size_average +size_average 为 True + ```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' +# PyTorch 写法 +torch.nn.MultiLabelMarginLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.MultiLabelMarginLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.MultiLabelMarginLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.MultiLabelMarginLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.MultiLabelMarginLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.MultiLabelMarginLoss(weight=w, reduction='mean') +``` + +reduce 为 False + +```python +# PyTorch 写法 +torch.nn.MultiLabelMarginLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.MultiLabelMarginLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.MultiLabelMarginLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.MultiLabelMarginLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.MultiLabelMarginLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.MultiLabelMarginLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.MultiLabelMarginLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.MultiLabelMarginLoss(weight=w, reduction='sum') +``` +reduction = 'none' ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md index e42656734a6..1f0f4c2ad54 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md @@ -21,17 +21,77 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | reduction | reduction | 指定应用于输出结果的计算方式。 | ### 转写示例 +#### size_average +size_average 为 True + ```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' +# PyTorch 写法 +torch.nn.MultiLabelSoftMarginLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.MultiLabelSoftMarginLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.MultiLabelSoftMarginLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.MultiLabelSoftMarginLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.MultiLabelSoftMarginLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.MultiLabelSoftMarginLoss(weight=w, reduction='mean') +``` + +reduce 为 False + +```python +# PyTorch 写法 +torch.nn.MultiLabelSoftMarginLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.MultiLabelSoftMarginLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.MultiLabelSoftMarginLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.MultiLabelSoftMarginLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.MultiLabelSoftMarginLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.MultiLabelSoftMarginLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.MultiLabelSoftMarginLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.MultiLabelSoftMarginLoss(weight=w, reduction='sum') +``` +reduction = 'none' ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiMarginLoss.md index 713da40d42d..cefa8ae4bc2 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiMarginLoss.md @@ -23,20 +23,77 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | reduction | reduction | 指定应用于输出结果的计算方式,可选值有 `none`、`mean` 和 `sum`。默认为 `mean`,计算 mini-batch loss 均值。设置为 `sum` 时,计算 mini-batch loss 的总和。设置为 `none` 时,则返回 loss Tensor。默认值下为 `mean`。 | ### 转写示例 +#### size_average +size_average 为 True + ```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' - -# 如果 PyTorch 存在 reduction 参数,则直接覆盖 -if 'reduction' not in kwargs: - kwargs['reduction'] = reduction +# PyTorch 写法 +torch.nn.MultiMarginLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.MultiMarginLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.MultiMarginLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.MultiMarginLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.MultiMarginLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.MultiMarginLoss(weight=w, reduction='mean') +``` + +reduce 为 False + +```python +# PyTorch 写法 +torch.nn.MultiMarginLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.MultiMarginLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.MultiMarginLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.MultiMarginLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.MultiMarginLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.MultiMarginLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.MultiMarginLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.MultiMarginLoss(weight=w, reduction='sum') +``` +reduction = 'none' ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.NLLLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.NLLLoss.md index f9f20d00838..270510e7b4e 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.NLLLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.NLLLoss.md @@ -29,15 +29,6 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | reduction | reduction | 表示应用于输出结果的计算方式。 | ### 转写示例 -#### size_average -```python -# Paddle 写法 -torch.nn.NLLLoss(size_average=True) - -# Paddle 写法 -paddle.nn.NLLLoss(reduction='mean') -``` - #### size_average size_average 为 True diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.PoissonNLLLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.PoissonNLLLoss.md index d7cb3058cba..87fc2fcbd35 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.PoissonNLLLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.PoissonNLLLoss.md @@ -23,20 +23,75 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | reduction | reduction | 指定应用于输出结果的计算方式,可选值有 `none`、`mean` 和 `sum`。默认为 `mean`,计算 mini-batch loss 均值。设置为 `sum` 时,计算 mini-batch loss 的总和。设置为 `none` 时,则返回 loss Tensor。默认值下为 `mean`。两者完全一致。 | ### 转写示例 +#### size_average +size_average 为 True + +```python +# PyTorch 写法 +torch.nn.PoissonNLLLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.PoissonNLLLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.PoissonNLLLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.PoissonNLLLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.PoissonNLLLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.PoissonNLLLoss(weight=w, reduction='mean') +``` + +reduce 为 False + ```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' - -# 如果 PyTorch 存在 reduction 参数,则直接覆盖 -if 'reduction' not in kwargs: - kwargs['reduction'] = reduction +# PyTorch 写法 +torch.nn.PoissonNLLLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.PoissonNLLLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.PoissonNLLLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.PoissonNLLLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.PoissonNLLLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.PoissonNLLLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.PoissonNLLLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.PoissonNLLLoss(weight=w, reduction='sum') ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SmoothL1Loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SmoothL1Loss.md index a81d3efa0b1..3ea1a12e759 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SmoothL1Loss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SmoothL1Loss.md @@ -28,15 +28,7 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 转写示例 #### size_average -```python -# Paddle 写法 -torch.nn.SmoothL1Loss(size_average=True) - -# Paddle 写法 -paddle.nn.SmoothL1Loss(reduction='mean') -``` - -#### size_averagesize_average 为 True +size_average 为 True ```python # PyTorch 写法 torch.nn.SmoothL1Loss(size_average=True) @@ -54,7 +46,8 @@ torch.nn.SmoothL1Loss(size_average=False) paddle.nn.SmoothL1Loss(reduction='sum') ``` -#### reducereduce 为 True +#### reduce +reduce 为 True ```python # PyTorch 写法 torch.nn.SmoothL1Loss(reduce=True) @@ -72,7 +65,8 @@ torch.nn.SmoothL1Loss(reduce=False) paddle.nn.SmoothL1Loss(reduction='none') ``` -#### reductionreduction 为'none' +#### reduction +reduction 为'none' ```python # PyTorch 写法 torch.nn.SmoothL1Loss(reduction='none') diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SoftMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SoftMarginLoss.md index 6868324c3ba..6e8afad2049 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SoftMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SoftMarginLoss.md @@ -24,17 +24,76 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 转写示例 #### size_average +size_average 为 True + ```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' +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='mean') +``` + +reduce 为 False + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='sum') +``` +reduction = 'none' ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.TripletMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.TripletMarginLoss.md index b074981e813..ae2f33d1fba 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.TripletMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.TripletMarginLoss.md @@ -24,17 +24,77 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | reduction | reduction | 指定应用于输出结果的计算方式。 | ### 转写示例 +#### size_average +size_average 为 True + ```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' +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, size_average=True) + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='mean') +``` + +size_average 为 False + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, size_average=False) + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='sum') +``` + +#### reduce +reduce 为 True + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduce=True) + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='mean') +``` + +reduce 为 False + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduce=False) + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='none') +``` + +#### reduction +reduction 为'none' + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduction='none') + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='none') +``` + +reduction 为'mean' + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduction='mean') + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='mean') +``` + +reduction 为'sum' + +```python +# PyTorch 写法 +torch.nn.TripletMarginLoss(weight=w, reduction='sum') + +# Paddle 写法 +paddle.nn.TripletMarginLoss(weight=w, reduction='sum') +``` +reduction = 'none' ``` From bb38b3e4d57865241806046b5cba4e34492301ae Mon Sep 17 00:00:00 2001 From: zty-king <17786324919@163.com> Date: Thu, 6 Nov 2025 05:07:27 +0000 Subject: [PATCH 2/2] fix --- .../torch_more_args/torch.nn.CosineEmbeddingLoss.md | 2 -- .../torch_more_args/torch.nn.HingeEmbeddingLoss.md | 2 -- .../torch_more_args/torch.nn.MultiLabelMarginLoss.md | 2 -- .../torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md | 2 -- .../api_difference/torch_more_args/torch.nn.MultiMarginLoss.md | 2 -- .../api_difference/torch_more_args/torch.nn.SoftMarginLoss.md | 2 -- .../torch_more_args/torch.nn.TripletMarginLoss.md | 2 -- 7 files changed, 14 deletions(-) diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CosineEmbeddingLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CosineEmbeddingLoss.md index b095446bf9b..5f58e0303aa 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CosineEmbeddingLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CosineEmbeddingLoss.md @@ -93,5 +93,3 @@ torch.nn.CosineEmbeddingLoss(weight=w, reduction='sum') # Paddle 写法 paddle.nn.CosineEmbeddingLoss(weight=w, reduction='sum') ``` -reduction = 'none' -``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.HingeEmbeddingLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.HingeEmbeddingLoss.md index 91e8bda22c4..b098c562767 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.HingeEmbeddingLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.HingeEmbeddingLoss.md @@ -98,5 +98,3 @@ torch.nn.HingeEmbeddingLoss(weight=w, reduction='sum') # Paddle 写法 paddle.nn.HingeEmbeddingLoss(weight=w, reduction='sum') ``` -reduction = 'none' -``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelMarginLoss.md index b733ab856c8..e21e9406749 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelMarginLoss.md @@ -92,5 +92,3 @@ torch.nn.MultiLabelMarginLoss(weight=w, reduction='sum') # Paddle 写法 paddle.nn.MultiLabelMarginLoss(weight=w, reduction='sum') ``` -reduction = 'none' -``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md index 1f0f4c2ad54..604265f4e40 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md @@ -93,5 +93,3 @@ torch.nn.MultiLabelSoftMarginLoss(weight=w, reduction='sum') # Paddle 写法 paddle.nn.MultiLabelSoftMarginLoss(weight=w, reduction='sum') ``` -reduction = 'none' -``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiMarginLoss.md index cefa8ae4bc2..e45cda33913 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiMarginLoss.md @@ -95,5 +95,3 @@ torch.nn.MultiMarginLoss(weight=w, reduction='sum') # Paddle 写法 paddle.nn.MultiMarginLoss(weight=w, reduction='sum') ``` -reduction = 'none' -``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SoftMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SoftMarginLoss.md index 6e8afad2049..02c1eedd7a9 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SoftMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SoftMarginLoss.md @@ -95,5 +95,3 @@ torch.nn.TripletMarginLoss(weight=w, reduction='sum') # Paddle 写法 paddle.nn.TripletMarginLoss(weight=w, reduction='sum') ``` -reduction = 'none' -``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.TripletMarginLoss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.TripletMarginLoss.md index ae2f33d1fba..eb7e7d35688 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.TripletMarginLoss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.TripletMarginLoss.md @@ -96,5 +96,3 @@ torch.nn.TripletMarginLoss(weight=w, reduction='sum') # Paddle 写法 paddle.nn.TripletMarginLoss(weight=w, reduction='sum') ``` -reduction = 'none' -```