From the course: Machine Learning with ML.NET

Getting started with ML.NET - .NET Tutorial

From the course: Machine Learning with ML.NET

Getting started with ML.NET

- In this video, we'll be looking at how to get started with ML.NET. There are three ways to use ML.NET. You can use ML.NET through a framework API, so you can write code in C sharp and F sharp, so this is a code for this way of getting started. You can get started with a visual tool called Model Builder, where, inside of Visual Studio, you can use a GUI base to build custom machine learning models. You can also use the ML.NET CLI for doing cross-platform development on Mac and the Linux as well. Let's look at what are the basics of a machine learning workflow. There are three key tenants of a machine learning workflow. One is around preparing your data. The second is around building and training your machine learning model, where you'll get to evaluate different algorithms with different settings and see which is the best model, and the last part is going to be around, once you have your model, how do you consume the model in your application, which could either be a web app, a desktop app, a mobile app, gaming app, or any kind of application. And so there are five key steps. The first two are around connecting to your data, whether it's stored in a database or in a CSV file, and you're extracting your features, which is more relevant to your problem. So, for example, for sentiment analysis, you want to extract sentiment text as the features of your model. Then you want to train the model on using different algorithms, different settings, and then you want to evaluate the model which performs best for your scenario. Lastly, you want to consume the model, where you can take this model and use it in any application. Now we'll look at how can you get started with the machine learning workflow in ML.NET across all the three ways that we have. The first way that we look at is getting started with ML.NET using Model Builder. So, this is Visual Studio 2019. You can also use it in 2017 as well, where, once you install the model builder extension, machine learning is enabled for all .NET projects. In this case, I have a console app. So I can right-click ConsoleApp and say, "Add", "Machine Learning", which will launch Model Builder as a tabbed window in the Visual Studio, too. I can start by choosing a scenario. In this case, the scenarios are around whether I want to do Sentiment Analysis, where I want to classify sentiments as positive or negative. I can do issue classification, where I want to classify issues into three different categories, or price prediction, where I want to predict a price of taxi fare, or I can build my custom scenario as well. So I'll choose Sentiment Analysis. The next step in the process is around data, where you connect your data, you load your data, and you extract the features which are relevant to your scenario. In this case, I can either connect to File or SQL Server, so I'm going to choose File, and I can upload either a CSV or a TSV file. So, I can launch this. I can go to my desktop and I can upload the Wikipedia dataset. Now, what this screen shows you is a sample of the first 10 rows of your data. So, you can see what kind of sentiment you get for that particular sentiment text. The next step in the data process is choosing what do you want to predict. So in this case, I'm going to predict the sentiments. This is the output of your machine learning model process. So I'm going to choose Sentiment as the column I want to predict. The third step is around training. So here, Model Builder uses automated machine learning under the covers, which explores various models and settings, so that you as a developer, don't have to worry about the intrinsics of machine learning algorithms and setting and figuring out which is the best model to use. You can run this tool for a certain number of seconds. The longer that you run it for, the better model that you'll get. And in the next step, you can evaluate the performance of this model. So, in step 4, what I'm showing over here is what was the current best model, which was SdcaLogisticRegressionBinary. This was the accuracy that I got, and the tool also explored different models, so here's a summary of all the different models that were tried, and I can see what was the accuracy of the other models as well. The last step in this process is around consuming the model. So in this case, I can hit "Add Projects", which is going to add two projects to my solution. One is a console app, which has code for how the model was trained in this Model Builder CS, and now the class library, which has the model lot zip file, which is the machine learning model that was trained, that I can include in my application. So, I'm going to add a reference to the generated class library by saying, "Add Reference", and then I'm going to use this code from the screen in my ConsoleApp, where I'm going to try predicting sentiments. So, let's include a few namespaces here. Let's include the namespace to the model input, model output, and so in this case I can say, input.SentimentText ="This is rude". So, here is where I can try out different kinds of input, and in my output, I can say, "Console.WriteLine", and I can predict the result of calling this model on this particular input. So, let's go ahead and run this application. And you'll see that the predicted sentiment was true for this particular comment. So, we saw, this was a very quick overview of how to get started with ML.NET Model Builder as a way to build custom machine learning model. Our next step in this process is around getting started with the ML.NET CLI. So we'll take a look at how do you get started with ML.NET on a Mac or Linux. What I have over here is the terminal, and I've installed ML.NET CLI, which is a .NET global tool, and so, if I do ML net... So, this is a CLI command, and I can say "mlnet auto-train", which will list out all the options that are possible on training a machine learning model. So, let's go ahead and train a custom machine learning model for Sentiment Analysis. What I have done over here is I've downloaded the Wikipedia dataset, which has a set of sentiment and sentiment text, and I'm going to go ahead and create a .NET application as the first step. And so here, I'm going to create a new app, called the ConsumeModelConsoleApp, and so this is a .NET console app, where we'll add this machine learning model to our application. Next step is around training the machine learning model. So in this case, I'm going to use a command called "mlnet auto-train" with the binary classification, "task" and with the dataset as the Wikipedia dataset, and in this case, the label column name, which is the column that you want to predict, or the output of your machine learning model as "Sentiment", and I'm going to train the model for 10 seconds. Now, the longer that you train for, the better model that you'll get. When I start training, what happens behind the covers is the CLI uses automated machine learning, which explores different models with various settings to give you the best possible model for your scenario. So you, as a developer, don't have to worry about learning the intrinsics and fine-tuning the machine learning models. So in this case, I got a summary saying that there were about five models tried, the best model was Averaged Perceptron with about a 70% accuracy, and it created two projects, one, a class library project to consume the model, and second a console app, which has code for how the model was trained. So, let's go ahead and add this model library to our application. So, I'm going to go to my app, which is the ConsumeModelApp, and I'm going to do two steps here. First is adding a reference to the class library and second is copying the model file to this application. So, let's go ahead and add a reference to the generated class library, and let's go ahead and add the machine learning model as well. So you're going to copy the machine learning model, and we're going to add a reference to ML.NET as a framework, by using this command. And so, now we're going to go back into VS Code, and we're going to open this project that we just created, and so far, this project has nothing, as you can see, just ConsoleApp, and now we'll add code to consume the model that we just added. So, this is the code to consume the model in a ConsoleApp. I'll create an ML context, which is a machine learning context for ML.NET to do different tasks. First, task will be around loading the model, creating the prediction engine, and then creating input file, my model input, where I'm going to give the sentiment that I want to predict, and I'm going to output the sentiment of my application. So, now that we've added code to consume the model, let's see what the output of this code looks like. So I'm going to go back to my terminal, and I'm going to run this application. And so, when I say ".NET run", it's going to take that text and it's going to predict whether this comment was a toxic sentiment or not, and in this case, it turns out that this is a toxic sentiment. So, we just saw how to get started with ML.NET on a Mac or a Linux machine. Now that we're back in Windows, we'll look at the third way of getting started with ML.NET, which is through the ML.NET API. So let's switch over to Visual Studio. This is a console application, which is a document called ConsoleApp, and I'm going to show you how to do the same scenario of sentiment analysis using the ML.NET API. So, this is the dataset that I have, which is the Wikipedia dataset. It has two columns, Sentiment and SentimentText, and I'm going to train a machine learning model, which is going to take all this SentimentText and is going to predict what is the actual sentiment. The first thing I want to show you as part of the machine learning workflow is around loading data and extracting features. And to do that, we start by creating a new ML context. So, this is a context that is going to hold all the ML.NET intrinsics. This is similar to Entity Framework context or any other contexts that you're used to in the .NET ecosystem. Then I'm going to load my data into the mlContext pipeline. So, the step over here is around loading this Wikipedia dataset. I have defined my model input type, which is going to be, what does the input columns look like. In this case, I have two columns, Sentiment and SentimentText. So I'm going to load the data into these types. The second step around in the machine learning processes after you've loaded your data, is around extracting features and choosing a machine learning algorithm. And, so in this case, the only features I have is the SentimentText. So I'm going to do text featurization the next step is around adding adding an algorithm for binary classification. In this case, I'm going to choose a trainer called SdcaLogisticRegression, specify what is the output column I want to predict, in this case, Sentiment, and give it the input features for training. So, in this case, SentimentText. I'm going to train this particular machine learning model, and then I'm going to evaluate the performance of this model by checking what was the accuracy that I get of this model? The last step in this process is around trying the model. So, once I've trained the model, I can create a prediction engine, which is a prediction engine I can use to predict the outcome of a certain thing. In this case, I'm going to create a sample input, that I'm going to call in the prediction engine, which is going to return me to what was the result of this prediction. So, let's go ahead and run this particular application. And so, the steps that this is going to go through is, it's going to load the data, it's going to train the model, and then it's going to create the prediction engine pool, and it's going to try the model with a sample input that I gave. And so, in this case, what it predicted for this input text, "This was rude" was this is a toxic sentiment. So, just to summarize, this was an easy way to get started with the ML.NET API, where the first step is around creating the ML context, the second step is around loading data, and then around extracting features and adding a machine learning algorithm. Next step is around training the machine learning model and then evaluating the machine learning model, and the last step is around trying the machine learning model with your custom data. So, in this series of video we saw how to get started with ABL.NET. There are three ways that we saw through the code. First approach using the ML.NET API, Visual Studio to UI-based approach using Model Builder, and a cross-platform approach using the ML.NET CLI. In the next video, we will look at how to build a custom machine learning model for doing sentiment analysis.

Contents