L i3ddlZddlmZmZmZmZddlmZmZm Z m Z ddl Z gdZ e ddZ Gdd ee ZGd d eeZGd d eeZGddeeZGddeeZGddeeeZy)N)IterableIteratorSequenceSized)GenericOptionalTypeVarUnion) BatchSampler RandomSamplerSamplerSequentialSamplerSubsetRandomSamplerWeightedRandomSampler_T_coT) covariantc:eZdZdZddeeddfdZdeefdZ y)r a;Base class for all Samplers. Every Sampler subclass has to provide an :meth:`__iter__` method, providing a way to iterate over indices or lists of indices (batches) of dataset elements, and may provide a :meth:`__len__` method that returns the length of the returned iterators. Args: data_source (Dataset): This argument is not used and will be removed in 2.2.0. You may still have custom implementation that utilizes it. Example: >>> # xdoctest: +SKIP >>> class AccedingSequenceLengthSampler(Sampler[int]): >>> def __init__(self, data: List[str]) -> None: >>> self.data = data >>> >>> def __len__(self) -> int: >>> return len(self.data) >>> >>> def __iter__(self) -> Iterator[int]: >>> sizes = torch.tensor([len(x) for x in self.data]) >>> yield from torch.argsort(sizes).tolist() >>> >>> class AccedingSequenceLengthBatchSampler(Sampler[List[int]]): >>> def __init__(self, data: List[str], batch_size: int) -> None: >>> self.data = data >>> self.batch_size = batch_size >>> >>> def __len__(self) -> int: >>> return (len(self.data) + self.batch_size - 1) // self.batch_size >>> >>> def __iter__(self) -> Iterator[List[int]]: >>> sizes = torch.tensor([len(x) for x in self.data]) >>> for batch in torch.chunk(torch.argsort(sizes), len(self)): >>> yield batch.tolist() .. note:: The :meth:`__len__` method isn't strictly required by :class:`~torch.utils.data.DataLoader`, but is expected in any calculation involving the length of a :class:`~torch.utils.data.DataLoader`. N data_sourcereturnc4|ddl}|jdyy)Nrzz`data_source` argument is not used and will be removed in 2.2.0.You may still have custom implementation that utilizes it.)warningswarn)selfrrs ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/utils/data/sampler.py__init__zSampler.__init__Fs"  "  MMM  #ctN)NotImplementedErrorrs r__iter__zSampler.__iter__Os!!rr) __name__ __module__ __qualname____doc__rrrrrr!rrr r s/'RHUOt"(5/"rr cJeZdZUdZeed<deddfdZdeefdZ defdZ y)rz~Samples elements sequentially, always in the same order. Args: data_source (Dataset): dataset to sample from rrNc||_yr)r)rrs rrzSequentialSampler.__init__ws &rcPttt|jSr)iterrangelenrr s rr!zSequentialSampler.__iter__zsE#d../011rc,t|jSr)r,rr s r__len__zSequentialSampler.__len__}s4##$$r) r"r#r$r%r__annotations__rrintr!r.r&rrrrns> 'E'd'2(3-2%%rrc eZdZUdZeed<eed< d dededeeddfdZ e defdZ de efd Z defd Zy) r aSamples elements randomly. If without replacement, then sample from a shuffled dataset. If with replacement, then user can specify :attr:`num_samples` to draw. Args: data_source (Dataset): dataset to sample from replacement (bool): samples are drawn on-demand with replacement if ``True``, default=``False`` num_samples (int): number of samples to draw, default=`len(dataset)`. generator (Generator): Generator used in sampling. r replacementN num_samplesrc"||_||_||_||_t |jt st d|jt |jtr|jdkrtd|jy)N;replacement should be a boolean value, but got replacement=rDnum_samples should be a positive integer value, but got num_samples=) rr2 _num_samples generator isinstancebool TypeErrorr3r0 ValueError)rrr2r3r8s rrzRandomSampler.__init__s'&'"$**D1MdN^N^M_` $**C0D4D4D4IVW[WgWgVhi 5Jrc\|jt|jS|jSr)r7r,rr s rr3zRandomSampler.num_sampless-    $t''( (   rc#Kt|j}|jptt j dtj jj}t j}|j|n |j}|jrt|jdzD]?}t j|dtj |jEd{At j||jdzftj |jEd{yt|j|zD]/}t j ||jEd{1t j ||jd|j|zEd{y777H7 w)Nr&dtype )rA)highsizer@r8r8)r,rr8r0torchemptyint64random_item Generator manual_seedr2r+r3randinttolistrandperm)rnseedr8_s rr!zRandomSampler.__iter__s   ! >> !u{{2U[[9AACHHJKD)I  ! !$ 'I   4++r12  ==ekkY&( }}&&+-kk#  fh   4++q01 K >>!yAHHJJJ K~~a9=DDF&$""Q&   K sJC=G"?GA G" G AG"G>G"G G"G"G" G"c|jSrr3r s rr.zRandomSampler.__len__r)FNN)r"r#r$r%rr/r:rr0rpropertyr3rr!r.r&rrr r s  "%) c]   ,!S!! (3-6  rr cXeZdZUdZeeed<ddeeddfdZdeefdZ defdZ y) rzSamples elements randomly from a given list of indices, without replacement. Args: indices (sequence): a sequence of indices generator (Generator): Generator used in sampling. indicesNrc ||_||_yr)rWr8)rrWr8s rrzSubsetRandomSampler.__init__s "rc#Ktjt|j|jj D]}|j|ywNrD)rErNr,rWr8rM)ris rr!zSubsetRandomSampler.__iter__sDDLL 1T^^LSSU "A,,q/ ! "sAAc,t|jSr)r,rWr s rr.zSubsetRandomSampler.__len__s4<<  rr) r"r#r$r%rr0r/rrr!r.r&rrrrsFc]# #$#"(3-"!!rrc eZdZUdZej ed<eed<eed< d de e dededdfdZ de efdZ defd Zy) raSamples 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] weightsr3r2Nrct|trt|ts|dkrtd|t|tstd|t j |tj }t|jdk7r!tdt|j||_ ||_ ||_ ||_ y)Nrr6r5r?z=weights should be a 1d sequence but given weights have shape )r9r0r:r<rE as_tensordoubler,shapetupler^r3r2r8)rr^r3r2r8weights_tensors rrzWeightedRandomSampler.__init__s;,+t,aVWbVcd +t,Mk][  E ~## $ )&&+N,@,@&A%BD  & &&"rc#Ktj|j|j|j|j }t |jEd{y7wrZ)rE multinomialr^r3r2r8r*rM)r rand_tensors rr!zWeightedRandomSampler.__iter__!sL'' LL$**D,<,<  **,---sAA( A&!A(c|jSrrSr s rr.zWeightedRandomSampler.__len__'rTr)TN)r"r#r$r%rETensorr/r0r:rfloatrrr!r.r&rrrrss4\\ ! #%## #  #@.(3-.   rrcbeZdZdZdeeeeefdededdfdZ de e efdZ defd Z y) r aWraps another sampler to yield a mini-batch of indices. Args: sampler (Sampler or Iterable): Base sampler. Can be any iterable object batch_size (int): Size of mini-batch. drop_last (bool): If ``True``, the sampler will drop the last batch if its size would be less than ``batch_size`` Example: >>> list( ... BatchSampler( ... SequentialSampler(range(10)), batch_size=3, drop_last=False ... ) ... ) [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] >>> list( ... BatchSampler(SequentialSampler(range(10)), batch_size=3, drop_last=True) ... ) [[0, 1, 2], [3, 4, 5], [6, 7, 8]] sampler batch_size drop_lastrNct|trt|ts|dkrtd|t|tstd|||_||_||_y)NrzBbatch_size should be a positive integer value, but got batch_size=z7drop_last should be a boolean value, but got drop_last=)r9r0r:r<rmrnro)rrmrnros rrzBatchSampler.__init__Asr:s+*d+QTU_T`a )T*I)U  $"rc#2Kt|j}|jr$|g|jz}t |D]}g| ygt j ||j}|r*|gt j ||j}|r)yywr)r*rmrornzip itertoolsislice)r sampler_iterargsbatch_droplastbatchs rr!zBatchSampler.__iter__ZsDLL) >> >DOO3D"%t* (''' (Gi&&|T__EFE J)**<IJs BBBc|jr"t|j|jzSt|j|jzdz |jzS)Nr`)ror,rmrnr s rr.zBatchSampler.__len__gsI >>t||$7 7 %7!;O Or)r"r#r$r%r r r0rr:rrlistr!r.r&rrr r +si*#ws|Xc]23## #  #2 K(49- KPPrr )rscollections.abcrrrrtypingrrr r rE__all__rr r0rr rrrzr r&rrr~s??44   4(4"gen4"d% %&H GCLH V!'#,!,F GCLF RDP749%DPr