class_docstr: A trainer for training models. 
class_docstr_tok: ["A","trainer","for","training","models","."]
code: def __init__(
      self,
      args=None,
      vocab=None,
      pretrain=None,
      model_file=None,
      device=None,
      train_classifier_only=False,
      foundation_cache=None,
  ):
      if model_file is not None:
          # load everything from file
          self.load(model_file, pretrain, args, foundation_cache)
      else:
          assert all(var is not None for var in [args, vocab, pretrain])
          # build model from scratch
          self.args = args
          self.vocab = vocab
          self.model = NERTagger(
              args,
              vocab,
              emb_matrix=pretrain.emb,
              foundation_cache=foundation_cache,
          )
  
      # if this wasn't set anywhere, we use a default of the 0th tagset
      # we don't set this as a default in the options so that
      # we can distinguish "intentionally set to 0" and "not set at all"
      if self.args.get("predict_tagset", None) is None:
          self.args["predict_tagset"] = 0
  
      if train_classifier_only:
          logger.info("Disabling gradient for non-classifier layers")
          exclude = ["tag_clf", "crit"]
          for pname, p in self.model.named_parameters():
              if pname.split(".")[0] not in exclude:
                  p.requires_grad = False
      self.model = self.model.to(device)
      self.optimizer = utils.get_optimizer(
          self.args["optim"],
          self.model,
          self.args["lr"],
          momentum=self.args["momentum"],
          bert_learning_rate=self.args.get("bert_learning_rate", 0.0),
      )
  
imports: import sys
  import logging
  import torch
  from torch import nn
  from stanza.models.common.foundation_cache import NoTransformerFoundationCache
  from stanza.models.common.trainer import Trainer
  from stanza.models.common.vocab import VOCAB_PREFIX, VOCAB_PREFIX_SIZE
  from stanza.models.common import utils, loss
  from stanza.models.ner.model import NERTagger
  from stanza.models.ner.vocab import MultiVocab
  from stanza.models.common.crf import viterbi_decode
label_desc: Use image node_id 1 to create a new Trainer object from inherited base classes: BaseTrainer with example: obj = Trainer(args, vocab, pretrain, model_file, device, train_classifier_only, foundation_cache)