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 #4644

Merged
merged 30 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
de3d418
Create TripletMarginWithDistanceLoss_cn.rst
yangguohao Apr 17, 2022
8e80927
Merge branch 'PaddlePaddle:develop' into triplet_margin_distance_loss
yangguohao Apr 17, 2022
b1eab7c
Update Overview_cn.rst
yangguohao Apr 17, 2022
0b59483
Merge branch 'PaddlePaddle:develop' into triplet_margin_distance_loss
yangguohao Apr 18, 2022
6d80ea4
Merge branch 'PaddlePaddle:develop' into triplet_margin_distance_loss
yangguohao Apr 27, 2022
817a924
Update Overview_cn.rst
yangguohao Apr 27, 2022
f271539
Update TripletMarginWithDistanceLoss_cn.rst
yangguohao Apr 27, 2022
8ea441d
Create triplet_margin_with_distance_loss_cn.rst
yangguohao Apr 27, 2022
9654503
Update TripletMarginWithDistanceLoss_cn.rst
yangguohao Apr 27, 2022
7c7f772
Update triplet_margin_with_distance_loss_cn.rst
yangguohao Apr 27, 2022
36cb64c
Merge branch 'PaddlePaddle:develop' into triplet_margin_distance_loss
yangguohao Apr 28, 2022
21674d5
Merge branch 'PaddlePaddle:develop' into triplet_margin_distance_loss
yangguohao May 5, 2022
8fd9e9e
Update TripletMarginWithDistanceLoss_cn.rst
yangguohao May 5, 2022
0aedc77
Update triplet_margin_with_distance_loss_cn.rst
yangguohao May 5, 2022
ced2866
Update TripletMarginWithDistanceLoss_cn.rst
yangguohao May 5, 2022
58fe4d3
Update triplet_margin_with_distance_loss_cn.rst
yangguohao May 5, 2022
824d904
Update triplet_margin_with_distance_loss_cn.rst
yangguohao May 5, 2022
35687d7
Update TripletMarginWithDistanceLoss_cn.rst
yangguohao May 5, 2022
b2204f3
Update TripletMarginWithDistanceLoss_cn.rst
yangguohao May 7, 2022
2f7d3f8
Update triplet_margin_with_distance_loss_cn.rst
yangguohao May 9, 2022
6aa92a1
Update TripletMarginWithDistanceLoss_cn.rst
yangguohao May 9, 2022
ba6b33d
Update triplet_margin_with_distance_loss_cn.rst
yangguohao May 9, 2022
a5e4d8d
Update triplet_margin_with_distance_loss_cn.rst
yangguohao May 9, 2022
988c79f
Update triplet_margin_with_distance_loss_cn.rst
yangguohao May 17, 2022
899c9b5
Update TripletMarginWithDistanceLoss_cn.rst
yangguohao May 17, 2022
532ad3e
Update Overview_cn.rst
yangguohao May 17, 2022
580cc9d
Update TripletMarginWithDistanceLoss_cn.rst
yangguohao May 18, 2022
ed91ef5
Update triplet_margin_with_distance_loss_cn.rst
yangguohao May 18, 2022
bf3ae2c
Update triplet_margin_with_distance_loss_cn.rst
yangguohao May 20, 2022
f1d8878
Merge branch 'develop' into triplet_margin_distance_loss
Ligoml Jun 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api/paddle/nn/Overview_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ Loss层
" :ref:`paddle.nn.MSELoss <cn_api_paddle_nn_MSELoss>` ", "均方差误差损失层"
" :ref:`paddle.nn.NLLLoss <cn_api_nn_loss_NLLLoss>` ", "NLLLoss层"
" :ref:`paddle.nn.SmoothL1Loss <cn_api_paddle_nn_SmoothL1Loss>` ", "平滑L1损失层"
" :ref:`paddle.nn.TripletMarginWithDistanceLoss <cn_api_paddle_nn_TripletMarginWithDistanceLoss>` ", "TripletMarginWithDistanceLoss层"

.. _vision_layers:

Expand Down Expand Up @@ -475,6 +476,7 @@ Embedding相关函数
" :ref:`paddle.nn.functional.smooth_l1_loss <cn_paddle_nn_functional_loss_smooth_l1>` ", "用于计算平滑L1损失"
" :ref:`paddle.nn.functional.softmax_with_cross_entropy <cn_api_fluid_layers_softmax_with_cross_entropy>` ", "将softmax操作、交叉熵损失函数的计算过程进行合并"
" :ref:`paddle.nn.functional.margin_cross_entropy <cn_api_paddle_nn_functional_margin_cross_entropy>` ", "支持 ``Arcface``,``Cosface``,``Sphereface`` 的结合 Margin 损失函数"
" :ref:`paddle.nn.functional.triplet_margin_with_distance_loss <cn_api_paddle_nn_functional_triplet_margin_with_distance_loss>` ", "用户自定义距离函数用于计算triplet margin loss 损失"

.. _common_functional:

Expand Down
50 changes: 50 additions & 0 deletions docs/api/paddle/nn/TripletMarginWithDistanceLoss_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.. _cn_api_paddle_nn_TripletMarginWithDistanceLoss:

TripletMarginWithDistanceLoss
-------------------------------

.. py:class:: paddle.nn.TripletMarginWithDistanceLoss(distance_function=None, margin: float = 1.0, swap: bool = False, reduction: str = 'mean', name:str=None)

创建一个TripletMarginWithDistanceLoss的可调用类,通过计算输入 `input` 和 `positive` 和 `negative` 间的 `triplet margin loss` 损失,测量样本之间,即 `input` 与 `positive examples` 和 `negative examples` 的相对相似性。


损失函数按照下列公式计算

.. math::
L(input, pos, neg) = \max \{d(input_i, pos_i) - d(input_i, neg_i) + {\rm margin}, 0\}


其中的距离函数 ``distance_function`` 可以由用户自定义,使用 lambda 或是 def 都可以。如果未定义则调用2范数计算距离

.. math::
d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_2


``margin`` 为(input,positive)与(input,negative)的距离间隔, ``swap`` 为True时,会比较(input,negative)和(positive,negative)的大小,并将(input,negative)的值换为其中较小的值,内容详见论文 `Learning shallow convolutional feature descriptors with triplet losses <http://www.bmva.org/bmvc/2016/papers/paper119/paper119.pdf>`_。

最后,该api会添加 `reduce` 操作到前面的输出Out上。当 `reduction` 为 `none` 时,直接返回最原始的 `Out` 结果。当 `reduction` 为 `mean` 时,
返回输出的均值 :math:`Out = MEAN(Out)` 。当 `reduction` 为 `sum` 时,返回输出的求和 :math:`Out = SUM(Out)` 。


参数
:::::::::
- **distance_function** (可选) - 手动指定范数,默认为None, 使用欧式距离。
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • 参数的数据类型需要增加
  • , -->

- **margin** (float,可选) - 手动指定间距,默认为1。
- **swap** (bool,可选) - 默认为False。
- **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.

image

- 同TripletMarginLoss,需要注意中英文标点符号问题 - reduction显示不正常

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

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.

已修改


形状
:::::::::
- **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]` 。

返回
:::::::::
返回计算 TripletMarginWithDistanceLoss 的可调用对象。

代码示例
:::::::::
COPY-FROM: paddle.nn.TripletMarginWithDistanceLoss
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.. _cn_api_paddle_nn_functional_triplet_margin_with_distance_loss:

triplet_margin_with_distance_loss
-------------------------------

.. py:function:: paddle.nn.functional.triplet_margin_with_distance_loss(input, positive, negative, distance_function=None, swap: bool = False, margin: float = 1.0, reduction: str = 'mean', name:str=None)

计算输入 `input` 和 `positive` 和 `negative` 间的 `triplet margin loss` 损失。


损失函数按照下列公式计算

.. math::
L(input, pos, neg) = \max \{d(input_i, pos_i) - d(input_i, neg_i) + {\rm margin}, 0\}


其中的距离函数 ``distance_function`` 可以由用户自定义,使用 lambda 或是 def 都可以。如果未定义则调用2范数计算距离

.. math::
d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_2


``margin`` 为(input,positive)与(input,negative)的距离间隔, ``swap`` 为True时,会比较(input,negative)和(positive,negative)的大小,并将(input,negative)的值换为其中较小的值,内容详见论文 `Learning shallow convolutional feature descriptors with triplet losses <http://www.bmva.org/bmvc/2016/papers/paper119/paper119.pdf>`_。

最后,该算子会添加 `reduce` 操作到前面的输出Out上。当 `reduction` 为 `none` 时,直接返回最原始的 `Out` 结果。当 `reduction` 为 `mean` 时,返回输出的均值 :math:`Out = MEAN(Out)` 。当 `reduction` 为 `sum` 时,返回输出的求和 :math:`Out = SUM(Out)` 。


参数
:::::::::
Copy link
Collaborator

Choose a reason for hiding this comment

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

参考TripletMarginWithDistanceLoss进行修改

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, *]` ,正样本,维度、数据类型与输入 ``input`` 相同。
- **negative** (Tensor) - :math:`[N, *]` ,负样本,维度、数据类型与输入 ``input`` 相同。
- **distance_function** (Callable,可选) - 手动指定范数,默认为 None, 计算欧式距离。
- **swap** (bool,可选) - 默认为 False。
- **margin** (float,可选) - 手动指定间距,默认为1。
- **reduction** (str,可选) - 指定应用于输出结果的计算方式,可选值有: ``'none'``, ``'mean'``, ``'sum'`` 。默认为 ``'mean'``,计算 Loss 的均值;设置为 ``'sum'`` 时,计算 Loss 的总和;设置为 ``'none'`` 时,则返回原始 Loss。
- **name** (str,可选) - 操作的名称(可选,默认值为None)。更多信息请参见 :ref:`api_guide_Name` 。

形状
:::::::::
- **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]` 。

返回
:::::::::
输出的Tensor。如果 :attr:`reduction` 是 ``'none'``, 则输出的维度为 :math:`[N, *]` , 与输入 ``input`` 的形状相同。如果 :attr:`reduction` 是 ``'mean'`` 或 ``'sum'``, 则输出的维度为 :math:`[1]` 。

代码示例
:::::::::
COPY-FROM: paddle.nn.functional.triplet_margin_with_distance_loss