BalancedEpisodicDataLoader<T, TInput, TOutput>

Models & Types · AiDotNet.Data.Loaders

Provides balanced episodic task sampling that ensures equal class representation across multiple tasks.

Inheritance: DataLoaderBase<T> → EpisodicDataLoaderBase<T, TInput, TOutput> → BalancedEpisodicDataLoader<T, TInput, TOutput>

For Beginners

Standard random sampling might pick some classes more often than others by chance. This can cause problems in meta-learning:

  • The model might learn some classes better than others
  • Training could be biased toward frequently-sampled classes
  • Evaluation metrics might be skewed

The balanced loader solves this by:

  • Tracking how many times each class has been selected
  • Preferring classes that haven’t been used as much
  • Ensuring fair representation across all classes over time

When to use this:

  • Long meta-training runs where balanced class exposure matters
  • When your dataset has many classes and you want uniform coverage
  • When evaluating meta-learning algorithms fairly across all classes

Trade-off: Less random than uniform sampling, but more balanced. Good for training, but you might want standard EpisodicDataLoader for final evaluation to match real-world randomness.

How It Works

The BalancedEpisodicDataLoader extends the standard episodic loader by tracking class usage across multiple tasks and preferentially sampling under-represented classes. This ensures that over many episodes, all classes appear roughly the same number of times, preventing bias toward frequently-sampled classes.

Thread Safety: This class is not thread-safe due to internal state tracking. Create separate instances for concurrent task generation.

Performance: Slightly slower than standard EpisodicDataLoader due to usage tracking and weighted sampling, but still O(nWay × (kShot + queryShots)) for task creation.

Example

// Load a dataset with imbalanced class distribution
var features = new Matrix<double>(1000, 784);
var labels = new Vector<double>(1000);  // 20 classes with varying frequencies
            
// Create balanced loader - all classes will be sampled equally over time
var loader = new BalancedEpisodicDataLoader<double, Matrix<double>, Vector<double>>(
    datasetX: features,
    datasetY: labels,
    nWay: 5,
    kShot: 3,
    queryShots: 10,
    seed: 42
);
            
// Over 1000 episodes, each class will appear in roughly the same number of tasks
for (int episode = 0; episode < 1000; episode++)
{
    var task = loader.GetNextTask();
    // Train on balanced distribution of classes
}

Constructors

Constructor Summary
BalancedEpisodicDataLoader(Matrix<T> datasetX, Vector<T> datasetY, int nWay, int kShot, int queryShots, int? seed) Initializes a new instance of the BalancedEpisodicDataLoader for balanced N-way K-shot task sampling.