code: def test_f1_measure(self):
      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],
          ]
      )
      # [True Positive, True Negative, True Negative,
      #  False Negative, True Negative, False Negative]
      targets = torch.Tensor([0, 4, 1, 0, 3, 0])
      f1_measure(predictions, targets)
      precision, recall, f1 = f1_measure.get_metric()
      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()
      numpy.testing.assert_almost_equal(precision, 1.0)
      numpy.testing.assert_almost_equal(recall, 0.333333333)
      numpy.testing.assert_almost_equal(f1, 0.499999999)
  
      # Test the same thing with a mask:
      mask = torch.Tensor([1, 0, 1, 1, 1, 0])
      f1_measure(predictions, targets, mask)
      precision, recall, f1 = f1_measure.get_metric()
      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()
      numpy.testing.assert_almost_equal(precision, 1.0)
      numpy.testing.assert_almost_equal(recall, 0.5)
      numpy.testing.assert_almost_equal(f1, 0.6666666666)
  
file_path: magnitude/pymagnitude/third_party/allennlp/tests/training/metrics/f1_measure_test.py
imports: from __future__ import with_statement
  from __future__ import absolute_import
  import torch
  import pytest
  import numpy
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.common.checks import ConfigurationError
  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() without return types