Get started with ML.NET
In this post I am going to explore ML.NET Programing with Visual Studio 2022 and use the program steps which I have described in my previous post program steps in ML.NET
prerequisite
- Visual Studio 2022
- .NET 7 (or .NET 6) is installed
- .NET desktop development is installed with ML.NET Model builder look to the following figure:
Create your app with Visual Studio 2022
Open Visual Studio and create a new .NET console app:
- Select Create a new project from the Visual Studio 2022 start window.
- Select the C# Console App project template.
- Change the project name to MLApp.
- Make sure Place solution and project in the same directory is unchecked.
- Select the Next button.
- Select .NET 7.0 (Standard Term support) as the Framework.
- Select the Create button. Visual Studio creates your project and loads the
Program.cs
file.
Add machine learning
- Right-click on the MLApp project in Solution Explorer and select Add > Machine Learning Model
2. In the Add New dialog, make sure Machine Learning Model (ML.NET) is selected.
3. Change the Name field to SentimentModel.mbconfig and select the Add button.
A new file named SentimentModel.mbconfig
is added to your solution and the Model Builder UI opens in a new docked tool window in Visual Studio. The mbconfig file is simply a JSON file that keeps track of the state of the UI.
Model Builder will guide you through the process of building a machine learning model in the following steps.
Pick a scenario
To generate your model, you first need to select your machine learning scenario. Model Builder supports several scenarios, I am taking Data classificationhere in the following figure:
Note: If the screenshots don’t match with what you see, you may need to update your version of Model Builder. Go to Extensions > Manage Extensions to make sure that there are no available updates for Model Builder. The version used in this tutorial is 16.14.0.
In this case, you’ll predict sentiment based on the content (text) of customer reviews.
- In the Model Builder Scenario screen, select the Data classification scenario, since you’re predicting which category a comment falls into (positive or negative).
- After selecting the Data classification scenario, you must choose your training environment. While some scenarios support training in Azure, Classification currently only supports local training, so keep the Local environment selected and move on to the Data step and you see the following:
Download and add data
Download the Sentiment Labelled Sentences datasets from the UCI Machine Learning Repository. Unzip sentiment labelled sentences.zip
and save the yelp_labelled.txt
file to the MLApp directory.
Each row in yelp_labelled.txt
represents a different review of a restaurant left by a user on Yelp. The first column represents the comment left by the user, and the second column represents the sentiment of the text (0 is negative, 1 is positive). The columns are separated by tabs, and the dataset has no header. The data looks like the following:
Add data
In Model Builder, you can add data from a local file or connect to a SQL Server database. In this case, you’ll add yelp_labelled.txt
from a file.
- Select File as the input data source type.
- Browse for
yelp_labelled.txt
. Once you select your dataset, a preview of your data appears in the Data Preview section. Since your dataset does not have a header, headers are auto-generated (“col0” and “col1”). - Under Column to predict (Label), select “col1”. The Label is what you’re predicting, which in this case is the sentiment found in the second column (“col1”) of the dataset.
- The columns that are used to help predict the Label are called Features. All of the columns in the dataset besides the Label are automatically selected as Features. In this case, the review comment column (“col0”) is the Feature column. You can update the Feature columns and modify other data loading options in Advanced data options, but it is not necessary for this example.
After adding your data, go to the Train step.
Train your model
Now, you’ll train your model with the yelp_labelled.txt
dataset.
Model Builder evaluates many models with varying algorithms and settings based on the amount of training time given to build the best performing model.
- Change the Time to train, which is the amount of time you’d like Model Builder to explore various models, to 60 seconds (you can try increasing this number if no models are found after training) . Note that for larger datasets, the training time will be longer. Model Builder automatically adjusts the training time based on the dataset size.
- Select Start training to start the training process. Once training starts, you can see the time remaining.
Training results
Once training is done, you can see a summary of the training results.
- Best accuracy – This shows you the accuracy of the best model that Model Builder found. Higher accuracy means the model predicted more correctly on test data.
- Best model – This shows you which algorithm performed the best during Model Builder’s exploration.
- Training time – This shows you the total amount of time that was spent training / exploring models.
- Models explored (total) – This shows you the total number of models explored by Model Builder in the given amount of time.
If you want, you can view more information about the training session in the Machine Learning Output window.
After model training finishes, go to the Evaluate step.
Evaluate your model
The Evaluate step shows you the best-performing algorithm and the best accuracy and lets you try out the model in the UI.
Try out your model
You can make predictions on sample input in the Try your model section. The textbox is pre-filled with the first line of data from your dataset, but you can change the input and select the Predict button to try out different sentiment predictions.
In this case, 0 means negative sentiment and 1 means positive sentiment
Note: If your model is not performing well (for example, if the Accuracy is low or if the model only predicts ‘1’ values), you can try adding more time and training again. This is a sample using a very small dataset; for production-level models, you’d want to add a lot more data and training time.
After evaluating and trying out your model, move on to the Consume step.
Generate code
After training is completed, three files are automatically added as code-behind to the SentimentModel.mbconfig
:
SentimentModel.zip
: This file is the trained ML.NET model, which is a serialized zip file.SentimentModel.consumption.cs
: This file contains the model input and output classes and aPredict
method that can be used for model consumption.SentimentModel.training.cs
: This file contains the training pipeline (data transforms, algorithm, and algorithm parameters) used to train the final model.
In the Consume step in Model Builder, a code snippet is provided which creates sample input for the model and uses the model to make a prediction on that input.
The code of SentimentModel.Consumption.cs is as following.
// This file was auto-generated by ML.NET Model Builder.
using Microsoft.ML;
using Microsoft.ML.Data;
using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
namespace MLApp
{
public partial class SentimentModel
{
/// <summary>
/// model input class for SentimentModel.
/// </summary>
#region model input class
public class ModelInput
{
[ColumnName(@"col0")]
public string Col0 { get; set; }
[ColumnName(@"col1")]
public float Col1 { get; set; }
}
#endregion
/// <summary>
/// model output class for SentimentModel.
/// </summary>
#region model output class
public class ModelOutput
{
[ColumnName(@"col0")]
public float[] Col0 { get; set; }
[ColumnName(@"col1")]
public uint Col1 { get; set; }
[ColumnName(@"Features")]
public float[] Features { get; set; }
[ColumnName(@"PredictedLabel")]
public float PredictedLabel { get; set; }
[ColumnName(@"Score")]
public float[] Score { get; set; }
}
#endregion
private static string MLNetModelPath = Path.GetFullPath("SentimentModel.zip");
public static readonly Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(() => CreatePredictEngine(), true);
/// <summary>
/// Use this method to predict on <see cref="ModelInput"/>.
/// </summary>
/// <param name="input">model input.</param>
/// <returns><seealso cref=" ModelOutput"/></returns>
public static ModelOutput Predict(ModelInput input)
{
var predEngine = PredictEngine.Value;
return predEngine.Predict(input);
}
private static PredictionEngine<ModelInput, ModelOutput> CreatePredictEngine()
{
var mlContext = new MLContext();
ITransformer mlModel = mlContext.Model.Load(MLNetModelPath, out var _);
return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
}
}
}
Model Builder also offers Project templates that you can optionally add to your solution. There are two project templates (a console app and a web API), both which consume the trained model.
Next step is consume your model.
Consume your model
The last step is to consume your trained model in the end-user application.
- Replace the Program.cs code in your MLApp project with the following code:
using MLApp;
// Add input data
var sampleData = new SentimentModel.ModelInput()
{
Col0 = "This restaurant was wonderful."
};
// Load model and predict output of sample data
var result = SentimentModel.Predict(sampleData);
// If Prediction is 1, sentiment is "Positive"; otherwise, sentiment is "Negative"
var sentiment = result.PredictedLabel == 1 ? "Positive" : "Negative";
Console.WriteLine($"Text: {sampleData.Col0}\nSentiment: {sentiment}");
Run MLApp (select Ctrl+F5 or Debug > Start Without Debugging). You should see the following output, predicting whether the input statement is positive or negative.
As you see from above the result was:
Text: This restaurant was wonderful.
Sentiment: Positive
Now we have build the first machine learning model with ML.NET Model Builder! and we have got the basics of machine learning.
Conclusion
In this post I have gone through a basic machine learning by the steps: creating MLApp, Pick a scenario, Download Add Data, Training model, Gen Evaluating model, Generating code and Consumption mode..
Source code can be find in my GitHub
My next post explains Taxi Fare Prediction with ML.NET
This post is part of “ML.NET-Step by step”.