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_model_can_train_save_and_load(
      self, param_file, tolerance=1e-4, cuda_device=-1
  ):
      save_dir = self.TEST_DIR / "save_and_load_test"
      archive_file = save_dir / "model.tar.gz"
      model = train_model_from_file(param_file, save_dir)
      loaded_model = load_archive(
          archive_file, cuda_device=cuda_device
      ).model
      state_keys = list(model.state_dict().keys())
      loaded_state_keys = list(loaded_model.state_dict().keys())
      assert state_keys == loaded_state_keys
      # First we make sure that the state dict (the parameters) are the same for both models.
      for key in state_keys:
          assert_allclose(
              model.state_dict()[key].cpu().numpy(),
              loaded_model.state_dict()[key].cpu().numpy(),
              err_msg=key,
          )
      params = Params.from_file(param_file)
      reader = DatasetReader.from_params(params["dataset_reader"])
  
      # Need to duplicate params because Iterator.from_params will consume.
      iterator_params = params["iterator"]
      iterator_params2 = Params(
          copy.deepcopy(iterator_params.as_dict())
      )
  
      iterator = DataIterator.from_params(iterator_params)
      iterator2 = DataIterator.from_params(iterator_params2)
  
      # We'll check that even if we index the dataset with each model separately, we still get
      # the same result out.
      model_dataset = reader.read(params["validation_data_path"])
      iterator.index_with(model.vocab)
      model_batch = next(
          iterator(
              model_dataset, shuffle=False, cuda_device=cuda_device
          )
      )
  
      loaded_dataset = reader.read(params["validation_data_path"])
      iterator2.index_with(loaded_model.vocab)
      loaded_batch = next(
          iterator2(
              loaded_dataset, shuffle=False, cuda_device=cuda_device
          )
      )
  
      # Check gradients are None for non-trainable parameters and check that
      # trainable parameters receive some gradient if they are trainable.
      self.check_model_computes_gradients_correctly(model, model_batch)
  
      # The datasets themselves should be identical.
      assert list(model_batch.keys()) == list(loaded_batch.keys())
      for key in list(model_batch.keys()):
          self.assert_fields_equal(
              model_batch[key], loaded_batch[key], key, 1e-6
          )
  
      # Set eval mode, to turn off things like dropout, then get predictions.
      model.eval()
      loaded_model.eval()
      # Models with stateful RNNs need their states reset to have consistent
      # behavior after loading.
      for model_ in [model, loaded_model]:
          for module in model_.modules():
              if hasattr(module, "stateful") and module.stateful:
                  module.reset_states()
      model_predictions = model(**model_batch)
      loaded_model_predictions = loaded_model(**loaded_batch)
  
      # Check loaded model's loss exists and we can compute gradients, for continuing training.
      loaded_model_loss = loaded_model_predictions["loss"]
      assert loaded_model_loss is not None
      loaded_model_loss.backward()
  
      # Both outputs should have the same keys and the values for these keys should be close.
      for key in list(model_predictions.keys()):
          self.assert_fields_equal(
              model_predictions[key],
              loaded_model_predictions[key],
              name=key,
              tolerance=tolerance,
          )
  
      return model, loaded_model
  
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 2 for calling the ModelTestCase obj's underlying member method code with example usage: obj.ensure_model_can_train_save_and_load(param_file, tolerance, cuda_device) and returns: model,  loaded_model