class_docstr: Transformers base model (for working with pytorch-transformers models)
class_docstr_tok: ["Transformers","base","model","(","for","working","with","pytorch-transformers","models",")"]
docstr: Run model training
  batch_mapper: a function that maps a batch into parameters that the model
                expects in the forward method (for use with custom heads and models).
                If None it will default to the basic models input structure.
  logging_callback_fn: a function that is called in each evaluation step
                with the model as a parameter.
docstr_tok: ["Run","model","training","batch_mapper",":","a","function","that","maps","a","batch","into","parameters","that","the","model","expects","in","the","forward","method","(","for","use","with","custom","heads","and","models",")",".","If","None","it","will","default","to","the","basic","models","input","structure",".","logging_callback_fn",":","a","function","that","is","called","in","each","evaluation","step","with","the","model","as","a","parameter","."]
code: def _train(
      self,
      data_set: DataLoader,
      dev_data_set: Union[DataLoader, List[DataLoader]] = None,
      test_data_set: Union[DataLoader, List[DataLoader]] = None,
      gradient_accumulation_steps: int = 1,
      per_gpu_train_batch_size: int = 8,
      max_steps: int = -1,
      num_train_epochs: int = 3,
      max_grad_norm: float = 1.0,
      logging_steps: int = 50,
      save_steps: int = 100,
      best_result_file: str = None,
  ):
      """Run model training
      batch_mapper: a function that maps a batch into parameters that the model
                    expects in the forward method (for use with custom heads and models).
                    If None it will default to the basic models input structure.
      logging_callback_fn: a function that is called in each evaluation step
                    with the model as a parameter.
  
      """
      t_total, num_train_epochs = self.get_train_steps_epochs(
          max_steps,
          num_train_epochs,
          gradient_accumulation_steps,
          len(data_set),
      )
      if self.optimizer is None and self.scheduler is None:
          logger.info("Loading default optimizer and scheduler")
          self.setup_default_optimizer(total_steps=t_total)
  
      train_batch_size = per_gpu_train_batch_size * max(1, self.n_gpus)
      logger.info("***** Running training *****")
      logger.info("  Num examples = %d", len(data_set.dataset))
      logger.info("  Num Epochs = %d", num_train_epochs)
      logger.info(
          "  Instantaneous batch size per GPU/CPU = %d",
          per_gpu_train_batch_size,
      )
      logger.info(
          "  Total train batch size (w. parallel, distributed & accumulation) = %d",
          train_batch_size * gradient_accumulation_steps,
      )
      logger.info(
          "  Gradient Accumulation steps = %d",
          gradient_accumulation_steps,
      )
      logger.info("  Total optimization steps = %d", t_total)
  
      global_step = 0
      best_dev = 0
      dev_test = 0
      best_model_path = os.path.join(self.output_path, "best_dev")
      tr_loss, logging_loss = 0.0, 0.0
      self.model.zero_grad()
      train_iterator = trange(num_train_epochs, desc="Epoch")
  
      for epoch, _ in enumerate(train_iterator):
          print("****** Epoch: " + str(epoch))
          epoch_iterator = tqdm(data_set, desc="Train iteration")
          for step, batch in enumerate(epoch_iterator):
              self.model.train()
              batch = tuple(t.to(self.device) for t in batch)
              inputs = self._batch_mapper(batch)
              outputs = self.model(**inputs)
              loss = outputs[0]  # get loss
  
              if self.n_gpus > 1:
                  loss = (
                      loss.mean()
                  )  # mean() to average on multi-gpu parallel training
              if gradient_accumulation_steps > 1:
                  loss = loss / gradient_accumulation_steps
  
              loss.backward()
              torch.nn.utils.clip_grad_norm_(
                  self.model.parameters(), max_grad_norm
              )
  
              tr_loss += loss.item()
              if (step + 1) % gradient_accumulation_steps == 0:
                  self.optimizer.step()
                  self.scheduler.step()
                  self.model.zero_grad()
                  global_step += 1
  
                  if (
                      logging_steps > 0
                      and global_step % logging_steps == 0
                  ):
                      # Log metrics and run evaluation on dev/test
                      best_dev, dev_test = self.update_best_model(
                          dev_data_set,
                          test_data_set,
                          best_dev,
                          dev_test,
                          best_result_file,
                          save_path=best_model_path,
                      )
                      logger.info(
                          "lr = {}".format(self.scheduler.get_lr()[0])
                      )
                      logger.info(
                          "loss = {}".format(
                              (tr_loss - logging_loss) / logging_steps
                          )
                      )
                      logging_loss = tr_loss
  
                  if save_steps > 0 and global_step % save_steps == 0:
                      # Save model checkpoint
                      self.save_model_checkpoint(
                          output_path=self.output_path,
                          name="checkpoint-{}".format(global_step),
                      )
  
              if 0 < max_steps < global_step:
                  epoch_iterator.close()
                  break
          if 0 < max_steps < global_step:
              train_iterator.close()
              break
  
      logger.info(
          " global_step = %s, average loss = %s", global_step, tr_loss
      )
      logger.info("lr = {}".format(self.scheduler.get_lr()[0]))
      logger.info(
          "loss = {}".format((tr_loss - logging_loss) / logging_steps)
      )
      # final evaluation:
      self.update_best_model(
          dev_data_set,
          test_data_set,
          best_dev,
          dev_test,
          best_result_file,
          save_path=best_model_path,
      )
  
imports: import io
  import logging
  import os
  from typing import List, Union
  import torch
  from torch.utils.data import DataLoader
  from tqdm import tqdm, trange
  from transformers import AdamW, BertConfig, BertTokenizer, RobertaConfig, RobertaTokenizer, XLMConfig, XLMTokenizer, XLNetConfig, XLNetTokenizer, get_linear_schedule_with_warmup
  from nlp_architect.models import TrainableModel
  from nlp_architect.models.transformers.quantized_bert import QuantizedBertConfig
label_desc: Use image node_id 14 for calling the TransformerBase obj's underlying member method code with example usage: obj._train(data_set, dev_data_set, test_data_set, gradient_accumulation_steps, per_gpu_train_batch_size, max_steps, num_train_epochs, max_grad_norm, logging_steps, save_steps, best_result_file) without return types