Table of Contents

Class AiModelBuilder<T, TInput, TOutput>

Namespace
AiDotNet
Assembly
AiDotNet.dll

Computer vision extensions for AiModelBuilder.

public class AiModelBuilder<T, TInput, TOutput> : IAiModelBuilder<T, TInput, TOutput>

Type Parameters

T

The numeric type used for calculations.

TInput

The input type.

TOutput

The output type.

Inheritance
AiModelBuilder<T, TInput, TOutput>
Implements
IAiModelBuilder<T, TInput, TOutput>
Inherited Members

Remarks

This class uses the builder pattern to configure various components of a machine learning model before building and using it for predictions.

For Beginners: Think of this class as a recipe builder for creating AI models. You add different ingredients (like data normalization, feature selection, etc.) and then "cook" (build) the final model. This approach makes it easy to customize your model without having to understand all the complex details at once.

Training Infrastructure Example: Complete example showing experiment tracking, checkpointing, model registry, and hyperparameter optimization working together:

// 1. Create training infrastructure components
var experimentTracker = new ExperimentTracker<double>("./mlruns");
var checkpointManager = new CheckpointManager<double, double[], double>("./checkpoints");
var modelRegistry = new ModelRegistry<double, double[], double>("./models");
var bayesianOptimizer = new BayesianOptimizer<double, double[], double>(
    maximize: false,  // Minimize loss
    acquisitionFunction: AcquisitionFunctionType.ExpectedImprovement,
    nInitialPoints: 5,
    seed: 42);

// 2. Create an experiment and start a run
var experimentId = experimentTracker.CreateExperiment(
    "image-classification",
    description: "CNN training with hyperparameter tuning",
    tags: new Dictionary<string, string> { ["team"] = "ml-research" });

var run = experimentTracker.StartRun(experimentId, "baseline-run");
run.LogParameters(new Dictionary<string, object>
{
    ["learning_rate"] = 0.001,
    ["batch_size"] = 32,
    ["epochs"] = 100
});

// 3. Configure the builder with training infrastructure
var builder = new AiModelBuilder<double, double[], double>()
    .ConfigureModel(neuralNetwork)
    .ConfigureOptimizer(adamOptimizer)
    .ConfigureExperimentTracker(experimentTracker)
    .ConfigureCheckpointManager(checkpointManager)
    .ConfigureModelRegistry(modelRegistry)
    .ConfigureHyperparameterOptimizer(bayesianOptimizer);

// 4. Train and track progress
for (int epoch = 0; epoch < 100; epoch++)
{
    // Train epoch...
    var loss = TrainEpoch(model, data);
    var accuracy = Evaluate(model, validationData);

    // Log metrics to experiment tracker
    run.LogMetric("loss", loss, step: epoch);
    run.LogMetric("accuracy", accuracy, step: epoch);

    // Save checkpoint periodically
    if (epoch % 10 == 0)
    {
        checkpointManager.SaveCheckpoint(
            model, optimizer, epoch, totalSteps,
            new Dictionary<string, double> { ["loss"] = loss, ["accuracy"] = accuracy });
    }
}

// 5. Complete the run and register the model
run.Complete();

var modelVersion = modelRegistry.RegisterModel(
    model, "cnn-classifier", ModelType.NeuralNetwork,
    new Dictionary<string, object> { ["final_accuracy"] = 0.95 });

// 6. Promote model to production
modelRegistry.TransitionStage(modelVersion.ModelId, modelVersion.Version, ModelStage.Production);

Properties

DetectionVisualizer

Gets the detection visualizer.

public DetectionVisualizer<T>? DetectionVisualizer { get; }

Property Value

DetectionVisualizer<T>

InstanceSegmenter

Gets the configured instance segmenter.

public InstanceSegmenterBase<T>? InstanceSegmenter { get; }

Property Value

InstanceSegmenterBase<T>

MaskVisualizer

Gets the mask visualizer.

public MaskVisualizer<T>? MaskVisualizer { get; }

Property Value

MaskVisualizer<T>

OCRVisualizer

Gets the OCR visualizer.

public OCRVisualizer<T>? OCRVisualizer { get; }

Property Value

OCRVisualizer<T>

ObjectDetector

Gets the configured object detector.

public ObjectDetectorBase<T>? ObjectDetector { get; }

Property Value

ObjectDetectorBase<T>

ObjectTracker

Gets the configured object tracker.

public ObjectTrackerBase<T>? ObjectTracker { get; }

Property Value

ObjectTrackerBase<T>

SceneTextReader

Gets the configured scene text reader.

public SceneTextReader<T>? SceneTextReader { get; }

Property Value

SceneTextReader<T>

Methods

AskAgentAsync(string)

Asks the agent a question about your model building process. Only available after calling ConfigureAgentAssistance().

public Task<string> AskAgentAsync(string question)

Parameters

question string

Natural language question to ask the agent.

Returns

Task<string>

The agent's answer based on your current configuration.

Remarks

For Beginners: Use this to get AI-powered advice during model building.

Example:

var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureAgentAssistance(apiKey: "sk-...");

var advice = await builder.AskAgentAsync(
    "Should I use Ridge or Lasso regression for my dataset with 50 features?");
Console.WriteLine(advice);

Exceptions

InvalidOperationException

Thrown if ConfigureAgentAssistance() hasn't been called.

BuildAsync()

Builds a predictive model using data from ConfigureDataLoader() or meta-learning from ConfigureMetaLearning().

public Task<AiModelResult<T, TInput, TOutput>> BuildAsync()

Returns

Task<AiModelResult<T, TInput, TOutput>>

A task that represents the asynchronous operation, containing the trained model.

Remarks

For Beginners: Use this method when you've configured either:

  • A data loader (via ConfigureDataLoader) - the loader provides the training data
  • Meta-learning (via ConfigureMetaLearning) - trains your model to learn NEW tasks quickly

Data Loader Path:

  • LoadAsync() is called on the data loader
  • Features and Labels are extracted from the loader
  • Training proceeds using the loaded data

Meta-Learning Path:

  • Trains a model that can quickly adapt to new tasks
  • Uses episodic data from the meta-learner configuration

Example with data loader:

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureDataLoader(DataLoaders.FromArrays(features, labels))
    .ConfigureModel(model)
    .BuildAsync();

Example with meta-learning:

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureMetaLearning(metaLearner)
    .BuildAsync();

Exceptions

InvalidOperationException

Thrown when neither ConfigureDataLoader() nor ConfigureMetaLearning() has been called.

ConfigureABTesting(ABTestingConfig?)

Configures A/B testing to compare multiple model versions by splitting traffic.

public IAiModelBuilder<T, TInput, TOutput> ConfigureABTesting(ABTestingConfig? config = null)

Parameters

config ABTestingConfig

The A/B testing configuration (optional, disables A/B testing if null).

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: A/B testing lets you safely test a new model version on a small percentage of users before fully deploying it. For example, you might send 10% of traffic to a new model and 90% to the current model, then compare performance metrics to decide which is better.

This is useful for:

  • Testing new models in production safely
  • Gradually rolling out changes
  • Making data-driven decisions about which model to use

Example:

// 90% on v1.0 (stable), 10% on v2.0 (experimental)
var abConfig = new ABTestingConfig
{
    Enabled = true,
    TrafficSplit = new Dictionary<string, double> { { "1.0", 0.9 }, { "2.0", 0.1 } },
    ControlVersion = "1.0"
};
var result = await builder
    .ConfigureModel(model)
    .ConfigureABTesting(abConfig)
    .BuildAsync();

ConfigureAdversarialRobustness(AdversarialRobustnessConfiguration<T, TInput, TOutput>?)

Configures adversarial robustness and AI safety features for the model.

public IAiModelBuilder<T, TInput, TOutput> ConfigureAdversarialRobustness(AdversarialRobustnessConfiguration<T, TInput, TOutput>? configuration = null)

Parameters

configuration AdversarialRobustnessConfiguration<T, TInput, TOutput>

The adversarial robustness configuration. When null, uses industry-standard defaults.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

This unified configuration provides comprehensive control over all aspects of adversarial robustness and AI safety:

  • Safety FilteringInput validation and output filtering for harmful content
  • Adversarial AttacksFGSM, PGD, CW, AutoAttack for robustness testing
  • Adversarial DefensesAdversarial training, input preprocessing, ensemble methods
  • Certified RobustnessRandomized smoothing, IBP, CROWN for provable guarantees
  • Content ModerationPrompt injection detection, PII filtering for LLMs
  • Red TeamingAutomated adversarial prompt generation for evaluation

For Beginners: This is your one-stop configuration for making your model safe and robust. When called with no parameters (null), industry-standard defaults are applied automatically. You can use factory methods like AdversarialRobustnessConfiguration.BasicSafety() for common setups, or customize individual options for your specific needs.

// Use industry-standard defaults
builder.ConfigureAdversarialRobustness();

// Basic safety filtering
builder.ConfigureAdversarialRobustness(AdversarialRobustnessConfiguration<double, Vector<double>, int>.BasicSafety());

// Comprehensive robustness with certified guarantees
builder.ConfigureAdversarialRobustness(AdversarialRobustnessConfiguration<double, Vector<double>, int>.Comprehensive());

// LLM safety with content moderation
builder.ConfigureAdversarialRobustness(AdversarialRobustnessConfiguration<double, string, string>.ForLLM());

// Custom configuration
builder.ConfigureAdversarialRobustness(new AdversarialRobustnessConfiguration<double, Vector<double>, int>
{
    Enabled = true,
    Options = new AdversarialRobustnessOptions<double>
    {
        EnableSafetyFiltering = true,
        EnableAdversarialTraining = true,
        EnableCertifiedRobustness = true
    },
    UseCertifiedInference = true
});

ConfigureAgentAssistance(AgentConfiguration<T>)

Enables AI agent assistance during the model building process.

public IAiModelBuilder<T, TInput, TOutput> ConfigureAgentAssistance(AgentConfiguration<T> configuration)

Parameters

configuration AgentConfiguration<T>

The agent configuration containing API key, provider, and assistance options.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: This enables an AI agent to help you during model building. By default, the agent will:

  • Analyze your data characteristics
  • Suggest appropriate model types (if you haven't chosen one)
  • Recommend hyperparameter values
  • Provide insights on feature importance

The API key is stored securely and will be reused during inference if you call AskAsync() on the trained model.

Example with defaults:

var agentConfig = new AgentConfiguration<double>
{
    ApiKey = "sk-...",
    Provider = LLMProvider.OpenAI,
    IsEnabled = true
};

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureAgentAssistance(agentConfig)
    .BuildAsync();

Example with customization:

var agentConfig = new AgentConfiguration<double>
{
    ApiKey = "sk-...",
    Provider = LLMProvider.OpenAI,
    IsEnabled = true,
    AssistanceOptions = AgentAssistanceOptions.Create()
        .EnableModelSelection()
        .EnableHyperparameterTuning()
        .DisableFeatureAnalysis()
};

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureAgentAssistance(agentConfig)
    .BuildAsync();

ConfigureAugmentation(AugmentationConfig?)

Configures data augmentation for training and inference.

public IAiModelBuilder<T, TInput, TOutput> ConfigureAugmentation(AugmentationConfig? config = null)

Parameters

config AugmentationConfig

Augmentation configuration. If null, uses industry-standard defaults with automatic data-type detection.

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

Data augmentation creates variations of training data on-the-fly to help models generalize better. This configuration covers both training-time augmentation and Test-Time Augmentation (TTA) for improved inference accuracy.

For Beginners: Augmentation is like showing the model many variations of the same data. For images, this might include rotations, flips, and color changes. The model learns to recognize objects regardless of these variations.

Key features:

  • Automatic data-type detection (image, tabular, audio, text, video)
  • Industry-standard defaults that work well out-of-the-box
  • Test-Time Augmentation (TTA) enabled by default for better predictions

Example - Simple usage with defaults:

var result = builder
    .ConfigureModel(myModel)
    .ConfigureAugmentation()  // Uses auto-detected defaults
    .Build(X, y);

Example - Custom configuration:

var result = builder
    .ConfigureModel(myModel)
    .ConfigureAugmentation(new AugmentationConfig
    {
        EnableTTA = true,
        TTANumAugmentations = 8,
        ImageSettings = new ImageAugmentationSettings
        {
            EnableFlips = true,
            EnableRotation = true,
            RotationRange = 20.0
        }
    })
    .Build(images, labels);

ConfigureAutoML(AutoMLOptions<T, TInput, TOutput>?)

Configures AutoML using facade-style options (recommended for most users).

public IAiModelBuilder<T, TInput, TOutput> ConfigureAutoML(AutoMLOptions<T, TInput, TOutput>? options = null)

Parameters

options AutoMLOptions<T, TInput, TOutput>

AutoML options (budget, strategy, and optional overrides). If null, defaults are used.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: AutoML automatically tries different models/settings to find a strong result. With this overload you only choose a budget (how much time to spend), and AiDotNet handles the rest.

ConfigureAutoML(IAutoMLModel<T, TInput, TOutput>)

Configures an AutoML model for automatic machine learning optimization.

public IAiModelBuilder<T, TInput, TOutput> ConfigureAutoML(IAutoMLModel<T, TInput, TOutput> autoMLModel)

Parameters

autoMLModel IAutoMLModel<T, TInput, TOutput>

The AutoML model instance to use for hyperparameter search and model selection.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: AutoML (Automated Machine Learning) automatically searches for the best model and hyperparameters for your problem. Instead of manually trying different models and settings, AutoML does this for you.

When you configure an AutoML model: - The Build() method will run the AutoML search process - AutoML will try different models and hyperparameters - The best model found will be returned as your trained model - You can configure search time limits, candidate models, and optimization metrics

Example:

// Advanced usage: plug in your own AutoML implementation.
// Most users should prefer the ConfigureAutoML(AutoMLOptions<...>) overload instead.
var autoML = new RandomSearchAutoML<double, Matrix<double>, Vector<double>>();
autoML.SetTimeLimit(TimeSpan.FromMinutes(30));
autoML.SetCandidateModels(new List<ModelType> { ModelType.RandomForest, ModelType.GradientBoosting });

var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>() .ConfigureAutoML(autoML) .Build(trainingData, trainingLabels);

ConfigureBenchmarking(BenchmarkingOptions?)

Configures benchmarking to run standardized benchmark suites and attach a structured report to the built model.

public IAiModelBuilder<T, TInput, TOutput> ConfigureBenchmarking(BenchmarkingOptions? options = null)

Parameters

options BenchmarkingOptions

Benchmarking options (suites, sampling, failure policy). If null, sensible defaults are used.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

This integrates benchmarking into the facade flow: users select suites via enums and receive a structured report, without wiring benchmark implementations manually.

For Beginners: This is like running a standardized test after building your model to see how it performs.

ConfigureBiasDetector(IBiasDetector<T>)

Configures the bias detector component for ethical AI evaluation.

public IAiModelBuilder<T, TInput, TOutput> ConfigureBiasDetector(IBiasDetector<T> detector)

Parameters

detector IBiasDetector<T>

The bias detector implementation to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Bias detection helps ensure your model treats different groups fairly. You can choose from different bias detection strategies like Disparate Impact (80% rule), Demographic Parity, or Equal Opportunity. This component will be used to evaluate your trained model's fairness across demographic groups.

ConfigureCaching(CacheConfig?)

Configures model caching to avoid reloading models from disk repeatedly.

public IAiModelBuilder<T, TInput, TOutput> ConfigureCaching(CacheConfig? config = null)

Parameters

config CacheConfig

The caching configuration (optional, uses default cache settings if null).

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Caching keeps frequently-used models in memory so they load instantly. Like keeping your favorite apps open on your phone instead of closing and reopening them.

Benefits:

  • Much faster inference (no model loading time)
  • Better throughput for multiple requests
  • Configurable cache size and eviction policies

Example:

// Enable caching with default settings (10 models, LRU eviction)
var result = await builder
    .ConfigureModel(model)
    .ConfigureCaching()
    .BuildAsync();

ConfigureCheckpointManager(ICheckpointManager<T, TInput, TOutput>)

Configures checkpoint management for saving and restoring training state.

public IAiModelBuilder<T, TInput, TOutput> ConfigureCheckpointManager(ICheckpointManager<T, TInput, TOutput> manager)

Parameters

manager ICheckpointManager<T, TInput, TOutput>

The checkpoint manager implementation to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Checkpoints are like save points in a video game. They let you pause training and resume later, or go back to an earlier state if something goes wrong.

ConfigureCompression(CompressionConfig?)

Configures model compression for reducing model size during serialization.

public IAiModelBuilder<T, TInput, TOutput> ConfigureCompression(CompressionConfig? config = null)

Parameters

config CompressionConfig

The compression configuration (optional, uses automatic mode if null).

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Compression makes your model smaller for storage and faster to load. When you save (serialize) your model, compression automatically reduces its size. When you load (deserialize) it, decompression happens transparently.

Benefits:

  • 50-90% smaller model files
  • Faster model loading and deployment
  • Lower storage and bandwidth costs
  • Enables deployment on resource-constrained devices

Compression is applied during serialization (saving) and reversed during deserialization (loading). You never need to handle compression manually - it happens behind the scenes.

Example:

// Use automatic compression (recommended for most cases)
var result = await builder
    .ConfigureModel(model)
    .ConfigureCompression()  // Uses industry-standard defaults
    .BuildAsync();

// Model is now configured to compress on save
builder.SaveModel(result, "model.bin");  // Compressed automatically
var loaded = builder.LoadModel("model.bin");  // Decompressed automatically

// Or customize compression settings
var result = await builder
    .ConfigureCompression(new CompressionConfig
    {
        Mode = ModelCompressionMode.Full,
        Type = CompressionType.HybridHuffmanClustering,
        NumClusters = 256
    })
    .BuildAsync();

ConfigureCrossValidation(ICrossValidator<T, TInput, TOutput>)

Configures the cross-validation strategy for automatic model evaluation during training.

public IAiModelBuilder<T, TInput, TOutput> ConfigureCrossValidation(ICrossValidator<T, TInput, TOutput> crossValidator)

Parameters

crossValidator ICrossValidator<T, TInput, TOutput>

The cross-validation strategy to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Cross-validation tests how well your model will perform on new data by training and testing it multiple times on different subsets of your training data. If you configure both a cross-validator and model evaluator (via ConfigureModelEvaluator), cross-validation will automatically run during Build() and the results will be included in your trained model.

ConfigureCurriculumLearning(CurriculumLearningOptions<T, TInput, TOutput>?)

Configures curriculum learning for training with ordered sample difficulty.

public IAiModelBuilder<T, TInput, TOutput> ConfigureCurriculumLearning(CurriculumLearningOptions<T, TInput, TOutput>? options = null)

Parameters

options CurriculumLearningOptions<T, TInput, TOutput>

Curriculum learning options (schedule type, phases, difficulty estimation). If null, sensible defaults are used (Linear schedule, 5 phases, loss-based difficulty).

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Curriculum Learning trains models by presenting samples in order of difficulty, starting with easy examples and gradually introducing harder ones. This often leads to faster convergence and better final performance compared to random training order.

ConfigureDataLoader(IDataLoader<T>)

Configures the data loader for providing training data.

public IAiModelBuilder<T, TInput, TOutput> ConfigureDataLoader(IDataLoader<T> dataLoader)

Parameters

dataLoader IDataLoader<T>

The data loader that provides training data.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: A data loader handles loading your data from various sources (files, databases, memory, URLs) and provides it in a format suitable for model training.

You can use:

  • IInputOutputDataLoader for standard supervised learning (features + labels)
  • IGraphDataLoader for graph neural networks
  • IEpisodicDataLoader for meta-learning

Example:

// Simple in-memory data
var loader = DataLoaders.FromArrays(features, labels);

// Or graph data
var graphLoader = new CitationNetworkLoader("cora");

var result = await builder
    .ConfigureDataLoader(loader)
    .ConfigureModel(model)
    .BuildAsync();

ConfigureDataPreprocessor(IDataPreprocessor<T, TInput, TOutput>)

Configures how the data should be preprocessed before training.

public IAiModelBuilder<T, TInput, TOutput> ConfigureDataPreprocessor(IDataPreprocessor<T, TInput, TOutput> dataPreprocessor)

Parameters

dataPreprocessor IDataPreprocessor<T, TInput, TOutput>

The data preprocessing strategy to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Data preprocessing cleans and prepares your raw data before feeding it to the model. It's like washing and cutting vegetables before cooking. This might include handling missing values, converting text to numbers, or combining related features.

ConfigureDataVersionControl(IDataVersionControl<T>)

Configures data version control for tracking dataset changes.

public IAiModelBuilder<T, TInput, TOutput> ConfigureDataVersionControl(IDataVersionControl<T> dataVersionControl)

Parameters

dataVersionControl IDataVersionControl<T>

The data version control implementation to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Data version control is like Git, but for your datasets. It tracks what data was used for training each model and lets you reproduce experiments.

ConfigureDistributedTraining(ICommunicationBackend<T>?, DistributedStrategy, IShardingConfiguration<T>?)

Configures distributed training across multiple GPUs or machines.

public IAiModelBuilder<T, TInput, TOutput> ConfigureDistributedTraining(ICommunicationBackend<T>? backend = null, DistributedStrategy strategy = DistributedStrategy.DDP, IShardingConfiguration<T>? configuration = null)

Parameters

backend ICommunicationBackend<T>

Communication backend to use. If null, uses InMemoryCommunicationBackend.

strategy DistributedStrategy

Distributed training strategy. Default is DDP.

configuration IShardingConfiguration<T>

Optional sharding configuration for advanced settings like gradient compression, parameter grouping, etc.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

When distributed training is configured, the Build() method will automatically wrap the model and optimizer with their distributed counterparts based on the chosen strategy. This enables training across multiple GPUs or machines with automatic parameter sharding and gradient synchronization.

For Beginners: This enables distributed training across multiple GPUs or machines. You can call it with no parameters for sensible defaults, or customize as needed.

When you configure this, the builder automatically handles all the complexity:

  • Your model gets split across GPUs (parameter sharding)
  • Gradients are synchronized automatically
  • Training is coordinated across all processes

You just train as normal - the distributed magic happens behind the scenes!

ConfigureExperimentTracker(IExperimentTracker<T>)

Configures experiment tracking for logging and organizing training runs.

public IAiModelBuilder<T, TInput, TOutput> ConfigureExperimentTracker(IExperimentTracker<T> tracker)

Parameters

tracker IExperimentTracker<T>

The experiment tracker implementation to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: An experiment tracker is like a lab notebook for your ML experiments. It logs parameters, metrics, and artifacts so you can compare runs and reproduce results.

ConfigureExport(ExportConfig?)

Configures export settings for deploying the model to different platforms.

public IAiModelBuilder<T, TInput, TOutput> ConfigureExport(ExportConfig? config = null)

Parameters

config ExportConfig

The export configuration (optional, uses CPU/ONNX if null).

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Export settings determine how your trained model will be saved for deployment. Different platforms need different formats:

  • ONNX: Universal format, works everywhere (recommended)
  • TensorRT: NVIDIA GPUs, maximum performance
  • CoreML: Apple devices (iPhone, iPad, Mac)
  • TFLite: Android devices and edge hardware
  • WASM: Run models in web browsers

Configure this BEFORE training if you know your target platform, so the model can be optimized accordingly. After training, use the Export methods on AiModelResult.

Example:

// Configure for TensorRT deployment with FP16 quantization
var exportConfig = new ExportConfig
{
    TargetPlatform = TargetPlatform.TensorRT,
    Quantization = QuantizationMode.Float16
};
var result = await builder
    .ConfigureModel(model)
    .ConfigureExport(exportConfig)
    .BuildAsync();

// After training, export the model
result.ExportToTensorRT("model.trt");

ConfigureFairnessEvaluator(IFairnessEvaluator<T>)

Configures the fairness evaluator component for ethical AI evaluation.

public IAiModelBuilder<T, TInput, TOutput> ConfigureFairnessEvaluator(IFairnessEvaluator<T> evaluator)

Parameters

evaluator IFairnessEvaluator<T>

The fairness evaluator implementation to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Fairness evaluation measures how equitably your model performs. You can choose evaluators that compute different sets of fairness metrics, from basic (just key metrics) to comprehensive (all fairness measures). This helps ensure your AI system is not only accurate but also ethical.

ConfigureFeatureSelector(IFeatureSelector<T, TInput>)

Configures which features (input variables) should be used in the model.

public IAiModelBuilder<T, TInput, TOutput> ConfigureFeatureSelector(IFeatureSelector<T, TInput> selector)

Parameters

selector IFeatureSelector<T, TInput>

The feature selection strategy to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Sometimes, not all of your data is useful for making predictions. Feature selection helps pick out which parts of your data are most important. For example, when predicting house prices, the number of bedrooms might be important, but the house's street number probably isn't.

ConfigureFederatedLearning(FederatedLearningOptions, IAggregationStrategy<IFullModel<T, TInput, TOutput>>?, IClientSelectionStrategy?, IFederatedServerOptimizer<T>?, IFederatedHeterogeneityCorrection<T>?, IHomomorphicEncryptionProvider<T>?)

Enables federated learning training using the provided options.

public IAiModelBuilder<T, TInput, TOutput> ConfigureFederatedLearning(FederatedLearningOptions options, IAggregationStrategy<IFullModel<T, TInput, TOutput>>? aggregationStrategy = null, IClientSelectionStrategy? clientSelectionStrategy = null, IFederatedServerOptimizer<T>? serverOptimizer = null, IFederatedHeterogeneityCorrection<T>? heterogeneityCorrection = null, IHomomorphicEncryptionProvider<T>? homomorphicEncryptionProvider = null)

Parameters

options FederatedLearningOptions

Federated learning configuration options.

aggregationStrategy IAggregationStrategy<IFullModel<T, TInput, TOutput>>

Optional aggregation strategy override (null uses defaults based on options).

clientSelectionStrategy IClientSelectionStrategy

Optional client selection strategy override (null uses defaults based on options).

serverOptimizer IFederatedServerOptimizer<T>

Optional server-side optimizer override (null uses defaults based on options).

heterogeneityCorrection IFederatedHeterogeneityCorrection<T>
homomorphicEncryptionProvider IHomomorphicEncryptionProvider<T>

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

ConfigureFewShotExampleSelector(IFewShotExampleSelector<T>?)

Configures the few-shot example selector for selecting examples to include in prompts.

public IAiModelBuilder<T, TInput, TOutput> ConfigureFewShotExampleSelector(IFewShotExampleSelector<T>? selector = null)

Parameters

selector IFewShotExampleSelector<T>

The few-shot example selector to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

A few-shot example selector chooses the most relevant examples to include in prompts based on the current query. Different strategies include random selection, fixed order, and similarity-based selection.

For Beginners: Few-shot learning teaches the model by showing it examples. The selector picks which examples to show for each new query.

Example:

var selector = new RandomExampleSelector<double>(seed: 42);
selector.AddExample(new FewShotExample { Input = "Hello", Output = "Hola" });
selector.AddExample(new FewShotExample { Input = "Goodbye", Output = "Adiós" });

var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureFewShotExampleSelector(selector)
    .ConfigureModel(model);

ConfigureFineTuning(FineTuningConfiguration<T, TInput, TOutput>?)

Configures fine-tuning for the model using preference learning, RLHF, or other alignment methods.

public IAiModelBuilder<T, TInput, TOutput> ConfigureFineTuning(FineTuningConfiguration<T, TInput, TOutput>? configuration = null)

Parameters

configuration FineTuningConfiguration<T, TInput, TOutput>

The fine-tuning configuration including training data. When null, uses industry-standard defaults.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

This configuration enables post-training fine-tuning using various alignment techniques:

  • Supervised Fine-Tuning (SFT)Traditional fine-tuning on labeled examples
  • Direct Preference Optimization (DPO)Learn from human preferences without reward models
  • Simple Preference Optimization (SimPO)Reference-free, length-normalized preference learning
  • Group Relative Policy Optimization (GRPO)Memory-efficient RL without critic models
  • Odds Ratio Preference Optimization (ORPO)Combined SFT + preference in one step
  • Identity Preference Optimization (IPO)Regularized preference optimization
  • Kahneman-Tversky Optimization (KTO)Utility-maximizing preference learning
  • Contrastive Preference Optimization (CPO)Contrastive learning for preferences
  • Constitutional AI (CAI)Self-improvement with constitutional principles
  • Reinforcement Learning from Human Feedback (RLHF)Classic PPO-based alignment

For Beginners: Fine-tuning helps align your model with human preferences. When called with no parameters (null), industry-standard defaults are applied automatically. Training data should be set in the configuration's TrainingData property. Use factory methods like FineTuningConfiguration.ForDPO(data) for quick setup. DPO and SimPO are simpler (no reward model needed), while RLHF and GRPO provide more control.

// Use industry-standard defaults (training data set separately)
builder.ConfigureFineTuning();

// DPO fine-tuning with preference pairs
var preferenceData = new FineTuningData<double, string, string>
{
    Inputs = prompts,
    ChosenOutputs = preferredResponses,
    RejectedOutputs = rejectedResponses
};
builder.ConfigureFineTuning(FineTuningConfiguration<double, string, string>.ForDPO(preferenceData));

// GRPO for RL-based alignment
var rlData = new FineTuningData<double, string, string>
{
    Inputs = prompts,
    Rewards = rewardScores
};
builder.ConfigureFineTuning(FineTuningConfiguration<double, string, string>.ForGRPO(rlData));

// Custom fine-tuning configuration
builder.ConfigureFineTuning(new FineTuningConfiguration<double, Vector<double>, int>
{
    Enabled = true,
    Options = new FineTuningOptions<double>
    {
        MethodType = FineTuningMethodType.SimPO,
        LearningRate = 1e-5,
        Epochs = 3,
        SimPOGamma = 1.0
    },
    TrainingData = myPreferenceData
});

ConfigureFitDetector(IFitDetector<T, TInput, TOutput>)

Configures how to detect if the model is overfitting or underfitting.

public IAiModelBuilder<T, TInput, TOutput> ConfigureFitDetector(IFitDetector<T, TInput, TOutput> detector)

Parameters

detector IFitDetector<T, TInput, TOutput>

The fit detection strategy to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: This helps detect if your model is learning too much from the training data (overfitting) or not learning enough (underfitting). It's like having a teacher who can tell if a student is just memorizing answers or not studying enough.

ConfigureFitnessCalculator(IFitnessCalculator<T, TInput, TOutput>)

Configures how to measure the model's performance.

public IAiModelBuilder<T, TInput, TOutput> ConfigureFitnessCalculator(IFitnessCalculator<T, TInput, TOutput> calculator)

Parameters

calculator IFitnessCalculator<T, TInput, TOutput>

The fitness calculation strategy to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: This determines how we score how well our model is doing. Different problems might need different scoring methods. For example, when predicting house prices, we might care about the average error in dollars, but when predicting if an email is spam, we might care more about the percentage of emails correctly classified.

ConfigureGpuAcceleration(GpuAccelerationConfig?)

public IAiModelBuilder<T, TInput, TOutput> ConfigureGpuAcceleration(GpuAccelerationConfig? config = null)

Parameters

config GpuAccelerationConfig

Returns

IAiModelBuilder<T, TInput, TOutput>

ConfigureHyperparameterOptimizer(IHyperparameterOptimizer<T, TInput, TOutput>, HyperparameterSearchSpace?, int)

Configures hyperparameter optimization for automatic tuning of model settings.

public IAiModelBuilder<T, TInput, TOutput> ConfigureHyperparameterOptimizer(IHyperparameterOptimizer<T, TInput, TOutput> optimizer, HyperparameterSearchSpace? searchSpace = null, int nTrials = 10)

Parameters

optimizer IHyperparameterOptimizer<T, TInput, TOutput>

The hyperparameter optimizer implementation to use.

searchSpace HyperparameterSearchSpace

The hyperparameter search space defining parameter ranges. If null, hyperparameter optimization is disabled.

nTrials int

Number of trials to run. Default is 10.

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Hyperparameter optimization automatically finds the best settings for your model (like learning rate, number of layers, etc.) instead of you having to guess.

ConfigureInferenceOptimizations(InferenceOptimizationConfig?)

Configures inference-time optimizations for faster predictions.

public IAiModelBuilder<T, TInput, TOutput> ConfigureInferenceOptimizations(InferenceOptimizationConfig? config = null)

Parameters

config InferenceOptimizationConfig

Inference optimization configuration (optional, uses defaults if null).

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Inference optimization makes your model's predictions faster and more efficient.

Key features enabled:

  • KV Cache: Speeds up transformer/attention models by 2-10x
  • Batching: Groups predictions for higher throughput
  • Speculative Decoding: Speeds up text generation by 1.5-3x

Example:

var result = await new AiModelBuilder<double, ...>()
    .ConfigureModel(myModel)
    .ConfigureInferenceOptimizations()  // Uses sensible defaults
    .BuildAsync();

// Or with custom settings:
var config = new InferenceOptimizationConfig
{
    EnableKVCache = true,
    MaxBatchSize = 64,
    EnableSpeculativeDecoding = true
};

var result = await builder
    .ConfigureInferenceOptimizations(config)
    .BuildAsync();

ConfigureInstanceSegmenter(InstanceSegmenterBase<T>)

Configures an instance segmenter for the builder.

public AiModelBuilder<T, TInput, TOutput> ConfigureInstanceSegmenter(InstanceSegmenterBase<T> segmenter)

Parameters

segmenter InstanceSegmenterBase<T>

The instance segmenter to use.

Returns

AiModelBuilder<T, TInput, TOutput>

The builder for method chaining.

Remarks

For Beginners: Instance segmentation provides pixel-level masks for each detected object, not just bounding boxes.

ConfigureJitCompilation(JitCompilationConfig?)

Configures JIT (Just-In-Time) compilation for accelerated model inference.

public IAiModelBuilder<T, TInput, TOutput> ConfigureJitCompilation(JitCompilationConfig? config = null)

Parameters

config JitCompilationConfig

The JIT compilation configuration. If null, uses default settings with JIT enabled.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

JIT compilation converts your model's computation graph into optimized native code, providing significant performance improvements (5-10x faster) for inference. The compilation happens once during model building, then the optimized code is reused for all predictions.

For Beginners: JIT compilation makes your model's predictions much faster by "pre-compiling" the calculations into optimized code before you start using it.

Benefits:

  • 2-3x faster for simple operations
  • 5-10x faster for complex models
  • Automatic operation fusion and optimization
  • Near-zero overhead for cached compilations

When to use JIT:

  • Production inference (maximize speed)
  • Batch processing (repeated predictions)
  • Large or complex models (more optimization opportunities)

When NOT to use JIT:

  • Training (JIT is for inference only)
  • Very simple models (compilation overhead exceeds benefits)
  • Models with dynamic structure

Important: Your model must implement IJitCompilable to support JIT compilation. Currently, models built with TensorOperations computation graphs are supported. Neural networks using layer-based architecture will be supported in a future update.

Example usage:

var result = await new AiModelBuilder<double, Tensor<double>, Tensor<double>>()
    .ConfigureModel(myModel)
    .ConfigureJitCompilation(new JitCompilationConfig
    {
        Enabled = true,
        CompilerOptions = new JitCompilerOptions
        {
            EnableOperationFusion = true,     // Biggest performance gain
            EnableDeadCodeElimination = true,
            EnableConstantFolding = true,
            EnableCaching = true
        },
        ThrowOnFailure = false  // Graceful fallback if JIT not supported
    })
    .BuildAsync();

// Predictions now use JIT-compiled code (5-10x faster!)
var prediction = result.Predict(newData);

Simple usage (uses defaults):

var result = await new AiModelBuilder<double, Tensor<double>, Tensor<double>>()
    .ConfigureModel(myModel)
    .ConfigureJitCompilation()  // Enables JIT with default settings
    .BuildAsync();

ConfigureKnowledgeDistillation(KnowledgeDistillationOptions<T, TInput, TOutput>?)

Configures knowledge distillation to train a smaller, faster student model from a larger teacher model.

public IAiModelBuilder<T, TInput, TOutput> ConfigureKnowledgeDistillation(KnowledgeDistillationOptions<T, TInput, TOutput>? options = null)

Parameters

options KnowledgeDistillationOptions<T, TInput, TOutput>

The knowledge distillation configuration options.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Knowledge distillation is a technique to compress a large, accurate "teacher" model into a smaller, faster "student" model while preserving most of the teacher's accuracy. Think of it like an expert (teacher) teaching a student - the student learns not just the answers, but also the reasoning process.

Benefits: - **Model Compression**: 40-90% size reduction with 90-97% accuracy preserved - **Faster Inference**: Smaller models run 2-10x faster - **Edge Deployment**: Deploy on mobile devices, IoT, browsers - **Cost Reduction**: Lower compute and memory costs

Common Use Cases: - Deploy BERT/GPT models on mobile devices (DistilBERT is 40% smaller, 60% faster) - Run vision models on edge devices (MobileNet distilled from ResNet) - Reduce cloud compute costs for inference - Multi-teacher ensembles distilled into single student

Quick Start Example:

// Configure knowledge distillation with default settings (good for most cases)
var distillationOptions = new KnowledgeDistillationOptions<Vector<double>, Vector<double>, double>
{
    TeacherModelType = TeacherModelType.NeuralNetwork,
    StrategyType = DistillationStrategyType.ResponseBased,
    Temperature = 3.0,      // Soften predictions (2-5 typical)
    Alpha = 0.3,            // 30% hard labels, 70% teacher knowledge
    Epochs = 20,
    BatchSize = 32,
    LearningRate = 0.001
};

var result = await new AiModelBuilder<double, Vector<double>, Vector<double>>()
    .ConfigureModel(studentModel)
    .ConfigureKnowledgeDistillation(distillationOptions)
    .BuildAsync();

Advanced Techniques: - **Response-Based**: Standard Hinton distillation (recommended start) - **Feature-Based**: Match intermediate layer representations - **Attention-Based**: For transformers (BERT, GPT) - **Relational**: Preserve relationships between samples - **Self-Distillation**: Model teaches itself for better calibration - **Ensemble**: Multiple teachers for richer knowledge

Key Parameters: - **Temperature** (2-5): Higher = softer predictions, more knowledge transfer - **Alpha** (0.2-0.5): Lower = rely more on teacher, higher = rely more on labels - **Strategy**: ResponseBased (standard), FeatureBased (deeper), AttentionBased (transformers) - **Teacher Type**: NeuralNetwork (single), Ensemble (multiple), Self (no separate teacher)

Success Stories: - DistilBERT: 40% smaller than BERT, 97% performance, 60% faster - TinyBERT: 7.5x smaller than BERT for mobile deployment - MobileNet: Distilled from ResNet, 10x fewer parameters - SqueezeNet: AlexNet-level accuracy at 50x smaller size

References: - Hinton et al. (2015). Distilling the Knowledge in a Neural Network - Sanh et al. (2019). DistilBERT - Park et al. (2019). Relational Knowledge Distillation

ConfigureLoRA(ILoRAConfiguration<T>)

Configures LoRA (Low-Rank Adaptation) for parameter-efficient fine-tuning.

public IAiModelBuilder<T, TInput, TOutput> ConfigureLoRA(ILoRAConfiguration<T> loraConfiguration)

Parameters

loraConfiguration ILoRAConfiguration<T>

The LoRA configuration implementation to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: LoRA enables parameter-efficient fine-tuning by adding small "correction layers" to your neural network. This lets you adapt large pre-trained models with 100x fewer parameters, making fine-tuning much faster and more memory-efficient. The configuration determines which layers get LoRA adaptations and how they behave (rank, scaling, freezing).

ConfigureMemoryManagement(TrainingMemoryConfig?)

Configures memory management for training including gradient checkpointing, activation pooling, and model sharding.

public IAiModelBuilder<T, TInput, TOutput> ConfigureMemoryManagement(TrainingMemoryConfig? configuration = null)

Parameters

configuration TrainingMemoryConfig

The memory configuration to use. If null, uses default settings.

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Training large neural networks requires a lot of memory. Memory management helps you train bigger models by:

  • Gradient Checkpointing: Trades compute for memory by recomputing activations during backpropagation instead of storing them all.
  • Activation Pooling: Reuses memory buffers to reduce garbage collection.
  • Model Sharding: Splits large models across multiple GPUs.

Available Presets:

  • TrainingMemoryConfig.MemoryEfficient() - Maximum memory savings
  • TrainingMemoryConfig.SpeedOptimized() - Maximum speed
  • TrainingMemoryConfig.MultiGpu(n) - Multi-GPU training
  • TrainingMemoryConfig.ForTransformers() - Optimized for transformers
  • TrainingMemoryConfig.ForConvNets() - Optimized for CNNs
// Using a preset configuration
builder.ConfigureMemoryManagement(TrainingMemoryConfig.MemoryEfficient());

// Using a custom configuration
builder.ConfigureMemoryManagement(new TrainingMemoryConfig
{
    UseGradientCheckpointing = true,
    CheckpointEveryNLayers = 2,
    UseActivationPooling = true,
    MaxPoolMemoryMB = 2048
});

// Multi-GPU training
builder.ConfigureMemoryManagement(TrainingMemoryConfig.MultiGpu(4));

ConfigureMetaLearning(IMetaLearner<T, TInput, TOutput>)

Configures a meta-learning algorithm for training models that can quickly adapt to new tasks.

public IAiModelBuilder<T, TInput, TOutput> ConfigureMetaLearning(IMetaLearner<T, TInput, TOutput> metaLearner)

Parameters

metaLearner IMetaLearner<T, TInput, TOutput>

The meta-learning algorithm to use (e.g., ReptileTrainer).

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: If you configure this, Build() will do meta-training instead of regular training. The meta-learner should be created with all its dependencies (model, loss function, episodic data loader).

ConfigureMixedPrecision(MixedPrecisionConfig?)

public IAiModelBuilder<T, TInput, TOutput> ConfigureMixedPrecision(MixedPrecisionConfig? config = null)

Parameters

config MixedPrecisionConfig

Returns

IAiModelBuilder<T, TInput, TOutput>

ConfigureModel(IFullModel<T, TInput, TOutput>)

Configures the core algorithm to use for predictions.

public IAiModelBuilder<T, TInput, TOutput> ConfigureModel(IFullModel<T, TInput, TOutput> model)

Parameters

model IFullModel<T, TInput, TOutput>

The prediction algorithm to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: This is the main "brain" of your AI model - the algorithm that will learn patterns from your data and make predictions. Different algorithms work better for different types of problems, so you can choose the one that fits your needs.

ConfigureModelEvaluator(IModelEvaluator<T, TInput, TOutput>)

Configures the model evaluator component for comprehensive model evaluation and cross-validation.

public IAiModelBuilder<T, TInput, TOutput> ConfigureModelEvaluator(IModelEvaluator<T, TInput, TOutput> evaluator)

Parameters

evaluator IModelEvaluator<T, TInput, TOutput>

The model evaluator implementation to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: The model evaluator helps you understand how well your model performs. If you configure both a model evaluator and cross-validator (via ConfigureCrossValidation), cross-validation will automatically run during Build() on your training data, and the results will be included in your trained model.

ConfigureModelRegistry(IModelRegistry<T, TInput, TOutput>)

Configures model registry for centralized model storage and versioning.

public IAiModelBuilder<T, TInput, TOutput> ConfigureModelRegistry(IModelRegistry<T, TInput, TOutput> registry)

Parameters

registry IModelRegistry<T, TInput, TOutput>

The model registry implementation to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: A model registry is like a library for your trained models. It keeps track of all your models, their versions, and which ones are in production.

ConfigureNormalizer(INormalizer<T, TInput, TOutput>)

Configures how the input data should be normalized (scaled).

public IAiModelBuilder<T, TInput, TOutput> ConfigureNormalizer(INormalizer<T, TInput, TOutput> normalizer)

Parameters

normalizer INormalizer<T, TInput, TOutput>

The normalization strategy to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Normalization makes sure all your data is on a similar scale. For example, if you have data about people's ages (0-100) and incomes ($0-$1,000,000), normalization might scale both to ranges like 0-1 so the model doesn't think income is 10,000 times more important than age just because the numbers are bigger.

ConfigureObjectDetector(ObjectDetectorBase<T>)

Configures an object detector for the builder.

public AiModelBuilder<T, TInput, TOutput> ConfigureObjectDetector(ObjectDetectorBase<T> detector)

Parameters

detector ObjectDetectorBase<T>

The object detector to use.

Returns

AiModelBuilder<T, TInput, TOutput>

The builder for method chaining.

Examples

var builder = new AiModelBuilder<float, Tensor<float>, DetectionResult<float>>()
    .ConfigureObjectDetector(new YOLOv8<float>(options))
    .Build();

Remarks

For Beginners: Use this to add object detection capabilities to your model. The detector will identify and locate objects in images.

ConfigureObjectTracker(ObjectTrackerBase<T>)

Configures an object tracker for video tracking.

public AiModelBuilder<T, TInput, TOutput> ConfigureObjectTracker(ObjectTrackerBase<T> tracker)

Parameters

tracker ObjectTrackerBase<T>

The object tracker to use.

Returns

AiModelBuilder<T, TInput, TOutput>

The builder for method chaining.

Remarks

For Beginners: Object tracking maintains identity of objects across video frames, assigning consistent IDs.

ConfigureOptimizer(IOptimizer<T, TInput, TOutput>)

Configures the optimization algorithm to find the best model parameters.

public IAiModelBuilder<T, TInput, TOutput> ConfigureOptimizer(IOptimizer<T, TInput, TOutput> optimizationAlgorithm)

Parameters

optimizationAlgorithm IOptimizer<T, TInput, TOutput>

The optimization algorithm to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: The optimizer helps find the best settings for your model. It's like having someone adjust the knobs on a radio to get the clearest signal. The optimizer tries different settings and keeps the ones that work best.

ConfigureOutlierRemoval(IOutlierRemoval<T, TInput, TOutput>)

Configures how to detect and handle outliers in the data.

public IAiModelBuilder<T, TInput, TOutput> ConfigureOutlierRemoval(IOutlierRemoval<T, TInput, TOutput> outlierRemoval)

Parameters

outlierRemoval IOutlierRemoval<T, TInput, TOutput>

The outlier removal strategy to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Outliers are unusual data points that are very different from the rest of your data. For example, if you're analyzing house prices and most are between $100,000-$500,000, a $10,000,000 mansion would be an outlier. These unusual points can sometimes confuse the model, so we might want to handle them specially.

ConfigurePostprocessing(IDataTransformer<T, TOutput, TOutput>?)

Configures the output postprocessing using a single transformer.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePostprocessing(IDataTransformer<T, TOutput, TOutput>? transformer = null)

Parameters

transformer IDataTransformer<T, TOutput, TOutput>

Returns

IAiModelBuilder<T, TInput, TOutput>

Remarks

Use this overload when you only need a single postprocessing transformer. For multiple transformers, use the overload that takes an Action to build a pipeline.

For Beginners: This is a simple way to add just one postprocessing step:

builder.ConfigurePostprocessing(new SoftmaxTransformer<double>());
If you need multiple steps, use the pipeline builder overload instead.

ConfigurePostprocessing(PostprocessingPipeline<T, TOutput, TOutput>?)

Configures the output postprocessing using an existing pipeline.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePostprocessing(PostprocessingPipeline<T, TOutput, TOutput>? pipeline = null)

Parameters

pipeline PostprocessingPipeline<T, TOutput, TOutput>

Returns

IAiModelBuilder<T, TInput, TOutput>

Remarks

Use this overload when you have a pre-configured PostprocessingPipeline instance. If null is passed, an empty postprocessing pipeline will be created.

For Beginners: Use this when you've already created a pipeline elsewhere:

var myPipeline = new PostprocessingPipeline<double, Vector<double>, Vector<double>>()
    .Add(new SoftmaxTransformer<double>());

builder.ConfigurePostprocessing(myPipeline);

ConfigurePostprocessing(Action<PostprocessingPipeline<T, TOutput, TOutput>>?)

Configures the output postprocessing pipeline for the model using a fluent builder.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePostprocessing(Action<PostprocessingPipeline<T, TOutput, TOutput>>? pipelineBuilder = null)

Parameters

pipelineBuilder Action<PostprocessingPipeline<T, TOutput, TOutput>>

Returns

IAiModelBuilder<T, TInput, TOutput>

Remarks

The postprocessing pipeline transforms model outputs into the desired format. This includes operations like applying softmax, decoding labels, filtering results, and formatting outputs for specific use cases.

For Beginners: This lets you chain multiple postprocessing steps together:

builder.ConfigurePostprocessing(pipeline => pipeline
    .Add(new SoftmaxTransformer<double>())
    .Add(new LabelDecoder<double>(labels)));
The pipeline will apply these transformations in order to model outputs, and can reverse them if needed.

ConfigurePreprocessing(IDataTransformer<T, TInput, TInput>?)

Configures a single preprocessing transformer.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePreprocessing(IDataTransformer<T, TInput, TInput>? transformer = null)

Parameters

transformer IDataTransformer<T, TInput, TInput>

The transformer to use for preprocessing.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

Use this overload when you only need a single transformer. For multiple transformers, use the overload that takes an Action to build a pipeline.

For Beginners: This is a simple way to add just one preprocessing step:

builder.ConfigurePreprocessing(new StandardScaler<double>());
If you need multiple steps, use the pipeline builder overload instead.

ConfigurePreprocessing(PreprocessingPipeline<T, TInput, TInput>?)

Configures a pre-built preprocessing pipeline.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePreprocessing(PreprocessingPipeline<T, TInput, TInput>? pipeline = null)

Parameters

pipeline PreprocessingPipeline<T, TInput, TInput>

The preprocessing pipeline to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

Use this overload when you have a pre-configured pipeline that you want to reuse across multiple model builders.

For Beginners: Use this when you've already created a pipeline elsewhere:

var myPipeline = new PreprocessingPipeline<double, Matrix<double>, Matrix<double>>()
    .Add(new StandardScaler<double>());

builder.ConfigurePreprocessing(myPipeline);

ConfigurePreprocessing(Action<PreprocessingPipeline<T, TInput, TInput>>?)

Configures a preprocessing pipeline using a builder action.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePreprocessing(Action<PreprocessingPipeline<T, TInput, TInput>>? pipelineBuilder = null)

Parameters

pipelineBuilder Action<PreprocessingPipeline<T, TInput, TInput>>

An action that configures the preprocessing pipeline.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

The new preprocessing pipeline replaces the legacy normalizer system with a more flexible, composable approach supporting scalers, encoders, imputers, and feature generators.

For Beginners: This lets you chain multiple preprocessing steps together:

builder.ConfigurePreprocessing(pipeline => pipeline
    .Add(new SimpleImputer<double>(ImputationStrategy.Mean))
    .Add(new StandardScaler<double>())
    .Add(new PolynomialFeatures<double>(degree: 2)));
The pipeline will apply these transformations in order during training, and remember them for predictions on new data.

ConfigureProfiling(ProfilingConfig?)

Configures performance profiling for training and inference operations.

public IAiModelBuilder<T, TInput, TOutput> ConfigureProfiling(ProfilingConfig? config = null)

Parameters

config ProfilingConfig

The profiling configuration, or null to use industry-standard defaults.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Profiling measures how long different parts of your ML code take to run. Think of it like a stopwatch for your code - it helps you find bottlenecks and optimize performance.

The profiling report will be available in the result after training:

var result = await builder
    .ConfigureProfiling() // Enable with defaults
    .Build(features, labels);

// Access the profiling report
var report = result.ProfilingReport;
Console.WriteLine(report?.GetFormattedSummary());

Features tracked:

  • Operation timing: How long each training step, forward pass, backward pass takes
  • Memory allocations: How much memory is used during training
  • Call hierarchy: Which operations call which other operations
  • Percentiles: P50 (median), P95, P99 timing for statistical analysis

ConfigureProgramSynthesis(ProgramSynthesisOptions?)

Configures program synthesis (code generation / repair) settings with sensible defaults.

public IAiModelBuilder<T, TInput, TOutput> ConfigureProgramSynthesis(ProgramSynthesisOptions? options = null)

Parameters

options ProgramSynthesisOptions

Optional configuration options. If null, safe industry-standard defaults are used.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

Program synthesis focuses on code-oriented tasks such as generation, completion, and repair. This method wires up the default program-synthesis components and chooses safe default values when options are not provided (for example, a safe maximum sequence length and vocabulary size).

Tokenizer selection: - If options provides a tokenizer, it is used. - Otherwise, if a tokenizer was configured earlier via ConfigureTokenizer(ITokenizer?, TokenizationConfig?), that tokenizer is reused. - If no tokenizer is available, a code-aware tokenizer is created automatically based on the target language.

Model selection: - The builder creates a program-synthesis model based on the configured model kind (for example CodeBERT / GraphCodeBERT / CodeT5). - If the created model is compatible with this builder’s TInput and TOutput, the model is applied. If not compatible, the tokenizer/options are still configured, but the existing model is left unchanged.

For Beginners: Use this when you want a ready-to-use setup for code tasks and you do not want to manually choose every low-level component (tokenizer, defaults, and model configuration).

Simple usage (defaults):

var result = await new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureProgramSynthesis()
    .BuildAsync();

Custom usage:

var result = await new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureProgramSynthesis(new ProgramSynthesisOptions
    {
        TargetLanguage = ProgramLanguage.CSharp,
        ModelKind = ProgramSynthesisModelKind.CodeT5,
        MaxSequenceLength = 1024
    })
    .BuildAsync();

ConfigureProgramSynthesisServing(ProgramSynthesisServingClientOptions?, IProgramSynthesisServingClient?)

Configures program synthesis to use AiDotNet.Serving for program execution and evaluation (optional).

public IAiModelBuilder<T, TInput, TOutput> ConfigureProgramSynthesisServing(ProgramSynthesisServingClientOptions? options = null, IProgramSynthesisServingClient? client = null)

Parameters

options ProgramSynthesisServingClientOptions

Serving client options. If null (and client is null), a default configuration is used that targets http://localhost:52432/.

client IProgramSynthesisServingClient

Optional custom client implementation. When provided, this takes precedence over options.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

Some program-synthesis workflows need to run or evaluate generated programs (for example, execute code against test cases). This method lets you route those operations through a Serving endpoint (or a custom client), which is useful for centralized execution, resource control, and isolation.

Precedence rules: - If client is provided, it is used. - Otherwise, if options is provided it is used. - Otherwise, a default configuration is used that targets http://localhost:52432/.

For Beginners: If you only want the model to generate code, you can skip this. If you want to automatically execute or evaluate generated code, configure Serving. If you're running AiDotNet.Serving locally with default settings, calling this method with no parameters is enough.

Example:

var result = await new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureProgramSynthesis()
    .ConfigureProgramSynthesisServing() // Defaults to http://localhost:52432/
    .BuildAsync();

ConfigurePromptAnalyzer(IPromptAnalyzer?)

Configures a prompt analyzer for analyzing prompt quality, metrics, and potential issues.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePromptAnalyzer(IPromptAnalyzer? analyzer = null)

Parameters

analyzer IPromptAnalyzer

The prompt analyzer implementation, or null to use default.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

A prompt analyzer examines prompts to provide metrics like token count, estimated cost, complexity scores, and can detect potential issues like prompt injection or unclear instructions.

For Beginners: The prompt analyzer is like a "spell checker" for your prompts.

It helps you understand:

  • How many tokens your prompt uses (affects cost)
  • How complex your prompt is
  • Whether there might be issues with your prompt

Example:

var analyzer = new PromptAnalyzer();

var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigurePromptAnalyzer(analyzer)
    .ConfigureModel(model);

// After building, the trained model can analyze prompts
var metrics = trainedModel.AnalyzePrompt("Your prompt text...");
Console.WriteLine($"Token count: {metrics.TokenCount}");

ConfigurePromptChain(IChain<string, string>?)

Configures the prompt chain for composing multiple language model operations.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePromptChain(IChain<string, string>? chain = null)

Parameters

chain IChain<string, string>

The chain to use for processing prompts.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

A chain orchestrates multiple language model calls, tools, and transformations into a cohesive workflow. Chains can be sequential, conditional, or parallel.

For Beginners: A chain connects multiple steps into a complete workflow, like a recipe where each step builds on the previous one.

Example:

var chain = new SequentialChain<string, string>("CustomerSupport")
    .AddStep("classify", ClassifyEmail)
    .AddStep("respond", GenerateResponse);

var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigurePromptChain(chain)
    .ConfigureModel(model);

ConfigurePromptCompressor(IPromptCompressor?)

Configures a prompt compressor for reducing prompt token counts while preserving meaning.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePromptCompressor(IPromptCompressor? compressor = null)

Parameters

compressor IPromptCompressor

The prompt compressor implementation, or null to use default.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

A prompt compressor reduces the length of prompts to save on API costs and fit within context windows. Different compression strategies include removing redundancy, summarizing sections, and caching repeated content.

For Beginners: The prompt compressor makes your prompts shorter without losing meaning.

Benefits:

  • Lower API costs (fewer tokens = less money)
  • Faster responses (shorter prompts process faster)
  • Fit within model limits (some models have token limits)

Example:

var compressor = new RedundancyCompressor();

var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigurePromptCompressor(compressor)
    .ConfigureModel(model);

// After building, the trained model can compress prompts
var result = trainedModel.CompressPrompt("Your long verbose prompt...");
Console.WriteLine($"Original: {result.OriginalTokenCount}, Compressed: {result.CompressedTokenCount}");

ConfigurePromptOptimizer(IPromptOptimizer<T>?)

Configures the prompt optimizer for automatically improving prompts.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePromptOptimizer(IPromptOptimizer<T>? optimizer = null)

Parameters

optimizer IPromptOptimizer<T>

The prompt optimizer to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

A prompt optimizer automatically refines prompts to achieve better performance on a specific task. Optimization strategies include discrete search, gradient-based methods, and evolutionary algorithms.

For Beginners: A prompt optimizer automatically improves your prompts by testing variations and keeping the best-performing ones.

Example:

var optimizer = new DiscreteSearchOptimizer<double>();

var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigurePromptOptimizer(optimizer)
    .ConfigureModel(model);

// Later, optimize a prompt
var optimized = optimizer.Optimize(
    initialPrompt: "Classify sentiment:",
    evaluationFunction: EvaluatePrompt,
    maxIterations: 50
);

ConfigurePromptTemplate(IPromptTemplate?)

Configures the prompt template for language model interactions.

public IAiModelBuilder<T, TInput, TOutput> ConfigurePromptTemplate(IPromptTemplate? template = null)

Parameters

template IPromptTemplate

The prompt template to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

A prompt template provides a structured way to create prompts for language models by combining a template string with runtime variables.

For Beginners: A prompt template is like a form with blanks to fill in. You define the structure once and fill in different values each time you use it.

Example:

var template = new SimplePromptTemplate("Translate {text} from {source} to {target}");
var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigurePromptTemplate(template)
    .ConfigureModel(model);

ConfigureQuantization(QuantizationConfig?)

Configures model quantization for reducing model size and improving inference speed.

public IAiModelBuilder<T, TInput, TOutput> ConfigureQuantization(QuantizationConfig? config = null)

Parameters

config QuantizationConfig

The quantization configuration (optional, uses no quantization if null).

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Quantization compresses your model by using smaller numbers (like 8-bit instead of 32-bit). This makes your model:

  • Smaller (50-75% size reduction)
  • Faster (2-4x speedup)
  • Use less memory

The trade-off is a small accuracy loss (usually 1-5%). For most applications, this is acceptable.

Example:

// Use Float16 quantization (recommended for most cases)
var result = await builder
    .ConfigureModel(model)
    .ConfigureQuantization(new QuantizationConfig { Mode = QuantizationMode.Float16 })
    .BuildAsync();

ConfigureReasoning(ReasoningConfig?)

Configures advanced reasoning capabilities for the model using Chain-of-Thought, Tree-of-Thoughts, and Self-Consistency strategies.

public IAiModelBuilder<T, TInput, TOutput> ConfigureReasoning(ReasoningConfig? config = null)

Parameters

config ReasoningConfig

The reasoning configuration (optional, uses defaults if null).

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Reasoning capabilities make AI models "think step by step" instead of giving quick answers that might be wrong. Just like a student showing their work on a math test, reasoning strategies help the AI: - Break down complex problems into manageable steps - Explore multiple solution approaches - Verify and refine its answers - Provide transparent, explainable reasoning

After building your model, use the reasoning methods on AiModelResult:

  • ReasonAsync(): Solve problems with configurable reasoning strategies
  • QuickReasonAsync(): Fast answers for simple problems
  • DeepReasonAsync(): Thorough analysis for complex problems

Example:

// Configure reasoning during model building
var agentConfig = new AgentConfiguration<double>
{
    ApiKey = "sk-...",
    Provider = LLMProvider.OpenAI,
    IsEnabled = true
};

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureAgentAssistance(agentConfig)
    .ConfigureReasoning()
    .BuildAsync();

// Use reasoning on the trained model
var reasoningResult = await result.ReasonAsync(
    "Explain why this prediction was made and what factors contributed most?",
    ReasoningMode.ChainOfThought
);
Console.WriteLine(reasoningResult.FinalAnswer);

ConfigureRegularization(IRegularization<T, TInput, TOutput>)

Configures regularization to prevent overfitting in the model.

public IAiModelBuilder<T, TInput, TOutput> ConfigureRegularization(IRegularization<T, TInput, TOutput> regularization)

Parameters

regularization IRegularization<T, TInput, TOutput>

The regularization strategy to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Regularization helps prevent your model from "memorizing" the training data instead of learning general patterns. It's like teaching a student to understand the concepts rather than just memorizing answers to specific questions. This helps the model perform better on new, unseen data.

ConfigureReinforcementLearning(RLTrainingOptions<T>)

Configures reinforcement learning options for training an RL agent.

public IAiModelBuilder<T, TInput, TOutput> ConfigureReinforcementLearning(RLTrainingOptions<T> options)

Parameters

options RLTrainingOptions<T>

The reinforcement learning configuration options.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Reinforcement learning trains an agent through trial and error in an environment. This method configures all aspects of RL training:

  • The environment (simulation/game for the agent to learn from)
  • Training parameters (episodes, steps, batch size)
  • Exploration strategies (how to balance trying new things vs using learned behavior)
  • Replay buffers (how to store and sample past experiences)
  • Callbacks for monitoring training progress

After configuring RL options, use BuildAsync(episodes) to train the agent.

Example:

var options = new RLTrainingOptions<double>
{
    Environment = new CartPoleEnvironment<double>(),
    Episodes = 1000,
    MaxStepsPerEpisode = 500,
    OnEpisodeComplete = (metrics) => Console.WriteLine($"Episode {metrics.Episode}: {metrics.TotalReward}")
};

var result = await new AiModelBuilder<double, Vector<double>, Vector<double>>()
    .ConfigureReinforcementLearning(options)
    .ConfigureModel(new DQNAgent<double>())
    .BuildAsync();

ConfigureRetrievalAugmentedGeneration(IRetriever<T>?, IReranker<T>?, IGenerator<T>?, IEnumerable<IQueryProcessor>?, IGraphStore<T>?, KnowledgeGraph<T>?, IDocumentStore<T>?)

Configures the retrieval-augmented generation (RAG) components for use during model inference.

public IAiModelBuilder<T, TInput, TOutput> ConfigureRetrievalAugmentedGeneration(IRetriever<T>? retriever = null, IReranker<T>? reranker = null, IGenerator<T>? generator = null, IEnumerable<IQueryProcessor>? queryProcessors = null, IGraphStore<T>? graphStore = null, KnowledgeGraph<T>? knowledgeGraph = null, IDocumentStore<T>? documentStore = null)

Parameters

retriever IRetriever<T>

Optional retriever for finding relevant documents. If not provided, standard RAG won't be available.

reranker IReranker<T>

Optional reranker for improving document ranking quality. If not provided, a default reranker will be used if RAG is configured.

generator IGenerator<T>

Optional generator for producing grounded answers. If not provided, a default generator will be used if RAG is configured.

queryProcessors IEnumerable<IQueryProcessor>

Optional query processors for improving search quality.

graphStore IGraphStore<T>

Optional graph storage backend for Graph RAG (e.g., MemoryGraphStore, FileGraphStore).

knowledgeGraph KnowledgeGraph<T>

Optional pre-configured knowledge graph. If null but graphStore is provided, a new one is created.

documentStore IDocumentStore<T>

Optional document store for hybrid vector + graph retrieval.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: RAG combines retrieval and generation to create answers backed by real documents. Configure it with:

  • A retriever (finds relevant documents from your collection) - required for standard RAG
  • A reranker (improves the ordering of retrieved documents) - optional, defaults provided
  • A generator (creates answers based on the documents) - optional, defaults provided
  • Optional query processors (improve search queries before retrieval)

Graph RAG: When graphStore or knowledgeGraph is provided, enables knowledge graph-based retrieval that finds related entities and their relationships, providing richer context than vector similarity alone. Traditional RAG finds similar documents using vectors. Graph RAG goes further by also exploring relationships between entities. For example, if you ask about "Paris", it can find not just documents mentioning Paris, but also related concepts like France, Eiffel Tower, and Seine River.

Hybrid Retrieval: When both knowledgeGraph and documentStore are provided, creates a HybridGraphRetriever that combines vector search and graph traversal for optimal results.

Disabling RAG: Call with all parameters as null to disable RAG functionality completely.

RAG operations are performed during inference (after model training) via the AiModelResult.

ConfigureSceneTextReader(SceneTextReader<T>)

Configures a scene text reader for OCR.

public AiModelBuilder<T, TInput, TOutput> ConfigureSceneTextReader(SceneTextReader<T> textReader)

Parameters

textReader SceneTextReader<T>

The scene text reader to use.

Returns

AiModelBuilder<T, TInput, TOutput>

The builder for method chaining.

Remarks

For Beginners: OCR (Optical Character Recognition) extracts text from images, useful for reading documents or scene text.

ConfigureSelfSupervisedLearning(Action<SSLConfig>?)

Configures self-supervised learning for unsupervised representation learning.

public IAiModelBuilder<T, TInput, TOutput> ConfigureSelfSupervisedLearning(Action<SSLConfig>? configure = null)

Parameters

configure Action<SSLConfig>

Optional action to configure SSL settings.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

Self-supervised learning (SSL) allows training powerful representations from unlabeled data. The learned representations can then be fine-tuned on smaller labeled datasets, often achieving better results than training from scratch.

For Beginners: SSL is like teaching a model to understand patterns in data without needing human labels. Think of it as the model learning to "see" or "understand" images/text before being taught specific tasks. This makes it much better at learning new tasks with less labeled data.

Supported Methods:

  • SimCLR: Contrastive learning with in-batch negatives (large batch sizes)
  • MoCo/MoCoV2/MoCoV3: Momentum contrastive with memory queue (efficient)
  • BYOL: No negatives required, uses momentum teacher
  • SimSiam: Simple Siamese networks with stop-gradient
  • BarlowTwins: Decorrelation-based, no negatives needed
  • DINO: Self-distillation for Vision Transformers
  • MAE: Masked autoencoding for ViT pretraining

Example - Basic SSL pretraining:

var result = builder
    .ConfigureModel(encoder)
    .ConfigureSelfSupervisedLearning()  // Uses SimCLR by default
    .Build(unlabeledImages);

Example - Custom SSL configuration:

var result = builder
    .ConfigureModel(encoder)
    .ConfigureSelfSupervisedLearning(ssl =>
    {
        ssl.Method = SSLMethodType.MoCoV3;
        ssl.PretrainingEpochs = 300;
        ssl.Temperature = 0.2;
        ssl.ProjectorOutputDimension = 256;
        ssl.MoCo = new MoCoConfig { Momentum = 0.99 };
    })
    .Build(unlabeledImages);

Example - BYOL without negative samples:

var result = builder
    .ConfigureModel(encoder)
    .ConfigureSelfSupervisedLearning(ssl =>
    {
        ssl.Method = SSLMethodType.BYOL;
        ssl.BYOL = new BYOLConfig { Momentum = 0.996 };
    })
    .Build(unlabeledImages);

ConfigureTelemetry(TelemetryConfig?)

Configures telemetry for tracking and monitoring model inference metrics.

public IAiModelBuilder<T, TInput, TOutput> ConfigureTelemetry(TelemetryConfig? config = null)

Parameters

config TelemetryConfig

The telemetry configuration (optional, uses default telemetry settings if null).

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Telemetry collects performance data about your model in production, like:

  • How long each inference takes (latency)
  • How many inferences per second (throughput)
  • When errors occur
  • Cache hit/miss rates
  • Which model versions are being used

This helps you:

  • Detect performance problems before users complain
  • Understand usage patterns
  • Debug production issues
  • Make informed decisions about model updates

Example:

// Enable telemetry with default settings
var result = await builder
    .ConfigureModel(model)
    .ConfigureTelemetry()
    .BuildAsync();

ConfigureTokenizer(ITokenizer?, TokenizationConfig?)

Configures tokenization for text-based input processing.

public IAiModelBuilder<T, TInput, TOutput> ConfigureTokenizer(ITokenizer? tokenizer = null, TokenizationConfig? config = null)

Parameters

tokenizer ITokenizer

The tokenizer to use for text processing.

config TokenizationConfig

Optional tokenization configuration. If null, default settings are used.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

Tokenization is the process of breaking text into smaller pieces (tokens) that can be processed by machine learning models. This is essential for NLP and text-based models.

For Beginners: Tokenization converts human-readable text into numbers that AI models understand.

Different tokenization strategies include:

  • BPE (Byte Pair Encoding): Used by GPT models, learns subword units from data
  • WordPiece: Used by BERT, splits unknown words into known subwords
  • SentencePiece: Language-independent tokenization used by many multilingual models

Example:

// Using BPE tokenizer
var tokenizer = BpeTokenizer.Train(corpus, vocabSize: 32000);
var builder = new AiModelBuilder<float, Matrix<float>, Vector<float>>()
    .ConfigureTokenizer(tokenizer)
    .ConfigureModel(new TransformerModel())
    .Build(trainingData);

// Or use AutoTokenizer for HuggingFace models
var tokenizer = AutoTokenizer.FromPretrained("bert-base-uncased");

ConfigureTokenizerFromPretrained(PretrainedTokenizerModel, TokenizationConfig?)

Configures tokenization using a pretrained tokenizer from HuggingFace Hub.

public IAiModelBuilder<T, TInput, TOutput> ConfigureTokenizerFromPretrained(PretrainedTokenizerModel model = PretrainedTokenizerModel.BertBaseUncased, TokenizationConfig? config = null)

Parameters

model PretrainedTokenizerModel

The pretrained tokenizer model to use. Defaults to BertBaseUncased.

config TokenizationConfig

Optional tokenization configuration.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: This is the easiest and most type-safe way to use industry-standard tokenizers. Using the enum ensures you always specify a valid model name.

Simply call without parameters for sensible defaults:

var builder = new AiModelBuilder<float, Matrix<float>, Vector<float>>()
    .ConfigureTokenizerFromPretrained()  // Uses BertBaseUncased by default
    .ConfigureModel(new BertModel())
    .Build(trainingData);

Or specify a model using the enum:

builder.ConfigureTokenizerFromPretrained(PretrainedTokenizerModel.Gpt2)

Available models include:

  • BertBaseUncased: BERT tokenizer for English text (default)
  • Gpt2, Gpt2Medium, Gpt2Large: GPT-2 tokenizers for text generation
  • RobertaBase, RobertaLarge: RoBERTa tokenizers (improved BERT)
  • T5Small, T5Base, T5Large: T5 tokenizers for text-to-text tasks
  • DistilBertBaseUncased: Faster, smaller BERT
  • CodeBertBase: For code understanding tasks

ConfigureTokenizerFromPretrained(string?, TokenizationConfig?)

Configures tokenization using a pretrained tokenizer from a custom HuggingFace model name or local path.

public IAiModelBuilder<T, TInput, TOutput> ConfigureTokenizerFromPretrained(string? modelNameOrPath = null, TokenizationConfig? config = null)

Parameters

modelNameOrPath string

The HuggingFace model name or local path. Defaults to "bert-base-uncased" if not specified.

config TokenizationConfig

Optional tokenization configuration.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

For Beginners: Use this overload when you need to specify a custom model name or path that isn't in the PretrainedTokenizerModel enum. For common models, prefer the enum-based overload for type safety.

Example with custom model:

// Use a custom or community model from HuggingFace
builder.ConfigureTokenizerFromPretrained("sentence-transformers/all-MiniLM-L6-v2")

If null or empty, defaults to "bert-base-uncased".

ConfigureTokenizerFromPretrainedAsync(PretrainedTokenizerModel, TokenizationConfig?)

Asynchronously configures the tokenizer by loading a pretrained model from HuggingFace Hub.

public Task<IAiModelBuilder<T, TInput, TOutput>> ConfigureTokenizerFromPretrainedAsync(PretrainedTokenizerModel model = PretrainedTokenizerModel.BertBaseUncased, TokenizationConfig? config = null)

Parameters

model PretrainedTokenizerModel

The pretrained tokenizer model to use.

config TokenizationConfig

Optional tokenization configuration.

Returns

Task<IAiModelBuilder<T, TInput, TOutput>>

A task that completes with the builder instance for method chaining.

Remarks

For Beginners: This is the async version of ConfigureTokenizerFromPretrained. Use this when you want to avoid blocking the thread while downloading tokenizer files from HuggingFace Hub. This is especially important in UI applications or web servers.

Example:

// Async configuration
await builder.ConfigureTokenizerFromPretrainedAsync(PretrainedTokenizerModel.BertBaseUncased);

ConfigureTokenizerFromPretrainedAsync(string?, TokenizationConfig?)

Asynchronously configures the tokenizer by loading a pretrained model from HuggingFace Hub using a model name or path.

public Task<IAiModelBuilder<T, TInput, TOutput>> ConfigureTokenizerFromPretrainedAsync(string? modelNameOrPath = null, TokenizationConfig? config = null)

Parameters

modelNameOrPath string

The HuggingFace model name or local path. Defaults to "bert-base-uncased" if not specified.

config TokenizationConfig

Optional tokenization configuration.

Returns

Task<IAiModelBuilder<T, TInput, TOutput>>

A task that completes with the builder instance for method chaining.

Remarks

For Beginners: This is the async version that accepts a custom model name or path. Use this when loading custom or community models without blocking the thread.

Example:

// Async configuration with custom model
await builder.ConfigureTokenizerFromPretrainedAsync("sentence-transformers/all-MiniLM-L6-v2");

ConfigureTrainingMonitor(ITrainingMonitor<T>)

Configures training monitoring for real-time visibility into training progress.

public IAiModelBuilder<T, TInput, TOutput> ConfigureTrainingMonitor(ITrainingMonitor<T> monitor)

Parameters

monitor ITrainingMonitor<T>

The training monitor implementation to use.

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: A training monitor is like a dashboard for your model training. It shows you how training is progressing, what resources are being used, and if there are any problems.

ConfigureTrainingPipeline(TrainingPipelineConfiguration<T, TInput, TOutput>?)

Configures a multi-stage training pipeline for advanced training workflows.

public IAiModelBuilder<T, TInput, TOutput> ConfigureTrainingPipeline(TrainingPipelineConfiguration<T, TInput, TOutput>? configuration = null)

Parameters

configuration TrainingPipelineConfiguration<T, TInput, TOutput>

The training pipeline configuration defining the stages to execute. When null, uses the default single-stage training based on other configured settings.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

ConfigureTrainingPipeline enables advanced multi-stage training workflows where each stage can have its own training method, optimizer, learning rate, and dataset. Stages execute sequentially, with each stage's output model becoming the next stage's input.

For Beginners: Think of this as a recipe with multiple cooking steps. Just like you might marinate, then sear, then bake - training can have multiple phases where each phase teaches the model something different.

Common Training Pipelines:

  • Standard AlignmentSFT → DPO (most common for chat models)
  • Full RLHFSFT → Reward Model → PPO
  • Constitutional AISFT → CAI critique/revision → preference
  • Curriculum LearningEasy data → Medium → Hard (progressive difficulty)
  • Iterative RefinementMultiple DPO rounds with decreasing beta
// Standard alignment pipeline (SFT → DPO)
builder.ConfigureTrainingPipeline(
    TrainingPipelineConfiguration<double, string, string>.StandardAlignment(sftData, preferenceData));

// Automatic pipeline based on available data
builder.ConfigureTrainingPipeline(
    TrainingPipelineConfiguration<double, string, string>.Auto(myData));

// Custom multi-stage pipeline with builder pattern
var pipeline = new TrainingPipelineConfiguration<double, string, string>()
    .AddSFTStage(stage => {
        stage.TrainingData = sftData;
        stage.Options = new FineTuningOptions<double> { Epochs = 3 };
    })
    .AddPreferenceStage(FineTuningMethodType.DPO, stage => {
        stage.TrainingData = preferenceData;
        stage.Options = new FineTuningOptions<double> { Beta = 0.1 };
    })
    .AddEvaluationStage();
builder.ConfigureTrainingPipeline(pipeline);

// Iterative refinement with multiple DPO rounds
builder.ConfigureTrainingPipeline(
    TrainingPipelineConfiguration<double, string, string>.IterativeRefinement(3, sftData, preferenceData));

// Custom stage with user-defined training logic
var customPipeline = new TrainingPipelineConfiguration<double, string, string>()
    .AddSFTStage()
    .AddCustomStage("My Custom Training", async (model, data, ct) => {
        // Custom training logic
        return model;
    });
builder.ConfigureTrainingPipeline(customPipeline);

ConfigureUncertaintyQuantification(UncertaintyQuantificationOptions?, UncertaintyCalibrationData<TInput, TOutput>?)

Configures uncertainty quantification (UQ) for inference-time uncertainty estimates.

public IAiModelBuilder<T, TInput, TOutput> ConfigureUncertaintyQuantification(UncertaintyQuantificationOptions? options = null, UncertaintyCalibrationData<TInput, TOutput>? calibrationData = null)

Parameters

options UncertaintyQuantificationOptions

Optional uncertainty quantification options. When null, industry-standard defaults are used and UQ is enabled.

calibrationData UncertaintyCalibrationData<TInput, TOutput>

Optional calibration data used by conformal prediction and probability calibration methods. When null, calibration-dependent features are skipped unless the library can infer calibration behavior from other configuration.

Returns

IAiModelBuilder<T, TInput, TOutput>

This builder instance for method chaining.

Remarks

Uncertainty quantification augments point predictions with uncertainty signals such as variance, predictive entropy, and mutual information.

For Beginners: This feature lets you ask the model not only "what is the prediction?", but also "how sure are you?".

Typical usage:

var loader = DataLoaders.FromTensors(xTrain, yTrain);
var result = await new AiModelBuilder<double, Tensor<double>, Tensor<double>>()
    .ConfigureDataLoader(loader)
    .ConfigureModel(model)
    .ConfigureOptimizer(optimizer)
    .ConfigureUncertaintyQuantification()
    .BuildAsync();

ConfigureVersioning(VersioningConfig?)

Configures model versioning for managing multiple versions of the same model.

public IAiModelBuilder<T, TInput, TOutput> ConfigureVersioning(VersioningConfig? config = null)

Parameters

config VersioningConfig

The versioning configuration (optional, uses "latest" version if null).

Returns

IAiModelBuilder<T, TInput, TOutput>

The builder instance for method chaining.

Remarks

For Beginners: Versioning helps you manage different versions of your model as it improves over time. You can:

  • Keep track of which version is deployed
  • Roll back to previous versions if needed
  • Use "latest" to always get the newest version
  • Compare performance between versions

Example:

// Enable versioning (defaults to "latest")
var result = await builder
    .ConfigureModel(model)
    .ConfigureVersioning()
    .BuildAsync();

ConfigureVisualization(VisualizationOptions?)

Configures visualization options for detection results.

public AiModelBuilder<T, TInput, TOutput> ConfigureVisualization(VisualizationOptions? options = null)

Parameters

options VisualizationOptions

Visualization options.

Returns

AiModelBuilder<T, TInput, TOutput>

The builder for method chaining.

DeserializeModel(byte[])

Converts a byte array back into a usable predictive model.

public AiModelResult<T, TInput, TOutput> DeserializeModel(byte[] modelData)

Parameters

modelData byte[]

The byte array containing the serialized model data.

Returns

AiModelResult<T, TInput, TOutput>

The deserialized predictive model that can be used to make predictions.

Remarks

For Beginners: Deserialization is the opposite of serialization - it takes the byte array representation of your model and converts it back into a usable model object. This is what happens behind the scenes when you load a model from a file.

You might use this directly if you retrieved a serialized model from a database or received it over a network.

LoadModel(string)

Loads a previously saved model from a file.

public AiModelResult<T, TInput, TOutput> LoadModel(string filePath)

Parameters

filePath string

The file path where the model was saved.

Returns

AiModelResult<T, TInput, TOutput>

The loaded predictive model that can be used to make predictions.

Remarks

For Beginners: This method lets you load a model that you previously saved using the SaveModel method. Once loaded, you can immediately use the model to make predictions without having to train it again.

This is useful when you want to use your model in different applications or at different times without the time and computational cost of retraining.

Predict(TInput, AiModelResult<T, TInput, TOutput>)

Uses a trained model to make predictions on new data.

public TOutput Predict(TInput newData, AiModelResult<T, TInput, TOutput> modelResult)

Parameters

newData TInput

The matrix of new input features to predict outcomes for.

modelResult AiModelResult<T, TInput, TOutput>

The trained predictive model to use for making predictions.

Returns

TOutput

A vector containing the predicted output values for each row in the input matrix.

Remarks

For Beginners: After training your model with the Build method, you can use this method to get predictions for new data. For example, if you trained a model to predict house prices based on features like size and location, you can now give it details of houses currently for sale (without knowing their prices) and the model will predict what their prices should be.

The input matrix should have the same number of columns (features) as the data you used to train the model.

SaveModel(AiModelResult<T, TInput, TOutput>, string)

Saves a trained model to a file so it can be used later without retraining.

public void SaveModel(AiModelResult<T, TInput, TOutput> modelResult, string filePath)

Parameters

modelResult AiModelResult<T, TInput, TOutput>

The trained predictive model to save.

filePath string

The file path where the model should be saved.

Remarks

For Beginners: Training a model can take time, so once you have a good model, you'll want to save it. This method lets you store your trained model in a file on your computer. Later, you can load this saved model and use it to make predictions without having to train it again.

Think of it like saving a document in a word processor - you can close the program and come back later to continue where you left off.

SerializeModel(AiModelResult<T, TInput, TOutput>)

Converts a trained model into a byte array for storage or transmission.

public byte[] SerializeModel(AiModelResult<T, TInput, TOutput> modelResult)

Parameters

modelResult AiModelResult<T, TInput, TOutput>

The trained predictive model to serialize.

Returns

byte[]

A byte array representing the serialized model.

Remarks

For Beginners: Serialization converts your model into a format (a series of bytes) that can be easily stored or sent over a network. This is the underlying mechanism that makes saving models possible.

You might use this directly if you want to store the model in a database or send it over a network, rather than saving it to a file.