class_docstr: BERT-based sequence classifier
docstr: Fine-tunes the BERT classifier using the given training data.
  
  Args:
      token_ids (list): List of training token id lists.
      input_mask (list): List of input mask lists.
      labels (list): List of training labels.
      token_type_ids (list, optional): List of lists. Each sublist
          contains segment ids indicating if the token belongs to
          the first sentence(0) or second sentence(1). Only needed
          for two-sentence tasks.
      num_gpus (int, optional): The number of gpus to use.
                                If None is specified, all available GPUs
                                will be used. Defaults to None.
      num_epochs (int, optional): Number of training epochs.
          Defaults to 1.
      batch_size (int, optional): Training batch size. Defaults to 32.
      lr (float): Learning rate of the Adam optimizer. Defaults to 2e-5.
      warmup_proportion (float, optional): Proportion of training to
          perform linear learning rate warmup for. E.g., 0.1 = 10% of
          training. Defaults to None.
      verbose (bool, optional): If True, shows the training progress and
          loss values. Defaults to True.
docstr_tok: ["Fine-tunes","the","BERT","classifier","using","the","given","training","data",".","Args",":","token_ids","(","list",")",":","List","of","training","token","id","lists",".","input_mask","(","list",")",":","List","of","input","mask","lists",".","labels","(","list",")",":","List","of","training","labels",".","token_type_ids","(","list",",","optional",")",":","List","of","lists",".","Each","sublist","contains","segment","ids","indicating","if","the","token","belongs","to","the","first","sentence","(","0",")","or","second","sentence","(","1",")",".","Only","needed","for","two-sentence","tasks",".","num_gpus","(","int",",","optional",")",":","The","number","of","gpus","to","use",".","If","None","is","specified",",","all","available","GPUs","will","be","used",".","Defaults","to","None",".","num_epochs","(","int",",","optional",")",":","Number","of","training","epochs",".","Defaults","to","1.","batch_size","(","int",",","optional",")",":","Training","batch","size",".","Defaults","to","32.","lr","(","float",")",":","Learning","rate","of","the","Adam","optimizer",".","Defaults","to","2e-5",".","warmup_proportion","(","float",",","optional",")",":","Proportion","of","training","to","perform","linear","learning","rate","warmup","for",".","E.g.",",","0.1","=","10","%","of","training",".","Defaults","to","None",".","verbose","(","bool",",","optional",")",":","If","True",",","shows","the","training","progress","and","loss","values",".","Defaults","to","True","."]
code: def fit(
      self,
      token_ids,
      input_mask,
      labels,
      token_type_ids=None,
      num_gpus=None,
      num_epochs=1,
      batch_size=32,
      lr=2e-5,
      warmup_proportion=None,
      verbose=True,
  ):
      """Fine-tunes the BERT classifier using the given training data.
  
      Args:
          token_ids (list): List of training token id lists.
          input_mask (list): List of input mask lists.
          labels (list): List of training labels.
          token_type_ids (list, optional): List of lists. Each sublist
              contains segment ids indicating if the token belongs to
              the first sentence(0) or second sentence(1). Only needed
              for two-sentence tasks.
          num_gpus (int, optional): The number of gpus to use.
                                    If None is specified, all available GPUs
                                    will be used. Defaults to None.
          num_epochs (int, optional): Number of training epochs.
              Defaults to 1.
          batch_size (int, optional): Training batch size. Defaults to 32.
          lr (float): Learning rate of the Adam optimizer. Defaults to 2e-5.
          warmup_proportion (float, optional): Proportion of training to
              perform linear learning rate warmup for. E.g., 0.1 = 10% of
              training. Defaults to None.
          verbose (bool, optional): If True, shows the training progress and
              loss values. Defaults to True.
      """
  
      device, num_gpus = get_device(num_gpus)
  
      self.model = move_to_device(self.model, device, num_gpus)
  
      token_ids_tensor = torch.tensor(token_ids, dtype=torch.long)
      input_mask_tensor = torch.tensor(input_mask, dtype=torch.long)
      labels_tensor = torch.tensor(labels, dtype=torch.long)
  
      if token_type_ids:
          token_type_ids_tensor = torch.tensor(
              token_type_ids, dtype=torch.long
          )
          train_dataset = TensorDataset(
              token_ids_tensor,
              input_mask_tensor,
              token_type_ids_tensor,
              labels_tensor,
          )
      else:
          train_dataset = TensorDataset(
              token_ids_tensor, input_mask_tensor, labels_tensor
          )
      train_sampler = RandomSampler(train_dataset)
  
      train_dataloader = DataLoader(
          train_dataset, sampler=train_sampler, batch_size=batch_size
      )
      # define optimizer and model parameters
      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": 0.01,
          },
          {
              "params": [
                  p
                  for n, p in param_optimizer
                  if any(nd in n for nd in no_decay)
              ],
              "weight_decay": 0.0,
          },
      ]
  
      num_batches = len(train_dataloader)
      num_train_optimization_steps = num_batches * num_epochs
  
      if warmup_proportion is None:
          opt = BertAdam(optimizer_grouped_parameters, lr=lr)
      else:
          opt = BertAdam(
              optimizer_grouped_parameters,
              lr=lr,
              t_total=num_train_optimization_steps,
              warmup=warmup_proportion,
          )
  
      # define loss function
      loss_func = nn.CrossEntropyLoss().to(device)
  
      # train
      self.model.train()  # training mode
  
      for epoch in range(num_epochs):
          training_loss = 0
          for i, batch in enumerate(
              tqdm(train_dataloader, desc="Iteration")
          ):
              if token_type_ids:
                  (
                      x_batch,
                      mask_batch,
                      token_type_ids_batch,
                      y_batch,
                  ) = tuple(t.to(device) for t in batch)
              else:
                  token_type_ids_batch = None
                  x_batch, mask_batch, y_batch = tuple(
                      t.to(device) for t in batch
                  )
  
              opt.zero_grad()
  
              y_h = self.model(
                  input_ids=x_batch,
                  token_type_ids=token_type_ids_batch,
                  attention_mask=mask_batch,
                  labels=None,
              )
              loss = loss_func(y_h, y_batch).mean()
  
              training_loss += loss.item()
  
              loss.backward()
              opt.step()
              if verbose:
                  if i % ((num_batches // 10) + 1) == 0:
                      logging.info(
                          "epoch:{}/{}; batch:{}->{}/{}; average training loss:{:.6f}".format(
                              epoch + 1,
                              num_epochs,
                              i + 1,
                              min(
                                  i + 1 + num_batches // 10, num_batches
                              ),
                              num_batches,
                              training_loss / (i + 1),
                          )
                      )
      # empty cache
      del [x_batch, y_batch, mask_batch, token_type_ids_batch]
      torch.cuda.empty_cache()
  
imports: import logging
  import warnings
  from collections.abc import Iterable
  from enum import Enum
  import torch
  from pytorch_pretrained_bert.tokenization import BertTokenizer
  from tqdm import tqdm
  import numpy
  import torch.nn
  from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset
  from pytorch_pretrained_bert.modeling import BertForSequenceClassification
  from pytorch_pretrained_bert.optimization import BertAdam
  from interpret_text.experimental.common.utils_pytorch import get_device, move_to_device
  from interpret_text.experimental.common.constants import BertTokens
  from collections import namedtuple
  from cached_property import cached_property
label_desc: Use image node_id 3 for calling the BERTSequenceClassifier obj's underlying member method code with example usage: obj.fit(token_ids, input_mask, labels, token_type_ids, num_gpus, num_epochs, batch_size, lr, warmup_proportion, verbose) without return types