class_docstr: Task for classification.
  
  Attributes:
      model:
          1. Model name
          2. Model name on Backprop's text-classification endpoint
          3. Model object that implements the text-classification task
      local (optional): Run locally. Defaults to False
      api_key (optional): Backprop API key for non-local inference
      device (optional): Device to run inference on. Defaults to "cuda" if available.
class_docstr_tok: ["Task","for","classification",".","Attributes",":","model",":","1",".","Model","name","2",".","Model","name","on","Backprop","'s","text-classification","endpoint","3",".","Model","object","that","implements","the","text-classification","task","local","(","optional",")",":","Run","locally",".","Defaults","to","False","api_key","(","optional",")",":","Backprop","API","key","for","non-local","inference","device","(","optional",")",":","Device","to","run","inference","on",".","Defaults","to","``","cuda","''","if","available","."]
docstr: Finetunes a text classification model on provided data.
  
  Args:
      params: Dict containing keys "texts" and "labels", with values being input/output data lists.
      validation_split: Float between 0 and 1 that determines percentage of data to use for validation.
      max_length: Int determining the maximum token length of input strings.
      epochs: Integer specifying how many training iterations to run.
      batch_size: Batch size when training. Leave as None to automatically determine batch size.
      optimal_batch_size: Optimal batch size for the model being trained -- defaults to model settings.
      early_stopping_epochs: Integer determining how many epochs will run before stopping without an improvement in validation loss.
      train_dataloader: Dataloader for providing training data when finetuning. Defaults to inbuilt dataloder.
      val_dataloader: Dataloader for providing validation data when finetuning. Defaults to inbuilt dataloader.
      step: Function determining how to call model for a training step. Defaults to step defined in this task class.
      configure_optimizers: Function that sets up the optimizer for training. Defaults to optimizer defined in this task class.
  
  Examples::
      
      import backprop
  
      tc = backprop.TextCLassification()
  
      # Set up input data. Labels will automatically be used to set up model with number of classes for classification.
      inp = ["This is a political news article", "This is a computer science research paper", "This is a movie review"]
      out = ["Politics", "Science", "Entertainment"]
      params = {"texts": inp, "labels": out}
  
      # Finetune
      tc.finetune(params)
docstr_tok: ["Finetunes","a","text","classification","model","on","provided","data",".","Args",":","params",":","Dict","containing","keys","``","texts","''","and","``","labels","''",",","with","values","being","input\/output","data","lists",".","validation_split",":","Float","between","0","and","1","that","determines","percentage","of","data","to","use","for","validation",".","max_length",":","Int","determining","the","maximum","token","length","of","input","strings",".","epochs",":","Integer","specifying","how","many","training","iterations","to","run",".","batch_size",":","Batch","size","when","training",".","Leave","as","None","to","automatically","determine","batch","size",".","optimal_batch_size",":","Optimal","batch","size","for","the","model","being","trained","--","defaults","to","model","settings",".","early_stopping_epochs",":","Integer","determining","how","many","epochs","will","run","before","stopping","without","an","improvement","in","validation","loss",".","train_dataloader",":","Dataloader","for","providing","training","data","when","finetuning",".","Defaults","to","inbuilt","dataloder",".","val_dataloader",":","Dataloader","for","providing","validation","data","when","finetuning",".","Defaults","to","inbuilt","dataloader",".","step",":","Function","determining","how","to","call","model","for","a","training","step",".","Defaults","to","step","defined","in","this","task","class",".","configure_optimizers",":","Function","that","sets","up","the","optimizer","for","training",".","Defaults","to","optimizer","defined","in","this","task","class",".","Examples",":",":","import","backprop","tc","=","backprop.TextCLassification","(",")","#","Set","up","input","data",".","Labels","will","automatically","be","used","to","set","up","model","with","number","of","classes","for","classification",".","inp","=","[","``","This","is","a","political","news","article","''",",","``","This","is","a","computer","science","research","paper","''",",","``","This","is","a","movie","review","''","]","out","=","[","``","Politics","''",",","``","Science","''",",","``","Entertainment","''","]","params","=","{","``","texts","''",":","inp",",","``","labels","''",":","out","}","#","Finetune","tc.finetune","(","params",")"]
code: def finetune(
      self,
      params,
      validation_split: Union[
          float, Tuple[List[int], List[int]]
      ] = 0.15,
      max_length: int = 128,
      epochs: int = 20,
      batch_size: int = None,
      optimal_batch_size: int = None,
      early_stopping_epochs: int = 1,
      train_dataloader=None,
      val_dataloader=None,
      step=None,
      configure_optimizers=None,
  ):
      """
      Finetunes a text classification model on provided data.
  
      Args:
          params: Dict containing keys "texts" and "labels", with values being input/output data lists.
          validation_split: Float between 0 and 1 that determines percentage of data to use for validation.
          max_length: Int determining the maximum token length of input strings.
          epochs: Integer specifying how many training iterations to run.
          batch_size: Batch size when training. Leave as None to automatically determine batch size.
          optimal_batch_size: Optimal batch size for the model being trained -- defaults to model settings.
          early_stopping_epochs: Integer determining how many epochs will run before stopping without an improvement in validation loss.
          train_dataloader: Dataloader for providing training data when finetuning. Defaults to inbuilt dataloder.
          val_dataloader: Dataloader for providing validation data when finetuning. Defaults to inbuilt dataloader.
          step: Function determining how to call model for a training step. Defaults to step defined in this task class.
          configure_optimizers: Function that sets up the optimizer for training. Defaults to optimizer defined in this task class.
  
      Examples::
  
          import backprop
  
          tc = backprop.TextCLassification()
  
          # Set up input data. Labels will automatically be used to set up model with number of classes for classification.
          inp = ["This is a political news article", "This is a computer science research paper", "This is a movie review"]
          out = ["Politics", "Science", "Entertainment"]
          params = {"texts": inp, "labels": out}
  
          # Finetune
          tc.finetune(params)
      """
      inputs = params["texts"]
      outputs = params["labels"]
  
      assert len(inputs) == len(outputs)
  
      step = step or self.step
      configure_optimizers = (
          configure_optimizers or self.configure_optimizers
      )
  
      labels = set(outputs)
      class_to_idx = {v: k for k, v in enumerate(labels)}
      labels = {k: v for k, v in enumerate(labels)}
  
      output_classes = [class_to_idx[i] for i in outputs]
  
      if hasattr(self.model, "pre_finetuning"):
          self.model.pre_finetuning(labels)
  
      dataset_params = {
          "inputs": inputs,
          "labels": outputs,
          "class_to_idx": class_to_idx,
          "max_length": max_length,
      }
  
      dataset = SingleLabelTextClassificationDataset(
          dataset_params,
          process_batch=self.model.process_batch,
          length=len(inputs),
      )
  
      super().finetune(
          dataset=dataset,
          validation_split=validation_split,
          epochs=epochs,
          batch_size=batch_size,
          optimal_batch_size=optimal_batch_size,
          early_stopping_epochs=early_stopping_epochs,
          train_dataloader=train_dataloader,
          val_dataloader=val_dataloader,
          step=step,
          configure_optimizers=configure_optimizers,
      )
  
file_path: backprop/backprop/tasks/text_classification.py
imports: from typing import List, Tuple, Union, Optional
  from backprop.models import BaseModel, AutoModel
  from .base import Task
  import torch
  from transformers.optimization import AdamW
  from backprop.utils.datasets import SingleLabelTextClassificationDataset
  import requests
label_desc: Use image node_id 6 for calling the TextClassification obj's underlying member method code with example usage: obj.finetune(params, validation_split, max_length, epochs, batch_size, optimal_batch_size, early_stopping_epochs, train_dataloader, val_dataloader, step, configure_optimizers) without return types