Using WeightedRandomSampler in PyTorch
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
WeightedRandomSampler in PyTorch lets you sample dataset items according to custom probabilities. It is commonly used for imbalanced datasets, but the crucial detail is that the sampler expects one weight per sample, not one weight per class.
That distinction is the source of most first-time mistakes. If your dataset has ten thousand examples, the sampler needs ten thousand weights.
The Mental Model
For class imbalance, the usual process is:
- count how many samples belong to each class
- compute a class weight such as the inverse frequency
- map each class weight onto every sample belonging to that class
- pass the full sample-level weight vector to
WeightedRandomSampler
So if class 0 is common and class 1 is rare, samples from class 1 receive larger weights and are drawn more often.
A Runnable Example
Here is a small imbalanced dataset with 90 examples from class 0 and 10 from class 1:
The important line is this one:
That converts a short class-weight vector into the per-sample weight vector the sampler actually needs.
Why replacement=True Is Common
For oversampling minority classes, replacement=True is usually the intended setting. It allows the same rare sample to be drawn more than once within an epoch, which is how oversampling actually happens.
If you set replacement=False, each sample can appear at most once in a pass. That can still be useful, but it reduces the balancing effect because minority examples cannot be repeated.
A simple rule is:
- use
replacement=Truewhen you want minority classes to appear more often - use
replacement=Falseonly when repeated sampling would be undesirable
Understanding num_samples
num_samples controls how many draws the sampler makes in one pass through the DataLoader. A common choice is the dataset length:
That produces an epoch with as many draws as there are original samples, but the class distribution of those draws is influenced by the weights.
You can choose a different value if you want longer or shorter effective epochs, but keeping it equal to the dataset size is the least surprising default.
Sampler Versus Loss Weighting
Balanced sampling and weighted loss are different tools. WeightedRandomSampler changes which examples the model sees and how often it sees them. A class-weighted loss changes how strongly each training example affects the optimization step.
You can combine them, but doing both means you are compensating for imbalance twice. That may be useful in some situations, but it should be a conscious decision rather than an automatic habit.
Common Pitfalls
The biggest mistake is passing class weights directly into WeightedRandomSampler. If your dataset has many samples, the sampler needs a weight vector of that same length.
Another pitfall is expecting every mini-batch to be perfectly balanced. The sampler improves balance over repeated draws, but small individual batches can still fluctuate.
A third issue is evaluating the model on oversampled data. Resampling is a training trick, not a substitute for realistic validation. Keep validation and test loaders representative of the real distribution.
Summary
- '
WeightedRandomSamplerexpects one weight per dataset sample.' - For class imbalance, compute class weights and then expand them into sample weights.
- '
replacement=Trueis common when you want minority classes to be oversampled.' - '
num_samplescontrols the effective epoch length.' - Use balanced sampling for training, but evaluate on a realistic validation set.

