Build A Machine Learning Model With ML.NET CLI

ML.NET

 
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.
 

ML.NET CLI

 
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.
 

Introduction

 
In this article, we are going to build an ML model with ML.NET framework using ML.CLI. I've chosen the sentiment analysis binary classification problem and will build, train, and consume the sentiment analysis ML model using ML.NET CLI. I'm using Windows OS for this article but you can also use Linux or macOS with the same scenario.
 
Prerequisites
Let's start,
 
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 you've installed .NET Core SDK, open Command Prompt or Power Shell and run the following command.
  1. dotnet tool install -g mlnet
If the tool installs successfully, you should see the following message.
 
Build A Machine Learning Model With ML.NET CLI
 
Step 2 - Create a .NET Core app
  1. mkdir SentimentAnalysisMLApp  
  2. cd SentimentAnalysisMLApp  
  3. dotnet new console -o SentimentAnalysisModelApp  
  • 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.
Build A Machine Learning Model With ML.NET CLI
 
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.
  1. Sentiment   SentimentText  
  2. 1           ==RUDE== Dude, you are rude upload that carl picture back, or else.  
  3. 1           == OK! ==  IM GOING TO VANDALIZE WILD ONES WIKI THEN!!!   
  4. 1           ==You're cool==  You seem like a really cool guy... *bursts out laughing at sarcasm*.  
  5. 1           ::::: Why are you threatening me? I'm not being disruptive, its you who is being disruptive.   
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.
  1. mlnet auto-train --task binary-classification --dataset "wikipedia-detox-data.tsv" --label-column-name "Sentiment" --max-exploration-time 10  
Build A Machine Learning Model With ML.NET CLI
 
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.
  1. cd SentimentAnalysisModelApp  
  2. dotnet add reference ../SampleBinaryClassification/SampleBinaryClassification.Model/  
Build A Machine Learning Model With ML.NET CLI
 
In your SentimentAnalysisModelApp, install Microsoft.ML Nuget Package.
 
In your command prompt or power shell, run the following commands.
  1. dotnet add package Microsoft.ML --version 1.0.0  
  • Copy the model MLModel.zip from SampleBinaryClassification.Model and paste it into SentimentAnalysisModelApp directory.
Replace the Program.cs code in your SentimentAnalysisMLApp with the following code.
  1. using System;    
  2. using SampleBinaryClassification.Model.DataModels;    
  3. using Microsoft.ML;    
  4.     
  5. namespace consumeModelApp    
  6. {    
  7.     class Program    
  8.     {    
  9.         static void Main(string[] args)    
  10.         {    
  11.             ConsumeModel(args[0]);    
  12.         }    
  13.     
  14.         public static void ConsumeModel(string text)    
  15.         {    
  16.             // Load the model    
  17.             MLContext mlContext = new MLContext();    
  18.     
  19.             ITransformer mlModel = mlContext.Model.Load("MLModel.zip"out var modelInputSchema);    
  20.     
  21.             var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);    
  22.     
  23.             // Use the code below to add input data    
  24.             var input = new ModelInput();    
  25.             input.SentimentText = text;    
  26.     
  27.             // Try model on sample data    
  28.             // True is toxic, false is non-toxic    
  29.             ModelOutput result = predEngine.Predict(input);    
  30.     
  31.             Console.WriteLine($"Text: {input.SentimentText} | Prediction: {(Convert.ToBoolean(result.Prediction) ? "Toxic" : "Non-toxic")} sentiment");    
  32.         }    
  33.     }    
  34. }    
Now run your SentimentAnalysisModelApp.
 
In your command prompt or power shell, run the following commands. First, make sure you are in SentimentAnalysisModelApp folder.
  1. dotnet run "OH MY just CALL THEM ROCK YOU IDIOTS"  

Conclusion

 
So in this article, 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.