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.30】 #34

Merged

Conversation

yangguohao
Copy link
Contributor

Create api_design_for_tripletmargindistanceloss.md

@dingjiaweiww
Copy link
Contributor

你的 PR 提交成功,感谢你对于开源项目的贡献,请检查 PR 提交格式和内容是否完备,具体请参考示例模版

@dingjiaweiww
Copy link
Contributor

PR 格式检查通过,你的PR 将接受Paddle 专家以及开源社区的review,请及时关注PR 动态

Copy link
Contributor

@shiyutang shiyutang left a comment

Choose a reason for hiding this comment

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

## 1、相关背景
为了提升飞桨API丰富度,支持科学计算领域API,Paddle需要扩充APIpaddle.nn.TripleMarginDistanceLoss以及paddle.nn.functional.triplet_margin_with_distance_loss
## 2、功能目标
paddle.nn.TripletMarginDistanceLoss 是三元损失函数,其针对 anchor 和正负对计算 P 范数距离下的三元损失,从而获得损失值。
Copy link
Contributor

Choose a reason for hiding this comment

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

描述不正确,不是P范数距离,而是任意给定距离函数

Comment on lines 22 to 24
对飞桨框架目前支持此功能的现状调研,如果不支持此功能,如是否可以有替代实现的API,是否有其他可绕过的方式,或者用其他API组合实现的方式;
目前paddle缺少相关功能实现。
需要独立设计实现相关的函数
Copy link
Contributor

Choose a reason for hiding this comment

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

说明语言可以去除,并且可以对相关替代方式进行说明

Comment on lines 27 to 39
Pytorch 中有相关的`torch.nn.functional.triplet_margin_with_distance_loss(anchor, positive, negative, margin=1.0, p=2, eps=1e-06, swap=False, size_average=None, reduce=None, reduction='mean') -> Tensor`和`torch.nn.TripletMarginLoss(margin=1.0, p=2.0, eps=1e-06, swap=False, size_average=None, reduce=None, reduction='mean') -> Tensor`

在 pytorch 中,介绍为:

> Creates a criterion that measures the triplet loss given an input tensors $x 1, x 2, x 3$ and a margin with a value greater than 0 . This is used for measuring a relative similarity between samples. A triplet is composed by a, $p$ and $n$ (i.e., anchor, positive examples and negative examples respectively). The shapes of all input tensors should be $(N, D)$.
>
> The distance swap is described in detail in the paper Learning shallow convolutional feature descriptors with triplet losses by V. Balntas, E. Riba et al.
> The loss function for each sample in the mini-batch is:
>
> $$
> L(a, p, n)=\max \left\{d\left(a_{i}, p_{i}\right)-d\left(a_{i}, n_{i}\right)+\operatorname{margin}, 0\right\}
> $$
>
Copy link
Contributor

Choose a reason for hiding this comment

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

这部分需要有关键代码和方案分析:https://github.com/pytorch/pytorch/blob/cfb6c942fed64dbb81ccc4f14b2a6650123af2e1/torch/nn/functional.py#L4564
另外tf的方案也可以寻找一下并同样说明



# 四、对比分析
paddle 和 pytorch整体框架相似,故直接采用其方法进行设计。
Copy link
Contributor

Choose a reason for hiding this comment

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

对比分析需要对比方案和选择原因

Comment on lines +51 to +52
- `paddle.nn.TripletMarginDistanceLoss(margin=1.0, distance_function=None, swap=False, reduction='mean', name=None) -> Tensor`
- `padde.nn.functional.triplet_margin_with_distance_loss(input, positive, negative, distance_function=None, margin=1.0, swap=False, reduction='mean', name=None) -> Tensor`
Copy link
Contributor

Choose a reason for hiding this comment

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

需要删除说明文字,并补充对于参数和输入的维度说明

Comment on lines 71 to 73
1.动态图,静态图,要与np计算下的结果输出需要一致。
2.自定义distanc_function动态图静态图下输出一致。
2.在swap下,动态图静态图输出结果一致。
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

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

@yangguohao 已review哈,请按上述描述优化你的RFC,感谢~

@paddle-bot-old
Copy link

你的PR有最新反馈,请及时修改。
There’s the latest feedback about your PR. Please check.

@shiyutang
Copy link
Contributor

如果修改完整,请rerequest-review
image

Copy link
Contributor

@shiyutang shiyutang left a comment

Choose a reason for hiding this comment

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

部分实现细节需要修改

1. 检查参数

1. 检查 reduction 有效性(同其余 functional loss 中的实现)
2. 检查输入的 dtype(含 `input`、`positive`、`negative`)(同其余 functional loss 中的实现)
Copy link
Contributor

Choose a reason for hiding this comment

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

输入损失的不一定为(N,dim),需要检查维度之后进行维度转换


2. 计算

1. 用户可传入distance_function参数,如果未指定则使用 `paddle.nn.PairWiseDistance` 分别计算得到正锚点与样本和负锚点与样本的距离。
Copy link
Contributor

Choose a reason for hiding this comment

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

可以替换为paddle.linalg.pnorm

- 2.CPU、GPU下计算一致。
- 3.各reduction下计算一致
- 4.各参数输入有效。
- 5.反向梯度的正确性。
Copy link
Contributor

Choose a reason for hiding this comment

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

目前已经调整基于python 进行组合的API不需要进行反向梯度的验证

Copy link
Contributor

@shiyutang shiyutang left a comment

Choose a reason for hiding this comment

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

LGTM

@shiyutang shiyutang merged commit d0c1818 into PaddlePaddle:master Mar 23, 2022
@paddle-bot-old
Copy link

你的PR已合入community库,请进行后续代码开发,并将代码提交至Paddle仓库。
Your PR has been merged into community repository. Please move on coding part and submit your code to corresponding repo.

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.

6 participants