Class RandomSampler
A sampler that randomly shuffles the dataset indices each epoch.
public class RandomSampler : DataSamplerBase, IDataSampler
- Inheritance
-
RandomSampler
- Implements
- Inherited Members
Remarks
RandomSampler is the default sampling strategy for most training scenarios. It shuffles the dataset indices using the Fisher-Yates algorithm for O(n) time complexity.
For Beginners: This sampler shuffles your data randomly before each training epoch. Shuffling is important because: - It prevents the model from learning patterns based on data order - It helps the model generalize better - It ensures different batches each epoch for varied gradient updates
Example:
var sampler = new RandomSampler(datasetSize: 1000, seed: 42);
foreach (var index in sampler.GetIndices())
{
// Process sample at index
}
Constructors
RandomSampler(int, int?)
Initializes a new instance of the RandomSampler class.
public RandomSampler(int datasetSize, int? seed = null)
Parameters
datasetSizeintThe total number of samples in the dataset.
seedint?Optional random seed for reproducibility.
Exceptions
- ArgumentOutOfRangeException
Thrown when datasetSize is less than 1.
Properties
Length
Gets the total number of samples this sampler will produce per epoch.
public override int Length { get; }
Property Value
Remarks
This may differ from the dataset size for oversampling or undersampling strategies.
Methods
GetIndicesCore()
Core implementation for generating indices. Override this in derived classes.
protected override IEnumerable<int> GetIndicesCore()
Returns
- IEnumerable<int>
An enumerable of sample indices.