class_docstr: Learning to Prompt (L2P) strategy.
  
  Technique introduced in:
  "Wang, Zifeng, et al. "Learning to prompt for continual learning."
  Proceedings of the IEEE/CVF Conference on Computer Vision and
  Pattern Recognition. 2022."
  
  Implementation based on:
  - https://github.com/JH-LEE-KR/l2p-pytorch
  - And implementations by Dario Salvati
  
  As a model_name, we expect to receive one of the model list in
  avalanche.models.vit
  
  Those models are based on the library timm.
class_docstr_tok: ["Learning","to","Prompt","(","L2P",")","strategy",".","Technique","introduced","in",":","''","Wang",",","Zifeng",",","et","al",".","``","Learning","to","prompt","for","continual","learning",".","''","Proceedings","of","the","IEEE\/CVF","Conference","on","Computer","Vision","and","Pattern","Recognition",".","2022",".","''","Implementation","based","on",":","-","https",":","\/\/github.com\/JH-LEE-KR\/l2p-pytorch","-","And","implementations","by","Dario","Salvati","As","a","model_name",",","we","expect","to","receive","one","of","the","model","list","in","avalanche.models.vit","Those","models","are","based","on","the","library","timm","."]
docstr: Init.
  
  :param model_name: Name of the model to use. For a complete list check             models.vit.py
  :param criterion: Loss functions used during training.             Default CrossEntropyLoss.
  :param train_mb_size: The train minibatch size. Defaults to 1.
  :param train_epochs: The number of training epochs. Defaults to 1.
  :param eval_mb_size: The eval minibatch size. Defaults to 1.
  :param device: The device to use. Defaults to None (cpu).
  :param plugins: Plugins to be added. Defaults to None.
  :param evaluator: (optional) instance of EvaluationPlugin for logging
      and metric computations.
  :param eval_every: the frequency of the calls to `eval` inside the
      training loop. -1 disables the evaluation. 0 means `eval` is called
      only at the end of the learning experience. Values >0 mean that
      `eval` is called every `eval_every` epochs and at the end of the
      learning experience.
  :param use_cls_features: Use an external pre-trained model to obtained             features to obtained the prompts.
  :param use_mask: Use mask to train only classification rows of the             classes of the current task. Default True.
  :param use_vit: Boolean to confirm the usage of a visual Transformer.            Default True
docstr_tok: ["Init",".",":","param","model_name",":","Name","of","the","model","to","use",".","For","a","complete","list","check","models.vit.py",":","param","criterion",":","Loss","functions","used","during","training",".","Default","CrossEntropyLoss",".",":","param","train_mb_size",":","The","train","minibatch","size",".","Defaults","to","1.",":","param","train_epochs",":","The","number","of","training","epochs",".","Defaults","to","1.",":","param","eval_mb_size",":","The","eval","minibatch","size",".","Defaults","to","1.",":","param","device",":","The","device","to","use",".","Defaults","to","None","(","cpu",")",".",":","param","plugins",":","Plugins","to","be","added",".","Defaults","to","None",".",":","param","evaluator",":","(","optional",")","instance","of","EvaluationPlugin","for","logging","and","metric","computations",".",":","param","eval_every",":","the","frequency","of","the","calls","to","`","eval","`","inside","the","training","loop",".","-1","disables","the","evaluation",".","0","means","`","eval","`","is","called","only","at","the","end","of","the","learning","experience",".","Values",">","0","mean","that","`","eval","`","is","called","every","`","eval_every","`","epochs","and","at","the","end","of","the","learning","experience",".",":","param","use_cls_features",":","Use","an","external","pre-trained","model","to","obtained","features","to","obtained","the","prompts",".",":","param","use_mask",":","Use","mask","to","train","only","classification","rows","of","the","classes","of","the","current","task",".","Default","True",".",":","param","use_vit",":","Boolean","to","confirm","the","usage","of","a","visual","Transformer",".","Default","True"]
code: def __init__(
      self,
      model_name: str,
      criterion: nn.Module = nn.CrossEntropyLoss(),
      train_mb_size: int = 1,
      train_epochs: int = 1,
      eval_mb_size: Optional[int] = 1,
      device: Union[str, torch.device] = "cpu",
      plugins: Optional[List["SupervisedPlugin"]] = None,
      evaluator: Union[
          EvaluationPlugin, Callable[[], EvaluationPlugin]
      ] = default_evaluator,
      eval_every: int = -1,
      peval_mode: str = "epoch",
      prompt_pool: bool = True,
      pool_size: int = 20,
      prompt_length: int = 5,
      top_k: int = 5,
      lr: float = 0.03,
      sim_coefficient: float = 0.1,
      prompt_key: bool = True,
      pretrained: bool = True,
      num_classes: int = 10,
      drop_rate: float = 0.0,
      drop_path_rate: float = 0.0,
      embedding_key: str = "cls",
      prompt_init: str = "uniform",
      batchwise_prompt: bool = False,
      head_type: str = "prompt",
      use_prompt_mask: bool = False,
      train_prompt_mask: bool = False,
      use_cls_features: bool = True,
      use_mask: bool = True,
      use_vit: bool = True,
      **kwargs,
  ):
      """Init.
  
          :param model_name: Name of the model to use. For a complete list check \
              models.vit.py
          :param criterion: Loss functions used during training. \
              Default CrossEntropyLoss.
          :param train_mb_size: The train minibatch size. Defaults to 1.
          :param train_epochs: The number of training epochs. Defaults to 1.
          :param eval_mb_size: The eval minibatch size. Defaults to 1.
          :param device: The device to use. Defaults to None (cpu).
          :param plugins: Plugins to be added. Defaults to None.
          :param evaluator: (optional) instance of EvaluationPlugin for logging
              and metric computations.
          :param eval_every: the frequency of the calls to `eval` inside the
              training loop. -1 disables the evaluation. 0 means `eval` is called
              only at the end of the learning experience. Values >0 mean that
              `eval` is called every `eval_every` epochs and at the end of the
              learning experience.
          :param use_cls_features: Use an external pre-trained model to obtained\
               features to obtained the prompts.
          :param use_mask: Use mask to train only classification rows of the \
              classes of the current task. Default True.
          :param use_vit: Boolean to confirm the usage of a visual Transformer.\
              Default True
          """
  
      if device is None:
          device = torch.device("cpu")
  
      self.num_classes = num_classes
      self.lr = lr
      self.sim_coefficient = sim_coefficient
      model = create_model(
          model_name=model_name,
          prompt_pool=prompt_pool,
          pool_size=pool_size,
          prompt_length=prompt_length,
          top_k=top_k,
          prompt_key=prompt_key,
          pretrained=pretrained,
          num_classes=num_classes,
          drop_rate=drop_rate,
          drop_path_rate=drop_path_rate,
          embedding_key=embedding_key,
          prompt_init=prompt_init,
          batchwise_prompt=batchwise_prompt,
          head_type=head_type,
          use_prompt_mask=use_prompt_mask,
      )
  
      for n, p in model.named_parameters():
          if n.startswith(
              tuple(
                  [
                      "blocks",
                      "patch_embed",
                      "cls_token",
                      "norm",
                      "pos_embed",
                  ]
              )
          ):
              p.requires_grad = False
  
      model.head = torch.nn.Linear(768, num_classes).to(device)
  
      optimizer = torch.optim.Adam(
          model.parameters(),
          betas=(0.9, 0.999),
          lr=self.lr,
      )
  
      super().__init__(
          model,
          optimizer,
          criterion,
          train_mb_size,
          train_epochs,
          eval_mb_size,
          device,
          plugins,
          evaluator,
          eval_every,
          peval_mode,
      )
  
      self._criterion = criterion
      self.use_cls_features = use_cls_features
      self.train_prompt_mask = train_prompt_mask
      self.use_mask = use_mask
      self.use_vit = use_vit
  
      if use_cls_features:
          self.original_vit = create_model(
              model_name=model_name,
              pretrained=pretrained,
              num_classes=num_classes,
              drop_rate=drop_rate,
              drop_path_rate=drop_path_rate,
          ).to(device)
  
          self.original_vit.reset_classifier(0)
  
          for p in self.original_vit.parameters():
              p.requires_grad = False
  
file_path: avalanche/avalanche/training/supervised/l2p.py
imports: from typing import Callable, List, Optional, Union
  import numpy
  import torch
  import torch.nn
  from avalanche.training.plugins import EvaluationPlugin
  from avalanche.training.plugins.strategy_plugin import SupervisedPlugin
  from avalanche.training.plugins.evaluation import default_evaluator
  from avalanche.training.templates import SupervisedTemplate
  from avalanche.models.vit import create_model
label_desc: Use image node_id 1 to create a new LearningToPrompt object from inherited base classes: SupervisedTemplate with example: obj = LearningToPrompt(model_name, criterion, train_mb_size, train_epochs, eval_mb_size, device, plugins, evaluator, eval_every, peval_mode, prompt_pool, pool_size, prompt_length, top_k, lr, sim_coefficient, prompt_key, pretrained, num_classes, drop_rate, drop_path_rate, embedding_key, prompt_init, batchwise_prompt, head_type, use_prompt_mask, train_prompt_mask, use_cls_features, use_mask, use_vit)