Machine Learning is quickly becoming a new strong trend with its full potential waiting to be discovered. If we’re to describe Machine Learning in one sentence, it would be this: an AI-based program that uses algorithms and statistical methods to train itself for performing required tasks better and does it without human intervention. When we’re creating ML systems, we expect them to evolve and reach predicted results, but we need to fetch lots of data for the program to start its learning process.
Historically, machine learning was tied to Python and R. But what to do when your developers don’t have extensive knowledge in any of these? The same question Microsoft engineers asked themselves and as a result, in 2018 officially released the ML.NET framework designed for .NET and C# developers. It is an open-source, cross-platform solution that runs not only on Windows but also on macOS and Linux. In this article, we’re going to explore the features and capabilities of ML.NET and explain how you can create a simple app in a short period of time.
Basics of Machine Learning
Before we proceed to ML.NET and its features, we believe it would be wise to make a quick introduction to Machine Learning. How about a quick example: you want a simple program to analyze photos of typical pets and humans to tell you who is shown in the picture. With Machine Learning enabled, your program will be able to determine whether it is an image of, let’s say, a dog/cat or not. The same program can also be done in the traditional way by writing typical features of animals and humans into the code. ML simplifies this routine as it allows your program to learn from the sets of data and figure out automatically what defines a cat, dog, and human. After analyzing all images, AI will be able to detect and compare various distinctive features: the size of the head, type of the nose, whiskers, claws, and others. The more pictures you “feed” to an ML app, the smarter it gets in terms of distinguishing photographed objects.
We can say that Machine Learning is really about programming the unprogrammable. It allows you to write a function that trains on sets of data to perform its functions. In our particular example, we will get an ML model that has memorized all the main features of pets that distinguish them from humans, so you don’t have to write your own code. The program quickly looks at all the images, and when the learning process is complete, you will have a model that can be used in other applications.
Such advancement in Machine Learning has opened many new possibilities for developers. Today we can use this method to create ML models for various needs. For instance, we can train an ML model to recognize audio and recommend tunes that particular users might like. Or we can use Machine Learning on texts to write sales forecasts, find movie recommendations, etc. Don’t forget the image recognition example we described earlier: when you can detect objects on images, you can build greater user experience for online cataloguing. In addition to everything mentioned above, ML can also be used to teach machines themselves so they can be much smarter. To make Machine Learning models, you need appropriate tools, and ML.NET might be exactly what you need.
Introduction to ML.NET
There are three ways to use ML.NET. Firstly, you can use it through a framework API, enabling you to write code in C# and F#, which is a quick way of getting started. Secondly, you can go for a visual tool called Model Builder – inside of Visual Studio, there is a GUI-based instrument for building custom ML models. Your third option is to choose ML.NET CLI for cross-platform development on Mac and Linux.
Let’s look at the standard machine learning workflow. There are three main stages in it:
- Preparing your data: to get the most realistic prediction results, you need to load lots of data for the model to train on.
- Building and training your ML model: to adjust the model for your needs, you need to select appropriate algorithms.
- Implementation of the model in your applications (web/mobile/etc.): any trained and saved model is ready to be transferred to an ML.NET application.
Each stage has a few consecutive steps. During the first stage, you need to load data and then extract features. So, everything starts with connecting initial data for the model (that data can be stored in a CSV file or database). When it is done, the extraction of the relevant features is required. For example, for sentiment analysis, you want to extract sentiment text to have key elements for your model.
During the second stage, you need to train and then evaluate your ML model. Training requires using different algorithms and settings to make it more efficient. When all training routines are completed, it is important to test and evaluate the model. If you are satisfied with the results, then you can proceed to the final stage. On the last step, you want to take the finalized model and use it in any application of your choice. Once you have created an ML model, you can save it and then use in integrations with other .NET apps.
Notable Features of ML.NET
Let’s take a look at the main features of the tool and the benefits it provides:
- Created for .NET specialists; allows them to use their code for ML-based apps.
- Understands both C# and F# programming languages.
- ML.NET is an open-source framework and supports cross-platform development.
- Available on all major operating systems: Windows, macOS, and Linux.
- Often used in Microsoft Azure, Windows, and Bing, but not limited to these and can be used with other tools (e.g., Accord.NET).
- Allows creating many different types of ML applications, including web, desktop, gaming, mobile, etc.
- All models created with ML.NET can be saved in binary files and then implemented in other .NET applications.
Being developed by Microsoft, ML.NET frequently receives new updates that are always packed with new features as well as bugfixes. To get a better understanding of the process of ML.NET’s evolution and Microsoft’s dedication to the cause, here is a summary of updates in chronological order:
- June 5th, 2018 ML.NET 0.2 (preview version): added new Clustering Tasks for Machine Learning.
- September 12th, 2018 ML.NET 0.5 (preview version): introduced a TensorFlow model scoring transform.
- October 8th, 2018 ML.NET 0.6 (preview version): enabled developers to use pre-trained ONNX models.
- November 8th, 2018 ML.NET 0.7 (preview version): major updated that has become a milestone for the framework. It received both x86/x64 support (previous versions understood only x64). Microsoft started adding new features regularly. In v.0.7, Microsoft enabled the NimbusML support (experimental Python bindings) and added anomaly detection scenarios.
- January 10th, 2019ML.NET 0.9 (preview version): has brought major improvements to the ML.NET API.
- May 6th, 2019 ML.NET 1.0: Automated machine learning feature was added along with the Model Builder tool and ML.NET CLI.
- June 11th, 2019 ML.NET 1.1: received a new Anomaly Detection algorithm.
- July 17th, 2019 ML.NET 1.2: models created in ML.NET can be integrated into web or serverless apps with Microsoft.Extensions.ML integration package.
- October 9th, 2018 ML.NET 1.4 (preview version): rolled out with a database loader that simplified the process of learning for developers that prefer relational databases.
How to Start with ML.NET?
As you see, ML.NET provides a simple and convenient technique for using Machine Learning algorithms in .NET apps without relying on third-party services. The latest update of ML.NET was released at the beginning of November 2019. And it is safe to say that this is already a stable and powerful library that contains enough tools and algorithms for solving various ML-related tasks. We absolutely love the ML.NET Model Builder extension for Visual Studio that makes the process of adding new models super convenient, so we recommend installing it right away. As this article is aiming to be a basic overview of the framework, we’re going to focus on basic tasks that can be performed with the help of ML.NET. This is how you can create a simple .NET Core app by starting a new project in Visual Studio:
After creating your project, you need to right-click its name and look for the “Add” submenu. Open it and then click on Machine Learning:
Then ML.NET Model Builder will ask you what scenario you would like to choose to achieve maximum efficiency. You can select one of the following:
- Sentiment Analysis — classifies data by dividing it into two categories (positive/negative). For example, it can determine whether a commentary is positive or not. Also, this type of analysis can detect spam in emails. In other words, if you need to predict a binary result, then this is going to be your best bet.
- Issue Classification — use this scenario when the result of the prediction is not binary (3+ categories).
- Price Prediction — this option can predict a numerical value based on your data, for example, it can give you a price prediction of the house you’re willing to sell (considering the number of rooms, floor area, yard, etc.).
- Custom scenario — allows you to create custom models, which can be useful when all previous scenarios cannot help you with the task at hand.
After choosing the most suitable scenario, you need to select the data source for your Machine Learning model – it can be either a file or SQL server. Keep in mind that ML.NET Model Builder allows using only CSV and TSV files up to 1GB for this purpose.
Now we can move to the next stage to see training options are available. The system will find the optimal algorithm and start the learning process. As the user, you will be able to set a time limit for learning. It is possible that the amount of time might deviate depending on the task and computing power of your machine. On the image below, you can check the recommended learning time (approximate, of course):
When the training phase is complete, the tool will display the algorithm with the best prediction results and its accuracy. On the fifth step, you can add the code to your project and complete the whole ML model. Before using the model in your apps, however, we recommend testing it first to be sure that your prediction, analysis, or classification works as intended.
You must always consider the balance of quality and quantity of data you are going to “feed” to your model. Most projects do require considerable amounts of data, thus a lot of time to digest it. If you are planning to give your model new relevant data for additional training as you go, then you can start using the model earlier.
To sum it all up, we can say that ML.NET is a fantastic framework with amazing support from Microsoft. It is the best choice for.NET developers interested in Machine Learning. Even if you are not familiar with Machine Learning, it is a good tool to start. With a wide array of features and user-friendly interface, you should not have any problems with makes your first steps in the world of Machine Learning (given you already have some experience with the .NET development). Keep learning, and never stop experimenting! Maybe you have some additional questions of this topic or maybe already want to start a project with ML.NET - in any case, feel free to contact us
anytime for consultation without any obligation!