docstr: Train the model with the given training objective
  Each training objective is sampled in turn for one batch.
  We sample only as many batches from each objective as there are in the smallest one
  to make sure of equal training with each dataset.
  
  :param train_dataloader: DataLoader with training InputExamples
  :param evaluator: An evaluator (sentence_transformers.evaluation) evaluates the model performance during training on held-out dev data. It is used to determine the best model that is saved to disc.
  :param epochs: Number of epochs for training
  :param loss_fct: Which loss function to use for training. If None, will use nn.BCEWithLogitsLoss() if self.config.num_labels == 1 else nn.CrossEntropyLoss()
  :param activation_fct: Activation function applied on top of logits output of model.
  :param scheduler: Learning rate scheduler. Available schedulers: constantlr, warmupconstant, warmuplinear, warmupcosine, warmupcosinewithhardrestarts
  :param warmup_steps: Behavior depends on the scheduler. For WarmupLinear (default), the learning rate is increased from o up to the maximal learning rate. After these many training steps, the learning rate is decreased linearly back to zero.
  :param optimizer_class: Optimizer
  :param optimizer_params: Optimizer parameters
  :param weight_decay: Weight decay for model parameters
  :param evaluation_steps: If > 0, evaluate the model using evaluator after each number of training steps
  :param output_path: Storage path for the model and evaluation files
  :param save_best_model: If true, the best model (according to evaluator) is stored at output_path
  :param max_grad_norm: Used for gradient normalization.
  :param use_amp: Use Automatic Mixed Precision (AMP). Only for Pytorch >= 1.6.0
  :param callback: Callback function that is invoked after each evaluation.
          It must accept the following three parameters in this order:
          `score`, `epoch`, `steps`
  :param show_progress_bar: If True, output a tqdm progress bar
docstr_tok: ["Train","the","model","with","the","given","training","objective","Each","training","objective","is","sampled","in","turn","for","one","batch",".","We","sample","only","as","many","batches","from","each","objective","as","there","are","in","the","smallest","one","to","make","sure","of","equal","training","with","each","dataset",".",":","param","train_dataloader",":","DataLoader","with","training","InputExamples",":","param","evaluator",":","An","evaluator","(","sentence_transformers.evaluation",")","evaluates","the","model","performance","during","training","on","held-out","dev","data",".","It","is","used","to","determine","the","best","model","that","is","saved","to","disc",".",":","param","epochs",":","Number","of","epochs","for","training",":","param","loss_fct",":","Which","loss","function","to","use","for","training",".","If","None",",","will","use","nn.BCEWithLogitsLoss","(",")","if","self.config.num_labels","==","1","else","nn.CrossEntropyLoss","(",")",":","param","activation_fct",":","Activation","function","applied","on","top","of","logits","output","of","model",".",":","param","scheduler",":","Learning","rate","scheduler",".","Available","schedulers",":","constantlr",",","warmupconstant",",","warmuplinear",",","warmupcosine",",","warmupcosinewithhardrestarts",":","param","warmup_steps",":","Behavior","depends","on","the","scheduler",".","For","WarmupLinear","(","default",")",",","the","learning","rate","is","increased","from","o","up","to","the","maximal","learning","rate",".","After","these","many","training","steps",",","the","learning","rate","is","decreased","linearly","back","to","zero",".",":","param","optimizer_class",":","Optimizer",":","param","optimizer_params",":","Optimizer","parameters",":","param","weight_decay",":","Weight","decay","for","model","parameters",":","param","evaluation_steps",":","If",">","0",",","evaluate","the","model","using","evaluator","after","each","number","of","training","steps",":","param","output_path",":","Storage","path","for","the","model","and","evaluation","files",":","param","save_best_model",":","If","true",",","the","best","model","(","according","to","evaluator",")","is","stored","at","output_path",":","param","max_grad_norm",":","Used","for","gradient","normalization",".",":","param","use_amp",":","Use","Automatic","Mixed","Precision","(","AMP",")",".","Only","for","Pytorch",">","=","1.6.0",":","param","callback",":","Callback","function","that","is","invoked","after","each","evaluation",".","It","must","accept","the","following","three","parameters","in","this","order",":","`","score","`",",","`","epoch","`",",","`","steps","`",":","param","show_progress_bar",":","If","True",",","output","a","tqdm","progress","bar"]
code: def fit(
      self,
      train_dataloader: DataLoader,
      evaluator: SentenceEvaluator = None,
      epochs: int = 1,
      loss_fct=None,
      activation_fct=nn.Identity(),
      scheduler: str = "WarmupLinear",
      warmup_steps: int = 10000,
      optimizer_class: Type[Optimizer] = torch.optim.AdamW,
      optimizer_params: Dict[str, object] = {"lr": 2e-5},
      weight_decay: float = 0.01,
      evaluation_steps: int = 0,
      output_path: str = None,
      save_best_model: bool = True,
      max_grad_norm: float = 1,
      use_amp: bool = False,
      callback: Callable[[float, int, int], None] = None,
      show_progress_bar: bool = True,
  ):
      """
      Train the model with the given training objective
      Each training objective is sampled in turn for one batch.
      We sample only as many batches from each objective as there are in the smallest one
      to make sure of equal training with each dataset.
  
      :param train_dataloader: DataLoader with training InputExamples
      :param evaluator: An evaluator (sentence_transformers.evaluation) evaluates the model performance during training on held-out dev data. It is used to determine the best model that is saved to disc.
      :param epochs: Number of epochs for training
      :param loss_fct: Which loss function to use for training. If None, will use nn.BCEWithLogitsLoss() if self.config.num_labels == 1 else nn.CrossEntropyLoss()
      :param activation_fct: Activation function applied on top of logits output of model.
      :param scheduler: Learning rate scheduler. Available schedulers: constantlr, warmupconstant, warmuplinear, warmupcosine, warmupcosinewithhardrestarts
      :param warmup_steps: Behavior depends on the scheduler. For WarmupLinear (default), the learning rate is increased from o up to the maximal learning rate. After these many training steps, the learning rate is decreased linearly back to zero.
      :param optimizer_class: Optimizer
      :param optimizer_params: Optimizer parameters
      :param weight_decay: Weight decay for model parameters
      :param evaluation_steps: If > 0, evaluate the model using evaluator after each number of training steps
      :param output_path: Storage path for the model and evaluation files
      :param save_best_model: If true, the best model (according to evaluator) is stored at output_path
      :param max_grad_norm: Used for gradient normalization.
      :param use_amp: Use Automatic Mixed Precision (AMP). Only for Pytorch >= 1.6.0
      :param callback: Callback function that is invoked after each evaluation.
              It must accept the following three parameters in this order:
              `score`, `epoch`, `steps`
      :param show_progress_bar: If True, output a tqdm progress bar
      """
      train_dataloader.collate_fn = self.smart_batching_collate
  
      if use_amp:
          from torch.cuda.amp import autocast
  
          scaler = torch.cuda.amp.GradScaler()
  
      self.model.to(self._target_device)
  
      if output_path is not None:
          os.makedirs(output_path, exist_ok=True)
  
      self.best_score = -9999999
      num_train_steps = int(len(train_dataloader) * epochs)
  
      # Prepare optimizers
      param_optimizer = list(self.model.named_parameters())
  
      no_decay = ["bias", "LayerNorm.bias", "LayerNorm.weight"]
      optimizer_grouped_parameters = [
          {
              "params": [
                  p
                  for n, p in param_optimizer
                  if not any(nd in n for nd in no_decay)
              ],
              "weight_decay": weight_decay,
          },
          {
              "params": [
                  p
                  for n, p in param_optimizer
                  if any(nd in n for nd in no_decay)
              ],
              "weight_decay": 0.0,
          },
      ]
  
      optimizer = optimizer_class(
          optimizer_grouped_parameters, **optimizer_params
      )
  
      if isinstance(scheduler, str):
          scheduler = SentenceTransformer._get_scheduler(
              optimizer,
              scheduler=scheduler,
              warmup_steps=warmup_steps,
              t_total=num_train_steps,
          )
  
      if loss_fct is None:
          loss_fct = (
              nn.BCEWithLogitsLoss()
              if self.config.num_labels == 1
              else nn.CrossEntropyLoss()
          )
  
      skip_scheduler = False
      for epoch in trange(
          epochs, desc="Epoch", disable=not show_progress_bar
      ):
          training_steps = 0
          self.model.zero_grad()
          self.model.train()
  
          for features, labels in tqdm(
              train_dataloader,
              desc="Iteration",
              smoothing=0.05,
              disable=not show_progress_bar,
          ):
              if use_amp:
                  with autocast():
                      model_predictions = self.model(
                          **features, return_dict=True
                      )
                      logits = activation_fct(model_predictions.logits)
                      if self.config.num_labels == 1:
                          logits = logits.view(-1)
                      loss_value = loss_fct(logits, labels)
  
                  scale_before_step = scaler.get_scale()
                  scaler.scale(loss_value).backward()
                  scaler.unscale_(optimizer)
                  torch.nn.utils.clip_grad_norm_(
                      self.model.parameters(), max_grad_norm
                  )
                  scaler.step(optimizer)
                  scaler.update()
  
                  skip_scheduler = (
                      scaler.get_scale() != scale_before_step
                  )
              else:
                  model_predictions = self.model(
                      **features, return_dict=True
                  )
                  logits = activation_fct(model_predictions.logits)
                  if self.config.num_labels == 1:
                      logits = logits.view(-1)
                  loss_value = loss_fct(logits, labels)
                  loss_value.backward()
                  torch.nn.utils.clip_grad_norm_(
                      self.model.parameters(), max_grad_norm
                  )
                  optimizer.step()
  
              optimizer.zero_grad()
  
              if not skip_scheduler:
                  scheduler.step()
  
              training_steps += 1
  
              if (
                  evaluator is not None
                  and evaluation_steps > 0
                  and training_steps % evaluation_steps == 0
              ):
                  self._eval_during_training(
                      evaluator,
                      output_path,
                      save_best_model,
                      epoch,
                      training_steps,
                      callback,
                  )
  
                  self.model.zero_grad()
                  self.model.train()
  
          if evaluator is not None:
              self._eval_during_training(
                  evaluator,
                  output_path,
                  save_best_model,
                  epoch,
                  -1,
                  callback,
              )
  
imports: from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
  import numpy
  import logging
  import os
  from typing import Dict, Type, Callable, List
  import torch
  from torch import nn
  from torch.optim import Optimizer
  from torch.utils.data import DataLoader
  from tqdm.autonotebook import tqdm, trange
  from ..None import SentenceTransformer, util
  from ..evaluation import SentenceEvaluator
label_desc: Use image node_id 4 for calling the CrossEncoder obj's underlying member method code with example usage: obj.fit(train_dataloader, evaluator, epochs, loss_fct, activation_fct, scheduler, warmup_steps, optimizer_class, optimizer_params, weight_decay, evaluation_steps, output_path, save_best_model, max_grad_norm, use_amp, callback, show_progress_bar) without return types