a
    7SicJ*                     @   s   d dl Z d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
mZ g dZedddZG dd	 d	e	e ZG d
d dee ZG dd dee ZG dd dee ZG dd dee ZG dd deee  ZdS )    N)Tensor)	IteratorIterableOptionalSequenceListTypeVarGenericSizedUnion)BatchSamplerRandomSamplerSamplerSequentialSamplerSubsetRandomSamplerWeightedRandomSamplerT_coT)	covariantc                   @   s6   e Zd ZdZee ddddZee dddZ	dS )	r   a  Base class for all Samplers.

    Every Sampler subclass has to provide an :meth:`__iter__` method, providing a
    way to iterate over indices of dataset elements, and a :meth:`__len__` method
    that returns the length of the returned iterators.

    .. 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_sourcereturnc                 C   s   d S N selfr   r   r   T/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/utils/data/sampler.py__init__   s    zSampler.__init__r   c                 C   s   t d S r   )NotImplementedErrorr   r   r   r   __iter__!   s    zSampler.__iter__)
__name__
__module____qualname____doc__r   r
   r   r   r   r    r   r   r   r   r      s   r   c                   @   sJ   e Zd ZU dZeed< eddddZee ddd	Z	edd
dZ
dS )r   z~Samples elements sequentially, always in the same order.

    Args:
        data_source (Dataset): dataset to sample from
    r   Nr   c                 C   s
   || _ d S r   )r   r   r   r   r   r   H   s    zSequentialSampler.__init__r   c                 C   s   t tt| jS r   )iterrangelenr   r   r   r   r   r    K   s    zSequentialSampler.__iter__c                 C   s
   t | jS r   )r'   r   r   r   r   r   __len__N   s    zSequentialSampler.__len__)r!   r"   r#   r$   r
   __annotations__r   r   intr    r(   r   r   r   r   r   @   s
   
r   c                   @   sn   e Zd ZU dZeed< eed< deeee ddddZ	e
ed	d
dZee d	ddZed	ddZdS )r   a  Samples 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   replacementFN)r   r+   num_samplesr   c                 C   s^   || _ || _|| _|| _t| jts4td| jt| jt	rJ| jdkrZt
d| jd S )N=replacement should be a boolean value, but got replacement={}r   Fnum_samples should be a positive integer value, but got num_samples={})r   r+   _num_samples	generator
isinstancebool	TypeErrorformatr,   r*   
ValueError)r   r   r+   r,   r0   r   r   r   r   _   s    zRandomSampler.__init__r   c                 C   s   | j d u rt| jS | j S r   )r/   r'   r   r   r   r   r   r,   n   s    

zRandomSampler.num_samplesc                 c   s   t | j}| jd u rDttjdtjd  }t	 }|
| n| j}| jrt| jd D ]"}tj|dtj|d E d H  q^tj|| jd ftj|d E d H  nRt| j| D ]}tj||d E d H  qtj||d d | j|  E d H  d S )Nr   dtype    )r8   )highsizer7   r0   r0   )r'   r   r0   r*   torchemptyint64random_item	Generatormanual_seedr+   r&   r,   randinttolistrandperm)r   nseedr0   _r   r   r   r    u   s    

 (zRandomSampler.__iter__c                 C   s   | j S r   r,   r   r   r   r   r(      s    zRandomSampler.__len__)FNN)r!   r"   r#   r$   r
   r)   r2   r   r*   r   propertyr,   r   r    r(   r   r   r   r   r   R   s   
	  r   c                   @   sT   e Zd ZU dZee ed< dee ddddZee ddd	Z	edd
dZ
dS )r   zSamples elements randomly from a given list of indices, without replacement.

    Args:
        indices (sequence): a sequence of indices
        generator (Generator): Generator used in sampling.
    indicesN)rK   r   c                 C   s   || _ || _d S r   )rK   r0   )r   rK   r0   r   r   r   r      s    zSubsetRandomSampler.__init__r   c                 c   s,   t jt| j| jdD ]}| j| V  qd S Nr;   )r<   rE   r'   rK   r0   )r   ir   r   r   r       s    zSubsetRandomSampler.__iter__c                 C   s
   t | jS r   )r'   rK   r   r   r   r   r(      s    zSubsetRandomSampler.__len__)N)r!   r"   r#   r$   r   r*   r)   r   r   r    r(   r   r   r   r   r      s
   
r   c                   @   sd   e Zd ZU dZeed< eed< eed< dee	 eedddd	Z
ee d
ddZed
ddZdS )r   a  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:
        >>> 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]
    weightsr,   r+   TN)rN   r,   r+   r   c                 C   sj   t |trt |ts|dkr*td|t |tsBtd|tj|tjd| _|| _	|| _
|| _d S )Nr   r.   r-   r6   )r1   r*   r2   r5   r4   r<   	as_tensordoublerN   r,   r+   r0   )r   rN   r,   r+   r0   r   r   r   r      s    
zWeightedRandomSampler.__init__r   c                 c   s0   t j| j| j| j| jd}t| E d H  d S rL   )r<   multinomialrN   r,   r+   r0   r%   rD   )r   Zrand_tensorr   r   r   r       s    zWeightedRandomSampler.__iter__c                 C   s   | j S r   rI   r   r   r   r   r(      s    zWeightedRandomSampler.__len__)TN)r!   r"   r#   r$   r   r)   r*   r2   r   floatr   r   r    r(   r   r   r   r   r      s   
 
r   c                   @   sX   e Zd ZdZeee ee f eeddddZ	e
ee  dddZedd	d
ZdS )r   ai  Wraps 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]]
    N)sampler
batch_size	drop_lastr   c                 C   sX   t |trt |ts|dkr*td|t |tsBtd||| _|| _|| _d S )Nr   zDbatch_size should be a positive integer value, but got batch_size={}z9drop_last should be a boolean value, but got drop_last={})r1   r*   r2   r5   r4   rS   rT   rU   )r   rS   rT   rU   r   r   r   r      s    
zBatchSampler.__init__r   c                 #   s   | j rNt| j z" fddt| jD }|V  W q tyH   Y qY q0 qnbdg| j }d}| jD ]4}|||< |d7 }|| jkrd|V  d}dg| j }qd|dkr|d | V  d S )Nc                    s   g | ]}t  qS r   )next).0rH   Zsampler_iterr   r   
<listcomp>       z)BatchSampler.__iter__.<locals>.<listcomp>r      )rU   r%   rS   r&   rT   StopIteration)r   batchZidx_in_batchidxr   rX   r   r       s$    



zBatchSampler.__iter__c                 C   s4   | j rt| j| j S t| j| j d | j S d S )Nr[   )rU   r'   rS   rT   r   r   r   r   r(     s    zBatchSampler.__len__)r!   r"   r#   r$   r   r   r*   r   r2   r   r   r   r    r(   r   r   r   r   r      s   $r   )r<   r   typingr   r   r   r   r   r   r	   r
   r   __all__r   r   r*   r   r   r   r   r   r   r   r   r   <module>   s   ,	.9+