code: def compute_scores(self, logits, targets):
      def _compute_score(tensor):
          """
          Uses a perplexity reduction function to compute a score for
          a given tensor, e.g. the mean perplexity. Filters ignored tensor
          items -- these are 0 by default.
  
          """
          return torch.exp(self.perplexity_func(tensor))
  
      # compute cross-entropy loss of logits wrt targets -- don't reduce
      # to access the loss of each item in the batch
      scores = F.cross_entropy(
          logits.permute(0, 2, 1),
          targets,
          ignore_index=self.pad_index,
          reduction="none",
      )
      # scores is 0 at positions of the target == pad_index,
      non_padding_scores_all_sentences = []
      for sentence_score, sentence_target in zip(scores, targets):
          non_padding_score_per_sentence = sentence_score[
              sentence_target != self.pad_index
          ]
          # exclude padding position at each sentence, filter the sentence
          # from score calculation if every target in the
          # sentence == self.pad_index
          if non_padding_score_per_sentence.numel() > 0:
              non_padding_scores_all_sentences.append(
                  non_padding_score_per_sentence
              )
  
      if len(non_padding_scores_all_sentences) == 0:
          return
  
      return map(
          lambda x: _compute_score(x).item(),
          non_padding_scores_all_sentences,
      )
  
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 9 for calling the LanguageModelMetricReporter obj's underlying member method code with example usage: obj.compute_scores(logits, targets) and returns: map, torch