-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add triplet loss for metric lerning. #11461
Conversation
2. Add python wrapper.
@BigFishMaster 你那里有 |
@wanghaoshuang 我稍后看看哈 |
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.
Please complete the doc, especially the loss calculation formula, for conveniently review. Thanks! @wanghaoshuang @BigFishMaster
|
||
template <typename T, int MajorType = Eigen::RowMajor, | ||
typename IndexType = Eigen::DenseIndex> | ||
using EigenVector = framework::EigenVector<T, MajorType, IndexType>; |
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.
EigenVector
is not used in following code.
auto blas = math::GetBlas<DeviceContext, T>(context); | ||
auto x_mat = math::CreateMatrixDescriptor(x_dims, 0, false); | ||
auto x_mat_trans = math::CreateMatrixDescriptor(x_dims, 0, true); | ||
blas.MatMul(*logits, x_mat, *logits, x_mat_trans, T(1), &distances, T(0)); |
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.
Since the input is 2D tensor, there is no need to use math::CreateMatrixDescriptor
, refer the MatMul inferface:
https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/fluid/operators/math/blas_impl.h#L176
using EigenVector = framework::EigenVector<T, MajorType, IndexType>; | ||
|
||
using DIM1 = Eigen::array<int, 1>; | ||
using DIM2 = Eigen::array<int, 2>; |
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.
DIM1 -> Array1 ?
DIM2 -> Array2?
…o origin/triplet
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.
非常赞的公式推导!
- a negative sample with a different class | ||
We define the three samples as $a$, $p$, $n$. Then the loss of | ||
the triplet (a, p, n) is: | ||
$$L = max(d(a, p) - d(a, n) + margin, 0)$$ |
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.
前后留空格。
"computed with loss together in one call. It is a 2-D Tensor of " | ||
"the shape [N, feature_len].") | ||
.AsIntermediate(); | ||
AddAttr<float>("margin", "(float), The min margin between two sample."); |
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.
min -> minimum
We define the three samples as $a$, $p$, $n$. Then the loss of | ||
the triplet (a, p, n) is: | ||
$$L = max(d(a, p) - d(a, n) + margin, 0)$$ | ||
In which, $d(a, p)$ means the distance between $a$ and $p$. The negative should |
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.
distance -> L2 distance ?
@@ -5184,3 +5185,52 @@ def crop(x, shape=None, offsets=None, name=None): | |||
outputs={'Out': out}, | |||
attrs=None if len(attrs) == 0 else attrs) | |||
return out | |||
|
|||
|
|||
def dense_triplet_loss(input, label, margin=0.01): |
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.
Add name:
dense_triplet_loss(input, label, margin=0.01, name=None)
"and K is the feature length in each sample."); | ||
AddInput("Label", | ||
"(Tensor) The ground truth which is a 2-D tensor. " | ||
"Label is a Tensor<int64> with shape [N x 1]. "); |
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.
这里要求Label是按index排过序的吗?
self.batch_size = 9 | ||
self.feature_len = 3 | ||
self.class_num = 4 | ||
self.eps = 0.01 |
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.
self.eps -> self.margin ?
self.check_grad( | ||
["Logits"], | ||
"Loss", | ||
max_relative_error=0.05, |
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.
forward里有relu,这里的gradient check有随机错误吗?
前向的计算过程:https://wanghaoshuang.github.io/2018/07/Dense-Triplet_Loss/