code: def __init__(
      self,
      args,
      vocab,
      emb_matrix=None,
      share_hid=False,
      foundation_cache=None,
  ):
      super().__init__()
  
      self.vocab = vocab
      self.args = args
      self.share_hid = share_hid
      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:
          # frequent word embeddings
          self.word_emb = nn.Embedding(
              len(vocab["word"]),
              self.args["word_emb_dim"],
              padding_idx=0,
          )
          self.lemma_emb = nn.Embedding(
              len(vocab["lemma"]),
              self.args["word_emb_dim"],
              padding_idx=0,
          )
          input_size += self.args["word_emb_dim"] * 2
  
      if self.args["tag_emb_dim"] > 0:
          self.upos_emb = nn.Embedding(
              len(vocab["upos"]),
              self.args["tag_emb_dim"],
              padding_idx=0,
          )
  
          if not isinstance(vocab["xpos"], CompositeVocab):
              self.xpos_emb = nn.Embedding(
                  len(vocab["xpos"]),
                  self.args["tag_emb_dim"],
                  padding_idx=0,
              )
          else:
              self.xpos_emb = nn.ModuleList()
  
              for l in vocab["xpos"].lens():
                  self.xpos_emb.append(
                      nn.Embedding(
                          l, self.args["tag_emb_dim"], padding_idx=0
                      )
                  )
  
          self.ufeats_emb = nn.ModuleList()
  
          for l in vocab["feats"].lens():
              self.ufeats_emb.append(
                  nn.Embedding(
                      l, self.args["tag_emb_dim"], padding_idx=0
                  )
              )
  
          input_size += self.args["tag_emb_dim"] * 2
  
      if self.args["char"] and self.args["char_emb_dim"] > 0:
          if self.args.get("charlm", None):
              if args[
                  "charlm_forward_file"
              ] is None or not os.path.exists(
                  args["charlm_forward_file"]
              ):
                  raise FileNotFoundError(
                      "Could not find forward character model: {}  Please specify with --charlm_forward_file".format(
                          args["charlm_forward_file"]
                      )
                  )
              if args[
                  "charlm_backward_file"
              ] is None or not os.path.exists(
                  args["charlm_backward_file"]
              ):
                  raise FileNotFoundError(
                      "Could not find backward character model: {}  Please specify with --charlm_backward_file".format(
                          args["charlm_backward_file"]
                      )
                  )
              logger.debug(
                  "Depparse model loading charmodels: %s and %s",
                  args["charlm_forward_file"],
                  args["charlm_backward_file"],
              )
              add_unsaved_module(
                  "charmodel_forward",
                  load_charlm(
                      args["charlm_forward_file"],
                      foundation_cache=foundation_cache,
                  ),
              )
              add_unsaved_module(
                  "charmodel_backward",
                  load_charlm(
                      args["charlm_backward_file"],
                      foundation_cache=foundation_cache,
                  ),
              )
              input_size += (
                  self.charmodel_forward.hidden_dim()
                  + self.charmodel_backward.hidden_dim()
              )
          else:
              self.charmodel = CharacterModel(args, vocab)
              self.trans_char = nn.Linear(
                  self.args["char_hidden_dim"],
                  self.args["transformed_dim"],
                  bias=False,
              )
              input_size += self.args["transformed_dim"]
  
      # TODO: refactor with POS
      if self.args["bert_model"]:
          if args.get("bert_hidden_layers", False):
              # The average will be offset by 1/N so that the default zeros
              # repressents an average of the N layers
              self.bert_layer_mix = nn.Linear(
                  args["bert_hidden_layers"], 1, bias=False
              )
              nn.init.zeros_(self.bert_layer_mix.weight)
          else:
              # an average of layers 2, 3, 4 will be used
              # (for historic reasons)
              self.bert_layer_mix = None
          if self.args.get("bert_finetune", False):
              bert_model, bert_tokenizer = load_bert(
                  self.args["bert_model"]
              )
              self.bert_model = bert_model
              add_unsaved_module("bert_tokenizer", bert_tokenizer)
          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
              add_unsaved_module("bert_model", bert_model)
              add_unsaved_module("bert_tokenizer", bert_tokenizer)
          input_size += bert_model.config.hidden_size
      else:
          self.bert_model = None
          self.bert_tokenizer = None
  
      if self.args["pretrain"]:
          # pretrained embeddings, by default this won't be saved into model file
          add_unsaved_module(
              "pretrained_emb",
              nn.Embedding.from_pretrained(
                  torch.from_numpy(emb_matrix), freeze=True
              ),
          )
          self.trans_pretrained = nn.Linear(
              emb_matrix.shape[1],
              self.args["transformed_dim"],
              bias=False,
          )
          input_size += self.args["transformed_dim"]
  
      # recurrent layers
      self.parserlstm = HighwayLSTM(
          input_size,
          self.args["hidden_dim"],
          self.args["num_layers"],
          batch_first=True,
          bidirectional=True,
          dropout=self.args["dropout"],
          rec_dropout=self.args["rec_dropout"],
          highway_func=torch.tanh,
      )
      self.drop_replacement = nn.Parameter(
          torch.randn(input_size) / np.sqrt(input_size)
      )
      self.parserlstm_h_init = nn.Parameter(
          torch.zeros(
              2 * self.args["num_layers"], 1, self.args["hidden_dim"]
          )
      )
      self.parserlstm_c_init = nn.Parameter(
          torch.zeros(
              2 * self.args["num_layers"], 1, self.args["hidden_dim"]
          )
      )
  
      # classifiers
      self.unlabeled = DeepBiaffineScorer(
          2 * self.args["hidden_dim"],
          2 * self.args["hidden_dim"],
          self.args["deep_biaff_hidden_dim"],
          1,
          pairwise=True,
          dropout=args["dropout"],
      )
      self.deprel = DeepBiaffineScorer(
          2 * self.args["hidden_dim"],
          2 * self.args["hidden_dim"],
          self.args["deep_biaff_hidden_dim"],
          len(vocab["deprel"]),
          pairwise=True,
          dropout=args["dropout"],
      )
      if args["linearization"]:
          self.linearization = DeepBiaffineScorer(
              2 * self.args["hidden_dim"],
              2 * self.args["hidden_dim"],
              self.args["deep_biaff_hidden_dim"],
              1,
              pairwise=True,
              dropout=args["dropout"],
          )
      if args["distance"]:
          self.distance = DeepBiaffineScorer(
              2 * self.args["hidden_dim"],
              2 * self.args["hidden_dim"],
              self.args["deep_biaff_hidden_dim"],
              1,
              pairwise=True,
              dropout=args["dropout"],
          )
  
      # criterion
      self.crit = nn.CrossEntropyLoss(
          ignore_index=-1, reduction="sum"
      )  # ignore padding
  
      self.drop = nn.Dropout(args["dropout"])
      self.worddrop = WordDropout(args["word_dropout"])
  
file_path: stanza/stanza/models/depparse/model.py
imports: import logging
  import os
  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.bert_embedding import extract_bert_embeddings
  from stanza.models.common.biaffine import DeepBiaffineScorer
  from stanza.models.common.foundation_cache import load_bert, load_charlm
  from stanza.models.common.hlstm import HighwayLSTM
  from stanza.models.common.dropout import WordDropout
  from stanza.models.common.vocab import CompositeVocab
  from stanza.models.common.char_model import CharacterModel, CharacterLanguageModel
label_desc: Use image node_id 1 to create a new Parser object from inherited base classes: nn with example: obj = Parser(args, vocab, emb_matrix, share_hid, foundation_cache)