code: def test_convert_to_weights(self):
      a = torch.LongTensor([0, 1, 2, 3]).to(TEST_DEVICE)
      p = torch.LongTensor([4, 4, 4, 4]).to(TEST_DEVICE)
      n = torch.LongTensor([5, 5, 6, 6]).to(TEST_DEVICE)
      for dtype in TEST_DTYPES:
          weights = lmu.convert_to_weights(
              (a, p, n),
              labels=torch.arange(7).to(TEST_DEVICE),
              dtype=dtype,
          )
          correct_weights = torch.tensor(
              [0.25, 0.25, 0.25, 0.25, 1, 0.5, 0.5], dtype=dtype
          ).to(TEST_DEVICE)
          self.assertTrue(torch.all(weights == correct_weights))
  
      a = torch.LongTensor([]).to(TEST_DEVICE)
      p = torch.LongTensor([]).to(TEST_DEVICE)
      n = torch.LongTensor([]).to(TEST_DEVICE)
      for dtype in TEST_DTYPES:
          weights = lmu.convert_to_weights(
              (a, p, n),
              labels=torch.arange(7).to(TEST_DEVICE),
              dtype=dtype,
          )
          correct_weights = torch.tensor(
              [1, 1, 1, 1, 1, 1, 1], dtype=dtype
          ).to(TEST_DEVICE)
          self.assertTrue(torch.all(weights == correct_weights))
  
      # Case where convert_to_weights is used with ref_emb.
      # In this case, indices_tuple will include indices
      # that point to ref_emb, which may be larger than the query
      batch_size = 32
      labels = torch.randint(0, 10, size=(batch_size,)).to(TEST_DEVICE)
      a = torch.randint(0, batch_size, size=(256,)).to(TEST_DEVICE)
      p = torch.randint(0, batch_size * 2, size=(256,)).to(TEST_DEVICE)
      n = torch.randint(0, batch_size * 2, size=(256,)).to(TEST_DEVICE)
      for dtype in TEST_DTYPES:
          weights = lmu.convert_to_weights(
              (a, p, n), labels=labels, dtype=dtype, using_ref=True
          )
  
          _, counts = torch.unique(a, return_counts=True, sorted=True)
          counts = counts.type(weights.dtype) / torch.max(counts)
          # Will fail on cuda if there is an indexing error
          self.assertTrue(torch.equal(weights, counts))
  
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 4 for calling the TestLossAndMinerUtils obj's underlying member method code with example usage: obj.test_convert_to_weights() without return types