class_docstr: Multi-Task transformer-agnostic model
  Args:
      tasks: Dict of task names along with the labels for each task,
      max_seq_len(int): maximum length of the input token sequence.
      gradient_accumulation_steps(default:1): number of gradient accumulation steps,
      steps_per_epoch(int): number of steps taken per epoch. Specify if gradient_accumulation_steps > 1
      backbone_model(str): name of HuggingFace.Transformers backbone model. Default: 'bert-base-cased'
      multilabel(default: False): set to true for multilabel classification,
      return_probas(default: False): set true to return prediction probabilities,
      freeze_embeddings(default: False): set true to freeze BERT embeddings
      dropout(default: None): dropout for the final model layer.
      If not set, defaults to the parameter hidden_dropout_prob of original model
      cuda_cache_size(default:3): predicts cache size. Recommended if we need classify one samples for many tasks. 0 if we don't use cache
      cuda_cache(default:True): if True, store cache on GPU
      seed(default:42): Torch manual_random_seed
class_docstr_tok: ["Multi-Task","transformer-agnostic","model","Args",":","tasks",":","Dict","of","task","names","along","with","the","labels","for","each","task",",","max_seq_len","(","int",")",":","maximum","length","of","the","input","token","sequence",".","gradient_accumulation_steps","(","default:1",")",":","number","of","gradient","accumulation","steps",",","steps_per_epoch","(","int",")",":","number","of","steps","taken","per","epoch",".","Specify","if","gradient_accumulation_steps",">","1","backbone_model","(","str",")",":","name","of","HuggingFace.Transformers","backbone","model",".","Default",":","'bert-base-cased'","multilabel","(","default",":","False",")",":","set","to","true","for","multilabel","classification",",","return_probas","(","default",":","False",")",":","set","true","to","return","prediction","probabilities",",","freeze_embeddings","(","default",":","False",")",":","set","true","to","freeze","BERT","embeddings","dropout","(","default",":","None",")",":","dropout","for","the","final","model","layer",".","If","not","set",",","defaults","to","the","parameter","hidden_dropout_prob","of","original","model","cuda_cache_size","(","default:3",")",":","predicts","cache","size",".","Recommended","if","we","need","classify","one","samples","for","many","tasks",".","0","if","we","do","n't","use","cache","cuda_cache","(","default",":","True",")",":","if","True",",","store","cache","on","GPU","seed","(","default:42",")",":","Torch","manual_random_seed"]
docstr: Train model on given batch.
  This method calls train_op using features and y (labels).
  Args:
      features: batch of InputFeatures
      y: batch of labels (class id)
  Returns:
      dict with loss for each task
docstr_tok: ["Train","model","on","given","batch",".","This","method","calls","train_op","using","features","and","y","(","labels",")",".","Args",":","features",":","batch","of","InputFeatures","y",":","batch","of","labels","(","class","id",")","Returns",":","dict","with","loss","for","each","task"]
code: def train_on_batch(self, *args):
      """Train model on given batch.
      This method calls train_op using features and y (labels).
      Args:
          features: batch of InputFeatures
          y: batch of labels (class id)
      Returns:
          dict with loss for each task
      """
      log.debug(f"Training for {args}")
      error_msg = (
          f"Len of arguments {len(args)} is WRONG. "
          f"Correct is {2 * self.n_tasks} as n_tasks is {self.n_tasks}"
      )
      if len(args) != 2 * self.n_tasks:
          raise Exception(error_msg)
      ids_to_iterate = [
          k for k in range(self.n_tasks) if len(args[k]) > 0
      ]
      if len(ids_to_iterate) == 0:
          raise Exception(f"No examples given! Given args {args}")
      elif len(ids_to_iterate) > 1:
          raise Exception(
              "Samples from more than 1 task in train_on_batch"
          )
      task_id = ids_to_iterate[0]
      _input, batch_size = self._make_input(
          task_features=args[task_id],
          task_id=task_id,
          labels=args[task_id + self.n_tasks],
      )
      if _input == {}:
          raise Exception("Empty input!")
  
      if self.prev_id is None:
          self.prev_id = task_id
      elif self.prev_id != task_id and not self.printed:
          log.info("Seen samples from different tasks")
          self.printed = True
      if "token_type_ids" not in _input:
          _input["token_type_ids"] = None
      loss, logits = self.model(task_id=task_id, **_input)
      if self.is_data_parallel:
          loss = loss.mean()
      loss = loss / self.gradient_accumulation_steps
      loss.backward()
  
      # Clip the norm of the gradients to 1.0.
      # This is to help prevent the "exploding gradients" problem.
      if self.clip_norm:
          torch.nn.utils.clip_grad_norm_(
              self.model.parameters(), self.clip_norm
          )
  
      if (
          self.steps_taken + 1
      ) % self.gradient_accumulation_steps == 0 or (
          self.steps_per_epoch is not None
          and (self.steps_taken + 1) % self.steps_per_epoch == 0
      ):
          self.optimizer.step()
          self.optimizer.zero_grad()
      self.train_losses[task_id] = loss.item()
      self.steps_taken += 1
      log.debug(f"train {task_id} {logits}")
      return {"losses": self.train_losses}
  
imports: from collections.abc import Iterable
  from logging import getLogger
  from pathlib import Path
  from typing import Dict, Optional
  import numpy
  import torch
  import torch.nn
  from torch.nn import CrossEntropyLoss, MSELoss, BCEWithLogitsLoss
  from transformers import AutoConfig, AutoModel
  from deeppavlov.core.common.errors import ConfigError
  from deeppavlov.core.common.registry import register
  from deeppavlov.core.models.torch_model import TorchModel
  from deeppavlov.models.torch_bert.torch_transformers_sequence_tagger import token_from_subtoken, token_labels_to_subtoken_labels
label_desc: Use image node_id 6 for calling the MultiTaskTransformer obj's underlying member method code with example usage: obj.train_on_batch() and returns: dict