Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Build A Machine Learning Model With ML.NET CLI
WhatsApp
Habibul Rehman
5y
15.6k
0
9
300
Article
SentimentAnalysisMLApp.zip
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
.NET Core SDK
ML.NET CLI
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.
dotnet tool install -g mlnet
If the tool installs successfully, you should see the following message.
Step 2 - Create a .NET Core app
mkdir SentimentAnalysisMLApp
cd SentimentAnalysisMLApp
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.
Sentiment SentimentText
1
==RUDE== Dude, you are rude upload that carl picture back,
or
else
.
1
== OK! == IM GOING TO VANDALIZE WILD ONES WIKI THEN!!!
1
==You're cool== You seem like a really cool guy... *bursts out laughing at sarcasm*.
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.
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.
cd SentimentAnalysisModelApp
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.
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.
using
System;
using
SampleBinaryClassification.Model.DataModels;
using
Microsoft.ML;
namespace
consumeModelApp
{
class
Program
{
static
void
Main(
string
[] args)
{
ConsumeModel(args[0]);
}
public
static
void
ConsumeModel(
string
text)
{
// Load the model
MLContext mlContext =
new
MLContext();
ITransformer mlModel = mlContext.Model.Load(
"MLModel.zip"
,
out
var modelInputSchema);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
// Use the code below to add input data
var input =
new
ModelInput();
input.SentimentText = text;
// Try model on sample data
// True is toxic, false is non-toxic
ModelOutput result = predEngine.Predict(input);
Console.WriteLine($
"Text: {input.SentimentText} | Prediction: {(Convert.ToBoolean(result.Prediction) ? "
Toxic
" : "
Non-toxic
")} sentiment"
);
}
}
}
Now run your SentimentAnalysisModelApp.
In your command prompt or power shell, run the following commands. First, make sure you are in SentimentAnalysisModelApp folder.
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.
.NET Core
Machine Learning
ML.NET
ML.NET CLI
Up Next
Ebook Download
View all
Python Libraries for Machine Learning
Read by 801 people
Download Now!
Learn
View all
Finchship
We Provide Web, Desktop and Mobile Apps Solution
Membership not found