code: def test_f1_measure(self, device: str):
      f1_measure = F1Measure(positive_label=0)
      predictions = torch.tensor(
          [
              [0.35, 0.25, 0.1, 0.1, 0.2],
              [0.1, 0.6, 0.1, 0.2, 0.0],
              [0.1, 0.6, 0.1, 0.2, 0.0],
              [0.1, 0.5, 0.1, 0.2, 0.0],
              [0.1, 0.2, 0.1, 0.7, 0.0],
              [0.1, 0.6, 0.1, 0.2, 0.0],
          ],
          device=device,
      )
      # [True Positive, True Negative, True Negative,
      #  False Negative, True Negative, False Negative]
      targets = torch.tensor([0, 4, 1, 0, 3, 0], device=device)
      f1_measure(predictions, targets)
      metrics = f1_measure.get_metric()
      precision = metrics["precision"]
      recall = metrics["recall"]
      f1 = metrics["f1"]
      assert f1_measure._true_positives == 1.0
      assert f1_measure._true_negatives == 3.0
      assert f1_measure._false_positives == 0.0
      assert f1_measure._false_negatives == 2.0
      f1_measure.reset()
      # check value
      assert_allclose(precision, 1.0)
      assert_allclose(recall, 0.333333333)
      assert_allclose(f1, 0.499999999)
      # check type
      assert isinstance(precision, float)
      assert isinstance(recall, float)
      assert isinstance(f1, float)
  
      # Test the same thing with a mask:
      mask = torch.tensor(
          [True, False, True, True, True, False], device=device
      )
      f1_measure(predictions, targets, mask)
      metrics = f1_measure.get_metric()
      precision = metrics["precision"]
      recall = metrics["recall"]
      f1 = metrics["f1"]
      assert f1_measure._true_positives == 1.0
      assert f1_measure._true_negatives == 2.0
      assert f1_measure._false_positives == 0.0
      assert f1_measure._false_negatives == 1.0
      f1_measure.reset()
      assert_allclose(precision, 1.0)
      assert_allclose(recall, 0.5)
      assert_allclose(f1, 0.6666666666)
  
file_path: allennlp/tests/training/metrics/f1_measure_test.py
imports: import pytest
  import torch
  from torch.testing import assert_allclose
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.testing import AllenNlpTestCase, multi_device, run_distributed_test, global_distributed_metric
  from allennlp.training.metrics import F1Measure
label_desc: Use image node_id 2 for calling the F1MeasureTest obj's underlying member method code with example usage: obj.test_f1_measure(device) without return types