code: def test_get_random_triplet_indices(self):
      for dtype in TEST_DTYPES:
          for _ in range(10):
              labels = (
                  torch.randint(low=0, high=5, size=(100,))
                  .to(TEST_DEVICE)
                  .type(dtype)
              )
              a, p, n = lmu.get_random_triplet_indices(labels)
  
              l_a, l_p, l_n = labels[a], labels[p], labels[n]
              self.assertTrue(torch.all(a != p))
              self.assertTrue(torch.all(l_a == l_p))
              self.assertTrue(torch.all(l_p != l_n))
  
              labels = (
                  torch.randint(low=0, high=5, size=(100,))
                  .to(TEST_DEVICE)
                  .type(dtype)
              )
              ref_labels = (
                  torch.randint(low=0, high=5, size=(50,))
                  .to(TEST_DEVICE)
                  .type(dtype)
              )
              a, p, n = lmu.get_random_triplet_indices(
                  labels, ref_labels=ref_labels
              )
  
              l_a = labels[a]
              l_p = ref_labels[p]
              l_n = ref_labels[n]
              self.assertTrue(torch.all(l_a == l_p))
              self.assertTrue(torch.all(l_p != l_n))
  
              # Test one-hot weights.
              labels = (
                  torch.randint(0, 3, (10,)).to(TEST_DEVICE).type(dtype)
              )
              w = (
                  torch.zeros((len(labels), len(labels)))
                  .to(TEST_DEVICE)
                  .type(dtype)
              )
              for i, label in enumerate(labels):
                  ind = torch.where(labels != label)[0]
                  j = ind[torch.randint(0, len(ind), (1,))[0]]
                  w[i, j] = 1.0
              a, p, n = lmu.get_random_triplet_indices(
                  labels, t_per_anchor=1, weights=w
              )
              self.assertTrue(torch.all(w[a, n] == 1.0))
  
              # Test one-hot weights.
              labels = (
                  torch.randint(0, 3, (100,))
                  .to(TEST_DEVICE)
                  .type(dtype)
              )
              ref_labels = (
                  torch.randint(0, 3, (50,)).to(TEST_DEVICE).type(dtype)
              )
              w = (
                  torch.zeros((len(labels), len(ref_labels)))
                  .to(TEST_DEVICE)
                  .type(dtype)
              )
              for i, label in enumerate(labels):
                  ind = torch.where(ref_labels != label)[0]
                  j = ind[torch.randint(0, len(ind), (1,))[0]]
                  w[i, j] = 1.0
              a, p, n = lmu.get_random_triplet_indices(
                  labels,
                  ref_labels=ref_labels,
                  t_per_anchor=1,
                  weights=w,
              )
              self.assertTrue(torch.all(w[a, n] == 1.0))
  
              # test no possible triplets
              labels = torch.arange(100).to(TEST_DEVICE).type(dtype)
              a, p, n = lmu.get_random_triplet_indices(labels)
              self.assertTrue(len(a) == len(p) == len(n) == 0)
  
              labels = torch.zeros(100).to(TEST_DEVICE).type(dtype)
              a, p, n = lmu.get_random_triplet_indices(labels)
              self.assertTrue(len(a) == len(p) == len(n) == 0)
  
              labels = torch.zeros(100).to(TEST_DEVICE).type(dtype)
              ref_labels = torch.ones(100).to(TEST_DEVICE).type(dtype)
              a, p, n = lmu.get_random_triplet_indices(
                  labels, ref_labels=ref_labels
              )
              self.assertTrue(len(a) == len(p) == len(n) == 0)
  
              labels = torch.zeros(100).to(TEST_DEVICE).type(dtype)
              ref_labels = torch.zeros(100).to(TEST_DEVICE).type(dtype)
              a, p, n = lmu.get_random_triplet_indices(
                  labels, ref_labels=ref_labels
              )
              self.assertTrue(len(a) == len(p) == len(n) == 0)
  
              labels = torch.zeros(100).to(TEST_DEVICE).type(dtype)
              ref_labels = torch.ones(100).to(TEST_DEVICE).type(dtype)
              ref_labels[0] = 0
              a, p, n = lmu.get_random_triplet_indices(
                  labels, ref_labels=ref_labels
              )
              self.assertTrue(len(a) == len(p) == len(n) == 100)
  
file_path: pytorch-metric-learning/tests/utils/test_loss_and_miner_utils.py
imports: import unittest
  import torch
  from pytorch_metric_learning.utils import loss_and_miner_utils
  from ..None import TEST_DEVICE, TEST_DTYPES
label_desc: Use image node_id 5 for calling the TestLossAndMinerUtils obj's underlying member method code with example usage: obj.test_get_random_triplet_indices() without return types