code: def build_optimizer(args, model, build_simple_adadelta=False):
      """
      Build an optimizer based on the arguments given
  
      If we are "multistage" training and epochs_trained < epochs // 2,
      we build an AdaDelta optimizer instead of whatever was requested
      The build_simple_adadelta parameter controls this
      """
      if build_simple_adadelta:
          optim_type = "adadelta"
          bert_finetune = args.get("stage1_bert_finetune", False)
          if bert_finetune:
              bert_learning_rate = args["stage1_bert_learning_rate"]
          learning_eps = DEFAULT_LEARNING_EPS["adadelta"]
          learning_rate = args["stage1_learning_rate"]
          learning_rho = DEFAULT_LEARNING_RHO
          weight_decay = DEFAULT_WEIGHT_DECAY["adadelta"]
      else:
          optim_type = args["optim"].lower()
          bert_finetune = args.get("bert_finetune", False)
          if bert_finetune:
              bert_learning_rate = args["bert_learning_rate"]
          learning_beta2 = args["learning_beta2"]
          learning_eps = args["learning_eps"]
          learning_rate = args["learning_rate"]
          learning_rho = args["learning_rho"]
          momentum = args["learning_momentum"]
          weight_decay = args["learning_weight_decay"]
  
      base_parameters = [
          param
          for name, param in model.named_parameters()
          if not model.is_unsaved_module(name)
          and not name.startswith("bert_model.")
      ]
      parameters = [
          {"param_group_name": "base", "params": base_parameters},
      ]
      if bert_finetune:
          bert_parameters = [
              param
              for name, param in model.named_parameters()
              if not model.is_unsaved_module(name)
              and name.startswith("bert_model.")
          ]
          logger.debug(
              "Finetuning %d transformer parameters"
              % len(bert_parameters)
          )
          if (
              len(bert_parameters) > 0
              and args["bert_finetune_layers"] is not None
          ):
              num_layers = model.bert_model.config.num_hidden_layers
              start_layer = num_layers - args["bert_finetune_layers"]
              bert_parameters = []
              for layer_num in range(start_layer, num_layers):
                  # print([name for name, param in model.named_parameters()
                  #       if not model.is_unsaved_module(name) and name.startswith("bert_model.") and "layer.%d." % layer_num in name])
                  bert_parameters.extend(
                      [
                          param
                          for name, param in model.named_parameters()
                          if not model.is_unsaved_module(name)
                          and name.startswith("bert_model.")
                          and "layer.%d." % layer_num in name
                      ]
                  )
          if len(bert_parameters) > 0:
              parameters.append(
                  {
                      "param_group_name": "bert",
                      "params": bert_parameters,
                      "lr": learning_rate * bert_learning_rate,
                      "weight_decay": weight_decay
                      * args["bert_weight_decay"],
                  }
              )
  
      if optim_type == "sgd":
          logger.info(
              "Building SGD with lr=%f, momentum=%f, weight_decay=%f",
              learning_rate,
              momentum,
              weight_decay,
          )
          optimizer = optim.SGD(
              parameters,
              lr=learning_rate,
              momentum=momentum,
              weight_decay=weight_decay,
          )
      elif optim_type == "adadelta":
          logger.info(
              "Building Adadelta with lr=%f, eps=%f, weight_decay=%f, rho=%f",
              learning_rate,
              learning_eps,
              weight_decay,
              learning_rho,
          )
          optimizer = optim.Adadelta(
              parameters,
              lr=learning_rate,
              eps=learning_eps,
              weight_decay=weight_decay,
              rho=learning_rho,
          )
      elif optim_type == "adamw":
          logger.info(
              "Building AdamW with lr=%f, beta2=%f, eps=%f, weight_decay=%f",
              learning_rate,
              learning_beta2,
              learning_eps,
              weight_decay,
          )
          optimizer = optim.AdamW(
              parameters,
              lr=learning_rate,
              betas=(0.9, learning_beta2),
              eps=learning_eps,
              weight_decay=weight_decay,
          )
      elif optim_type == "adabelief":
          try:
              from adabelief_pytorch import AdaBelief
          except ModuleNotFoundError as e:
              raise ModuleNotFoundError(
                  "Could not create adabelief optimizer.  Perhaps the adabelief-pytorch package is not installed"
              ) from e
          logger.info(
              "Building AdaBelief with lr=%f, eps=%f, weight_decay=%f",
              learning_rate,
              learning_eps,
              weight_decay,
          )
          # TODO: make these args
          optimizer = AdaBelief(
              parameters,
              lr=learning_rate,
              eps=learning_eps,
              weight_decay=weight_decay,
              weight_decouple=False,
              rectify=False,
          )
      elif optim_type == "madgrad" or optim_type == "mirror_madgrad":
          try:
              import madgrad
          except ModuleNotFoundError as e:
              raise ModuleNotFoundError(
                  "Could not create madgrad optimizer.  Perhaps the madgrad package is not installed"
              ) from e
          if optim_type == "madgrad":
              logger.info(
                  "Building madgrad with lr=%f, weight_decay=%f, momentum=%f",
                  learning_rate,
                  weight_decay,
                  momentum,
              )
              optimizer = madgrad.MADGRAD(
                  parameters,
                  lr=learning_rate,
                  weight_decay=weight_decay,
                  momentum=momentum,
              )
          else:
              logger.info(
                  "Building mirror madgrad with lr=%f, weight_decay=%f, momentum=%f",
                  learning_rate,
                  weight_decay,
                  momentum,
              )
              optimizer = madgrad.MirrorMADGRAD(
                  parameters,
                  lr=learning_rate,
                  weight_decay=weight_decay,
                  momentum=momentum,
              )
      else:
          raise ValueError("Unknown optimizer: %s" % optim)
      return optimizer
  
file_path: stanza/stanza/models/constituency/utils.py
imports: from collections import Counter, deque
  import copy
  import logging
  import torch.nn
  from torch import optim
  from stanza.models.common.doc import TEXT, Document
  from stanza.utils.get_tqdm import get_tqdm
label_desc: Use image node_id 5 for calling a global function with example usage: build_optimizer(args, model, build_simple_adadelta) and returns: optimizer