mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
[c++] Distance-agnostic triplet margin loss (#45377)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/45377 This PR adds a C++ implementation of the TripletMarginWithDistanceLoss, for which the Python implementation was introduced in PR #43680. It's based on PR #44072, but I'm resubmitting this to unlink it from Phabricator. Test Plan: Imported from OSS Reviewed By: izdeby Differential Revision: D24003973 fbshipit-source-id: 2d9ada7260a6f27425ff2fdbbf623dad0fb79405
This commit is contained in:
committed by
Facebook GitHub Bot
parent
181afd5220
commit
c9bb990707
@@ -682,6 +682,56 @@ TEST_F(FunctionalTest, TripletMarginLoss) {
|
||||
ASSERT_TRUE(output.allclose(expected, 1e-04));
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, TripletMarginWithDistanceLossDefaultParity) {
|
||||
// Check that if we use torch::pairwise_distance with the default
|
||||
// TripletMarginLoss options as our distance function, the outputs
|
||||
// are equal (i.e., equal under defaults).
|
||||
|
||||
std::vector<TripletMarginWithDistanceLossOptions::reduction_t>
|
||||
reductions = {torch::kSum, torch::kMean, torch::kNone};
|
||||
std::vector<float> margins = {0.5, 1.0, 1.5};
|
||||
std::vector<bool> swaps = {true, false};
|
||||
|
||||
for (auto& reduction : reductions) {
|
||||
for (auto& margin : margins) {
|
||||
for (const auto& swap : swaps) {
|
||||
auto anchor =
|
||||
torch::randn({100, 128}, torch::dtype(torch::kFloat).requires_grad(true));
|
||||
auto positive =
|
||||
torch::randn({100, 128}, torch::dtype(torch::kFloat).requires_grad(true));
|
||||
auto negative =
|
||||
torch::randn({100, 128}, torch::dtype(torch::kFloat).requires_grad(true));
|
||||
|
||||
auto basicOptions = F::TripletMarginLossFuncOptions()
|
||||
.reduction(reduction)
|
||||
.margin(margin)
|
||||
.swap(swap);
|
||||
auto distanceOptions =
|
||||
F::TripletMarginWithDistanceLossFuncOptions()
|
||||
.reduction(reduction)
|
||||
.margin(margin)
|
||||
.swap(swap);
|
||||
TripletMarginLoss basicLoss(basicOptions);
|
||||
TripletMarginWithDistanceLoss distanceLoss(distanceOptions);
|
||||
|
||||
auto basicOutput =
|
||||
F::triplet_margin_loss(anchor, positive, negative, basicOptions);
|
||||
auto distanceOutput = F::triplet_margin_with_distance_loss(
|
||||
anchor, positive, negative, distanceOptions);
|
||||
|
||||
ASSERT_TRUE(distanceOutput.allclose(basicOutput, 1e-6, 1e-6));
|
||||
|
||||
// handle for torch::kNone reduction
|
||||
auto sum = distanceOutput.sum();
|
||||
sum.backward();
|
||||
ASSERT_EQ(anchor.sizes(), anchor.grad().sizes());
|
||||
ASSERT_EQ(positive.sizes(), positive.grad().sizes());
|
||||
ASSERT_EQ(negative.sizes(), negative.grad().sizes());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(FunctionalTest, NLLLoss) {
|
||||
auto input = torch::tensor({{-0.1315, -3.1315, -2.5315},
|
||||
{-3.7038, -0.1038, -2.6038},
|
||||
|
||||
Reference in New Issue
Block a user