Skip to content

Getting Started

franklinic edited this page Jan 19, 2026 · 1 revision

Getting Started with AiDotNet

This guide will help you get up and running with AiDotNet in minutes.

Prerequisites

  • .NET 8.0 SDK or later
  • Visual Studio 2022, VS Code, or JetBrains Rider
  • (Optional) CUDA-capable GPU for accelerated training

Installation

Via NuGet Package Manager

dotnet add package AiDotNet

Via Package Manager Console (Visual Studio)

Install-Package AiDotNet

Via PackageReference

Add to your .csproj file:

<PackageReference Include="AiDotNet" Version="*" />

For more detailed installation options, see the Installation Guide.

Architecture: The Facade Pattern

AiDotNet uses a facade pattern to provide a simple, unified API for all AI/ML tasks. You only need to learn two main classes:

Class Purpose
AiModelBuilder<T, TInput, TOutput> Build and train any model
AiModelResult<T, TInput, TOutput> Access trained model and perform inference

This design:

  • Hides internal complexity
  • Provides a consistent API across all model types
  • Makes it easy to switch between algorithms
  • Ensures best practices are followed automatically

Your First Model

All AiDotNet functionality goes through the AiModelBuilder. Here's a complete example:

Example 1: Simple Classification

using AiDotNet;

// Training data: features and labels
var features = new double[,]
{
    { 5.1, 3.5, 1.4, 0.2 },  // Setosa
    { 4.9, 3.0, 1.4, 0.2 },  // Setosa
    { 7.0, 3.2, 4.7, 1.4 },  // Versicolor
    { 6.4, 3.2, 4.5, 1.5 },  // Versicolor
    { 6.3, 3.3, 6.0, 2.5 },  // Virginica
    { 5.8, 2.7, 5.1, 1.9 }   // Virginica
};

var labels = new int[] { 0, 0, 1, 1, 2, 2 };

// Build and train the model using the facade
var result = await new AiModelBuilder<double, double[], int>()
    .WithTrainingData(features, labels)
    .WithClassifier(ClassifierType.RandomForest)
    .BuildAsync();

// Make predictions through the result object
var prediction = result.Predict(new double[] { 5.5, 2.5, 4.0, 1.3 });
Console.WriteLine($"Predicted class: {prediction}");

// View metrics
Console.WriteLine($"Accuracy: {result.Metrics.Accuracy:P2}");

Example 2: Neural Network

using AiDotNet;

// Build a neural network for MNIST digit classification
var result = await new AiModelBuilder<float, Tensor<float>, int>()
    .WithTrainingData(trainImages, trainLabels)
    .WithNeuralNetwork(nn => nn
        .AddFlattenLayer()
        .AddDenseLayer(128, Activation.ReLU)
        .AddDropout(0.2)
        .AddDenseLayer(64, Activation.ReLU)
        .AddOutputLayer(10, Activation.Softmax))
    .WithOptimizer(OptimizerType.Adam, learningRate: 0.001)
    .WithLossFunction(LossType.CrossEntropy)
    .WithEpochs(10)
    .WithBatchSize(32)
    .BuildAsync();

// Inference through the result object
var prediction = result.Predict(testImage);
Console.WriteLine($"Predicted digit: {prediction}");
Console.WriteLine($"Test Accuracy: {result.Metrics.Accuracy:P2}");

Example 3: Regression

using AiDotNet;

// House price prediction
var features = new double[,]
{
    { 1400, 3, 2 },  // sqft, bedrooms, bathrooms
    { 1600, 3, 2 },
    { 1700, 3, 2 },
    { 1875, 4, 3 },
    { 2350, 4, 3 }
};

var prices = new double[] { 245000, 312000, 279000, 308000, 450000 };

var result = await new AiModelBuilder<double, double[], double>()
    .WithTrainingData(features, prices)
    .WithRegressor(RegressorType.GradientBoosting)
    .BuildAsync();

// Inference through the result object
var predictedPrice = result.Predict(new double[] { 2000, 4, 3 });
Console.WriteLine($"Predicted price: ${predictedPrice:N0}");

Core Concepts

AiModelBuilder

The AiModelBuilder<T, TInput, TOutput> is the only entry point for building models:

  • T - The numeric type (usually float or double)
  • TInput - Input data type (e.g., double[], Tensor<T>, string)
  • TOutput - Output type (e.g., int for classification, double for regression)

Configuration Options

var result = await new AiModelBuilder<double, double[], int>()
    .WithTrainingData(features, labels)
    .WithClassifier(ClassifierType.SVM)

    // Data preprocessing
    .ConfigurePreprocessing(p => p
        .Normalize()
        .HandleMissingValues())

    // Cross-validation
    .WithCrossValidation(folds: 5)

    // Train/test split
    .WithTestSplit(0.2)  // 20% for testing

    // Telemetry and logging
    .ConfigureTelemetry(t => t.Enabled = true)

    .BuildAsync();

AiModelResult - Accessing Results

The AiModelResult object provides all inference capabilities and metrics:

// Make predictions (all inference goes through the result object)
var prediction = result.Predict(newSample);
var batchPredictions = result.Predict(samples);

// Get probabilities (for classification)
var probabilities = result.PredictProbabilities(newSample);

// Training metrics
var accuracy = result.Metrics.Accuracy;
var precision = result.Metrics.Precision;
var recall = result.Metrics.Recall;
var f1Score = result.Metrics.F1Score;

// For regression
var mse = result.Metrics.MeanSquaredError;
var mae = result.Metrics.MeanAbsoluteError;
var r2 = result.Metrics.RSquared;

// Training info
var trainingTime = result.TrainingTime;

Next Steps

Now that you have the basics, explore these topics:

  1. Neural Networks - Deep learning architectures
  2. Classical ML - Traditional machine learning algorithms
  3. Computer Vision - Image processing and detection
  4. NLP - Natural language processing
  5. RAG & LLMs - Retrieval-augmented generation
  6. Reinforcement Learning - RL agents and environments

Interactive Playground

Want to try AiDotNet without installing anything? Visit our Interactive Playground to write and run code directly in your browser!

Getting Help

Clone this wiki locally