3
I4W^¼   ã               @   sx   d dl Z d dlmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZ	G dd„ deƒZ
dS )é    N)Úint_classesc               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSampleraé  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`.
    c             C   s   d S )N© )ÚselfÚdata_sourcer   r   ú9/tmp/pip-build-e66j97b2/torch/torch/utils/data/sampler.pyÚ__init__   s    zSampler.__init__c             C   s   t ‚d S )N)ÚNotImplementedError)r   r   r   r   Ú__iter__   s    zSampler.__iter__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r   r   r   r   r      s   
r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚSequentialSamplerzƒSamples elements sequentially, always in the same order.

    Arguments:
        data_source (Dataset): dataset to sample from
    c             C   s
   || _ d S )N)r   )r   r   r   r   r   r   :   s    zSequentialSampler.__init__c             C   s   t tt| jƒƒƒS )N)ÚiterÚrangeÚlenr   )r   r   r   r   r
   =   s    zSequentialSampler.__iter__c             C   s
   t | jƒS )N)r   r   )r   r   r   r   Ú__len__@   s    zSequentialSampler.__len__N)r   r   r   r   r   r
   r   r   r   r   r   r   3   s   r   c               @   s6   e Zd ZdZddd„Zedd„ ƒZdd	„ Zd
d„ ZdS )ÚRandomSampleraõ  Samples elements randomly. If without replacement, then sample from a shuffled dataset.
    If with replacement, then user can specify :attr:`num_samples` to draw.

    Arguments:
        data_source (Dataset): dataset to sample from
        replacement (bool): samples are drawn with replacement if ``True``, default=``False``
        num_samples (int): number of samples to draw, default=`len(dataset)`. This argument
            is supposed to be specified only when `replacement` is ``True``.
    FNc             C   sr   || _ || _|| _t| jtƒs.tdj| jƒƒ‚| jd k	rF| rFtdƒ‚t| jtƒ s^| jdkrntdj| jƒƒ‚d S )Nz=replacement should be a boolean value, but got replacement={}zfWith replacement=False, num_samples should not be specified, since a random permute will be performed.r   zFnum_samples should be a positive integer value, but got num_samples={})	r   ÚreplacementÚ_num_samplesÚ
isinstanceÚboolÚ
ValueErrorÚformatÚnum_samplesÚint)r   r   r   r   r   r   r   r   O   s    
zRandomSampler.__init__c             C   s   | j d krt| jƒS | j S )N)r   r   r   )r   r   r   r   r   `   s    

zRandomSampler.num_samplesc             C   s@   t | jƒ}| jr.ttj|| jftjd�jƒ ƒS ttj	|ƒjƒ ƒS )N)ÚhighÚsizeÚdtype)
r   r   r   r   ÚtorchÚrandintr   Zint64ÚtolistÚrandperm)r   Únr   r   r   r
   g   s    
zRandomSampler.__iter__c             C   s   | j S )N)r   )r   r   r   r   r   m   s    zRandomSampler.__len__)FN)	r   r   r   r   r   Úpropertyr   r
   r   r   r   r   r   r   D   s
   	
r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚSubsetRandomSamplerz“Samples elements randomly from a given list of indices, without replacement.

    Arguments:
        indices (sequence): a sequence of indices
    c             C   s
   || _ d S )N)Úindices)r   r'   r   r   r   r   x   s    zSubsetRandomSampler.__init__c                s   ‡ fdd„t jtˆ jƒƒD ƒS )Nc             3   s   | ]}ˆ j | V  qd S )N)r'   )Ú.0Úi)r   r   r   ú	<genexpr>|   s    z/SubsetRandomSampler.__iter__.<locals>.<genexpr>)r    r#   r   r'   )r   r   )r   r   r
   {   s    zSubsetRandomSampler.__iter__c             C   s
   t | jƒS )N)r   r'   )r   r   r   r   r   ~   s    zSubsetRandomSampler.__len__N)r   r   r   r   r   r
   r   r   r   r   r   r&   q   s   r&   c               @   s*   e Zd ZdZd
dd„Zdd„ Zdd„ Zd	S )ÚWeightedRandomSampleraÙ  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.

    Example:
        >>> list(WeightedRandomSampler([0.1, 0.9, 0.4, 0.7, 3.0, 0.6], 5, replacement=True))
        [0, 0, 0, 1, 0]
        >>> list(WeightedRandomSampler([0.9, 0.4, 0.05, 0.2, 0.3, 0.1], 5, replacement=False))
        [0, 1, 4, 3, 2]
    Tc             C   sf   t |tƒ st |tƒs|dkr,tdj|ƒƒ‚t |tƒsDtdj|ƒƒ‚tj|tjd�| _|| _	|| _
d S )Nr   zFnum_samples should be a positive integer value, but got num_samples={}z=replacement should be a boolean value, but got replacement={})r   )r   Ú_int_classesr   r   r   r    Z	as_tensorÚdoubleÚweightsr   r   )r   r.   r   r   r   r   r   r   “   s    
zWeightedRandomSampler.__init__c             C   s   t tj| j| j| jƒjƒ ƒS )N)r   r    Zmultinomialr.   r   r   r"   )r   r   r   r   r
   Ÿ   s    zWeightedRandomSampler.__iter__c             C   s   | j S )N)r   )r   r   r   r   r   ¢   s    zWeightedRandomSampler.__len__N)T)r   r   r   r   r   r
   r   r   r   r   r   r+   ‚   s   
r+   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚBatchSampleraB  Wraps another sampler to yield a mini-batch of indices.

    Args:
        sampler (Sampler): Base sampler.
        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]]
    c             C   sr   t |tƒstdj|ƒƒ‚t |tƒ s6t |tƒs6|dkrDtdj|ƒƒ‚t |tƒs\tdj|ƒƒ‚|| _|| _|| _d S )NzMsampler should be an instance of torch.utils.data.Sampler, but got sampler={}r   zDbatch_size should be a positive integer value, but got batch_size={}z9drop_last should be a boolean value, but got drop_last={})	r   r   r   r   r,   r   ÚsamplerÚ
batch_sizeÚ	drop_last)r   r0   r1   r2   r   r   r   r   ¶   s    

zBatchSampler.__init__c             c   sT   g }x0| j D ]&}|j|ƒ t|ƒ| jkr|V  g }qW t|ƒdkrP| j rP|V  d S )Nr   )r0   Úappendr   r1   r2   )r   ÚbatchÚidxr   r   r   r
   Æ   s    
zBatchSampler.__iter__c             C   s4   | j rt| jƒ| j S t| jƒ| j d | j S d S )Né   )r2   r   r0   r1   )r   r   r   r   r   Ð   s    zBatchSampler.__len__N)r   r   r   r   r   r
   r   r   r   r   r   r/   ¦   s   
r/   )r    Z
torch._sixr   r,   Úobjectr   r   r   r&   r+   r/   r   r   r   r   Ú<module>   s   .-$