class_docstr: A subclass of [`AllenNlpTestCase`](./test_case.md)
  with added methods for testing [`Model`](../../models/model.md) subclasses.
class_docstr_tok: ["A","subclass","of","[","`","AllenNlpTestCase","`","]","(",".\/test_case.md",")","with","added","methods","for","testing","[","`","Model","`","]","(","..","\/","..","\/models\/model.md",")","subclasses","."]
docstr: Ensures that the model performs the same on a batch of instances as on individual instances.
  Ignores metrics matching the regexp .*loss.* and those specified explicitly.
  
  # Parameters
  
  keys_to_ignore : `Iterable[str]`, optional (default=`()`)
      Names of metrics that should not be taken into account, e.g. "batch_weight".
docstr_tok: ["Ensures","that","the","model","performs","the","same","on","a","batch","of","instances","as","on","individual","instances",".","Ignores","metrics","matching","the","regexp",".","*","loss",".","*","and","those","specified","explicitly",".","#","Parameters","keys_to_ignore",":","`","Iterable","[","str","]","`",",","optional","(","default=","`","(",")","`",")","Names","of","metrics","that","should","not","be","taken","into","account",",","e.g",".","``","batch_weight","''","."]
code: def ensure_batch_predictions_are_consistent(
      self, keys_to_ignore: Iterable[str] = ()
  ):
      """
      Ensures that the model performs the same on a batch of instances as on individual instances.
      Ignores metrics matching the regexp .*loss.* and those specified explicitly.
  
      # Parameters
  
      keys_to_ignore : `Iterable[str]`, optional (default=`()`)
          Names of metrics that should not be taken into account, e.g. "batch_weight".
      """
      self.model.eval()
      single_predictions = []
      for i, instance in enumerate(self.instances):
          dataset = Batch([instance])
          tensors = dataset.as_tensor_dict(
              dataset.get_padding_lengths()
          )
          result = self.model(**tensors)
          single_predictions.append(result)
      full_dataset = Batch(self.instances)
      batch_tensors = full_dataset.as_tensor_dict(
          full_dataset.get_padding_lengths()
      )
      batch_predictions = self.model(**batch_tensors)
      for i, instance_predictions in enumerate(single_predictions):
          for key, single_predicted in instance_predictions.items():
              tolerance = 1e-6
              if "loss" in key:
                  # Loss is particularly unstable; we'll just be satisfied if everything else is
                  # close.
                  continue
              if key in keys_to_ignore:
                  continue
              single_predicted = single_predicted[0]
              batch_predicted = batch_predictions[key][i]
              if isinstance(single_predicted, torch.Tensor):
                  if single_predicted.size() != batch_predicted.size():
                      slices = tuple(
                          slice(0, size)
                          for size in single_predicted.size()
                      )
                      batch_predicted = batch_predicted[slices]
                  assert_allclose(
                      single_predicted.data.numpy(),
                      batch_predicted.data.numpy(),
                      atol=tolerance,
                      err_msg=key,
                  )
              else:
                  assert single_predicted == batch_predicted, key
  
imports: import copy
  import json
  from os import PathLike
  import random
  from typing import Any, Dict, Iterable, Set, Union
  import torch
  import numpy
  from numpy.testing import assert_allclose
  from allennlp.commands.train import train_model_from_file
  from allennlp.common import Params
  from allennlp.common.testing.test_case import AllenNlpTestCase
  from allennlp.data import DatasetReader, Vocabulary
  from allennlp.data import DataLoader
  from allennlp.data.batch import Batch
  from allennlp.models import load_archive, Model
  from allennlp.training import GradientDescentTrainer
  from allennlp.confidence_checks.normalization_bias_verification import NormalizationBiasVerification
label_desc: Use image node_id 7 for calling the ModelTestCase obj's underlying member method code with example usage: obj.ensure_batch_predictions_are_consistent(keys_to_ignore) without return types