code: def score_dataset(
      model,
      dataset,
      label_map=None,
      remap_labels=None,
      forgive_unmapped_labels=False,
  ):
      """
      remap_labels: a dict from old label to new label to use when
      testing a classifier on a dataset with a simpler label set.
      For example, a model trained on 5 class sentiment can be tested
      on a binary distribution with {"0": "0", "1": "0", "3": "1", "4": "1"}
  
      forgive_unmapped_labels says the following: in the case that the
      model predicts "2" in the above example for remap_labels, instead
      treat the model's prediction as whichever label it gave the
      highest score
      """
      model.eval()
      if label_map is None:
          label_map = {x: y for (y, x) in enumerate(model.labels)}
      correct = 0
      dataset_lengths = data.sort_dataset_by_len(dataset)
  
      for length in dataset_lengths.keys():
          # TODO: possibly break this up into smaller batches
          batch = dataset_lengths[length]
          expected_labels = [label_map[x.sentiment] for x in batch]
  
          output = model(batch)
  
          for i in range(len(expected_labels)):
              predicted = torch.argmax(output[i])
              predicted_label = predicted.item()
              if remap_labels:
                  if predicted_label in remap_labels:
                      predicted_label = remap_labels[predicted_label]
                  else:
                      found = False
                      if forgive_unmapped_labels:
                          items = []
                          for j in range(len(output[i])):
                              items.append((output[i][j].item(), j))
                          items.sort(key=lambda x: -x[0])
                          for _, item in items:
                              if item in remap_labels:
                                  predicted_label = remap_labels[item]
                                  found = True
                                  break
                      # if slack guesses allowed, none of the existing
                      # labels matched, so we count it wrong.  if slack
                      # guesses not allowed, just count it wrong
                      if not found:
                          continue
  
              if predicted_label == expected_labels[i]:
                  correct = correct + 1
      return correct
  
imports: import argparse
  import ast
  import logging
  import os
  import random
  import re
  from enum import Enum
  import torch
  import torch.nn
  from stanza.models.common import loss
  from stanza.models.common import utils
  from stanza.models.pos.vocab import CharVocab
  import stanza.models.classifiers.data
  from stanza.models.classifiers.trainer import Trainer
  from stanza.models.classifiers.utils import WVType, ExtraVectors, ModelType
  from stanza.utils.confusion import format_confusion, confusion_to_accuracy, confusion_to_macro_f1
label_desc: Use image node_id 6 for calling a global function with example usage: score_dataset(model, dataset, label_map, remap_labels, forgive_unmapped_labels) and returns: correct