class_docstr: Stores a constituency model and its optimizer
class_docstr_tok: ["Stores","a","constituency","model","and","its","optimizer"]
docstr: Load pretrained pieces and then build a new model
docstr_tok: ["Load","pretrained","pieces","and","then","build","a","new","model"]
code: def build_new_model(args, train_set):
      """
      Load pretrained pieces and then build a new model
      """
      if train_set is None:
          raise ValueError(
              "Must have a train set to build a new model - needed for labels and delta word vectors"
          )
  
      labels = data.dataset_labels(train_set)
  
      if args.model_type == ModelType.CNN:
          pretrain = Trainer.load_pretrain(args, foundation_cache=None)
          elmo_model = (
              utils.load_elmo(args.elmo_model)
              if args.use_elmo
              else None
          )
          charmodel_forward = load_charlm(args.charlm_forward_file)
          charmodel_backward = load_charlm(args.charlm_backward_file)
          bert_model, bert_tokenizer = load_bert(args.bert_model)
  
          extra_vocab = data.dataset_vocab(train_set)
          model = cnn_classifier.CNNClassifier(
              pretrain=pretrain,
              extra_vocab=extra_vocab,
              labels=labels,
              charmodel_forward=charmodel_forward,
              charmodel_backward=charmodel_backward,
              elmo_model=elmo_model,
              bert_model=bert_model,
              bert_tokenizer=bert_tokenizer,
              args=args,
          )
          model = model.to(args.device)
      elif args.model_type == ModelType.CONSTITUENCY:
          # this passes flags such as "constituency_backprop" from
          # the classifier to the TreeEmbedding as the "backprop" flag
          parser_args = {
              x[len("constituency_") :]: y
              for x, y in vars(args).items()
              if x.startswith("constituency_")
          }
          parser_args.update(
              {
                  "wordvec_pretrain_file": args.wordvec_pretrain_file,
                  "charlm_forward_file": args.charlm_forward_file,
                  "charlm_backward_file": args.charlm_backward_file,
                  "bert_model": args.bert_model,
                  # we found that finetuning from the classifier output
                  # all the way to the bert layers caused the bert model
                  # to go astray
                  # could make this an option... but it is much less accurate
                  # with the Bert finetuning
                  # noting that the constituency parser itself works better
                  # after finetuning, of course
                  "bert_finetune": False,
                  "stage1_bert_finetune": False,
              }
          )
          logger.info(
              "Building constituency classifier using %s as the base model"
              % args.constituency_model
          )
          tree_embedding = TreeEmbedding.from_parser_file(parser_args)
          model = constituency_classifier.ConstituencyClassifier(
              tree_embedding=tree_embedding, labels=labels, args=args
          )
          model = model.to(args.device)
      else:
          raise ValueError(
              "Unhandled model type {}".format(args.model_type)
          )
  
      optimizer = Trainer.build_optimizer(model, args)
  
      return Trainer(model, optimizer)
  
file_path: stanza/stanza/models/classifiers/trainer.py
imports: import logging
  import os
  import torch
  import torch.optim
  import stanza.models.classifiers.data
  import stanza.models.classifiers.cnn_classifier
  import stanza.models.classifiers.constituency_classifier
  from stanza.models.classifiers.utils import ModelType
  from stanza.models.common.foundation_cache import load_bert, load_charlm, load_pretrain
  from stanza.models.common.pretrain import Pretrain
  from stanza.models.constituency.tree_embedding import TreeEmbedding
label_desc: Use image node_id 5 for calling the Trainer obj's underlying member method code with example usage: obj.build_new_model(args, train_set) and returns: Trainer