class_docstr: Test individual Metric classes.
class_docstr_tok: ["Test","individual","Metric","classes","."]
code: def test_average_metric_from_mask(self) -> None:
      # first test case. batchsize=3, num_tokens=10
      token_values_1 = torch.FloatTensor(
          [
              [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
              [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8],
              [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0],
          ]
      )
      token_mask_1 = torch.LongTensor(
          [
              [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
              [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
          ]
      )
      output_1 = [
          AverageMetric(55, 10),
          AverageMetric(-30, 6),
          AverageMetric(12.5, 5),
      ]
  
      # second test case. batchsize=4, num_tokens=5
      token_values_2 = torch.FloatTensor(
          [
              [1, 2, 3, 4, 5],
              [1.5, 0, -1, 3, -4],
              [-3, -2, -1, 0, 1],
              [4, 5, 6, 7, 8],
          ]
      )
      token_mask_2 = torch.LongTensor(
          [
              [1, 1, 1, 1, 1],
              [1, 1, 1, 0, 0],
              [1, 0, 1, 0, 1],
              [0, 0, 0, 0, 0],
          ]
      )
      output_2 = [
          AverageMetric(15, 5),
          AverageMetric(0.5, 3),
          AverageMetric(-3, 3),
          AverageMetric(0, 0),
      ]
  
      input_and_outputs = [
          (token_values_1, token_mask_1, output_1),
          (token_values_2, token_mask_2, output_2),
      ]
  
      for token_values, token_mask, output in input_and_outputs:
          actual_output = AverageMetric.from_mask(
              token_values, token_mask
          )
          self.assertEqual(len(actual_output), len(output))
          # Because Metric.from_mask() calls Metric.many(), which in turn converts tensors to lists,
          # it possible for the actual and expected outputs to be close to each other but not exactly equal.
          for a, o in zip(actual_output, output):
              self.assertIsInstance(a, type(o))
              self.assertAlmostEqual(a.value(), o.value(), places=6)
  
imports: import unittest
  from typing import Dict
  import torch
  import random
  from parlai.core.metrics import AverageMetric, SumMetric, FixedMetric, Metrics, GlobalAverageMetric, MacroAverageMetric, TimerMetric, aggregate_unnamed_reports, aggregate_named_reports, InterDistinctMetric, IntraDistinctMetric, FairseqBleuMetric
  from parlai.core.torch_classifier_agent import ConfusionMatrixMetric, WeightedF1Metric, AUCMetrics
  from parlai.core.torch_generator_agent import PPLMetric
  import parlai.utils.testing
label_desc: Use image node_id 7 for calling the TestMetric obj's underlying member method code with example usage: obj.test_average_metric_from_mask() without return types