class_docstr: Arguments for ``Trainer`` class that is used for adversarial training.
  
  Args:
      num_epochs (:obj:`int`, `optional`, defaults to :obj:`3`):
          Total number of epochs for training.
      num_clean_epochs (:obj:`int`, `optional`, defaults to :obj:`1`):
          Number of epochs to train on just the original training dataset before adversarial training.
      attack_epoch_interval (:obj:`int`, `optional`, defaults to :obj:`1`):
          Generate a new adversarial training set every `N` epochs.
      early_stopping_epochs (:obj:`int`, `optional`, defaults to :obj:`None`):
          Number of epochs validation must increase before stopping early (:obj:`None` for no early stopping).
      learning_rate (:obj:`float`, `optional`, defaults to :obj:`5e-5`):
          Learning rate for optimizer.
      num_warmup_steps (:obj:`int` or :obj:`float`, `optional`, defaults to :obj:`500`):
          The number of steps for the warmup phase of linear scheduler.
          If :obj:`num_warmup_steps` is a :obj:`float` between 0 and 1, the number of warmup steps will be :obj:`math.ceil(num_training_steps * num_warmup_steps)`.
      weight_decay (:obj:`float`, `optional`, defaults to :obj:`0.01`):
          Weight decay (L2 penalty).
      per_device_train_batch_size (:obj:`int`, `optional`, defaults to :obj:`8`):
          The batch size per GPU/CPU for training.
      per_device_eval_batch_size (:obj:`int`, `optional`, defaults to :obj:`32`):
          The batch size per GPU/CPU for evaluation.
      gradient_accumulation_steps (:obj:`int`, `optional`, defaults to :obj:`1`):
          Number of updates steps to accumulate the gradients before performing a backward/update pass.
      random_seed (:obj:`int`, `optional`, defaults to :obj:`786`):
          Random seed for reproducibility.
      parallel (:obj:`bool`, `optional`, defaults to :obj:`False`):
          If :obj:`True`, train using multiple GPUs using :obj:`torch.DataParallel`.
      load_best_model_at_end (:obj:`bool`, `optional`, defaults to :obj:`False`):
          If :obj:`True`, keep track of the best model across training and load it at the end.
      alpha (:obj:`float`, `optional`, defaults to :obj:`1.0`):
          The weight for adversarial loss.
      num_train_adv_examples (:obj:`int` or :obj:`float`, `optional`, defaults to :obj:`-1`):
          The number of samples to successfully attack when generating adversarial training set before start of every epoch.
          If :obj:`num_train_adv_examples` is a :obj:`float` between 0 and 1, the number of adversarial examples generated is
          fraction of the original training set.
      query_budget_train (:obj:`int`, `optional`, defaults to :obj:`None`):
          The max query budget to use when generating adversarial training set. :obj:`None` means infinite query budget.
      attack_num_workers_per_device (:obj:`int`, defaults to `optional`, :obj:`1`):
          Number of worker processes to run per device for attack. Same as :obj:`num_workers_per_device` argument for :class:`~textattack.AttackArgs`.
      output_dir (:obj:`str`, `optional`):
          Directory to output training logs and checkpoints. Defaults to :obj:`./outputs/%Y-%m-%d-%H-%M-%S-%f` format.
      checkpoint_interval_steps (:obj:`int`, `optional`, defaults to :obj:`None`):
          If set, save model checkpoint after every `N` updates to the model.
      checkpoint_interval_epochs (:obj:`int`, `optional`, defaults to :obj:`None`):
          If set, save model checkpoint after every `N` epochs.
      save_last (:obj:`bool`, `optional`, defaults to :obj:`True`):
          If :obj:`True`, save the model at end of training. Can be used with :obj:`load_best_model_at_end` to save the best model at the end.
      log_to_tb (:obj:`bool`, `optional`, defaults to :obj:`False`):
          If :obj:`True`, log to Tensorboard.
      tb_log_dir (:obj:`str`, `optional`, defaults to :obj:`"./runs"`):
          Path of Tensorboard log directory.
      log_to_wandb (:obj:`bool`, `optional`, defaults to :obj:`False`):
          If :obj:`True`, log to Wandb.
      wandb_project (:obj:`str`, `optional`, defaults to :obj:`"textattack"`):
          Name of Wandb project for logging.
      logging_interval_step (:obj:`int`, `optional`, defaults to :obj:`1`):
          Log to Tensorboard/Wandb every `N` training steps.
class_docstr_tok: ["Arguments","for","``","Trainer","``","class","that","is","used","for","adversarial","training",".","Args",":","num_epochs","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","3","`",")",":","Total","number","of","epochs","for","training",".","num_clean_epochs","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","1","`",")",":","Number","of","epochs","to","train","on","just","the","original","training","dataset","before","adversarial","training",".","attack_epoch_interval","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","1","`",")",":","Generate","a","new","adversarial","training","set","every","`","N","`","epochs",".","early_stopping_epochs","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","None","`",")",":","Number","of","epochs","validation","must","increase","before","stopping","early","(",":","obj",":","`","None","`","for","no","early","stopping",")",".","learning_rate","(",":","obj",":","`","float","`",",","`","optional","`",",","defaults","to",":","obj",":","`","5e-5","`",")",":","Learning","rate","for","optimizer",".","num_warmup_steps","(",":","obj",":","`","int","`","or",":","obj",":","`","float","`",",","`","optional","`",",","defaults","to",":","obj",":","`","500","`",")",":","The","number","of","steps","for","the","warmup","phase","of","linear","scheduler",".","If",":","obj",":","`","num_warmup_steps","`","is","a",":","obj",":","`","float","`","between","0","and","1",",","the","number","of","warmup","steps","will","be",":","obj",":","`","math.ceil","(","num_training_steps","*","num_warmup_steps",")","`",".","weight_decay","(",":","obj",":","`","float","`",",","`","optional","`",",","defaults","to",":","obj",":","`","0.01","`",")",":","Weight","decay","(","L2","penalty",")",".","per_device_train_batch_size","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","8","`",")",":","The","batch","size","per","GPU\/CPU","for","training",".","per_device_eval_batch_size","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","32","`",")",":","The","batch","size","per","GPU\/CPU","for","evaluation",".","gradient_accumulation_steps","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","1","`",")",":","Number","of","updates","steps","to","accumulate","the","gradients","before","performing","a","backward\/update","pass",".","random_seed","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","786","`",")",":","Random","seed","for","reproducibility",".","parallel","(",":","obj",":","`","bool","`",",","`","optional","`",",","defaults","to",":","obj",":","`","False","`",")",":","If",":","obj",":","`","True","`",",","train","using","multiple","GPUs","using",":","obj",":","`","torch.DataParallel","`",".","load_best_model_at_end","(",":","obj",":","`","bool","`",",","`","optional","`",",","defaults","to",":","obj",":","`","False","`",")",":","If",":","obj",":","`","True","`",",","keep","track","of","the","best","model","across","training","and","load","it","at","the","end",".","alpha","(",":","obj",":","`","float","`",",","`","optional","`",",","defaults","to",":","obj",":","`","1.0","`",")",":","The","weight","for","adversarial","loss",".","num_train_adv_examples","(",":","obj",":","`","int","`","or",":","obj",":","`","float","`",",","`","optional","`",",","defaults","to",":","obj",":","`","-1","`",")",":","The","number","of","samples","to","successfully","attack","when","generating","adversarial","training","set","before","start","of","every","epoch",".","If",":","obj",":","`","num_train_adv_examples","`","is","a",":","obj",":","`","float","`","between","0","and","1",",","the","number","of","adversarial","examples","generated","is","fraction","of","the","original","training","set",".","query_budget_train","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","None","`",")",":","The","max","query","budget","to","use","when","generating","adversarial","training","set",".",":","obj",":","`","None","`","means","infinite","query","budget",".","attack_num_workers_per_device","(",":","obj",":","`","int","`",",","defaults","to","`","optional","`",",",":","obj",":","`","1","`",")",":","Number","of","worker","processes","to","run","per","device","for","attack",".","Same","as",":","obj",":","`","num_workers_per_device","`","argument","for",":","class",":","`","~textattack.AttackArgs","`",".","output_dir","(",":","obj",":","`","str","`",",","`","optional","`",")",":","Directory","to","output","training","logs","and","checkpoints",".","Defaults","to",":","obj",":","`",".\/outputs\/","%","Y-","%","m-","%","d-","%","H-","%","M-","%","S-","%","f","`","format",".","checkpoint_interval_steps","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","None","`",")",":","If","set",",","save","model","checkpoint","after","every","`","N","`","updates","to","the","model",".","checkpoint_interval_epochs","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","None","`",")",":","If","set",",","save","model","checkpoint","after","every","`","N","`","epochs",".","save_last","(",":","obj",":","`","bool","`",",","`","optional","`",",","defaults","to",":","obj",":","`","True","`",")",":","If",":","obj",":","`","True","`",",","save","the","model","at","end","of","training",".","Can","be","used","with",":","obj",":","`","load_best_model_at_end","`","to","save","the","best","model","at","the","end",".","log_to_tb","(",":","obj",":","`","bool","`",",","`","optional","`",",","defaults","to",":","obj",":","`","False","`",")",":","If",":","obj",":","`","True","`",",","log","to","Tensorboard",".","tb_log_dir","(",":","obj",":","`","str","`",",","`","optional","`",",","defaults","to",":","obj",":","`","``",".\/runs","''","`",")",":","Path","of","Tensorboard","log","directory",".","log_to_wandb","(",":","obj",":","`","bool","`",",","`","optional","`",",","defaults","to",":","obj",":","`","False","`",")",":","If",":","obj",":","`","True","`",",","log","to","Wandb",".","wandb_project","(",":","obj",":","`","str","`",",","`","optional","`",",","defaults","to",":","obj",":","`","``","textattack","''","`",")",":","Name","of","Wandb","project","for","logging",".","logging_interval_step","(",":","obj",":","`","int","`",",","`","optional","`",",","defaults","to",":","obj",":","`","1","`",")",":","Log","to","Tensorboard\/Wandb","every","`","N","`","training","steps","."]
docstr: Add listed args to command line parser.
docstr_tok: ["Add","listed","args","to","command","line","parser","."]
code: def _add_parser_args(cls, parser):
      """Add listed args to command line parser."""
      default_obj = cls()
  
      def int_or_float(v):
          try:
              return int(v)
          except ValueError:
              return float(v)
  
      parser.add_argument(
          "--num-epochs",
          "--epochs",
          type=int,
          default=default_obj.num_epochs,
          help="Total number of epochs for training.",
      )
      parser.add_argument(
          "--num-clean-epochs",
          type=int,
          default=default_obj.num_clean_epochs,
          help="Number of epochs to train on the clean dataset before adversarial training (N/A if --attack unspecified)",
      )
      parser.add_argument(
          "--attack-epoch-interval",
          type=int,
          default=default_obj.attack_epoch_interval,
          help="Generate a new adversarial training set every N epochs.",
      )
      parser.add_argument(
          "--early-stopping-epochs",
          type=int,
          default=default_obj.early_stopping_epochs,
          help="Number of epochs validation must increase before stopping early (-1 for no early stopping)",
      )
      parser.add_argument(
          "--learning-rate",
          "--lr",
          type=float,
          default=default_obj.learning_rate,
          help="Learning rate for Adam Optimization.",
      )
      parser.add_argument(
          "--num-warmup-steps",
          type=int_or_float,
          default=default_obj.num_warmup_steps,
          help="The number of steps for the warmup phase of linear scheduler.",
      )
      parser.add_argument(
          "--weight-decay",
          type=float,
          default=default_obj.weight_decay,
          help="Weight decay (L2 penalty).",
      )
      parser.add_argument(
          "--per-device-train-batch-size",
          type=int,
          default=default_obj.per_device_train_batch_size,
          help="The batch size per GPU/CPU for training.",
      )
      parser.add_argument(
          "--per-device-eval-batch-size",
          type=int,
          default=default_obj.per_device_eval_batch_size,
          help="The batch size per GPU/CPU for evaluation.",
      )
      parser.add_argument(
          "--gradient-accumulation-steps",
          type=int,
          default=default_obj.gradient_accumulation_steps,
          help="Number of updates steps to accumulate the gradients for, before performing a backward/update pass.",
      )
      parser.add_argument(
          "--random-seed",
          type=int,
          default=default_obj.random_seed,
          help="Random seed.",
      )
      parser.add_argument(
          "--parallel",
          action="store_true",
          default=default_obj.parallel,
          help="If set, run training on multiple GPUs.",
      )
      parser.add_argument(
          "--load-best-model-at-end",
          action="store_true",
          default=default_obj.load_best_model_at_end,
          help="If set, keep track of the best model across training and load it at the end.",
      )
      parser.add_argument(
          "--alpha",
          type=float,
          default=1.0,
          help="The weight of adversarial loss.",
      )
      parser.add_argument(
          "--num-train-adv-examples",
          type=int_or_float,
          default=default_obj.num_train_adv_examples,
          help="The number of samples to attack when generating adversarial training set. Default is -1 (which is all possible samples).",
      )
      parser.add_argument(
          "--query-budget-train",
          type=int,
          default=default_obj.query_budget_train,
          help="The max query budget to use when generating adversarial training set.",
      )
      parser.add_argument(
          "--attack-num-workers-per-device",
          type=int,
          default=default_obj.attack_num_workers_per_device,
          help="Number of worker processes to run per device for attack. Same as `num_workers_per_device` argument for `AttackArgs`.",
      )
      parser.add_argument(
          "--output-dir",
          type=str,
          default=default_output_dir(),
          help="Directory to output training logs and checkpoints.",
      )
      parser.add_argument(
          "--checkpoint-interval-steps",
          type=int,
          default=default_obj.checkpoint_interval_steps,
          help="Save model checkpoint after every N updates to the model.",
      )
      parser.add_argument(
          "--checkpoint-interval-epochs",
          type=int,
          default=default_obj.checkpoint_interval_epochs,
          help="Save model checkpoint after every N epochs.",
      )
      parser.add_argument(
          "--save-last",
          action="store_true",
          default=default_obj.save_last,
          help="If set, save the model at end of training. Can be used with `--load-best-model-at-end` to save the best model at the end.",
      )
      parser.add_argument(
          "--log-to-tb",
          action="store_true",
          default=default_obj.log_to_tb,
          help="If set, log to Tensorboard",
      )
      parser.add_argument(
          "--tb-log-dir",
          type=str,
          default=default_obj.tb_log_dir,
          help="Path of Tensorboard log directory.",
      )
      parser.add_argument(
          "--log-to-wandb",
          action="store_true",
          default=default_obj.log_to_wandb,
          help="If set, log to Wandb.",
      )
      parser.add_argument(
          "--wandb-project",
          type=str,
          default=default_obj.wandb_project,
          help="Name of Wandb project for logging.",
      )
      parser.add_argument(
          "--logging-interval-step",
          type=int,
          default=default_obj.logging_interval_step,
          help="Log to Tensorboard/Wandb every N steps.",
      )
  
      return parser
  
imports: from dataclasses import dataclass, field
  import datetime
  import os
  from typing import Union
  from textattack.datasets import HuggingFaceDataset
  from textattack.models.helpers import LSTMForClassification, WordCNNForClassification
  from textattack.models.wrappers import HuggingFaceModelWrapper, ModelWrapper, PyTorchModelWrapper
  from textattack.shared import logger
  from textattack.shared.utils import ARGS_SPLIT_TOKEN
  from .attack import Attack
  from .attack_args import ATTACK_RECIPE_NAMES
label_desc: Use image node_id 2 for calling the TrainingArgs obj's underlying member method code with example usage: obj._add_parser_args(cls, parser) and returns: parser, int, float