INTERACTIVE PLAYGROUND

Code Examples

Explore AiDotNet capabilities through curated code examples. Every example uses the AiModelBuilder facade pattern for consistent, production-ready code.

Quick Reference: AiModelBuilder Pattern

Program.cs
using AiDotNet;

// Every AiDotNet workflow follows this pattern:
var result = await new AiModelBuilder<double, double[], double>()
    .ConfigureModel(new SomeModel<double>(...))       // Set the model
    .ConfigureOptimizer(new AdamOptimizer<double>()) // Set optimizer
    .ConfigurePreprocessing()                       // Auto StandardScaler
    .BuildAsync(features, labels);                   // Train

var prediction = result.Predict(newData);            // Inference

Code Examples by Category

165+ examples available in the full playground. Here's a curated selection.

🚀

Getting Started

(2 examples)

Simple Regression

Beginner

Train a simple linear regression model using AiModelBuilder.

regressionlinearbasics
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Regression;

// Training data
var features = new double[,] { { 1.0 }, { 2.0 }, { 3.0 }, { 4.0 }, { 5.0 } };
var labels = new double[] { 2.1, 4.0, 5.9, 8.1, 10.0 };

// Build using AiModelBuilder facade pattern
var loader = DataLoaders.FromArrays(features, labels);
var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureDataLoader(loader)
    .ConfigureModel(new SimpleRegression<double>())
    .BuildAsync();

// Make predictions
var prediction = result.Predict(testData);
Console.WriteLine($"Prediction for x=6: {prediction[0]:F2}");

Binary Classification

Beginner

Classify data points using Support Vector Machine.

classificationSVMbasics
using AiDotNet;
using AiDotNet.Classification;

// Build and train an SVM classifier
var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureDataLoader(loader)
    .ConfigureModel(new SupportVectorMachine<double>(
        kernel: new RBFKernel<double>(gamma: 0.5)))
    .ConfigurePreprocessing(p => p.Add(new StandardScaler<double>()))
    .BuildAsync();

var prediction = result.Predict(newSample);
Console.WriteLine($"Predicted class: {prediction[0]}");
🧠

Neural Networks

(2 examples)

Image Classification

Intermediate

Train a convolutional neural network for image classification.

CNNclassificationdeep learning
using AiDotNet;
using AiDotNet.NeuralNetworks;

// Build a CNN classifier
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new NeuralNetwork<float>(
        new NeuralNetworkArchitecture<float>(
            inputFeatures: 784,
            numClasses: 10,
            complexity: NetworkComplexity.Medium)))
    .ConfigureOptimizer(new AdamOptimizer<float>(
        learningRate: 1e-3f))
    .ConfigurePreprocessing()
    .BuildAsync(features, labels);

var prediction = result.Predict(newImage);

Transformer Model

Advanced

Build a transformer model with multi-head self-attention.

transformerattentionNLP
using AiDotNet;
using AiDotNet.NeuralNetworks;

// Build a transformer model
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new Transformer<float>(
        dModel: 512, nHeads: 8, nLayers: 6,
        dFeedForward: 2048, vocabSize: 30000,
        maxSeqLength: 512))
    .ConfigureOptimizer(new AdamWOptimizer<float>(
        learningRate: 1e-4f, weightDecay: 0.01f))
    .BuildAsync(features, labels);

var prediction = result.Predict(tokenizedInput);
👁

Computer Vision

(2 examples)

Object Detection (YOLO)

Intermediate

Detect objects in images using YOLOv11.

YOLOdetectionreal-time
using AiDotNet;
using AiDotNet.ComputerVision;

// Object detection with YOLOv11
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new YoloV11<float>(
        variant: YoloVariant.Medium,
        numClasses: 80))
    .ConfigureOptimizer(new AdamOptimizer<float>())
    .ConfigurePreprocessing()
    .BuildAsync(images, boundingBoxes);

var detections = result.Predict(newImage);

Image Segmentation

Intermediate

Segment objects in images using Segment Anything Model.

segmentationSAMmasks
using AiDotNet;
using AiDotNet.ComputerVision;

// Segment Anything Model
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new SAM2<float>("sam2-base"))
    .ConfigureOptimizer(new AdamOptimizer<float>())
    .ConfigurePreprocessing()
    .BuildAsync(images, masks);

var segmentation = result.Predict(newImage);
📈

Time Series

(1 examples)

Stock Price Forecasting

Intermediate

Forecast stock prices using the Chronos foundation model.

forecastingChronosfinance
using AiDotNet;
using AiDotNet.TimeSeries;

// Time series forecasting with Chronos
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new Chronos<float>("chronos-t5-large"))
    .ConfigurePreprocessing()
    .BuildAsync(historicalPrices, futureValues);

// Forecast next 30 days
var forecast = result.Predict(recentPrices);
📝

NLP & Text Processing

(2 examples)

Sentiment Analysis

Intermediate

Analyze text sentiment using FinBERT.

NLPBERTsentiment
using AiDotNet;
using AiDotNet.NLP;

// Financial sentiment analysis
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new FinBERT<float>())
    .ConfigureOptimizer(new AdamOptimizer<float>())
    .ConfigurePreprocessing()
    .BuildAsync(financialTexts, sentimentLabels);

var sentiment = result.Predict(earningsCallText);

Named Entity Recognition

Intermediate

Extract named entities from clinical text.

NERBERTentities
using AiDotNet;
using AiDotNet.NLP;

// Clinical NER with ClinicalBERT
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new ClinicalBERT<float>())
    .ConfigurePreprocessing()
    .BuildAsync(clinicalNotes, entityLabels);

var entities = result.Predict(newClinicalNote);
// [Diagnosis: "pneumonia", Medication: "amoxicillin"]
🎵

Audio Processing

(1 examples)

Speech Recognition

Intermediate

Transcribe audio using Whisper.

ASRWhispertranscription
using AiDotNet;
using AiDotNet.Audio;

// Speech recognition with Whisper
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new Whisper<float>("whisper-large-v3"))
    .ConfigurePreprocessing()
    .BuildAsync(audioSamples, transcriptions);

var transcript = result.Predict(newAudio);
🎮

Reinforcement Learning

(1 examples)

PPO Agent

Advanced

Train a PPO agent for continuous control.

RLPPOpolicy gradient
using AiDotNet;
using AiDotNet.ReinforcementLearning;

// Train PPO agent
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new NeuralNetwork<float>(
        inputSize: 24, hiddenSize: 64, outputSize: 4))
    .ConfigureReinforcementLearning(new PPOOptions(
        clipEpsilon: 0.2f, entropyCoeff: 0.01f,
        numEpochs: 10, batchSize: 64))
    .ConfigureOptimizer(new AdamOptimizer<float>(lr: 3e-4f))
    .BuildAsync(features, labels);

var action = result.Predict(stateObservation);
🔧

LoRA & Fine-Tuning

(1 examples)

QLoRA Fine-Tuning

Advanced

Fine-tune a model with 4-bit quantized LoRA.

LoRAQLoRAPEFT
using AiDotNet;

// QLoRA fine-tuning with AiModelBuilder
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new NeuralNetwork<float>(
        inputSize: 768, hiddenSize: 256, outputSize: 10))
    .ConfigureLoRA(new QLoRAConfig(
        rank: 16, alpha: 32, quantBits: 4))
    .ConfigureOptimizer(new AdamOptimizer<float>(lr: 2e-4f))
    .BuildAsync(features, labels);

var prediction = result.Predict(newSample);
🔮

Clustering

(1 examples)

K-Means Clustering

Beginner

Group data points into clusters using K-Means.

clusteringK-Meansunsupervised
using AiDotNet;
using AiDotNet.Clustering;

// K-Means clustering
var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureDataLoader(loader)
    .ConfigureModel(new KMeans<double>(numClusters: 3))
    .BuildAsync();

var clusterAssignment = result.Predict(newDataPoint);
Console.WriteLine($"Cluster: {clusterAssignment[0]}");
🌐

Federated Learning

(1 examples)

Privacy-Preserving Training

Advanced

Train models across institutions without sharing data.

federatedprivacydistributed
using AiDotNet;

// Federated learning with differential privacy
var result = await new AiModelBuilder<float, float[], float>()
    .ConfigureModel(new NeuralNetwork<float>(
        inputSize: 784, hiddenSize: 128, outputSize: 10))
    .ConfigureFederatedLearning(new FederatedOptions(
        strategy: new FedAvg<float>(),
        rounds: 50, minClients: 2,
        privacyBudget: new DifferentialPrivacy(epsilon: 1.0)))
    .ConfigureOptimizer(new AdamOptimizer<float>())
    .BuildAsync(features, labels);

var prediction = result.Predict(newSample);

API Reference

Key namespaces in the AiDotNet framework.

AiDotNet

Core interfaces and the AiModelBuilder facade.

AiDotNet.NeuralNetworks

100+ neural network architectures: CNN, RNN, Transformer, GAN, VAE, GNN.

AiDotNet.Classification

50 classification algorithms: SVM, Random Forest, Gradient Boosting.

AiDotNet.Regression

57 regression algorithms: Linear, Ridge, Lasso, ElasticNet, SVR.

AiDotNet.ComputerVision

115+ vision models: YOLO, DETR, Faster R-CNN, SAM, OCR.

AiDotNet.Audio

250+ audio models: Whisper, Wav2Vec2, TTS, Speech Enhancement.

AiDotNet.ReinforcementLearning

50+ RL agents: DQN, PPO, SAC, DDPG, MCTS.

AiDotNet.LoRA

50+ PEFT adapters: QLoRA, DoRA, AdaLoRA, VeRA, GaLore.

Ready to build?

Install AiDotNet and start building AI applications in pure .NET.

dotnet add package AiDotNet