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: A simple test for model training behavior when you are not using configuration files. In
  this case, we don't have a story around saving and loading models (you need to handle that
  yourself), so we don't have tests for that.  We just test that the model can train, and that
  it computes gradients for all parameters.
  
  Because the `Trainer` already has a reference to a model and to a data loader, we just take
  the `Trainer` object itself, and grab the `Model` and other necessary objects from there.
  
  # Parameters
  
  trainer: `GradientDescentTrainer`
      The `Trainer` to use for the test, which already has references to a `Model` and a
      `DataLoader`, which we will use in the test.
  gradients_to_ignore : `Set[str]`, optional (default=`None`)
      This test runs a gradient check to make sure that we're actually computing gradients
      for all of the parameters in the model.  If you really want to ignore certain
      parameters when doing that check, you can pass their names here.  This is not
      recommended unless you're `really` sure you don't need to have non-zero gradients for
      those parameters (e.g., some of the beam search / state machine models have
      infrequently-used parameters that are hard to force the model to use in a small test).
  metric_to_check: `str`, optional (default = `None`)
      We may want to automatically perform a check that model reaches given metric when
      training (on validation set, if it is specified). It may be useful in CI, for example.
      You can pass any metric that is in your model returned metrics.
  metric_terminal_value: `str`, optional (default = `None`)
      When you set `metric_to_check`, you need to set the value this metric must converge to
  metric_tolerance: `float`, optional (default=`1e-4`)
      Tolerance to check you model metric against metric terminal value. One can expect some
      variance in model metrics when the training process is highly stochastic.
  disable_dropout : `bool`, optional (default = `True`)
      If True we will set all dropout to 0 before checking gradients. (Otherwise, with small
      datasets, you may get zero gradients because of unlucky dropout.)
docstr_tok: ["A","simple","test","for","model","training","behavior","when","you","are","not","using","configuration","files",".","In","this","case",",","we","do","n't","have","a","story","around","saving","and","loading","models","(","you","need","to","handle","that","yourself",")",",","so","we","do","n't","have","tests","for","that",".","We","just","test","that","the","model","can","train",",","and","that","it","computes","gradients","for","all","parameters",".","Because","the","`","Trainer","`","already","has","a","reference","to","a","model","and","to","a","data","loader",",","we","just","take","the","`","Trainer","`","object","itself",",","and","grab","the","`","Model","`","and","other","necessary","objects","from","there",".","#","Parameters","trainer",":","`","GradientDescentTrainer","`","The","`","Trainer","`","to","use","for","the","test",",","which","already","has","references","to","a","`","Model","`","and","a","`","DataLoader","`",",","which","we","will","use","in","the","test",".","gradients_to_ignore",":","`","Set","[","str","]","`",",","optional","(","default=","`","None","`",")","This","test","runs","a","gradient","check","to","make","sure","that","we","'re","actually","computing","gradients","for","all","of","the","parameters","in","the","model",".","If","you","really","want","to","ignore","certain","parameters","when","doing","that","check",",","you","can","pass","their","names","here",".","This","is","not","recommended","unless","you","'re","`","really","`","sure","you","do","n't","need","to","have","non-zero","gradients","for","those","parameters","(","e.g.",",","some","of","the","beam","search","\/","state","machine","models","have","infrequently-used","parameters","that","are","hard","to","force","the","model","to","use","in","a","small","test",")",".","metric_to_check",":","`","str","`",",","optional","(","default","=","`","None","`",")","We","may","want","to","automatically","perform","a","check","that","model","reaches","given","metric","when","training","(","on","validation","set",",","if","it","is","specified",")",".","It","may","be","useful","in","CI",",","for","example",".","You","can","pass","any","metric","that","is","in","your","model","returned","metrics",".","metric_terminal_value",":","`","str","`",",","optional","(","default","=","`","None","`",")","When","you","set","`","metric_to_check","`",",","you","need","to","set","the","value","this","metric","must","converge","to","metric_tolerance",":","`","float","`",",","optional","(","default=","`","1e-4","`",")","Tolerance","to","check","you","model","metric","against","metric","terminal","value",".","One","can","expect","some","variance","in","model","metrics","when","the","training","process","is","highly","stochastic",".","disable_dropout",":","`","bool","`",",","optional","(","default","=","`","True","`",")","If","True","we","will","set","all","dropout","to","0","before","checking","gradients",".","(","Otherwise",",","with","small","datasets",",","you","may","get","zero","gradients","because","of","unlucky","dropout",".",")"]
code: def ensure_model_can_train(
      self,
      trainer: GradientDescentTrainer,
      gradients_to_ignore: Set[str] = None,
      metric_to_check: str = None,
      metric_terminal_value: float = None,
      metric_tolerance: float = 1e-4,
      disable_dropout: bool = True,
  ):
      """
      A simple test for model training behavior when you are not using configuration files. In
      this case, we don't have a story around saving and loading models (you need to handle that
      yourself), so we don't have tests for that.  We just test that the model can train, and that
      it computes gradients for all parameters.
  
      Because the `Trainer` already has a reference to a model and to a data loader, we just take
      the `Trainer` object itself, and grab the `Model` and other necessary objects from there.
  
      # Parameters
  
      trainer: `GradientDescentTrainer`
          The `Trainer` to use for the test, which already has references to a `Model` and a
          `DataLoader`, which we will use in the test.
      gradients_to_ignore : `Set[str]`, optional (default=`None`)
          This test runs a gradient check to make sure that we're actually computing gradients
          for all of the parameters in the model.  If you really want to ignore certain
          parameters when doing that check, you can pass their names here.  This is not
          recommended unless you're `really` sure you don't need to have non-zero gradients for
          those parameters (e.g., some of the beam search / state machine models have
          infrequently-used parameters that are hard to force the model to use in a small test).
      metric_to_check: `str`, optional (default = `None`)
          We may want to automatically perform a check that model reaches given metric when
          training (on validation set, if it is specified). It may be useful in CI, for example.
          You can pass any metric that is in your model returned metrics.
      metric_terminal_value: `str`, optional (default = `None`)
          When you set `metric_to_check`, you need to set the value this metric must converge to
      metric_tolerance: `float`, optional (default=`1e-4`)
          Tolerance to check you model metric against metric terminal value. One can expect some
          variance in model metrics when the training process is highly stochastic.
      disable_dropout : `bool`, optional (default = `True`)
          If True we will set all dropout to 0 before checking gradients. (Otherwise, with small
          datasets, you may get zero gradients because of unlucky dropout.)
      """
      metrics = trainer.train()
      if metric_to_check is not None:
          metric_value = metrics.get(
              f"best_validation_{metric_to_check}"
          ) or metrics.get(f"training_{metric_to_check}")
          assert (
              metric_value is not None
          ), f"Cannot find {metric_to_check} in metrics.json file"
          assert (
              metric_terminal_value is not None
          ), "Please specify metric terminal value"
          assert (
              abs(metric_value - metric_terminal_value)
              < metric_tolerance
          )
  
      model_batch = next(iter(trainer.data_loader))
  
      # 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(
          trainer.model,
          model_batch,
          gradients_to_ignore,
          disable_dropout,
      )
  
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 4 for calling the ModelTestCase obj's underlying member method code with example usage: obj.ensure_model_can_train(trainer, gradients_to_ignore, metric_to_check, metric_terminal_value, metric_tolerance, disable_dropout) without return types