Machine Learning For .NET Developers (ML.NET)
Overview of ML.Net
- It’s a Free software machine learning library for the C# and F# programming languages.
- Allows developers to easily build, train, deploy, and consume custom models in their .NET applications.
- Doesn't require prior expertise in developing machine learning models or experience with other programming languages like Python or R.
- Provides data loading from files and databases, enables data transformations, and includes many ML algorithms.
- With ML.NET, you can train models for a variety of scenarios, like Image classification, Stock prediction, sentiment analysis…. etc.
ML.Net Model Builder
ML.Net has intuitive graphical Visual Studio extension to build, train, and deploy custom machine learning models.
Fig. 1. ML.NET Model Builder Interface
- Model Builder uses automated machine learning (AutoML) to explore different machine learning algorithms and settings to help you find the one that best suits your scenario.
- Model Builder generates the code to add the model to your .NET application.
NOTE: You don't need machine learning expertise to use Model Builder. All you need is some data, and a problem to solve!
Steps to build a Machine Learning model using ML.NET model Builder:
- Select Scenario: Choose the scenario type that, you expect, will solve the problem using your data. Each scenario maps to machine learning task, such as:
- Binary or Multi Classification
- Environment: Select the training environment that you want to perform training on it.
- Locally on your machine, or remotely on the cloud.
Fig. 2. Training Environment.
NOTE: Not all scenarios support remotely training!
- Get Data:
- Provide the dataset that will be used to train, evaluate, and choose the best model for your scenario.
- The supported data formats are: (.csv, .txt, .tsv, .jpg, .png, and SQL database).
- Once you provide your dataset, you should determine (label and features) to perform training.
Fig. 3. Preparing Dataset for Training.
- You should specify a time to train (in second).
* Longer training period allows to explore more models.
- Exploring multiple models to find you the best performing model.
- During training, you are not required to enter any input or tuning.
Fig. 4. Training Setup.
- Measuring the trained model accuracy by predicting the labels for unseen data (testing data) and compare the predicted labels with the original labels.
- The dataset will be splitted into (80% training set) and (20% testing set).
- Generates the model file and code that you can add to your .NET application.
- Model Builder will create the following projects:
- ML.ConsoleApp: Console applications to make predictions from your model.
- ML.Model: Model Input, Model Output, Model itself and API.
First ML.NET Project (EXAMPLE):
- Let's build a machine learning model with ML.NET using Visual Studio model builder!
- Text classification project
- To predict if the text is POSITIVE or NEGATIVE sentiment.
- Dataset link: Amazon comments labelled dataset
- Installation and Configuration:
- Install visual studio 2019.
- Open visual studio installer.
- Choose visual studio community 2019, and then select (modify).
- Select (individual components), add (ML.NET Model Builder).
- Install (.NET framework).
- Now open visual studio, start with (Continue without code).
- Go to (tools >> options >> preview features >> enable ML.Net model builder).
- Create New Project
- Search for a template (Windows Forms App (.NET Framework)).
- Select (Next), give a name for your project and then (Create).
- Once your project is created:
- Right click on project name >> Add >> Machine Learning.
- Now, pick a scenario from Model Builder:
- Choose (Text Classification).
- Select Training Environment:
- Train locally on machine.
- Click on (Data) for next step.
- Select File as the input data source type.
- Select the dataset that downloaded from the link, as .txt:
- Now, determine the (label and features) of the dataset to perform training.
- Label: col1, Features: col0
- Click on (Train) for next step.
- Now, set the appropriate time for train and start training .
- Once the training process is completed, the training results will be shown.
- Click on (Evaluate) for next step.
- Now, test and try the model (make prediction with new input).
- Give the model new input sample. Ex: “It’s a good day”.
- Once you click on (Predict), the model prediction result will be shown.
- The prediction results will be whether (1 or 0).
- 1: The input you have entered is a POSITIVE sentiment.
- 0: The input you have entered is a NEGATIVE sentiment.
- Click on (Code) for next step.
- Now, once you click on (Add Projects):
- All training and model code and files are added to your .NET project.
- Two projects will be added:
- Working on simple .NET project based on the trained and saved machine learning model.
- Firstly, install “ML.NET” package.
- Right click on project name >> Manage NuGet Packages >> Select (Browse) >> Search for (Microsoft.ML) and then install it.
- Change the (Platform Target).
- Right click on project name >> choose (Properties) >> select (Build) >> Change the platform target into (X64) >> then select (Rebuild) the project.
- Copy the following selected files from ML.Model and paste them into your project.
- Create the following form.
- Change the (namespace) for the following files and name them as the project name.
- Change the (Active Solution Platform).
- Click on (Build) >> Select (Configuration Manager) >> Change the active solution platform into (X64).
- Now, create an event and write a code for (Predict) button.
- The code performs the following:
- TextBox1 will take the input sample.
- The text you want to predict if it is positive or negative.
- The result of prediction will display in TextBox2.
- If the result of prediction is (0):
- TextBox3 will displays (NEGATIVE Comment).
- TextBox4 will displays the score of prediction.
- If the result of prediction is (1):
- TextBox3 will displays (POSITIVE Comment).
- TextBox4 will displays the score of prediction.
- Once you start running this project, you can make predictions on any “new data sample” using the trained and saved Machine Learning model.
- Prediction results examples:
- POSITIVE Sentiment:
- NEGATIVE Sentiment: