# Feeding a network with images of one label only in each batch

**URL:** <https://discuss.pytorch.org/t/feeding-a-network-with-images-of-one-label-only-in-each-batch/148339>\
**Category:** vision\
**Created:** [April 5, 2022, 12:50pm UTC](https://discuss.pytorch.org/t/feeding-a-network-with-images-of-one-label-only-in-each-batch/148339 "2022-04-05T12:50:38Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 6, 2022, 4:55am UTC](https://discuss.pytorch.org/t/feeding-a-network-with-images-of-one-label-only-in-each-batch/148339/2 "2022-04-06T04:55:28Z")

</div>

> [@Nimrod\_Daniel](#):
>
> My custom dataset can find the indexes of each label, but I don’t know if there’s a way to make the dataloader choose them somehow. As far as I know the dataloader’ sampler cannot deal with that.  
> Any idea how to do that?

A custom sampler sounds like the right idea.  
Once you have the labels, you could create a custom sampler (e.g. as a `BatchSampler`) and let it pass the indices for a single target only. [Here](https://discuss.pytorch.org/t/how-to-use-batchsampler-with-getitem-dataset/78788/4) is an example of the usage of a `BatchSampler` and for your use case you could derive from this class to implement a custom sample logic.

---

_[View the full topic](https://discuss.pytorch.org/t/feeding-a-network-with-images-of-one-label-only-in-each-batch/148339)._
