class_docstr: Token Classification Model with BERT, applicable for tasks such as Named Entity Recognition
class_docstr_tok: ["Token","Classification","Model","with","BERT",",","applicable","for","tasks","such","as","Named","Entity","Recognition"]
docstr: Initializes Token Classification Model.
docstr_tok: ["Initializes","Token","Classification","Model","."]
code: def __init__(self, cfg: DictConfig, trainer: Trainer = None):
      """Initializes Token Classification Model."""
      # extract str to int labels mapping if a mapping file provided
      if isinstance(cfg.label_ids, str):
          if os.path.exists(cfg.label_ids):
              logging.info(
                  f"Reusing label_ids file found at {cfg.label_ids}."
              )
              label_ids = get_labels_to_labels_id_mapping(cfg.label_ids)
              # update the config to store name to id mapping
              cfg.label_ids = OmegaConf.create(label_ids)
          else:
              raise ValueError(f"{cfg.label_ids} not found.")
  
      self.class_weights = None
      super().__init__(cfg=cfg, trainer=trainer)
  
      self.classifier = TokenClassifier(
          hidden_size=self.hidden_size,
          num_classes=len(self._cfg.label_ids),
          num_layers=self._cfg.head.num_fc_layers,
          activation=self._cfg.head.activation,
          log_softmax=False,
          dropout=self._cfg.head.fc_dropout,
          use_transformer_init=self._cfg.head.use_transformer_init,
      )
  
      self.loss = self.setup_loss(
          class_balancing=self._cfg.dataset.class_balancing
      )
  
      # setup to track metrics
      self.classification_report = ClassificationReport(
          len(self._cfg.label_ids),
          label_ids=self._cfg.label_ids,
          dist_sync_on_step=True,
      )
  
imports: import os
  from typing import List, Optional, Union
  import torch
  from omegaconf import DictConfig, OmegaConf
  from pytorch_lightning import Trainer
  from torch.utils.data import DataLoader
  from nemo.collections.common.losses import CrossEntropyLoss
  from nemo.collections.nlp.data.data_utils.data_preprocessing import get_labels_to_labels_id_mapping
  from nemo.collections.nlp.data.token_classification.token_classification_dataset import BertTokenClassificationDataset, BertTokenClassificationInferDataset
  from nemo.collections.nlp.data.token_classification.token_classification_utils import get_label_ids
  from nemo.collections.nlp.metrics.classification_report import ClassificationReport
  from nemo.collections.nlp.models.nlp_model import NLPModel
  from nemo.collections.nlp.modules.common import TokenClassifier
  from nemo.collections.nlp.parts.utils_funcs import get_classification_report, plot_confusion_matrix, tensor2list
  from nemo.core.classes.common import PretrainedModelInfo, typecheck
  from nemo.utils import logging
label_desc: Use image node_id 1 to create a new TokenClassificationModel object from inherited base classes: NLPModel with example: obj = TokenClassificationModel(cfg, trainer)