ML.NET is an opensource and cross-platform machine learning framework supported on Windows, Linux and macOS developed by Microsoft. ML.NET is specially built for .Net developers to let you re-use all the knowledge, skills, code and libraries you already have as a .NET developer so that you can easily integrate ML into your existing Web, Mobile, Desktop, Gaming and IoT apps.
The ML.NET command-line interface (CLI), provides tools for building machine learning model with ML.NET. ML.NET offers AutoML and productive tools to help you easily build, train, evaluate and deploy high-quality custom ML models.
In this Video, we are going to build an ML model with ML.NET framework using ML.CLI. Ive chosen the sentiment analysis binary classification problem and will build, train, and consume the sentiment analysis ML model using ML.NET CLI. Im using Windows OS for this Video but you can also use Linux or macOS with the same scenario.
- .NET Core SDK
- ML.NET CLI
Step 1 - Download and Install
Install .NET Core SDK
To build .NET apps you need to download and install .NET SDK (Software Development Kit). Click here to download and install.
Install ML.NET CLI
Once youve installed .NET Core SDK, open Command Prompt or Power Shell and run the following command.
If the tool installs successfully, you should see the following message.
Step 2 - Create a .NET Core app
- The mkdir command creates a new directory named "SentimentAnalysisMLApp" and cd command put you to the new created directory "SentimentAnalysisMLApp".
- The dotnet new console command create a new .Net Core console application and the -o SentimentAnalysisModelApp parameter creates a new directory where your new console application is stored.
Step 3 - Download dataset
As already specified, we are going to build an ML model for Sentiment Analysis. Download the Wikipedia detox dataset and save it as wikipedia-detox-data.tsv in the "SentimentAnalysisMLApp" directory.
Step 4 - Train model
Now we will train our model using wikipedia-detox-data.tsv dataset file.
In SentimentAnalysisMLApp folder open the Command Prompt or Power Shell and run the following commands.
The mlnet auto-train comman run ML.NET with AutoML to explore many iterations of the models with different transformations algorithms and then chose the best one.
- The --task parameter specifies the ML task which is "binary-classification" in this case.
- The --dataset parameter specifies the dataset file that we want to use for training and testing ML model. In this case, it is "wikipedia-detox-data.tsv".
- The --label-column parameter specifies the column name from the dataset that you want to predict. In this case, we want to predict "Sentiment" column.
- The --max-exploration-time parameter specifies the time that we want to give the ML.NET CLI to explore different ML models. In this case, we have set this to 10 seconds.
Step 5 - Generate Code
The ML.NET CLI adds both machine learning model and machine learning console app into our solution.
- The Model app contains the DataInput and DataOutput models for the ML model. It also contains the build model "MlModel.zip" file having the trained model.
- The Console app contains the code to train, evaluate and consume ML.NET model.
You can run the console app to predict the sentiment for a single statement from the dataset.
Step 6 - Consume the ML model
The ML.NET CLI has generated the trained model and code for you, so you can use the model in our other .NET applications.
From your SentimentAnalysisMLApp, add a reference to the generated library project "SampleBinaryClassification.Model".
In you command prompt or power shell run the following commands.
In your SentimentAnalysisModelApp, install Microsoft.ML Nuget Package.
In your command prompt or power shell, run the following commands.
- Copy the model MLModel.zip from SampleBinaryClassification.Model and paste it into SentimentAnalysisModelApp directory.
Now run your SentimentAnalysisModelApp.
In your command prompt or power shell, run the following commands. First, make sure you are in SentimentAnalysisModelApp folder.
So in this Video, we have learned how to build ML.NET machine learning model using ML.NET CLI.
We have solved this problem in the following below given steps,
- Download and Install the .NET Core SDK (Software Development Kit).
- Install ML.NET CLI.
- Download wikipedia-detox-dataset dataset for training our Machine Learning model.
- Use ML.NET CLI Model builder to build, train, evaluate and consume ML.NET ML model.
- Integrate ML.NET Machine Learning model into a C# Console App application.