StratifiedBatchSampler

Models & Types ยท AiDotNet.Data.Sampling

A batch sampler that ensures each batch contains samples from all classes.

Inheritance: DataSamplerBase โ†’ StratifiedBatchSampler

Implements: IBatchSampler, IStratifiedSampler

For Beginners

While StratifiedSampler ensures the overall epoch has the right class balance, StratifiedBatchSampler ensures EACH BATCH has balanced classes. This is helpful when:

  • Using batch normalization (needs balanced statistics per batch)
  • Doing contrastive learning (needs diverse samples in each batch)

How It Works

StratifiedBatchSampler creates batches where each batch has approximately the same class distribution. This is useful for batch normalization layers or when batch-level statistics are important.

Constructors

Constructor Summary
StratifiedBatchSampler(IEnumerable<int> labels, int numClasses, int batchSize, bool dropLast, int? seed) Initializes a new instance of the StratifiedBatchSampler class.

Properties

Property Summary
IReadOnlyList<int> Labels { get; set; }
bool DropLast { get; set; }
int BatchSize { get; set; }
int Length { get; }
int NumClasses { get; }

Methods

Method Summary
IEnumerable<int[]> GetBatchIndices()