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

Add triplet loss for metric lerning. #11461

Closed
wants to merge 19 commits into from

Conversation

wanghaoshuang
Copy link
Contributor

@wanghaoshuang wanghaoshuang commented Jun 14, 2018

@wanghaoshuang
Copy link
Contributor Author

wanghaoshuang commented Jun 25, 2018

@BigFishMaster 你那里有dense_triplet_loss比较专业的英文文档么?

@BigFishMaster
Copy link
Contributor

@wanghaoshuang 我稍后看看哈

Copy link
Contributor

@qingqing01 qingqing01 left a 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>;
Copy link
Contributor

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));
Copy link
Contributor

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>;
Copy link
Contributor

Choose a reason for hiding this comment

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

DIM1 -> Array1 ?
DIM2 -> Array2?

Copy link
Contributor

@qingqing01 qingqing01 left a 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)$$
Copy link
Contributor

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.");
Copy link
Contributor

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
Copy link
Contributor

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):
Copy link
Contributor

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]. ");
Copy link
Contributor

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
Copy link
Contributor

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,
Copy link
Contributor

Choose a reason for hiding this comment

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

forward里有relu,这里的gradient check有随机错误吗?

@wanghaoshuang wanghaoshuang deleted the triplet branch May 20, 2022 03:56
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

3 participants