class_docstr: Trainer is training and eval loop for adversarial training.
  
  It is designed to work with PyTorch and Transformers models.
  
  Args:
      model_wrapper (:class:`~textattack.models.wrappers.ModelWrapper`):
          Model wrapper containing both the model and the tokenizer.
      task_type (:obj:`str`, `optional`, defaults to :obj:`"classification"`):
          The task that the model is trained to perform.
          Currently, :class:`~textattack.Trainer` supports two tasks: (1) :obj:`"classification"`, (2) :obj:`"regression"`.
      attack (:class:`~textattack.Attack`):
          :class:`~textattack.Attack` used to generate adversarial examples for training.
      train_dataset (:class:`~textattack.datasets.Dataset`):
          Dataset for training.
      eval_dataset (:class:`~textattack.datasets.Dataset`):
          Dataset for evaluation
      training_args (:class:`~textattack.TrainingArgs`):
          Arguments for training.
  
  Example::
  
      >>> import textattack
      >>> import transformers
  
      >>> model = transformers.AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
      >>> tokenizer = transformers.AutoTokenizer.from_pretrained("bert-base-uncased")
      >>> model_wrapper = textattack.models.wrappers.HuggingFaceModelWrapper(model, tokenizer)
  
      >>> # We only use DeepWordBugGao2018 to demonstration purposes.
      >>> attack = textattack.attack_recipes.DeepWordBugGao2018.build(model_wrapper)
      >>> train_dataset = textattack.datasets.HuggingFaceDataset("imdb", split="train")
      >>> eval_dataset = textattack.datasets.HuggingFaceDataset("imdb", split="test")
  
      >>> # Train for 3 epochs with 1 initial clean epochs, 1000 adversarial examples per epoch, learning rate of 5e-5, and effective batch size of 32 (8x4).
      >>> training_args = textattack.TrainingArgs(
      ...     num_epochs=3,
      ...     num_clean_epochs=1,
      ...     num_train_adv_examples=1000,
      ...     learning_rate=5e-5,
      ...     per_device_train_batch_size=8,
      ...     gradient_accumulation_steps=4,
      ...     log_to_tb=True,
      ... )
  
      >>> trainer = textattack.Trainer(
      ...     model_wrapper,
      ...     "classification",
      ...     attack,
      ...     train_dataset,
      ...     eval_dataset,
      ...     training_args
      ... )
      >>> trainer.train()
  
  .. note::
      When using :class:`~textattack.Trainer` with `parallel=True` in :class:`~textattack.TrainingArgs`,
      make sure to protect the “entry point” of the program by using :obj:`if __name__ == '__main__':`.
      If not, each worker process used for generating adversarial examples will execute the training code again.
class_docstr_tok: ["Trainer","is","training","and","eval","loop","for","adversarial","training",".","It","is","designed","to","work","with","PyTorch","and","Transformers","models",".","Args",":","model_wrapper","(",":","class",":","`","~textattack.models.wrappers.ModelWrapper","`",")",":","Model","wrapper","containing","both","the","model","and","the","tokenizer",".","task_type","(",":","obj",":","`","str","`",",","`","optional","`",",","defaults","to",":","obj",":","`","``","classification","''","`",")",":","The","task","that","the","model","is","trained","to","perform",".","Currently",",",":","class",":","`","~textattack.Trainer","`","supports","two","tasks",":","(","1",")",":","obj",":","`","``","classification","''","`",",","(","2",")",":","obj",":","`","``","regression","''","`",".","attack","(",":","class",":","`","~textattack.Attack","`",")",":",":","class",":","`","~textattack.Attack","`","used","to","generate","adversarial","examples","for","training",".","train_dataset","(",":","class",":","`","~textattack.datasets.Dataset","`",")",":","Dataset","for","training",".","eval_dataset","(",":","class",":","`","~textattack.datasets.Dataset","`",")",":","Dataset","for","evaluation","training_args","(",":","class",":","`","~textattack.TrainingArgs","`",")",":","Arguments","for","training",".","Example",":",":",">",">",">","import","textattack",">",">",">","import","transformers",">",">",">","model","=","transformers.AutoModelForSequenceClassification.from_pretrained","(","``","bert-base-uncased","''",")",">",">",">","tokenizer","=","transformers.AutoTokenizer.from_pretrained","(","``","bert-base-uncased","''",")",">",">",">","model_wrapper","=","textattack.models.wrappers.HuggingFaceModelWrapper","(","model",",","tokenizer",")",">",">",">","#","We","only","use","DeepWordBugGao2018","to","demonstration","purposes",".",">",">",">","attack","=","textattack.attack_recipes.DeepWordBugGao2018.build","(","model_wrapper",")",">",">",">","train_dataset","=","textattack.datasets.HuggingFaceDataset","(","``","imdb","''",",","split=","''","train","''",")",">",">",">","eval_dataset","=","textattack.datasets.HuggingFaceDataset","(","``","imdb","''",",","split=","''","test","''",")",">",">",">","#","Train","for","3","epochs","with","1","initial","clean","epochs",",","1000","adversarial","examples","per","epoch",",","learning","rate","of","5e-5",",","and","effective","batch","size","of","32","(","8x4",")",".",">",">",">","training_args","=","textattack.TrainingArgs","(","...","num_epochs=3",",","...","num_clean_epochs=1",",","...","num_train_adv_examples=1000",",","...","learning_rate=5e-5",",","...","per_device_train_batch_size=8",",","...","gradient_accumulation_steps=4",",","...","log_to_tb=True",",","...",")",">",">",">","trainer","=","textattack.Trainer","(","...","model_wrapper",",","...","``","classification","''",",","...","attack",",","...","train_dataset",",","...","eval_dataset",",","...","training_args","...",")",">",">",">","trainer.train","(",")","..","note",":",":","When","using",":","class",":","`","~textattack.Trainer","`","with","`","parallel=True","`","in",":","class",":","`","~textattack.TrainingArgs","`",",","make","sure","to","protect","the","\u201c","entry","point","\u201d","of","the","program","by","using",":","obj",":","`","if","__name__","==","'__main__","'",":","`",".","If","not",",","each","worker","process","used","for","generating","adversarial","examples","will","execute","the","training","code","again","."]
docstr: Returns optimizer and scheduler to use for training. If you are
  overriding this method and do not want to use a scheduler, simply
  return :obj:`None` for scheduler.
  
  Args:
      model (:obj:`torch.nn.Module`):
          Model to be trained. Pass its parameters to optimizer for training.
      num_training_steps (:obj:`int`):
          Number of total training steps.
  Returns:
      Tuple of optimizer and scheduler :obj:`tuple[torch.optim.Optimizer, torch.optim.lr_scheduler._LRScheduler]`
docstr_tok: ["Returns","optimizer","and","scheduler","to","use","for","training",".","If","you","are","overriding","this","method","and","do","not","want","to","use","a","scheduler",",","simply","return",":","obj",":","`","None","`","for","scheduler",".","Args",":","model","(",":","obj",":","`","torch.nn.Module","`",")",":","Model","to","be","trained",".","Pass","its","parameters","to","optimizer","for","training",".","num_training_steps","(",":","obj",":","`","int","`",")",":","Number","of","total","training","steps",".","Returns",":","Tuple","of","optimizer","and","scheduler",":","obj",":","`","tuple","[","torch.optim.Optimizer",",","torch.optim.lr_scheduler._LRScheduler","]","`"]
code: def get_optimizer_and_scheduler(self, model, num_training_steps):
      """Returns optimizer and scheduler to use for training. If you are
      overriding this method and do not want to use a scheduler, simply
      return :obj:`None` for scheduler.
  
      Args:
          model (:obj:`torch.nn.Module`):
              Model to be trained. Pass its parameters to optimizer for training.
          num_training_steps (:obj:`int`):
              Number of total training steps.
      Returns:
          Tuple of optimizer and scheduler :obj:`tuple[torch.optim.Optimizer, torch.optim.lr_scheduler._LRScheduler]`
      """
      if isinstance(model, torch.nn.DataParallel):
          model = model.module
  
      if isinstance(model, transformers.PreTrainedModel):
          # Reference https://huggingface.co/transformers/training.html
          param_optimizer = list(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": self.training_args.weight_decay,
              },
              {
                  "params": [
                      p
                      for n, p in param_optimizer
                      if any(nd in n for nd in no_decay)
                  ],
                  "weight_decay": 0.0,
              },
          ]
  
          optimizer = transformers.optimization.AdamW(
              optimizer_grouped_parameters,
              lr=self.training_args.learning_rate,
          )
          if isinstance(self.training_args.num_warmup_steps, float):
              num_warmup_steps = math.ceil(
                  self.training_args.num_warmup_steps
                  * num_training_steps
              )
          else:
              num_warmup_steps = self.training_args.num_warmup_steps
  
          scheduler = (
              transformers.optimization.get_linear_schedule_with_warmup(
                  optimizer,
                  num_warmup_steps=num_warmup_steps,
                  num_training_steps=num_training_steps,
              )
          )
      else:
          optimizer = torch.optim.Adam(
              filter(lambda x: x.requires_grad, model.parameters()),
              lr=self.training_args.learning_rate,
          )
          scheduler = None
  
      return optimizer, scheduler
  
imports: import collections
  import json
  import logging
  import math
  import os
  import scipy
  import torch
  import tqdm
  import transformers
  import textattack
  from textattack.shared.utils import logger
  from .attack import Attack
  from .attack_args import AttackArgs
  from .attack_results import MaximizedAttackResult, SuccessfulAttackResult
  from .attacker import Attacker
  from .model_args import HUGGINGFACE_MODELS
  from .models.helpers import LSTMForClassification, WordCNNForClassification
  from .models.wrappers import ModelWrapper
  from .training_args import CommandLineTrainingArgs, TrainingArgs
label_desc: Use image node_id 7 for calling the Trainer obj's underlying member method code with example usage: obj.get_optimizer_and_scheduler(model, num_training_steps) and returns: optimizer,  scheduler