class_docstr: A subclass of :class:`~allennlp.common.testing.test_case.AllenNlpTestCase`
  with added methods for testing :class:`~allennlp.models.model.Model` subclasses.
class_docstr_tok: ["A","subclass","of",":","class",":","`","~allennlp.common.testing.test_case.AllenNlpTestCase","`","with","added","methods","for","testing",":","class",":","`","~allennlp.models.model.Model","`","subclasses","."]
code: def ensure_batch_predictions_are_consistent(self):
      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 list(
              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
              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
  
file_path: magnitude/pymagnitude/third_party/allennlp/common/testing/model_test_case.py
imports: from __future__ import division
  from __future__ import absolute_import
  from __future__ import print_function
  import copy
  from numpy.testing import assert_allclose
  import torch
  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 DataIterator, DatasetReader, Vocabulary
  from allennlp.data.dataset import Batch
  from allennlp.models import Model, load_archive
label_desc: Use image node_id 5 for calling the ModelTestCase obj's underlying member method code with example usage: obj.ensure_batch_predictions_are_consistent() without return types