Repository: pytorch
Issue: Mismatch in behaviour of WeightedRandomSampler and other samplers
Labels: todo,module: dataloader,triaged

## Issue description

All the other samplers for `torch.utils.data.DataLoader()` are designed to be used such that you can iterate an epoch (one epoch being the number of listed indices/data-points) over them by simply having them in a `for` loop. However, looping through a `DataLoader` with the sampler as `WeightedRandomSampler` simply occurs `num_samples` times. The `num_samples` argument is misleading, as one might imagine they need only one sample at a time, and/or they plan to update the weights after every sample.

It would be more consistent with other samplers if  the `WeightedRandomSampler` can be iterated over as many times as the length of the `weights` parameter (similar to how many times the `SubsetRandomSampler` iterates) and the `num_samples` argument is made to default to `1`


## Code example

Note: The `enumerate()` function is simply used to count the number of times the loop can procede

Code:
```python
import torch
from torchvision import datasets, transforms


mnist_test_dataset = datasets.MNIST(
    "data",
    train=False,
    download=True,
    transform=transforms.Compose([transforms.ToTensor()]),
)
train_loader_no_sampler = torch.utils.data.DataLoader(
    mnist_test_dataset, batch_size=1, shuffle=True
)
train_loader_random = torch.utils.data.DataLoader(
    mnist_test_dataset,
    sampler=torch.utils.data.sampler.RandomSampler(mnist_test_dataset),
)
train_loader_subsetrandom = torch.utils.data.DataLoader(
    mnist_test_dataset,
    sampler=torch.utils.data.sampler.SubsetRandomSampler(range(10000)),
)
train_loader_weightedrandom = torch.utils.data.DataLoader(
    mnist_test_dataset,
    sampler=torch.utils.data.sampler.WeightedRandomSampler(
        weights=[1] * 10000, num_samples=1
    ),
)

for iter_no, (data, label) in enumerate(train_loader_no_sampler):
    continue
print("No sampler: ", iter_no)

for iter_no, (data, label) in enumerate(train_loader_random):
    continue
print("Random sampler: ", iter_no)

for iter_no, (data, label) in enumerate(train_loader_subsetrandom):
    continue
print("Subset Random sampler: ", iter_no)

for iter_no, (data, label) in enumerate(train_loader_weightedrandom):
    continue
print("Weighted Random sampler: ", iter_no)

```

Output: (MNIST test dataset contains 10000 samples)
```
No sampler:  9999
Random sampler:  9999
Subset Random sampler:  9999
Weighted Random sampler:  0
```

## System Info

Collecting environment information...
PyTorch version: 0.4.0
Is debug build: No
CUDA used to build PyTorch: 8.0.61

OS: Ubuntu 16.04.4 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
CMake version: version 3.11.1

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 9.2.88
GPU models and configuration: GPU 0: GeForce 840M
Nvidia driver version: 396.24.02
cuDNN version: Probably one of the following:
/usr/local/cuda-9.0/lib64/libcudnn.so.7.1.4
/usr/local/cuda-9.0/lib64/libcudnn_static.a
/usr/local/cuda-9.2/lib64/libcudnn.so.7.1.4
/usr/local/cuda-9.2/lib64/libcudnn_static.a

Versions of relevant libraries:
[pip] numpy (1.14.5)
[pip] numpydoc (0.8.0)
[pip] torch (0.4.0)
[pip] torchvision (0.2.1)
[conda] magma-cuda80              2.3.0                         1    soumith
[conda] torch                     0.4.0                     <pip>
[conda] torchvision               0.2.1                     <pip>


cc @SsnL @VitalyFedyunin @ejguan