Build your first ML-Model with ML.NET Model Builder
Published Apr 22 2024 12:00 AM 11.3K Views
Copper Contributor

I am Hamna Khalil, a Beta Microsoft Learn Student Ambassador from Pakistan. Presently, I am in my third year of pursuing a bachelor's degree in Software Engineering at 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. 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 1: Download and install

Hamna_Khalil_0-1713521569419.png

Hamna_Khalil_1-1713521568392.png

 

Hamna_Khalil_2-1713521566735.png

 

Step 2: 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.

Hamna_Khalil_3-1713521567753.png

  • Select C# Console App project template.

Hamna_Khalil_4-1713521568668.png

  • Change the project name to myMLApp. Ensure that Place solution and project in the same directory is unchecked.

Hamna_Khalil_5-1713521569120.png

  • Select the Next button.
  • Select .NET 8.0 (Long Term support) as the Framework.

Hamna_Khalil_6-1713521566838.png

  • Select the Create button. 

 

Step 3: 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.

Hamna_Khalil_7-1713521567400.png

  • 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.

Hamna_Khalil_8-1713521569365.png

 

Step 4: Choose machine learning scenario

To build your sentiment analysis model, you first need to select your machine learning scenario. 

Hamna_Khalil_9-1713521570559.png

  • In the Model Builder Scenario screen, choose the Data classification scenario.

Hamna_Khalil_10-1713521566544.png

 

Step 5: Select training environment

After selecting the Data classification scenario, choose your training environment.

  • Select the Local (CPU).

Hamna_Khalil_11-1713521570404.png

 

Step 6: 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. 

Hamna_Khalil_12-1713521566897.png

  • 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". 

Hamna_Khalil_14-1713521572712.png

 

Step 7: 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.

Hamna_Khalil_15-1713521572719.png

 

Step 8: 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.

Hamna_Khalil_16-1713521573218.png

 

Step 9: 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.

Hamna_Khalil_18-1713521573779.png

  • 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}");

 

 

Hamna_Khalil_19-1713521573786.png

 

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.

Hamna_Khalil_20-1713521573823.png

 

Congratulations, you have successfully built and executed your first machine learning model with ML.NET Model Builder in Visual Studio!

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 equipped to seamlessly integrate advanced machine learning functionalities into your .NET applications.

 

Additional Resources

.NET

ML.NET

ML for Beginners 

Global AI Community

Microsoft AI Discord Community

Co-Authors
Version history
Last update:
‎Apr 25 2024 08:04 AM
Updated by: