Build your first ML-Model with ML.NET Model Builder
I’m Hamna Khalil, a Beta Microsoft Learn Student Ambassador from Pakistan. Currently, I’m a third-year student pursuing my bachelor’s degree in Software Engineering from Fatima Jinnah Women University.
Integrating machine learning functionalities into your .NET applications can be both exciting and daunting. However, with the advent of tools like ML.NET Model Builder and Visual Studio’s development environment, the process is significantly streamlined.
In this step-by-step guide, I’ll walk you through the process of building your first machine learning model using ML.NET Model Builder within Visual Studio 2022. From setting up your development environment to training and evaluating your model’s performance, each step is meticulously outlined to ensure a smooth and rewarding experience.
Step#01 Download and install
Download and install Visual Studio 2022 with the .NET desktop development workload selected, including the optional ML.NET Model Builder component.
After enabling ML.NET Model Builder in Visual Studio, download and install ML.NET Model Builder 2022
Step#02 Create .NET console app
To create a .NET console app in Visual Studio, follow these steps:
Open Visual Studio 2022 and select Create a new project.
Select C# Console App project template.
Change the project name to myMLApp. Ensure that Place solution and project in the same directory is unchecked.
Select the Next button.
Select .NET 8.0 (Long Term support) as the Framework.
Select the Create button.
Step#03 Add machine learning model
To add a machine learning model to your .NET console app in Visual Studio, follow these steps:
Right-click on the myMLApp project in Solution Explorer
Select Add > Machine Learning Model.
In the Add New Item dialog, ensure that Machine Learning Model (ML.NET) is selected.
Change the Name field to SentimentModel.mbconfig and select the Add button.
Step#04 Choose machine learning scenario
To build your sentiment analysis model, you first need to select your machine learning scenario.
In the Model Builder Scenario screen, choose the Data classification scenario.
Step#05 Select training environment
After selecting the Data classification scenario, choose your training environment.
Select the Local (CPU).
Step#06 Prepare data
Download the Sentiment Labelled Sentences dataset from the UCI Machine Learning Repository.
Unzip sentiment labelled sentences.zip and save the yelp_labelled.txt file to the myMLApp directory.
Select File as the input data source type.
Browse for the yelp_labelled.txt file. Once selected, a preview of your data will appear in the Data Preview section.
Under Column to predict (Label), select “col1”.
Step#07 Train the model
To train your model using Model Builder with the yelp_labelled.txt dataset, follow these steps:
Adjust Time to train to 60 seconds (you may increase this value if no models are found after training).
Select Start training to start the training process. Once training is done, you’ll see a summary of the training results.
Step#08 Evaluate model performance
The Evaluate step shows the best-performing algorithm and the best-accuracy.
In the Try your model section, the textbox is pre-filled with the first line of data from your dataset. You can modify this input to test different sentiment predictions.
After modifying the input, select the Predict button to see the model’s prediction.
Step#09 Consume the trained model
In the Consume step in Model Builder, a code snippet is provided that generates sample input for the model. Additionally, Model Builder offers two Project templates (a console app and a web API), both designed to consume the trained model.
To consume your trained model, replace the Program.cs code with the following code:
using MyMLApp;
// 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}”);
Step#10 Run and debug
Navigate to the Debug menu and select Start Without Debugging or use the shortcut Ctrl + F5.
After running, you should see the following output, predicting whether the input statement is positive or negative.
Congratulations on successfully building and executing your first machine learning model with ML.NET Model Builder in Visual Studio 2022!
From setting up your development environment to training and evaluating your model’s performance, you’ve acquired the skills to leverage ML.NET’s capabilities for crafting predictive models efficiently. Armed with the power of ML.NET, you’re now ready to seamlessly integrate advanced machine learning functionalities into your .NET applications.
Additional Resources
Microsoft AI Discord Community
Microsoft Tech Community – Latest Blogs –Read More