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.26 #4301

Merged
merged 28 commits into from Jun 10, 2022
Merged

Hackathon No.26 #4301

merged 28 commits into from Jun 10, 2022

Conversation

yangguohao
Copy link
Contributor

@yangguohao yangguohao commented Mar 13, 2022

TripletMarginLoss API 中文文档

PADDLEPADDLE_PR=40487

【Hackathon No.26】PaddlePaddle/Paddle#40487

@paddle-bot-old
Copy link

paddle-bot-old bot commented Mar 13, 2022

✅ This PR's description meets the template requirements!
Please wait for other CI results.

@paddle-bot-old
Copy link

Thanks for your contribution!


.. py:class:: paddle.nn.TripletMarginLoss(margin: float = 1.0, p: float = 2., eps: float = 1e-6, swap: bool = False,reduction: str = 'mean')

该OP可创建一个TripletMarginLoss的可调用类,计算输入 `input` 和 `positive` 和 `negative` 间的 `triplet margin loss` 损失。
Copy link
Collaborator

Choose a reason for hiding this comment

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

去掉「该OP」的描述,API和OP概念尽量不要混用~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p


然后, ``p`` 为距离函数的范数。 ``margin`` 为(input,positive)与(input,negative)的距离间隔, ``swap`` 的内容可以看论文Learning shallow convolutional feature descriptors with triplet losses by V. Balntas, E. Riba et al. 。
Copy link
Collaborator

Choose a reason for hiding this comment

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

论文最好附上链接,参考格式:
Learning shallow convolutional feature descriptors with triplet losses<http://www.bmva.org/bmvc/2016/papers/paper119/paper119.pdf>_

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

- **swap** (bool,可选)
- **margin** (float,可选) - 手动指定间距,默认为1
- **reduction**(str,可选) -指定应用于输出结果的计算方式,可选值有: ``'none'``, ``'mean'``, ``'sum'`` 。默认为 ``'mean'``,计算 Loss 的均值;设置为 ``'sum'`` 时,计算 Loss 的总和;设置为 ``'none'`` 时,则返回原始Loss。
- **name** (str,可选) - 操作的名称(可选,默认值为None)。更多信息请参见 :ref:`api_guide_Name` 。
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • 每一个参数都需要介绍,swap也需要
  • 中英文标点不要混用「,」-->「,」,参数末尾注意加「。」
  • 注意中英文文档需要同步修改

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

negative = paddle.to_tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]], dtype=paddle.float32)
triplet_margin_loss = L.TripletMarginLoss(margin=1.0, reduction='none')

loss = triplet_margin_loss(input, positive, negative,)
Copy link
Collaborator

Choose a reason for hiding this comment

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

另外需要增加 paddle.nn.functional.triplet_margin_loss的文档吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已添加

:::::::::

.. code-block:: python

Copy link
Collaborator

Choose a reason for hiding this comment

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

代码建议使用copy-from的方式,参考文档

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改


.. py:class:: paddle.nn.functional.triplet_margin_loss(input, positive, negative, p:float = 2.0, margin: float = 1.0, swap: bool = False, reduction: str = 'mean')

该 api 计算输入 `input` 和 `positive` 和 `negative` 间的 `triplet margin loss` 损失,测量 `input`与 `positive examples` 和 `negative examples` 之间的相对相似性。所有输入张量的形状都为 :math:`(N, *)`,`*` 是任意其他维度。
Copy link
Collaborator

Choose a reason for hiding this comment

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

image

注意 ``需要后面加空格才会生效

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改




``p`` 为距离函数的范数。 ``margin`` 为(input,positive)与(input,negative)的距离间隔, ``swap`` 的内容可以看论文 `Learning shallow convolutional feature descriptors with triplet losses <http://www.bmva.org/bmvc/2016/papers/paper119/paper119.pdf>`_。
Copy link
Collaborator

Choose a reason for hiding this comment

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

注意文档不要过于口语化:swap 的内容详见论文

- **negative** (Tensor) - :math:`[N, *]` ,负样本。
- **p** (float,可选) - 手动指定范数,默认为2。
- **swap** (bool,可选) - 默认为False。
- **margin** (float,可选) - 手动指定间距,默认为1。
Copy link
Collaborator

Choose a reason for hiding this comment

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

swapmargin换一下,注意和源码的参数顺序保持一致

- **swap** (bool,可选) - 默认为False。
- **margin** (float,可选) - 手动指定间距,默认为1。
- **reduction** (str,可选) - 指定应用于输出结果的计算方式,可选值有: ``'none'``, ``'mean'``, ``'sum'`` 。默认为 ``'mean'``,计算 Loss 的均值;设置为 ``'sum'`` 时,计算 Loss 的总和;设置为 ``'none'`` 时,则返回原始Loss。
- **name** (str,可选) - 操作的名称(可选,默认值为None)。更多信息请参见 :ref:`api_guide_Name` 。
Copy link
Collaborator

Choose a reason for hiding this comment

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

源码中有name参数吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

有name参数

- **p** (float,可选) - 手动指定范数,默认为2。
- **swap** (bool,可选) - 默认为False。
- **margin** (float,可选) - 手动指定间距,默认为1。
- **reduction** (str,可选) - 指定应用于输出结果的计算方式,可选值有: ``'none'``, ``'mean'``, ``'sum'`` 。默认为 ``'mean'``,计算 Loss 的均值;设置为 ``'sum'`` 时,计算 Loss 的总和;设置为 ``'none'`` 时,则返回原始Loss。
Copy link
Collaborator

Choose a reason for hiding this comment

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

, -->

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

- **input** (Tensor) - :math:`[N, * ]` , 其中N是batch_size, `*` 是任意其他维度。数据类型是float32、float64。
- **positive** (Tensor) - :math:`[N, *]` ,标签 ``positive`` 的维度、数据类型与输入 ``input`` 相同。
- **negative** (Tensor) - :math:`[N, *]` ,标签 ``negative`` 的维度、数据类型与输入 ``input`` 相同。
- **output** (Tensor) - 输出的Tensor。如果 :attr:`reduction` 是 ``'none'``, 则输出的维度为 :math:`[N, *]` , 与输入 ``input`` 的形状相同。如果 :attr:`reduction` 是 ``'mean'`` 或 ``'sum'``, 则输出的维度为 :math:`[1]` 。
Copy link
Collaborator

Choose a reason for hiding this comment

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

, -->

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

@Ligoml
Copy link
Collaborator

Ligoml commented May 13, 2022

CI没过,辛苦再检查一下
image

Copy link
Collaborator

@Ligoml Ligoml left a comment

Choose a reason for hiding this comment

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

LGTM

@Ligoml Ligoml merged commit 1f0fff2 into PaddlePaddle:develop Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants