code: def __init__(
      self,
      args,
      vocab,
      emb_matrix=None,
      foundation_cache=None,
      force_bert_saved=False,
  ):
      super().__init__()
  
      self.vocab = vocab
      self.args = args
      self.unsaved_modules = []
  
      def add_unsaved_module(name, module):
          self.unsaved_modules += [name]
          setattr(self, name, module)
  
      # input layers
      input_size = 0
      if self.args["word_emb_dim"] > 0:
          emb_finetune = self.args.get("emb_finetune", True)
  
          # load pretrained embeddings if specified
          word_emb = nn.Embedding(
              len(self.vocab["word"]), self.args["word_emb_dim"], PAD_ID
          )
          # if a model trained with no 'delta' vocab is loaded, and
          # emb_finetune is off, any resaving of the model will need
          # the updated vectors.  this is accounted for in load()
          if not emb_finetune or "delta" in self.vocab:
              # if emb_finetune is off
              # or if the delta embedding is present
              # then we won't fine tune the original embedding
              add_unsaved_module("word_emb", word_emb)
              self.word_emb.weight.detach_()
          else:
              self.word_emb = word_emb
          if emb_matrix is not None:
              self.init_emb(emb_matrix)
  
          # TODO: allow for expansion of delta embedding if new
          # training data has new words in it?
          self.delta_emb = None
          if "delta" in self.vocab:
              # zero inits seems to work better
              # note that the gradient will flow to the bottom and then adjust the 0 weights
              # as opposed to a 0 matrix cutting off the gradient if higher up in the model
              self.delta_emb = nn.Embedding(
                  len(self.vocab["delta"]),
                  self.args["word_emb_dim"],
                  PAD_ID,
              )
              nn.init.zeros_(self.delta_emb.weight)
              # if the model was trained with a delta embedding, but emb_finetune is off now,
              # then we will detach the delta embedding
              if not emb_finetune:
                  self.delta_emb.weight.detach_()
  
          input_size += self.args["word_emb_dim"]
  
      # TODO: this, pos, depparse should all be refactored
      # FIXME: possibly pos and depparse are all losing a finetuned transformer if loaded & saved
      # (the force_bert_saved option here handles that)
      if self.args.get("bert_model", None):
          # first we load the transformer model and possibly turn off its requires_grad parameters ...
          if self.args.get("bert_finetune", False):
              bert_model, bert_tokenizer = load_bert(
                  self.args["bert_model"]
              )
          else:
              bert_model, bert_tokenizer = load_bert(
                  self.args["bert_model"], foundation_cache
              )
              for n, p in bert_model.named_parameters():
                  p.requires_grad = False
          # then we attach it to the NER model
          # if force_bert_saved is True, that probably indicates the save file had a transformer in it
          # thus we need to save it again in the future to avoid losing it when resaving
          if self.args.get("bert_finetune", False) or force_bert_saved:
              self.bert_model = bert_model
              add_unsaved_module("bert_tokenizer", bert_tokenizer)
          else:
              add_unsaved_module("bert_model", bert_model)
              add_unsaved_module("bert_tokenizer", bert_tokenizer)
          input_size += self.bert_model.config.hidden_size
      else:
          self.bert_model = None
          self.bert_tokenizer = None
  
      if self.args["char"] and self.args["char_emb_dim"] > 0:
          if self.args["charlm"]:
              if args[
                  "charlm_forward_file"
              ] is None or not os.path.exists(
                  args["charlm_forward_file"]
              ):
                  raise ForwardCharlmNotFoundError(
                      "Could not find forward character model: {}  Please specify with --charlm_forward_file".format(
                          args["charlm_forward_file"]
                      ),
                      args["charlm_forward_file"],
                  )
              if args[
                  "charlm_backward_file"
              ] is None or not os.path.exists(
                  args["charlm_backward_file"]
              ):
                  raise BackwardCharlmNotFoundError(
                      "Could not find backward character model: {}  Please specify with --charlm_backward_file".format(
                          args["charlm_backward_file"]
                      ),
                      args["charlm_backward_file"],
                  )
              add_unsaved_module(
                  "charmodel_forward",
                  CharacterLanguageModel.load(
                      args["charlm_forward_file"], finetune=False
                  ),
              )
              add_unsaved_module(
                  "charmodel_backward",
                  CharacterLanguageModel.load(
                      args["charlm_backward_file"], finetune=False
                  ),
              )
              input_size += (
                  self.charmodel_forward.hidden_dim()
                  + self.charmodel_backward.hidden_dim()
              )
          else:
              self.charmodel = CharacterModel(
                  args, vocab, bidirectional=True, attention=False
              )
              input_size += self.args["char_hidden_dim"] * 2
  
      # optionally add a input transformation layer
      if self.args.get("input_transform", False):
          self.input_transform = nn.Linear(input_size, input_size)
      else:
          self.input_transform = None
  
      # recurrent layers
      self.taggerlstm = PackedLSTM(
          input_size,
          self.args["hidden_dim"],
          self.args["num_layers"],
          batch_first=True,
          bidirectional=True,
          dropout=0
          if self.args["num_layers"] == 1
          else self.args["dropout"],
      )
      # self.drop_replacement = nn.Parameter(torch.randn(input_size) / np.sqrt(input_size))
      self.drop_replacement = None
      self.taggerlstm_h_init = nn.Parameter(
          torch.zeros(
              2 * self.args["num_layers"], 1, self.args["hidden_dim"]
          ),
          requires_grad=False,
      )
      self.taggerlstm_c_init = nn.Parameter(
          torch.zeros(
              2 * self.args["num_layers"], 1, self.args["hidden_dim"]
          ),
          requires_grad=False,
      )
  
      # tag classifier
      tag_lengths = self.vocab["tag"].lens()
      self.num_output_layers = len(tag_lengths)
      if self.args.get("connect_output_layers"):
          tag_clfs = [
              nn.Linear(self.args["hidden_dim"] * 2, tag_lengths[0])
          ]
          for prev_length, next_length in zip(
              tag_lengths[:-1], tag_lengths[1:]
          ):
              tag_clfs.append(
                  nn.Linear(
                      self.args["hidden_dim"] * 2 + prev_length,
                      next_length,
                  )
              )
          self.tag_clfs = nn.ModuleList(tag_clfs)
      else:
          self.tag_clfs = nn.ModuleList(
              [
                  nn.Linear(self.args["hidden_dim"] * 2, num_tag)
                  for num_tag in tag_lengths
              ]
          )
      for tag_clf in self.tag_clfs:
          tag_clf.bias.data.zero_()
      self.crits = nn.ModuleList(
          [CRFLoss(num_tag) for num_tag in tag_lengths]
      )
  
      self.drop = nn.Dropout(args["dropout"])
      self.worddrop = WordDropout(args["word_dropout"])
      self.lockeddrop = LockedDropout(args["locked_dropout"])
  
imports: import os
  import logging
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence, pack_sequence, pad_sequence, PackedSequence
  from stanza.models.common.data import map_to_ids, get_long_tensor
  from stanza.models.common.exceptions import ForwardCharlmNotFoundError, BackwardCharlmNotFoundError
  from stanza.models.common.packed_lstm import PackedLSTM
  from stanza.models.common.dropout import WordDropout, LockedDropout
  from stanza.models.common.char_model import CharacterModel, CharacterLanguageModel
  from stanza.models.common.crf import CRFLoss
  from stanza.models.common.foundation_cache import load_bert
  from stanza.models.common.vocab import PAD_ID, UNK_ID, EMPTY_ID
  from stanza.models.common.bert_embedding import extract_bert_embeddings
label_desc: Use image node_id 1 to create a new NERTagger object from inherited base classes: nn with example: obj = NERTagger(args, vocab, emb_matrix, foundation_cache, force_bert_saved)