# Dealing with imbalanced datasets in pytorch

**URL:** <https://discuss.pytorch.org/t/dealing-with-imbalanced-datasets-in-pytorch/22596>\
**Category:** Uncategorized\
**Created:** [August 7, 2018, 1:37pm UTC](https://discuss.pytorch.org/t/dealing-with-imbalanced-datasets-in-pytorch/22596 "2018-08-07T13:37:47Z")\
**Posts on this page:** 1\
**Showing post:** 36

<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:** [March 4, 2020, 1:21pm UTC](https://discuss.pytorch.org/t/dealing-with-imbalanced-datasets-in-pytorch/22596/36 "2020-03-04T13:21:37Z")

</div>

Your idea of clustering the regression targets to a few clusters and assigning weights to these seem reasonable.  
You could either do it manually (as it seems to be the case now) or use something like k-means.  
Once you have the clusters, you could count the samples similar to a classification task and calculate the weights based on the number of samples for each cluster and create a mapping between the cluster and weight.  
After creating the weights, you could write a function, which accepts the current output batch with the regression prediction, as well as your cluster centers (k-means dict), and returns a batch of weights, which can then be multiplied to create the final loss.

---

_[View the full topic](https://discuss.pytorch.org/t/dealing-with-imbalanced-datasets-in-pytorch/22596)._
