# DataLoader api confusion

**URL:** <https://discuss.pytorch.org/t/dataloader-api-confusion/28098>\
**Category:** Uncategorized\
**Created:** [October 26, 2018, 9:35pm UTC](https://discuss.pytorch.org/t/dataloader-api-confusion/28098 "2018-10-26T21:35:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dashesy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dashesy/32/4869_2.png) [@dashesy](https://discuss.pytorch.org/u/dashesy)\
**Post date:** [October 26, 2018, 9:35pm UTC](https://discuss.pytorch.org/t/dataloader-api-confusion/28098/1 "2018-10-26T21:35:27Z")

</div>

1. Why `DataLoader` has both `sampler` as well as `batch_sampler`? while there is already a sampler that is `torch.utils.data.BatchSampler`? does that mean I can pass BatchSampler only as `batch_sampler` of DataLaoder?
2. If I want a `DistributedSampler` that is also in batches/`BatchSampler` and is random/`RandomSampler` I will have no option to mix them!  
both `DistributedSampler` and `RandomSampler` wrap a dataset/data\_source and only `BatchSampler` can wrap one of them.
3. Even if we could mix them, what would be difference of `RandomSampler(BatchSampler(DistributedSampler(dataset)))` and `BatchSampler(RandomSampler(DistributedSampler(dataset)))`.
4. So I think I should use `sampler=DistributedSampler(dataset)` and pass a `batch_size` to `DataLoader` (or use `sampler=BatchSampler(DistributedSampler(dataset))`). But how can I make it random? `shuffle` is mutually exclusive from `sampler` and I cannot mix `RandomSampler` with `DistributedSampler`.

---

<div class="post-metadata">

**Author:** ![dashesy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dashesy/32/4869_2.png) [@dashesy](https://discuss.pytorch.org/u/dashesy)\
**Post date:** [October 29, 2018, 8:39pm UTC](https://discuss.pytorch.org/t/dataloader-api-confusion/28098/2 "2018-10-29T20:39:32Z")

</div>

Looking at the code `DistributedSampler(RandomSampler(...))` is valid, `dataset` does not have to be a `Dataset`, it can be another `Sampler` because only `len()` matters that any `Sampler` provides.

---

<div class="post-metadata">

**Author:** ![dashesy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dashesy/32/4869_2.png) [@dashesy](https://discuss.pytorch.org/u/dashesy)\
**Post date:** [October 29, 2018, 9:23pm UTC](https://discuss.pytorch.org/t/dataloader-api-confusion/28098/3 "2018-10-29T21:23:41Z")

</div>

@fmassa I am reading [here](https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/data/samplers/distributed.py) you have added a new `DistributedSampler` that also has `shuffle`. Why not just pass `sampler=DistributedSampler(RandomSampler(dataset))` to a `DataLoader`?

I want to see if there is any particular issue with above.
