code: def helper(
      self,
      embeddings,
      labels,
      triplets,
      dtype,
      ref_emb=None,
      ref_labels=None,
  ):
      margin = 0.2
      loss_funcA = TripletMarginLoss(margin=margin)
      loss_funcB = TripletMarginLoss(
          margin=margin, reducer=MeanReducer()
      )
      loss_funcC = TripletMarginLoss(
          margin=margin, distance=CosineSimilarity()
      )
      loss_funcD = TripletMarginLoss(
          margin=margin,
          reducer=MeanReducer(),
          distance=CosineSimilarity(),
      )
      loss_funcE = TripletMarginLoss(margin=margin, smooth_loss=True)
  
      [lossA, lossB, lossC, lossD, lossE] = [
          x(embeddings, labels, ref_emb=ref_emb, ref_labels=ref_labels)
          for x in [
              loss_funcA,
              loss_funcB,
              loss_funcC,
              loss_funcD,
              loss_funcE,
          ]
      ]
  
      correct_loss = 0
      correct_loss_cosine = 0
      correct_smooth_loss = 0
      num_non_zero_triplets = 0
      num_non_zero_triplets_cosine = 0
      for a, p, n in triplets:
          anchor = embeddings[a]
          if ref_emb is not None:
              positive, negative = ref_emb[p], ref_emb[n]
          else:
              positive, negative = embeddings[p], embeddings[n]
          ap_dist = torch.sqrt(torch.sum((anchor - positive) ** 2))
          an_dist = torch.sqrt(torch.sum((anchor - negative) ** 2))
          curr_loss = torch.relu(ap_dist - an_dist + margin)
          curr_loss_cosine = torch.relu(
              torch.sum(anchor * negative)
              - torch.sum(anchor * positive)
              + margin
          )
          correct_smooth_loss += torch.nn.functional.softplus(
              ap_dist - an_dist + margin
          )
          if curr_loss > 0:
              num_non_zero_triplets += 1
          if curr_loss_cosine > 0:
              num_non_zero_triplets_cosine += 1
          correct_loss += curr_loss
          correct_loss_cosine += curr_loss_cosine
      rtol = 1e-2 if dtype == torch.float16 else 1e-5
      self.assertTrue(
          torch.isclose(
              lossA, correct_loss / num_non_zero_triplets, rtol=rtol
          )
      )
      self.assertTrue(
          torch.isclose(lossB, correct_loss / len(triplets), rtol=rtol)
      )
      self.assertTrue(
          torch.isclose(
              lossC,
              correct_loss_cosine / num_non_zero_triplets_cosine,
              rtol=rtol,
          )
      )
      self.assertTrue(
          torch.isclose(
              lossD, correct_loss_cosine / len(triplets), rtol=rtol
          )
      )
      self.assertTrue(
          torch.isclose(
              lossE, correct_smooth_loss / len(triplets), rtol=rtol
          )
      )
  
file_path: pytorch-metric-learning/tests/losses/test_triplet_margin_loss.py
imports: import unittest
  import torch
  from pytorch_metric_learning.distances import CosineSimilarity
  from pytorch_metric_learning.losses import TripletMarginLoss
  from pytorch_metric_learning.reducers import MeanReducer
  from ..None import TEST_DEVICE, TEST_DTYPES
  from ..zzz_testing_utils.testing_utils import angle_to_coord
  from .utils import get_triplet_embeddings_with_ref
label_desc: Use image node_id 3 for calling the TestTripletMarginLoss obj's underlying member method code with example usage: obj.helper(embeddings, labels, triplets, dtype, ref_emb, ref_labels) without return types