Use ML.NET Model Builder in Visual Studio to train and use your first machine learning model with ML.NET.
Install the ML.NET command line interface (CLI), then train and use your first machine learning model with ML.NET.
None.
10 minutes
An app that can predict whether the text from customer reviews is toxic (negative sentiment) or non-toxic (positive sentiment).
Model Builder ships with Visual Studio version 16.6.1 and later versions when you install one of the .NET workloads. Make sure to have the ML.NET Model Builder component checked in the installer when you download or modify Visual Studio:

ML.NET Model Builder is currently a Preview feature. So, in order to use the tool, you must go to Tools > Options > Environment > Preview Features and enable ML.NET Model Builder:

To build .NET apps, you need to download and install the .NET Core 3.1 SDK (Software Development Kit).
The ML.NET command-line interface (CLI), provides tools for building machine learning models with ML.NET.
Note: ML.NET CLI is currently in Preview.
Once you've installed the .NET SDK, open a new terminal and run the following command:
dotnet tool install -g mlnetIf the tool installs successfully, you should see the following message:
dotnet tool install -g mlnet
You can invoke the tool using the following command: mlnet
Tool 'mlnet' (version '16.1.1') was successfully installed.Note: If you're using a console other than Bash, then you'll need to give mlnet executable permissions and include mlnet to the system path.
If the command still gives you an error, use the I ran into an issue button below to get help fixing the problem.
Open Visual Studio and create a new .NET Core console app:
myMLApp.Right-click on the myMLApp project in Solution Explorer and select Add > Machine Learning.
This opens ML.NET Model Builder in a new docked tool window in Visual Studio. Model Builder will guide you through the process of building a machine learning model in the following steps.
In your terminal, run the following commands:
mkdir myMLApp
cd myMLApp
dotnet new console -o consumeModelAppThe mkdir command creates a new directory named myMLApp, and the cd myMLApp command puts you into the newly created app directory. The dotnet command creates a new C# application of type console for you. The -o parameter creates a directory named consumeModelApp where your app is stored, and populates it with the required files.
Your model training code will be generated in the upcoming steps.
To generate your model, you first need to select your machine learning scenario. Model Builder supports several scenarios:

In this case, you'll predict sentiment based on the content (text) of customer reviews.
In the Model Builder Scenario screen, select the Text Classification scenario, since you are predicting which category a comment falls into (positive or negative).
To generate your model, you need to select your machine learning scenario.
There are several ML scenarios that are supported by the ML.NET CLI:
In this case, you'll predict sentiment based on the content (text) of customer reviews, so you'll use classification.
Download the Wikipedia detox dataset and save it as wikipedia-detox-250-line-data.tsv in the myMLApp directory you created.
Each row in wikipedia-detox-250-line-data.tsv represents a different review left by a user on Wikipedia. The first column represents the sentiment of the text (0 is non-toxic, 1 is toxic), and the second column represents the comment left by the user. The columns are separated by tabs. The data looks like the following:
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!!!
0 I hope this helps.In Model Builder, you can add data from a local file or connect to a SQL Server database. In this case, you'll add wikipedia-detox-250-line-data.tsv from a file.
Select File as the input data source in the drop-down, and in Select a file find and select wikipedia-detox-250-line-data.tsv.
Under Column to predict (Label), select "Sentiment." The Label is what you're predicting, which in this case is the Sentiment found in the first column of the dataset.
The columns that are used to help predict the Label are called Features. In this case, the review comment is the Feature, so leave "SentimentText" checked as the Input Column (Feature).

After adding your data, go to the Train step.
Now you'll train your model with the wikipedia-detox-250-line-data.tsv dataset.
Model Builder evaluates many models with varying algorithms and settings to give you the best performing model.
Leave the Time to train, which is the amount of time you'd like Model Builder to explore various models, as 10 seconds. Note that for larger datasets, you should set a longer training time.
Select Start Training to start the training process. Once training starts, you can see the time remaining.

Once training is done, you can see a summary of the training results.

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.
In your terminal, run the following command (in your myMLApp folder):
mlnet classification --dataset "wikipedia-detox-250-line-data.tsv" --label-col "Sentiment" --train-time 10
The mlnet classification command runs ML.NET with AutoML to explore many iterations of classification models with varying combinations of data transformations, algorithms, and algorithm options and then chooses the highest performing model.
wikipedia-detox-250-line-data.tsv as the dataset (internally, the CLI will split the one dataset into training and testing datasets).While the ML.NET CLI is exploring different models, it displays the following data:
If you want, you can view more information about the training session in the log file generated by the CLI.
The Evaluate step shows you the best-performing algorithm and the best accuracy and lets you try out the model in the UI.
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 hit Predict to try out different Sentiment predictions.
In this case, 1 means negative sentiment and 0 means positive sentiment.
After evaluating and trying out your model, move on to the Code step.
After the ML.NET CLI selects the best model, it will display the training Summary, which shows you a summary of the exploration process, including how many models were explored in the given training time.
While the ML.NET CLI generates code for the highest performing model, it also displays the top models (up to 5) with the highest accuracy that it found in the given exploration time. It displays several evaluation metrics for those top models, including AUC, AUPRC, and F1-score. For more information, see ML.NET metrics.
In the Code step in Model Builder, select Add Projects.
Model Builder adds both the machine learning model and the projects for training and consuming the model to your solution. In the Solution Explorer, you should see the code files that were generated by Model Builder.

myMLAppML.ConsoleApp is a .NET console app that contains ModelBuilder.cs (used to build/train the model) and Program.cs (used to test run the model).
myMLAppML.Model is a .NET Standard class library that contains ModelInput.cs and ModelOutput.cs (input/output classes for model training and consumption), ConsumeModel.cs (class that contains method for model consumption), and MLModel.zip (trained serialized ML model).
The ML.NET CLI adds both the machine learning model and the projects for training and consuming the model to your solution, including:
SampleClassification.ConsoleApp), which contains ModelBuilder.cs (used to build/train the model) and Program.cs (used to run the model).SampleClassification.Model), which contains ModelInput.cs and ModelOutput.cs (input/output classes for model training and consumption) and MLModel.zip (generated serialized ML model).To try the model, you can run the console app (SampleClassification.ConsoleApp) to predict the sentiment of a single statement with the model.
Model Builder generates the trained model and code for you, including the next steps for model consumption, so you can easily use the model in your end-user .NET application.
Replace the Program.cs code in your myMLApp with the following code:
using System;
using MyMLAppML.Model;
namespace myMLApp
{
class Program
{
static void Main(string[] args)
{
// Add input data
var input = new ModelInput();
input.SentimentText = "That is rude.";
// Load model and predict output of sample data
ModelOutput result = ConsumeModel.Predict(input);
Console.WriteLine($"Text: {input.SentimentText}\nIs Toxic: {result.Prediction}");
}
}
}Run myMLApp. You should see the following output, predicting whether the input statement is toxic (true) or non-toxic (false).

The ML.NET CLI has generated the trained model and code for you, so you can now use the model in your other .NET applications (for example, your consumeModelApp console app) by following these steps:
Replace the Program.cs code in your consumeModelApp with the following code:
using System;
using SampleClassification.Model;
namespace consumeModelApp
{
class Program
{
static void Main(string[] args)
{
// Add input data
var input = new ModelInput();
input.SentimentText = "That is rude.";
// Load model and predict output of sample data
ModelOutput result = ConsumeModel.Predict(input);
Console.WriteLine($"Text: {input.SentimentText}\nIs Toxic: {result.Prediction}");
}
}
}Run your consumeModelApp. In your terminal, run the following command (make sure you are in your consumeModelApp directory):
dotnet runCongratulations, you've built your first machine learning model with ML.NET Model Builder!
Now that you've used Model Builder for Classification (specifically, sentiment analysis), you can try other scenarios. Try out the Value Prediction scenario (specifically price prediction) in Model Builder using the Taxi Fare dataset to keep building ML.NET models with Model Builder:
Download the Taxi Fare dataset
You might also be interested in...
Congratulations, you've built your first machine learning model with the ML.NET CLI!
Now that you've used the ML.NET CLI for Classification (specifically sentiment analysis), you can try other scenarios. Try out a Regression scenario (specifically price prediction) using the Taxi Fare dataset to keep building ML.NET models with the ML.NET CLI.
Download the Taxi Fare dataset
You might also be interested in...