ML.NET Documentation
repository·main·Indexed 27 days ago
https://github.com/dotnet/machinelearningA cross-platform, open-source machine learning framework for .NET developers. It enables building, training, and deploying custom ML models for classification, forecasting, and anomaly detection. The framework includes the GenAI Model package for generative AI workflows, featuring Torchsharp implementations of Microsoft's Phi-series models (Phi-2, Phi-3) and integration with Semantic Kernel and AutoGen.Net. It also provides detailed build instructions for MlNetMklDeps binaries across Windows, Linux, and OSX.
What's inside ML.NET
- ML.NET is a framework that provides state-of-the-art machine learning algorithms, transforms, and components. It is designed to be accessible to developers, data scientists, and information workers for use in various products, services, and devices.
Overview of the ML.NET CLI and AutoML
mainThe ML.NET CLI is a command-line tool designed to democratize machine learning for .NET developers. It automates the process of building custom models, even for those without deep data science expertise. The CLI leverages the .NET AutoML API to perform several foundational tasks:
- End-to-end model generation: Using commands like
mlnet newto generate trained models and the corresponding C# implementation code. - Feature Engineering: Automatic selection of input variables (columns) and automatic featurization.
- Model Selection: Automatic sweeping and selection of the best learners (algorithms) and hyperparameters.
The CLI is intended to be cross-platform (Windows, Mac, and Linux) and is designed to be implemented as a .NET Core Global Tool (package name
mlnet) that integrates with thedotnetCLI.- End-to-end model generation: Using commands like
Understand the IDataView Type System
mainThe
IDataViewsystem provides efficient, compositional transformation of schematized data. It is designed for high-dimensional and large datasets, suitable for single-node processing.Key characteristics:
- Immutability:
IDataViewobjects are immutable. Transformations (like normalization) create newIDataViewobjects rather than modifying the original. - Compositionality: New views are formed by applying transformations to existing views.
- Virtualization: Views are virtual and computed on-demand, unlike tables which are fully realized/persisted.
- Schema-driven: Every
IDataViewhas an associatedDataViewSchemadefining columns (names, types, indices, and annotations).
- Immutability:
Use Hogwild Stochastic Gradient Descent (SGD) for binary classification
mainThe Stochastic Gradient Descent (SGD) trainer in ML.NET implements the Hogwild algorithm. This is a stochastic optimization procedure designed for binary classification tasks. It supports multi-threading without locking, making it highly efficient. If your optimization problem is sparse, Hogwild SGD achieves a nearly optimal rate of convergence.Understand ML.NET high-level concepts
mainML.NET is built around several core abstractions used for model training and prediction:
- Data (
IDataView): A lazily-evaluated, immutable, schematized dataset similar to a SQL view. It uses aDataViewSchemato define columns (Name, Type, and Annotations). - Transformer (
ITransformer): A component that takes anIDataViewand returns a new, transformedIDataView. Transformers are lazy; computation only occurs when a cursor consumes the data. - Estimator (
IEstimator<T>): An object that learns from data. Calling.Fit(data)on an estimator produces aTransformer(the trained model). - Data Loader (
IDataLoader<TSource>): A component used to createIDataViewfrom a source (e.g., a file or an object). Loaders are also lazy. - Prediction Function (
PredictionFunction<TSrc, TDst>): A specialized object for high-performance, single-row predictions, avoiding the overhead of batch processing. - MLContext: The central entry point and catalog for all ML.NET operations, including data loading, transformations, and trainers.
- Data (
Understand ML.NET CLI generated assets
mainThe ML.NET CLI with AutoML generates several assets as part of its operation:
- Model .ZIP files: The trained models (defaults to the 'best model', but multiple can be generated using the
--best-models-countargument). - Generated Projects: C# code files for both training and scoring.
- Results Report: An HTML report containing model metrics, performance charts, and model explainability (feature importance).
- Model .ZIP files: The trained models (defaults to the 'best model', but multiple can be generated using the
Use LightGBM based transformers in ML.NET
mainThe
Microsoft.ML.LightGbmpackage provides LightGBM based transformers, which utilize a gradient boosting framework with tree-based learning algorithms. This package can be used for:- Classification/Categorization: e.g., dividing customer feedback into positive and negative categories.
- Regression: e.g., predicting continuous values like house prices based on size and location.
- Recommendations: e.g., suggesting products to shoppers based on purchase history.
Use Microsoft.ML.OnnxTransformer to run ONNX models
mainThe
Microsoft.ML.OnnxTransformerpackage allows you to integrate pre-trained ONNX models into your ML.NET pipelines. It handles data interop automatically for any type of ONNX model and supports image featurization using pre-trained Deep Neural Network (DNN) models.To get started, you can explore the Machine Learning Samples repository for practical implementation examples.
Understand ML.NET package dependencies and architecture
mainML.NET is a modular library system. The core package,
Microsoft.ML, is largely managed code and has no external dependencies. Most specialized algorithms, framework bindings (like TensorFlow or ONNX), and data loaders are provided via separate NuGet packages to allow for opt-in dependencies.Key Architectural Notes:
- Core:
Microsoft.MLprovidesMLContext, core transforms, and basic trainers. - Native Dependencies: Some packages require native libraries (e.g.,
Microsoft.ML.LightGBMrequiresLightGBM,Microsoft.ML.ImageAnalyticsrequireslibSkiaSharp). - Intel Acceleration: Packages like
Microsoft.ML.MKL.ComponentsandMicrosoft.ML.TimeSeriescan leverage Intel MKL for performance, but this is limited to x86/x64 architectures. - Framework Wrappers: Several packages act as wrappers for external engines, such as
Microsoft.ML.TensorFlow(TensorFlow),Microsoft.ML.OnnxTransformer(ONNX Runtime), andMicrosoft.ML.TorchSharp(libTorch).
- Core:
Explore ML.NET Key Features
mainML.NET is a cross-platform open-source machine learning framework for .NET developers. It supports a wide range of machine learning tasks, including:
- Classification/Categorization: Dividing data into categories (e.g., sentiment analysis).
- Regression: Predicting continuous values (e.g., house prices).
- Anomaly Detection: Identifying outliers (e.g., fraudulent transactions).
- Recommendations: Suggesting items based on user history.
- Time series/sequential data: Forecasting future values (e.g., weather or sales).
- Image classification: Categorizing images.
- Text classification: Categorizing documents by content.
- Sentence similarity: Measuring similarity between sentences.
Use Microsoft.ML.ImageAnalytics for image processing
mainTheMicrosoft.ML.ImageAnalyticspackage extends ML.NET with image processing transformer components. It is primarily used for tasks such as image classification (e.g., categorizing pathologies in medical images).Use FastTree decision tree trainers and featurizers
mainThe
Microsoft.ML.FastTreepackage provides decision tree-based trainers for various machine learning tasks, including classification, regression, and ranking. It is an extension to the mainMicrosoft.MLpackage.Key capabilities include:
- Classification/Categorization: Dividing data into categories (e.g., positive/negative feedback).
- Regression: Predicting continuous values (e.g., house prices).
- Recommendations: Suggesting items based on historical data.
- Ranking: Ordering items based on relevance.