-
-
Notifications
You must be signed in to change notification settings - Fork 10
Getting Started
This guide will help you get up and running with AiDotNet in minutes.
- .NET 8.0 SDK or later
- Visual Studio 2022, VS Code, or JetBrains Rider
- (Optional) CUDA-capable GPU for accelerated training
dotnet add package AiDotNetInstall-Package AiDotNetAdd to your .csproj file:
<PackageReference Include="AiDotNet" Version="*" />For more detailed installation options, see the Installation Guide.
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
All AiDotNet functionality goes through the AiModelBuilder. Here's a complete example:
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}");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}");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}");The AiModelBuilder<T, TInput, TOutput> is the only entry point for building models:
-
T- The numeric type (usuallyfloatordouble) -
TInput- Input data type (e.g.,double[],Tensor<T>,string) -
TOutput- Output type (e.g.,intfor classification,doublefor regression)
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();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;Now that you have the basics, explore these topics:
- Neural Networks - Deep learning architectures
- Classical ML - Traditional machine learning algorithms
- Computer Vision - Image processing and detection
- NLP - Natural language processing
- RAG & LLMs - Retrieval-augmented generation
- Reinforcement Learning - RL agents and environments
Want to try AiDotNet without installing anything? Visit our Interactive Playground to write and run code directly in your browser!
- FAQ - Common questions and answers
- Troubleshooting - Solutions to common issues
- GitHub Issues - Report bugs or request features
Getting Started
Core Concepts
Reference
Community