summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2021-03-01 11:20:34 +0800
committerJordan Gong <jordan.gong@protonmail.com>2021-03-01 11:20:34 +0800
commit4bdc37bbd86a83647bbbda7bd1367c08e6c6f6d4 (patch)
tree88840ce856b023b97138958f4b2cb7e2875bdd0e
parentfed5e6a9b35fda8306147e9ce772dfbf3142a061 (diff)
Remove identical sample in Batch All case
-rw-r--r--utils/triplet_loss.py5
1 files changed, 4 insertions, 1 deletions
diff --git a/utils/triplet_loss.py b/utils/triplet_loss.py
index db0cf0f..6822cf6 100644
--- a/utils/triplet_loss.py
+++ b/utils/triplet_loss.py
@@ -68,7 +68,10 @@ class BatchTripletLoss(nn.Module):
@staticmethod
def _all_distance(dist, y, p, n):
- positive_mask = y.unsqueeze(1) == y.unsqueeze(2)
+ # Unmask identical samples
+ positive_mask = torch.eye(
+ n, dtype=torch.bool, device=y.device
+ ) ^ (y.unsqueeze(1) == y.unsqueeze(2))
negative_mask = y.unsqueeze(1) != y.unsqueeze(2)
all_positive = dist[positive_mask].view(p, n, -1, 1)
all_negative = dist[negative_mask].view(p, n, 1, -1)