# How to balance data in PyTorch DataLoader

**URL:** <https://discuss.pytorch.org/t/how-to-balance-data-in-pytorch-dataloader/109028>\
**Category:** Uncategorized\
**Created:** [January 16, 2021, 12:59pm UTC](https://discuss.pytorch.org/t/how-to-balance-data-in-pytorch-dataloader/109028 "2021-01-16T12:59:15Z")\
**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:** [February 1, 2021, 8:38am UTC](https://discuss.pytorch.org/t/how-to-balance-data-in-pytorch-dataloader/109028/2 "2021-02-01T08:38:37Z")

</div>

You could use a `WeightedRandomSampler` and try to adapt [this example](https://discuss.pytorch.org/t/how-to-handle-imbalanced-classes/11264/2) for your use case.  
Based on the figure it seems you are working on a regression task, so you would need to create bins first before calculating the “class weights” (which would be bin weights in your case).

---

_[View the full topic](https://discuss.pytorch.org/t/how-to-balance-data-in-pytorch-dataloader/109028)._
