# Some problems with WeightedRandomSampler

**URL:** <https://discuss.pytorch.org/t/some-problems-with-weightedrandomsampler/23242>\
**Category:** Uncategorized\
**Created:** [August 16, 2018, 7:34am UTC](https://discuss.pytorch.org/t/some-problems-with-weightedrandomsampler/23242 "2018-08-16T07:34:59Z")\
**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:** [August 16, 2018, 11:09am UTC](https://discuss.pytorch.org/t/some-problems-with-weightedrandomsampler/23242/2 "2018-08-16T11:09:47Z")

</div>

I think you might pass the wrong weights to `WeightedRandomSampler`.  
The sequence of weights should correspond to your samples in the dataset.  
Here is a small example:

```python
weights = 1. / torch.tensor(class_sample_counts, dtype=torch.float)
samples_weights = weights[train_targets]

sampler = WeightedRandomSampler(
    weights=samples_weights,
    num_samples=len(samples_weights),
    replacement=True)

```

---

_[View the full topic](https://discuss.pytorch.org/t/some-problems-with-weightedrandomsampler/23242)._
