class_docstr: Few-shot learning is an important scenario for prompt-learning, this is a sampler that samples few examples over each class.
  
  Args:
      num_examples_total(:obj:`int`, optional): Sampling strategy ``I``: Use total number of examples for few-shot sampling.
      num_examples_per_label(:obj:`int`, optional): Sampling strategy ``II``: Use the number of examples for each label for few-shot sampling.
      also_sample_dev(:obj:`bool`, optional): Whether to apply the sampler to the dev data.
      num_examples_total_dev(:obj:`int`, optional): Sampling strategy ``I``: Use total number of examples for few-shot sampling.
      num_examples_per_label_dev(:obj:`int`, optional): Sampling strategy ``II``: Use the number of examples for each label for few-shot sampling.
class_docstr_tok: ["Few-shot","learning","is","an","important","scenario","for","prompt-learning",",","this","is","a","sampler","that","samples","few","examples","over","each","class",".","Args",":","num_examples_total","(",":","obj",":","`","int","`",",","optional",")",":","Sampling","strategy","``","I","``",":","Use","total","number","of","examples","for","few-shot","sampling",".","num_examples_per_label","(",":","obj",":","`","int","`",",","optional",")",":","Sampling","strategy","``","II","``",":","Use","the","number","of","examples","for","each","label","for","few-shot","sampling",".","also_sample_dev","(",":","obj",":","`","bool","`",",","optional",")",":","Whether","to","apply","the","sampler","to","the","dev","data",".","num_examples_total_dev","(",":","obj",":","`","int","`",",","optional",")",":","Sampling","strategy","``","I","``",":","Use","total","number","of","examples","for","few-shot","sampling",".","num_examples_per_label_dev","(",":","obj",":","`","int","`",",","optional",")",":","Sampling","strategy","``","II","``",":","Use","the","number","of","examples","for","each","label","for","few-shot","sampling","."]
docstr: Use the number of examples per class for few-shot sampling (Strategy ``II``).
  If the number of examples is not enough, a warning will pop up.
  
  Args:
      indices(:obj:`List`): The random indices of the whole datasets.
      labels(:obj:`List`): The list of the labels.
      num_examples_per_label(:obj:`int`): The total number of examples for each class.
  
  Returns:
      :obj:`List`: The selected indices with the size of ``num_examples_total``.
docstr_tok: ["Use","the","number","of","examples","per","class","for","few-shot","sampling","(","Strategy","``","II","``",")",".","If","the","number","of","examples","is","not","enough",",","a","warning","will","pop","up",".","Args",":","indices","(",":","obj",":","`","List","`",")",":","The","random","indices","of","the","whole","datasets",".","labels","(",":","obj",":","`","List","`",")",":","The","list","of","the","labels",".","num_examples_per_label","(",":","obj",":","`","int","`",")",":","The","total","number","of","examples","for","each","class",".","Returns",":",":","obj",":","`","List","`",":","The","selected","indices","with","the","size","of","``","num_examples_total","``","."]
code: def sample_per_label(
      self, indices: List, labels, num_examples_per_label
  ):
      """
      Use the number of examples per class for few-shot sampling (Strategy ``II``).
      If the number of examples is not enough, a warning will pop up.
  
      Args:
          indices(:obj:`List`): The random indices of the whole datasets.
          labels(:obj:`List`): The list of the labels.
          num_examples_per_label(:obj:`int`): The total number of examples for each class.
  
      Returns:
          :obj:`List`: The selected indices with the size of ``num_examples_total``.
      """
  
      ids_per_label = defaultdict(list)
      selected_ids = []
      for idx, label in zip(indices, labels):
          ids_per_label[label].append(idx)
      for label, ids in ids_per_label.items():
          tmp = np.array(ids)
          self.rng.shuffle(tmp)
          if len(tmp) < num_examples_per_label:
              logger.info(
                  "Not enough examples of label {} can be sampled".format(
                      label
                  )
              )
          selected_ids.extend(tmp[:num_examples_per_label].tolist())
      selected_ids = np.array(selected_ids)
      self.rng.shuffle(selected_ids)
      selected_ids = selected_ids.tolist()
      logger.info("Selected examples {}".format(selected_ids))
      return selected_ids
  
file_path: OpenPrompt/openprompt/data_utils/data_sampler.py
imports: from collections import defaultdict, namedtuple
  from typing import *
  import numpy
  from torch.utils.data import Dataset
  from torch.utils.data.dataset import Subset
  from openprompt.utils.logging import logger
  from typing import Union
label_desc: Use image node_id 5 for calling the FewShotSampler obj's underlying member method code with example usage: obj.sample_per_label(indices, labels, num_examples_per_label) and returns: selected_ids