Table of Contents

Class DataLoaderExtensions

Namespace
AiDotNet.Extensions
Assembly
AiDotNet.dll

Provides extension methods for data loaders to enhance batch iteration capabilities.

public static class DataLoaderExtensions
Inheritance
DataLoaderExtensions
Inherited Members

Remarks

These extension methods provide a fluent API for batch iteration, enabling PyTorch-style and TensorFlow-style data loading patterns.

For Beginners: Extension methods add new capabilities to existing types. These methods make it easy to iterate through your data in batches with a clean syntax:

// Fluent API for batch iteration
foreach (var batch in dataLoader.CreateBatches(batchSize: 32).Shuffled().DropLast())
{
    model.TrainOnBatch(batch);
}

Methods

CreateBatchesAsync<TBatch>(IBatchIterable<TBatch>, int?, int)

Creates an async batch configuration builder for fluent async batch iteration.

public static AsyncBatchConfigurationBuilder<TBatch> CreateBatchesAsync<TBatch>(this IBatchIterable<TBatch> source, int? batchSize = null, int prefetchCount = 2)

Parameters

source IBatchIterable<TBatch>

The batch iterable source.

batchSize int?

Optional batch size override.

prefetchCount int

Number of batches to prefetch. Default is 2.

Returns

AsyncBatchConfigurationBuilder<TBatch>

An async batch configuration builder for fluent configuration.

Type Parameters

TBatch

The type of batch returned by the data loader.

Remarks

This method starts a fluent chain for configuring async batch iteration with prefetching. Prefetching prepares batches in the background while the current batch is being processed.

For Beginners: Use this for async iteration with prefetching:

// Async iteration with prefetching
await foreach (var batch in dataLoader.CreateBatchesAsync(batchSize: 32, prefetchCount: 3))
{
    await model.TrainOnBatchAsync(batch);
}

CreateBatches<TBatch>(IBatchIterable<TBatch>, int?)

Creates a batch configuration builder for fluent batch iteration.

public static BatchConfigurationBuilder<TBatch> CreateBatches<TBatch>(this IBatchIterable<TBatch> source, int? batchSize = null)

Parameters

source IBatchIterable<TBatch>

The batch iterable source.

batchSize int?

Optional batch size override.

Returns

BatchConfigurationBuilder<TBatch>

A batch configuration builder for fluent configuration.

Type Parameters

TBatch

The type of batch returned by the data loader.

Remarks

This method starts a fluent chain for configuring batch iteration. Call methods like Shuffled(), DropLast(), WithSeed() to configure, then iterate using foreach or ToList().

For Beginners: This is the starting point for creating batches:

// Basic usage
var batches = dataLoader.CreateBatches(batchSize: 32);

// With configuration
var batches = dataLoader.CreateBatches(batchSize: 32)
    .Shuffled()
    .DropLast()
    .WithSeed(42);

// Iterate
foreach (var batch in batches)
{
    // Process batch
}