code: def train(self):
      self.step = 0
      self.model.train()
      self.logger.info("***** Running training *****")
      self.logger.info(
          "  Num instance = %d",
          len(self.train_data) * self.args.batch_size,
      )
      self.logger.info("  Num epoch = %d", self.args.num_epochs)
      self.logger.info("  Batch size = %d", self.args.batch_size)
      self.logger.info("  Learning rate = {}".format(self.args.lr))
      self.logger.info(
          "  Evaluate begin = %d", self.args.eval_begin_epoch
      )
  
      if self.args.load_path is not None:  # load model from load_path
          self.logger.info(
              "Loading model from {}".format(self.args.load_path)
          )
          self.model.load_state_dict(torch.load(self.args.load_path))
          self.logger.info("Load model successful!")
  
      with tqdm(
          total=self.train_num_steps,
          postfix="loss:{0:<6.5f}",
          leave=False,
          dynamic_ncols=True,
          initial=self.step,
      ) as pbar:
          self.pbar = pbar
          avg_loss = 0
          for epoch in range(1, self.args.num_epochs + 1):
              y_true, y_pred = [], []
              y_true_idx, y_pred_idx = [], []
              pbar.set_description_str(
                  desc="Epoch {}/{}".format(epoch, self.args.num_epochs)
              )
              for batch in self.train_data:
                  self.step += 1
                  batch = (
                      tup.to(self.args.device)
                      if isinstance(tup, torch.Tensor)
                      else tup
                      for tup in batch
                  )
                  attention_mask, labels, logits, loss = self._step(
                      batch, mode="train"
                  )
                  avg_loss += loss.detach().cpu().item()
  
                  loss.backward()
                  self.optimizer.step()
                  self.scheduler.step()
  
                  self.optimizer.zero_grad()
  
                  if isinstance(logits, torch.Tensor):
                      logits = (
                          logits.argmax(-1).detach().cpu().numpy()
                      )  # batch, seq, 1
                  label_ids = labels.to("cpu").numpy()
                  input_mask = attention_mask.to("cpu").numpy()
                  label_map = {
                      idx: label
                      for label, idx in self.label_map.items()
                  }
                  for i, mask in enumerate(input_mask):
                      temp_1 = []
                      temp_2 = []
                      temp_1_idx, temp_2_idx = [], []
                      for j, m in enumerate(mask):
                          if j == 0:
                              continue
                          if m:
                              if (
                                  label_map[label_ids[i][j]] != "X"
                                  and label_map[label_ids[i][j]]
                                  != "[SEP]"
                              ):
                                  temp_1.append(
                                      label_map[label_ids[i][j]]
                                  )
                                  temp_2.append(label_map[logits[i][j]])
                                  temp_1_idx.append(label_ids[i][j])
                                  temp_2_idx.append(logits[i][j])
                          else:
                              break
                      y_true.append(temp_1)
                      y_pred.append(temp_2)
                      y_true_idx.append(temp_1_idx)
                      y_pred_idx.append(temp_2_idx)
  
                  if self.step % self.refresh_step == 0:
                      avg_loss = float(avg_loss) / self.refresh_step
                      print_output = "loss:{:<6.5f}".format(avg_loss)
                      pbar.update(self.refresh_step)
                      pbar.set_postfix_str(print_output)
                      if self.writer:
                          self.writer.log({"avg_loss": avg_loss})
                      avg_loss = 0
  
              if epoch >= self.args.eval_begin_epoch:
                  if self.dev_data:
                      self.evaluate(epoch)  # generator to dev.
                  if self.test_data:
                      self.test(epoch)
  
          torch.cuda.empty_cache()
  
          pbar.close()
          self.pbar = None
          self.logger.info(
              "Get best dev performance at epoch {}, best dev f1 score is {}".format(
                  self.best_dev_epoch, self.best_dev_metric
              )
          )
          self.logger.info(
              "Get best test performance at epoch {}, best test f1 score is {}".format(
                  self.best_test_epoch, self.best_test_metric
              )
          )
  
file_path: DeepKE/src/deepke/name_entity_re/multimodal/modules/train.py
imports: import torch
  from torch import optim
  from tqdm import tqdm
  from transformers.optimization import get_linear_schedule_with_warmup
  from seqeval.metrics import classification_report
label_desc: Use image node_id 2 for calling the Trainer obj's underlying member method code with example usage: obj.train() without return types