Skip to content

Conversation

@zty-king
Copy link
Contributor

17种存在size_average与reduce的loss,添加size_average+reduce->reduction的转写示例

@paddle-bot
Copy link

paddle-bot bot commented Nov 13, 2025

感谢你贡献飞桨文档,文档预览构建中,Docs-New 跑完后即可预览,预览链接:http://preview-pr-7632.paddle-docs-preview.paddlepaddle.org.cn/documentation/docs/zh/api/index_cn.html
预览工具的更多说明,请参考:飞桨文档预览工具

@github-actions
Copy link

github-actions bot commented Nov 13, 2025

📚 本次 PR 文档预览链接(点击展开)
ℹ️ 预览提醒
请等待 Docs-NEW 流水线运行完成后再点击预览链接,否则可能会看到旧版本内容或遇到链接无法访问的情况。
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCELoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.BCEWithLogitsLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CosineEmbeddingLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.CrossEntropyLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.HingeEmbeddingLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.KLDivLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.L1Loss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MSELoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MarginRankingLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelMarginLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiLabelSoftMarginLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.MultiMarginLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.NLLLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.PoissonNLLLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SmoothL1Loss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.SoftMarginLoss.md: 点击预览
  • docs/guides/model_convert/convert_from_pytorch/api_difference/torch_more_args/torch.nn.TripletMarginLoss.md: 点击预览

@zty-king
Copy link
Contributor Author

@zhwesky2010 您有空了看看这个格式是否可行,这里CI报错但是报错文档打不开,实在不知道为什么有问题

# PyTorch 写法
torch.nn.BCELoss(size_average=True)
torch.nn.BCELoss(weight=w, size_average=False, reduce=True)
torch.nn.BCELoss(weight=w, size_average=False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一种写法
torch.nn.BCELoss(weight=w, reduction='sum)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里我看不是之前都只给size_average+reduce到reduction的转写示例嘛,reduction到reduction是一一对应的关系,还要写出来吗

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个也可以不写,是一一对应的


# Paddle 写法
paddle.nn.BCELoss(reduction='mean')
paddle.nn.BCELoss(weight=w, reduction='sum')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释一下:

## 以上写法都统一对应到如下写法


#### size_average
size_average 为 True
#### reduction 为 sum
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一共有34种用法,还有torch.nn.functional.* 下面有17个

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个路径下的loss,我看paddle都没支持size_average,reduce参数,要支持一下吗

Copy link
Collaborator

@zhwesky2010 zhwesky2010 Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个路径下的loss,我看paddle都没支持size_average,reduce参数,要支持一下吗

需要支持。同时之前的的legacy_reduction_decorator也需要在loss.py的functional API里都支持下。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由于loss.py下都是函数(之前的17个loss是类),所以需要在原来的legacy_reduction_decorator里新增一个判断,因为原来算位置参数需要跳过self,如果是函数的话就不用跳过,会引入新的开销;或者就再写一个装饰器,不过大部分代码都可以复用,您看哪个好一点

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由于loss.py下都是函数(之前的17个loss是类),所以需要在原来的legacy_reduction_decorator里新增一个判断,因为原来算位置参数需要跳过self,如果是函数的话就不用跳过,会引入新的开销;或者就再写一个装饰器,不过大部分代码都可以复用,您看哪个好一点

@zhwesky2010 您有空了看一下这个哈

@zty-king
Copy link
Contributor Author

zty-king commented Nov 18, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants