How to Build A Machine Learning Model With ML.NET CLI?. The Complete AI ML.NET Developer Course 2023 [Videos].

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.
Lets 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 youve 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.

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.

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           ==Youre cool==  You seem like a really cool guy... *bursts out laughing at sarcasm*.  
  5. 1           ::::: Why are you threatening me? Im 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  

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/  


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;    
  5. namespace consumeModelApp    
  6. {    
  7.     class Program    
  8.     {    
  9.         static void Main(string[] args)    
  10.         {    
  11.             ConsumeModel(args[0]);    
  12.         }    
  14.         public static void ConsumeModel(string text)    
  15.         {    
  16.             // Load the model    
  17.             MLContext mlContext = new MLContext();    
  19.             ITransformer mlModel = mlContext.Model.Load("MLModel.zip"out var modelInputSchema);    
  21.             var predEngine = mlContext.Model.CreatePredictionEngine(mlModel);    
  23.             // Use the code below to add input data    
  24.             var input = new ModelInput();    
  25.             input.SentimentText = text;    
  27.             // Try model on sample data    
  28.             // True is toxic, false is non-toxic    
  29.             ModelOutput result = predEngine.Predict(input);    
  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"  


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.

    See All

    Comments (336 Comments)

    Submit Your Comment

    See All Posts

    Related Posts

    AI ML.NET / Blog

    How to start with Machine Learning .NET (ML.NET)?

    In Build 2018, Microsoft introduced the preview of ML.NET (Machine Learning .NET) which is a cross-platform, open-source Machine Learning Framework. Yes, now it is easy to develop our own Machine Learning application or develop custom modules using Machine Learning Framework. ML.NET is a Machine Learning framework that was mainly developed for .NET developers. We can use C# or F# to develop ML.NET applications. ML.NET is open source and cross-platform and can run on Windows, Linux, and macOS. ML.NET is still in development and now we can use the preview version to work and play with ML.NET.
    7-Feb-2022 /25 /336

    AI ML.NET / Blog

    What Is Web3?

    Web3, or Web 3.0, is the next generation of the Web. Tim Barners-Lee called it the Semantic Web. Co-founder of Ethereum, Gavin Wood, who believes decentralized technologies will be the next generation of the Web, coined the term Web3 in 2014. And recently, Elon Musk asked on Twitter if anyone has seen web3. Twitter founder Jack Dorsey replied, "Its somewhere between a and z."
    7-Feb-2022 /25 /336

    AI ML.NET / Blog

    What Is Metaverse?

    The metaverse is a new buzzword that everyone is talking about today. If you pay attention to any trending technologies, Im sure you have heard of the metaverse by now. Some experts are calling it the next internet or the next computing technology. What is this metaverse? Why is it a hot topic today? How will the metaverse change the computing world? What role will the metaverse play in the future? Lets get all these answers right here, right now.
    7-Feb-2022 /25 /336