class_docstr: Samples elements from ``[0,..,len(weights)-1]`` with given probabilities (weights).
  
  Args:
      weights (sequence)   : a sequence of weights, not necessary summing up to one
      num_samples (int): number of samples to draw
      replacement (bool): if ``True``, samples are drawn with replacement.
          If not, they are drawn without replacement, which means that when a
          sample index is drawn for a row, it cannot be drawn again for that row.
      generator (Generator): Generator used in sampling.
  
  Example:
      >>> # xdoctest: +IGNORE_WANT("non-deterministic")
      >>> list(WeightedRandomSampler([0.1, 0.9, 0.4, 0.7, 3.0, 0.6], 5, replacement=True))
      [4, 4, 1, 4, 5]
      >>> list(WeightedRandomSampler([0.9, 0.4, 0.05, 0.2, 0.3, 0.1], 5, replacement=False))
      [0, 1, 4, 3, 2]
class_docstr_tok: ["Samples","elements","from","``","[","0",",","..",",","len","(","weights",")","-1","]","``","with","given","probabilities","(","weights",")",".","Args",":","weights","(","sequence",")",":","a","sequence","of","weights",",","not","necessary","summing","up","to","one","num_samples","(","int",")",":","number","of","samples","to","draw","replacement","(","bool",")",":","if","``","True","``",",","samples","are","drawn","with","replacement",".","If","not",",","they","are","drawn","without","replacement",",","which","means","that","when","a","sample","index","is","drawn","for","a","row",",","it","can","not","be","drawn","again","for","that","row",".","generator","(","Generator",")",":","Generator","used","in","sampling",".","Example",":",">",">",">","#","xdoctest",":","+IGNORE_WANT","(","``","non-deterministic","''",")",">",">",">","list","(","WeightedRandomSampler","(","[","0.1",",","0.9",",","0.4",",","0.7",",","3.0",",","0.6","]",",","5",",","replacement=True",")",")","[","4",",","4",",","1",",","4",",","5","]",">",">",">","list","(","WeightedRandomSampler","(","[","0.9",",","0.4",",","0.05",",","0.2",",","0.3",",","0.1","]",",","5",",","replacement=False",")",")","[","0",",","1",",","4",",","3",",","2","]"]
code: def __init__(
      self,
      weights: Sequence[float],
      num_samples: int,
      replacement: bool = True,
      generator=None,
  ) -> None:
      if (
          not isinstance(num_samples, int)
          or isinstance(num_samples, bool)
          or num_samples <= 0
      ):
          raise ValueError(
              f"num_samples should be a positive integer value, but got num_samples={num_samples}"
          )
      if not isinstance(replacement, bool):
          raise ValueError(
              f"replacement should be a boolean value, but got replacement={replacement}"
          )
  
      weights_tensor = torch.as_tensor(weights, dtype=torch.double)
      if len(weights_tensor.shape) != 1:
          raise ValueError(
              "weights should be a 1d sequence but given "
              f"weights have shape {tuple(weights_tensor.shape)}"
          )
  
      self.weights = weights_tensor
      self.num_samples = num_samples
      self.replacement = replacement
      self.generator = generator
  
imports: import torch
  from torch import Tensor
  from typing import Iterator, Iterable, Optional, Sequence, List, TypeVar, Generic, Sized, Union
label_desc: Use image node_id 1 to create a new WeightedRandomSampler object from inherited base classes: Sampler with example: obj = WeightedRandomSampler(weights, num_samples, replacement, generator)