class_docstr: Mixin for adding classification metrics to non-classifier models.
class_docstr_tok: ["Mixin","for","adding","classification","metrics","to","non-classifier","models","."]
docstr: Update the confusion matrix given the batch and predictions.
  
  :param predictions: List of strings of length batchsize. Each string is a label
      predicted by the classifier. A string will be None if the corresponding
      observation is empty.
  :param labels: List of label fields from the observations. Fields may be Nones
      if the observations are empty.
docstr_tok: ["Update","the","confusion","matrix","given","the","batch","and","predictions",".",":","param","predictions",":","List","of","strings","of","length","batchsize",".","Each","string","is","a","label","predicted","by","the","classifier",".","A","string","will","be","None","if","the","corresponding","observation","is","empty",".",":","param","labels",":","List","of","label","fields","from","the","observations",".","Fields","may","be","Nones","if","the","observations","are","empty","."]
code: def _update_confusion_matrix(
      self,
      predictions: List[Optional[str]],
      labels: List[Optional[List[str]]],
  ):
      """
      Update the confusion matrix given the batch and predictions.
  
      :param predictions: List of strings of length batchsize. Each string is a label
          predicted by the classifier. A string will be None if the corresponding
          observation is empty.
      :param labels: List of label fields from the observations. Fields may be Nones
          if the observations are empty.
      """
      f1_dict = {}
  
      # Check that predictions and labels have Nones in the same places, and then
      # filter the Nones out because we can't compute metrics with them
      assert len(predictions) == len(labels)
      assert all(
          [
              (pred is None and label is None)
              or (pred is not None and label is not None)
              for pred, label in zip(predictions, labels)
          ]
      )
      filtered_predictions = [
          pred for pred in predictions if pred is not None
      ]
      filtered_labels = [label for label in labels if label is not None]
  
      class_list = set(filtered_predictions + filtered_labels)
      for class_name in class_list:
          prec_str = f"class_{class_name}_prec"
          recall_str = f"class_{class_name}_recall"
          f1_str = f"class_{class_name}_f1"
          precision, recall, f1 = ConfusionMatrixMetric.compute_metrics(
              filtered_predictions, filtered_labels, class_name
          )
          f1_dict[class_name] = f1
          self.record_local_metric(prec_str, precision)
          self.record_local_metric(recall_str, recall)
          self.record_local_metric(f1_str, f1)
      self.record_local_metric(
          "weighted_f1", WeightedF1Metric.compute_many(f1_dict)
      )
  
imports: from parlai.core.params import ParlaiParser
  import random
  from typing import List, Optional
  import numpy
  import torch
  from torch import nn
  from parlai.agents.transformer.modules import TransformerDecoder, TransformerGeneratorModel
  from parlai.core.agents import Agent
  from parlai.core.message import Message
  from parlai.core.opt import Opt
  from parlai.core.torch_classifier_agent import ConfusionMatrixMetric, WeightedF1Metric
  from parlai.utils.misc import warn_once
label_desc: Use image node_id 2 for calling the ClassificationMixin obj's underlying member method code with example usage: obj._update_confusion_matrix(predictions, labels) without return types