summaryrefslogtreecommitdiff
path: root/utils/triplet_loss.py
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2021-03-25 15:46:31 +0800
committerJordan Gong <jordan.gong@protonmail.com>2021-03-25 15:46:31 +0800
commit8dd0c42dc341445987c6a907df909155a8b9abd2 (patch)
treec8b2ceedbb2513662b4cf151121ddf3ca2deb187 /utils/triplet_loss.py
parent478ecf50f3dd9e2c2f8ca62e6ecd4a65b2cc7c3a (diff)
parent104c6fbf0686828ed299b2a8bda1806a9b45f440 (diff)
Merge branch 'data_parallel' into data_parallel_py3.8data_parallel_py3.8
# Conflicts: # models/model.py
Diffstat (limited to 'utils/triplet_loss.py')
-rw-r--r--utils/triplet_loss.py9
1 files changed, 7 insertions, 2 deletions
diff --git a/utils/triplet_loss.py b/utils/triplet_loss.py
index 03fff21..5e3a97a 100644
--- a/utils/triplet_loss.py
+++ b/utils/triplet_loss.py
@@ -28,6 +28,7 @@ class BatchTripletLoss(nn.Module):
else: # is_all
positive_negative_dist = self._all_distance(dist, y, p, n)
+ non_zero_counts = None
if self.margin:
losses = F.relu(self.margin + positive_negative_dist).view(p, -1)
non_zero_counts = (losses != 0).sum(1).float()
@@ -35,14 +36,18 @@ class BatchTripletLoss(nn.Module):
loss_metric = self._none_zero_mean(losses, non_zero_counts)
else: # is_sum
loss_metric = losses.sum(1)
- return loss_metric, flat_dist, non_zero_counts
else: # Soft margin
losses = F.softplus(positive_negative_dist).view(p, -1)
if self.is_mean:
loss_metric = losses.mean(1)
else: # is_sum
loss_metric = losses.sum(1)
- return loss_metric, flat_dist, None
+
+ return {
+ 'loss': loss_metric,
+ 'dist': flat_dist,
+ 'counts': non_zero_counts
+ }
@staticmethod
def _batch_distance(x):