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

[Hackthon No.26] #31

Merged
merged 18 commits into from
Mar 23, 2022
Merged

Conversation

yangguohao
Copy link
Contributor

No description provided.

@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. 整体有较多位置修改,请参考案例:https://github.com/PaddlePaddle/community/blob/master/rfcs/APIs/20200301_api_design_for_quantile.md
  2. 另外需要恢复误删的 rfcs/APIs/api_design_template.md 文件

Comment on lines 27 to 45
Pytorch 中有相关的`torch.nn.functional.triplet_margin_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\}
> $$
>
> where
>
> $$
> d\left(x_{i}, y_{i}\right)=\left\|\mathbf{x}_{i}-\mathbf{y}_{i}\right\|_{p}
> $$

Copy link
Contributor

Choose a reason for hiding this comment

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

为 paddle 框架中新增计算损失函数的方法

# 二、飞桨现状
对飞桨框架目前支持此功能的现状调研,如果不支持此功能,如是否可以有替代实现的API,是否有其他可绕过的方式,或者用其他API组合实现的方式;
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 23 to 24
目前paddle缺少相关功能实现。
需要独立设计实现相关的函数
Copy link
Contributor

Choose a reason for hiding this comment

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

可以补充其他loss的实现方式,例如MSE Loss,从而明确参数规范等

目前paddle缺少相关功能实现。
需要独立设计实现相关的函数

# 三、业内方案调研
Copy link
Contributor

Choose a reason for hiding this comment

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

> $$

# 四、对比分析
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.

需要对torch和tf实现对比,二者的对参数的支持,输入维度的支持,距离函数的选择等都有所不同,这些都可以对比,并说明 paddle 的实现中应当如何选择

Copy link
Contributor

Choose a reason for hiding this comment

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

即两种方案的优劣

# 五、设计思路与实现方案

## 命名与参数设计
参考:[飞桨API 设计及命名规范]
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 55 to 56
- `paddle.nn.TripletMarginLoss(margin=1.0, p=2.0, epsilon=1e-06, swap=False, reduction='mean', name=None) -> Tensor`
- `padde.nn.functional.triplet_margin_loss(input, positive, negative, margin=1.0, p=2, epsilon=1e-06, 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.

输入和参数支持的维度、参数范围可以进一步说明


1. 检查 reduction 有效性(同其余 functional loss 中的实现)
2. 检查输入的 dtype(含 `anchor`、`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.

  1. 检查 anchorpositivenegative 三者维度是否对齐


测试考虑的case如下:
1.动态图,静态图,要与np计算下的结果输出需要一致。
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.

@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.

方案实现细节存在修改部分


2. 计算

1. 使用 `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.norm 底层和nn.PairwiseDistance 调用相同的 pnorm OP,并且可以直接进行函数调用


```
padde.nn.functional.triplet_margin_loss(input Tensor[float64 or float32] 维度为[batch_size,dim]
positive, 1 or -1 Tensor[float64 or float32],维度为[batch_size,dim] negative, 1 or -1 Tensor[float64 or float32],维度为[batch_size,dim]
Copy link
Contributor

Choose a reason for hiding this comment

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

为什么positive和negative需要限制为-1/1的tensor?

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.

由于输入损失的tensor不一定为(N,Dim)的形状,还需要对输入进行维度转换

@yangguohao yangguohao requested a review from shiyutang March 22, 2022 16:38
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 e79c634 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.

5 participants