code: def test_masked_computation(self, device: str):
      spearman_correlation = SpearmanCorrelation()
      batch_size = 10
      num_labels = 10
      predictions1 = torch.randn(batch_size, num_labels, device=device)
      labels1 = 0.5 * predictions1 + torch.randn(
          batch_size, num_labels, device=device
      )
  
      predictions2 = torch.randn(1, device=device).expand(num_labels)
      predictions2 = predictions2.unsqueeze(0).expand(batch_size, -1)
      labels2 = torch.randn(1, device=device).expand(num_labels)
      labels2 = 0.5 * predictions2 + labels2.unsqueeze(0).expand(
          batch_size, -1
      )
  
      # in most cases, the data is constructed like predictions_1, the data of such a batch different.
      # but in a few cases, for example, predictions_2, the data of such a batch is exactly the same.
      predictions_labels_ = [
          (predictions1, labels1),
          (predictions2, labels2),
      ]
  
      # Random binary mask
      mask = torch.randint(
          0, 2, size=(batch_size, num_labels), device=device
      ).bool()
  
      for predictions, labels in predictions_labels_:
          spearman_correlation.reset()
          spearman_correlation(predictions, labels, mask)
          expected_spearman_correlation = spearman_formula(
              predictions.view(-1), labels.view(-1), mask=mask.view(-1)
          )
  
          # because add mask, a batch of predictions or labels will have many 0,
          # spearman correlation algorithm will dependence the sorting position of a set of numbers,
          # too many identical numbers will result in different calculation results each time
          # but the positive and negative results are the same,
          # so here we only test the positive and negative results of the results.
          assert (
              expected_spearman_correlation
              * spearman_correlation.get_metric()
          ) > 0
  
file_path: allennlp/tests/training/metrics/spearman_correlation_test.py
imports: import math
  import pytest
  import torch
  from torch.testing import assert_allclose
  from allennlp.common.testing import AllenNlpTestCase, multi_device, global_distributed_metric, run_distributed_test
  from allennlp.training.metrics import SpearmanCorrelation
label_desc: Use image node_id 2 for calling the SpearmanCorrelationTest obj's underlying member method code with example usage: obj.test_masked_computation(device) without return types