Class WeightedSampler<T>
A sampler that samples indices based on their weights.
public class WeightedSampler<T> : WeightedSamplerBase<T>, IWeightedSampler<T>, IDataSampler
Type Parameters
TThe numeric type for weights.
- Inheritance
-
WeightedSampler<T>
- Implements
- Inherited Members
Remarks
WeightedSampler selects samples with probability proportional to their weights. This is useful for handling class imbalance, importance sampling, or focusing training on harder examples.
For Beginners: This sampler lets you control how often each sample appears:
- Higher weight = more likely to be selected
- Lower weight = less likely to be selected
Common uses:
- Class imbalance: Give higher weights to underrepresented classes
- Hard example mining: Give higher weights to samples the model struggles with
- Curriculum learning: Adjust weights during training based on difficulty
Example:
// Dataset has 900 cats, 100 dogs
// Give dogs 9x higher weight to balance
var weights = labels.Select(l => l == "dog" ? 9.0f : 1.0f).ToList();
var sampler = new WeightedSampler<float>(weights);
Constructors
WeightedSampler(IEnumerable<T>, int?, bool, int?)
Initializes a new instance of the WeightedSampler class.
public WeightedSampler(IEnumerable<T> weights, int? numSamples = null, bool replacement = true, int? seed = null)
Parameters
weightsIEnumerable<T>The weight for each sample. Must be non-negative.
numSamplesint?Number of samples to draw per epoch. Defaults to dataset size.
replacementboolWhether to sample with replacement.
seedint?Optional random seed for reproducibility.
Exceptions
- ArgumentNullException
Thrown when weights is null.
- ArgumentException
Thrown when weights is empty or contains negative values.
Methods
CreateBalancedWeights(IReadOnlyList<int>, int)
Creates weights that balance class frequencies.
public static T[] CreateBalancedWeights(IReadOnlyList<int> labels, int numClasses)
Parameters
labelsIReadOnlyList<int>The class labels for each sample.
numClassesintThe number of classes.
Returns
- T[]
Weights that inversely weight by class frequency.
Remarks
This helper method creates weights such that each class has equal total weight, effectively balancing an imbalanced dataset.
For Beginners: If you have 900 cats and 100 dogs, calling this method will give dogs 9x the weight of cats, so they get sampled equally often.
GetIndicesCore()
Core implementation for generating indices. Override this in derived classes.
protected override IEnumerable<int> GetIndicesCore()
Returns
- IEnumerable<int>
An enumerable of sample indices.