
    ϑi6                    ,   S SK Jr  S SKJrJrJrJr  S SKrSSK	J
r
  SSKJr  \(       a  S SKJrJrJrJr  S SKJr  S SKJr  \" S	5      r " S
 S\\   5      r " S S\\   5      r " S S\\   5      rSS jr " S S\\   5      r " S S\\   5      rg)    )annotations)TYPE_CHECKINGAnyGenericTypeVarN   )core)randperm)	GeneratorIteratorSequenceSized)Tensor_Tc                  Z    \ rS rSr% SrS\S'   S
SS jjrSS jr\(       a  SS jr	S	r
gS	r
g)Sampler(   a  
An abstract class to encapsulate methods and behaviors of samplers.

All sampler used by :code:`paddle.io.BatchSampler` should be a subclass
of :code:`paddle.io.Sampler`, BatchSampler subclasses should
implement following methods:

:code:`__iter__`: return sample index iterably, which iterate over indices
of dataset elements

:code:`__len__`: the number of sample in :attr:`data_source`


Args:
    data_source(Dataset, optional): this could be an instance of
            :code:`paddle.io.Dataset` other Python object which
            implemented :code:`__len__` for Sampler to get indices
            as the range of :attr:`dataset` length. Default None.

Returns:
    Sampler: an iterable object for sample indices iterating

Examples:

    .. code-block:: python

        >>> import numpy as np
        >>> from paddle.io import Dataset, Sampler

        >>> class RandomDataset(Dataset):  # type: ignore[type-arg]
        ...     def __init__(self, num_samples):
        ...         self.num_samples = num_samples
        ...
        ...     def __getitem__(self, idx):
        ...         image = np.random.random([784]).astype('float32')
        ...         label = np.random.randint(0, 9, (1, )).astype('int64')
        ...         return image, label
        ...
        ...     def __len__(self):
        ...         return self.num_samples
        ...
        >>> class MySampler(Sampler):  # type: ignore[type-arg]
        ...     def __init__(self, data_source):
        ...         self.data_source = data_source
        ...
        ...     def __iter__(self):
        ...         return iter(range(len(self.data_source)))  # type: ignore[arg-type]
        ...
        ...     def __len__(self):
        ...         return len(self.data_source)  # type: ignore[arg-type]
        ...
        >>> sampler = MySampler(data_source=RandomDataset(100))

        >>> for index in sampler:
        ...     print(index)
        0
        1
        2
        ...
        99

see `paddle.io.BatchSampler`
see `paddle.io.DataLoader`

Sized | Nonedata_sourceNc                    Xl         g Nr   selfr   s     \/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/io/dataloader/sampler.py__init__Sampler.__init__m       &    c                    [         er   )NotImplementedErrorr   s    r   __iter__Sampler.__iter__p   s    !!r   c                    g r    r"   s    r   __len__Sampler.__len__w   s    #r   r   r   )r   r   returnNone)r)   zIterator[_T]r)   int)__name__
__module____qualname____firstlineno____doc____annotations__r   r#   r   r'   __static_attributes__r&   r   r   r   r   (   s(    @D '"
 % r   r   c                  B    \ rS rSr% SrS\S'   S
S jrSS jrSS jrSr	g	)SequenceSamplerz   a  
Iterate samples sequentially, yield :code:`0, 1, 2, ..., len(data_source) -1`
generally,

Args:
    data_source(Dataset): dataset to sample, this could be an
            instance of :code:`paddle.io.Dataset` other Python
            object which implemented :code:`__len__`.

Returns:
    Sampler: a Sampler yield sample index sequentially

Examples:

    .. code-block:: python

        >>> import numpy as np
        >>> from paddle.io import Dataset, SequenceSampler

        >>> class RandomDataset(Dataset):  # type: ignore[type-arg]
        ...     def __init__(self, num_samples):
        ...         self.num_samples = num_samples
        ...
        ...     def __getitem__(self, idx):
        ...         image = np.random.random([784]).astype('float32')
        ...         label = np.random.randint(0, 9, (1, )).astype('int64')
        ...         return image, label
        ...
        ...     def __len__(self):
        ...         return self.num_samples
        ...
        >>> sampler = SequenceSampler(data_source=RandomDataset(100))

        >>> for index in sampler:
        ...     print(index)
        0
        1
        2
        ...
        99

see `paddle.io.Sampler`
r   r   c                    Xl         g r   r   r   s     r   r   SequenceSampler.__init__   r   r   c                P    [        [        [        U R                  5      5      5      $ r   )iterrangelenr   r"   s    r   r#   SequenceSampler.__iter__   s    E#d../011r   c                ,    [        U R                  5      $ r   )r<   r   r"   s    r   r'   SequenceSampler.__len__   s    4##$$r   r   N)r   r   r)   r*   r)   zIterator[int]r+   
r-   r.   r/   r0   r1   r2   r   r#   r'   r3   r&   r   r   r5   r5   z   s    *X '2%r   r5   c                      \ rS rSr% SrS\S'   S\S'   S\S'      S         SS
 jjr\SS j5       rSS jr	SS jr
Srg	)RandomSampler   a7  
Iterate samples randomly, yield shuffled indices, if :attr:`replacement=False`,
yield shuffled indices of the whole data source, if :attr:`replacement=True`,
:attr:`num_samples` can set to specify the sample number to draw.

Args:
    data_source(Dataset): dataset to sample, this could be an
            instance of :ref:`api_paddle_io_Dataset` or :ref:`api_paddle_io_IterableDataset` or other Python
            object which implemented :code:`__len__` to get indices as the range of :code:`dataset` length. Default None.
    replacement(bool, optional): If False, sample the whole dataset, If True,
            set :attr:`num_samples` for how many samples to draw. Default False.
    num_samples(int, optional): set sample number to draw. Default None, which is set to the length of `data_source`.
    generator(Generator, optional): specify a generator to sample the :code:`data_source`. Default None, disabled.

Returns:
    RandomSampler: a Sampler yield sample index randomly.

Examples:

    .. code-block:: python

        >>> import numpy as np
        >>> from paddle.io import Dataset, RandomSampler

        >>> np.random.seed(2023)
        >>> class RandomDataset(Dataset):  # type: ignore[type-arg]
        ...     def __init__(self, num_samples):
        ...         self.num_samples = num_samples
        ...
        ...     def __getitem__(self, idx):
        ...         image = np.random.random([784]).astype('float32')
        ...         label = np.random.randint(0, 9, (1, )).astype('int64')
        ...         return image, label
        ...
        ...     def __len__(self):
        ...         return self.num_samples
        ...
        >>> sampler = RandomSampler(data_source=RandomDataset(100))

        >>> for index in sampler:
        ...     print(index)
        56
        12
        68
        ...
        87
r   r   boolreplacement!Generator[int, None, None] | None	generatorNc                   Xl         X l        X0l        X@l        [	        U R                  [
        5      (       d  [        SU R                   35      eU R                  (       dQ  U R                  [        U R                   5      :  a.  [        SU R                   S[        U R                   5       35      e[	        U R                  [        5      (       a  U R                  S::  a  [        SU R                   35      eg )Nz:expect boolean value for replacement, but got replacement=zunum_samples should be smaller than or equal to length of data_source when replacement is False, but got num_samples: z > data_source: r   z>num_samples should be a positive integer, but got num_samples=)r   rF   _num_samplesrH   
isinstancerE   	TypeErrornum_samplesr<   
ValueErrorr,   )r   r   rF   rM   rH   s        r   r   RandomSampler.__init__   s     '&'"$**D11#//02 
 D$4$4s4;K;K7L$L((,(8(8'99I#dN^N^J_I`b 
 $**C00D4D4D4I''+'7'7&8:  5Jr   c                ^    U R                   c  [        U R                  5      $ U R                   $ r   )rJ   r<   r   r"   s    r   rM   RandomSampler.num_samples  s+    $t''((   r   c              #  d  #    [        U R                  5      nU R                  (       a7  [        U R                  5       H  n [        U R                  5      nUv   M     g U R                  (       aW  [        R                  R                  [        R                  " U5      U R                  SS9R                  5        H  nUv   M	     g [        R                  R                  [        R                  " U5      U R                  SS9R                  5        H  nUv   M	     g ! [         a       g f = f7f)NT)replaceF)r<   r   rH   r;   rM   nextStopIterationrF   nprandomchoicearangetolist)r   niindexs       r   r#   RandomSampler.__iter__  s       !>>4++, 0E  - YY--IIaL$"2"2D . &(E  K
  YY--IIaL$"2"2E . &(E  K % s*   ?D0DCD0
D-)D0,D--D0c                    U R                   $ r   )rM   r"   s    r   r'   RandomSampler.__len__!  s    r   )rJ   r   rH   rF   )FNN)
r   r   rF   rE   rM   z
int | NonerH   rG   r)   r*   r+   r@   )r-   r.   r/   r0   r1   r2   r   propertyrM   r#   r'   r3   r&   r   r   rC   rC      sz    .` 00
 ""&7;   	
 5 
< ! !
 * r   rC   c                   [        U [        R                  5      (       a  U R                  5       n [        U [        [
        45      (       a  [        R                  " U 5      n [        U [        R                  5      (       d   S5       e[        U R                  5      S::  d   S5       eU R                  SU R                  S   45      n [        R                  " U S:  5      (       d   S5       e[        R                  " U [        R                  :H  5      (       a   S5       e[        R                  " U [        R                  :H  5      (       a   S5       e[        R                   " U S:  S	S
9n[        R                  " US:  5      (       d   S5       eU(       d$  [        R                  " X1:  5      (       d   S5       eX R!                  S	S
9-  n / n[#        U R                  S   5       HD  n[        R$                  R'                  U R                  S	   XX   5      nUR)                  U5        MF     [        R                  " U5      $ )Nz=weights should be paddle.Tensor, numpy.ndarray, list or tuple   z$weights should be a 1-D or 2-D arrayg        z weights should be positive valuezweights should not be INFzweights should not be NaN   )axisr   z#weights should have positive valueszUweights positive value number should not less than num_samples when replacement=False)rK   r	   DenseTensornumpylisttuplerV   arrayndarrayr<   shapereshapeallanyinfnansumr;   rW   rX   append)weightsrM   rF   	non_zerosretsr\   rets          r   _weighted_samplery   %  s   '4++,,--/'D%=))((7#grzz** G* w}}"J$JJ"oor7==#456G66'S.!!E#EE!vvg'((E*EE(vvg'((E*EE(w}1-I66)a-  G"GG vvi.// 	
;	
/
 ++GD7==#$iiMM!k

 	C	 %
 88D>r   c                  j    \ rS rSr% SrS\S'   S\S'   S\S'    S       SS	 jjrSS
 jrSS jrSr	g)WeightedRandomSampleriE  a  
Random sample with given weights (probabilities), sample index will be in range
[0, len(weights) - 1], if :attr:`replacement` is True, index can be sampled
multiple times.

Args:
    weights(numpy.ndarray|paddle.Tensor|list|tuple): sequence of weights,
            should be numpy array, paddle.Tensor, list or tuple
    num_samples(int): set sample number to draw from sampler.
    replacement(bool): Whether to draw sample with replacements, default True

Returns:
    Sampler: a Sampler yield sample index randomly by given weights

Examples:

    .. code-block:: python

        >>> import numpy as np
        >>> from paddle.io import WeightedRandomSampler

        >>> np.random.seed(2023)
        >>> sampler = WeightedRandomSampler(
        ...     weights=[0.1, 0.3, 0.5, 0.7, 0.2],
        ...     num_samples=5,
        ...     replacement=True
        ... )
        >>> for index in sampler:
        ...     print(index)
        2
        4
        3
        1
        1
+npt.NDArray[Any] | Tensor | Sequence[float]ru   r,   rM   rE   rF   c                    [        U[        5      (       a  US::  a  [        S5      e[        U[        5      (       d  [        S5      eXl        X l        X0l        g )Nr   z(num_samples should be a positive integerz%replacement should be a boolean value)rK   r,   rN   rE   ru   rM   rF   )r   ru   rM   rF   s       r   r   WeightedRandomSampler.__init__n  sN     +s++{a/?GHH+t,,DEE&&r   c                    [        U R                  U R                  U R                  5      n[	        UR                  S5      R                  5       5      $ Nrd   )ry   ru   rM   rF   r:   rn   rZ   )r   idxss     r   r#   WeightedRandomSampler.__iter__|  sB    LL$**D,<,<
 DLL$++-..r   c                    [         R                  " U R                  R                  5      U R                  R                  S   -  nU R                  U-  $ r   )rV   prodru   rm   rM   )r   muls     r   r'   WeightedRandomSampler.__len__  s?    ggdll(()T\\-?-?-CC#%%r   )rM   rF   ru   NT)ru   r|   rM   r,   rF   rE   r)   r*   r@   r+   rA   r&   r   r   r{   r{   E  sV    "H 98 !	'<' ' 	'
 
'/&r   r{   c                  B    \ rS rSr% SrS\S'   S
S jrSS jrSS jrSr	g	)SubsetRandomSampleri  a  
Randomly sample elements from a given list of indices, without replacement.

Args:
    indices (sequence): a sequence of indices

Examples:

    .. code-block:: python

        >>> import paddle
        >>> from paddle.io import SubsetRandomSampler

        >>> paddle.seed(2023)
        >>> sampler = SubsetRandomSampler(indices=[1, 3, 5, 7, 9])

        >>> for index in sampler:
        ...     print(index)
        9
        3
        7
        5
        1

Sequence[int]indicesc                D    [        U5      S:X  a  [        S5      eXl        g )Nr   zHThe length of `indices` in SubsetRandomSampler should be greater than 0.)r<   rN   r   )r   r   s     r   r   SubsetRandomSampler.__init__  s%    w<1Z  r   c              #  x   #    [        [        U R                  5      5       H  nU R                  U   v   M     g 7fr   )r
   r<   r   )r   r\   s     r   r#   SubsetRandomSampler.__iter__  s+     #dll+,A,,q/! -s   8:c                ,    [        U R                  5      $ r   )r<   r   r"   s    r   r'   SubsetRandomSampler.__len__  s    4<<  r   )r   N)r   r   r)   r*   r@   r+   rA   r&   r   r   r   r     s    4 "!r   r   r   )
__future__r   typingr   r   r   r   rh   rV   	frameworkr	   tensorr
   collections.abcr   r   r   r   numpy.typingnptpaddler   r   r   r,   r5   rC   ry   r{   r   r&   r   r   <module>r      s    #    DD T]O&gbk O&d6%gcl 6%ro GCL o d@?&GCL ?&D)!'#, )!r   