WeightedSampler<T>

Models & Types · AiDotNet.Data.Sampling

A sampler that samples indices based on their weights.

Inheritance: DataSamplerBase → WeightedSamplerBase<T> → WeightedSampler<T>

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:

How It Works

WeightedSampler selects samples with probability proportional to their weights. This is useful for handling class imbalance, importance sampling, or focusing training on harder examples.

Constructors

Constructor Summary
WeightedSampler(IEnumerable<T> weights, int? numSamples, bool replacement, int? seed) Initializes a new instance of the WeightedSampler class.

Methods

Method Summary
T[] CreateBalancedWeights(IReadOnlyList<int> labels, int numClasses) Creates weights that balance class frequencies.