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
Hackathon No.26 #4301
Changes from 15 commits
1658baa
4447200
c70b155
36f86b9
56bbbbb
f77ee23
c9ae1a7
0ae27b2
73ef28d
e142011
03d5db6
7951669
0ffaad6
7a6465d
73fbccf
8b38389
a1ddf14
df1ec4e
62f96f0
7b965be
c69f80b
110fb36
1f2f74d
ba2d550
915461e
df72325
dc19e06
0aabfdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.. _cn_api_paddle_nn_TripletMarginLoss: | ||
|
||
TripletMarginLoss | ||
------------------------------- | ||
|
||
.. py:class:: paddle.nn.TripletMarginLoss(margin: float = 1.0, p: float = 2., eps: float = 1e-6, swap: bool = False,reduction: str = 'mean') | ||
|
||
创建一个TripletMarginLoss的可调用类。通过计算输入 `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\} | ||
|
||
|
||
其中的 | ||
|
||
.. math:: | ||
d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p | ||
|
||
|
||
``p`` 为距离函数的范数。 ``margin`` 为(input,positive)与(input,negative)的距离间隔。 | ||
|
||
最后,添加 `reduce` 操作到前面的输出Out上。当 `reduction` 为 `none` 时,直接返回最原始的 `Out` 结果。当 `reduction` 为 `mean` 时,返回输出的均值 :math:`Out = MEAN(Out)` 。当 `reduction` 为 `sum` 时,返回输出的求和 :math:`Out = SUM(Out)` 。 | ||
|
||
|
||
参数 | ||
::::::::: | ||
- **p** (float,可选) - 手动指定范数,默认为2。 | ||
- **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]` 。 | ||
|
||
返回 | ||
::::::::: | ||
返回计算TripletMarginLoss的可调用对象。 | ||
|
||
代码示例 | ||
::::::::: | ||
COPY-FROM: paddle.nn.TripletMarginLoss |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
.. _cn_api_paddle_nn_functional_triplet_margin_loss: | ||
|
||
triplet_margin_loss | ||
------------------------------- | ||
|
||
.. 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, *)`,`*` 是任意其他维度。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改 |
||
|
||
|
||
损失函数按照下列公式计算 | ||
|
||
.. math:: | ||
L(input, pos, neg) = \max \{d(input_i, pos_i) - d(input_i, neg_i) + {\rm margin}, 0\} | ||
|
||
距离函数为 | ||
|
||
.. math:: | ||
d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_2 | ||
|
||
|
||
|
||
``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>`_。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 注意文档不要过于口语化: |
||
|
||
最后,添加 `reduce` 操作到前面的输出Out上。当 `reduction` 为 `none` 时,直接返回最原始的 `Out` 结果。当 `reduction` 为 `mean` 时,返回输出的均值 :math:`Out = MEAN(Out)` 。当 `reduction` 为 `sum` 时,返回输出的求和 :math:`Out = SUM(Out)` 。 | ||
|
||
|
||
参数 | ||
::::::::: | ||
- **input** (Tensor) - :math:`[N, * ]` , 其中N是batch_size, `*` 是任意其他维度。数据类型是float32、float64。 | ||
- **positive** (Tensor) - :math:`[N, *]` ,正样本。 | ||
- **negative** (Tensor) - :math:`[N, *]` ,负样本。 | ||
- **p** (float,可选) - 手动指定范数,默认为2。 | ||
- **swap** (bool,可选) - 默认为False。 | ||
- **margin** (float,可选) - 手动指定间距,默认为1。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. swap和margin换一下,注意和源码的参数顺序保持一致 |
||
- **reduction** (str,可选) - 指定应用于输出结果的计算方式,可选值有: ``'none'``, ``'mean'``, ``'sum'`` 。默认为 ``'mean'``,计算 Loss 的均值;设置为 ``'sum'`` 时,计算 Loss 的总和;设置为 ``'none'`` 时,则返回原始Loss。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改 |
||
- **name** (str,可选) - 操作的名称(可选,默认值为None)。更多信息请参见 :ref:`api_guide_Name` 。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 源码中有name参数吗? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有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]` 。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改 |
||
|
||
返回 | ||
::::::::: | ||
返回计算的Loss。 | ||
|
||
代码示例 | ||
::::::::: | ||
COPY-FROM: paddle.nn.functional.triplet_margin_loss |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改