code: def add_batch_stats(
      self, n_batches, preds, targets, scores, loss, m_input, **context
  ):
      if isinstance(loss, torch.Tensor):
          loss = loss.item()
      num_words_in_batch = targets[1].sum().item()
      self.aggregate_loss += loss * num_words_in_batch
      self.total_num_tokens += num_words_in_batch
      if self.aggregate_metrics and num_words_in_batch > 0:
          # unpacks logits from `targets` and computes scores for
          # each item in the batch, e.g. sentence-level perplexity
          if isinstance(targets, tuple):
              targets = targets[0]
          scores = self.compute_scores(preds, targets)
          # scores is None when the every element in the target is self.pad_index
          if scores is not None:
              self.aggregate_scores(scores)
              self.aggregate_context(context)
  
imports: import json
  import math
  import operator
  import time
  import torch
  import torch.nn.functional
  from pytext.common.constants import Stage
  from pytext.config.module_config import PerplexityType
  from pytext.data import CommonMetadata
  from pytext.metrics.language_model_metrics import compute_language_model_metric, LanguageModelMetric
  from pytext.utils import cuda
  from .channel import ConsoleChannel, FileChannel
  from .metric_reporter import MetricReporter
label_desc: Use image node_id 3 for calling the LanguageModelMetricReporter obj's underlying member method code with example usage: obj.add_batch_stats(n_batches, preds, targets, scores, loss, m_input) without return types