Introduction To ML.NET &#8211; An ML Framework For DOTNET Developers
ML.NET is an open-source Machine Learning framework developed by the .NET Foundation, an organization incorporated by the Microsoft Corporation. Basically, it is a robust library which .NET developers can use to implement Machine Learning projects coded in C# or F# language across several Microsoft products. When used with NimbusML, it also supports Python-coded models. The Operating Systems (OS) which it supports are Windows, Linux and macOS. It is a cross-platform library which means, your code will run natively on any of these three compatible OS irrespective of the programming language used. ML.NET was originally written in C++ and C# languages. Its initial release was introduced on May 7, 2018. The latest version till date is ML.NET 1.5.4 which was released on December 17, 2020. It is supported on .NET Core (version 2.0 and above) as well as .NET Framework (version 4.6.1 and above). It currently works well on x64 and x86 process architectures. The framework enables integrating Machine Learning projects into .NET apps even if one does not have any prior experience handling ML tasks. A .NET developer can use the libraries and functionalities provided by the .NET ecosystem itself for building, training and shipping custom ML models with ML.NET. Have a look at how some of the leading companies of the world leverage ML.NET: SigParserAsgard SystemsendjinMicrosoft Real Estate & SecurityPower BIWilliams MullenMicrosoft Defender ATPBrenmorEvolution SoftwareScancam Steps to handle an ML scenario with ML.NET Here, we explain an example of performing a sentiment analysis task using ML.NET. Create ML.NET context Similar to a DbContext used to handle operations of Entity Framework, MLContext is required to load and transform data, select an appropriate algorithm for the ML task and train the model. Instantiate MLContext as follows: var object_name = new MLContext(); For instance, var my_context = new MLContext(); Read this to get a detailed explanation of the MLContext class. Load the data ML.NET represents data in the form of IDataView. Using LoadTextFromFile, you can load the data from some file or real-time streaming source into a IDataView which is nothing but a representation of your data in a systematic tabular form. It supports .txt, .csv and several other file formats for data loading. IDataView train_Data = mlContext.Data.LoadFromTextFile<SentimentInput (dataPath, separatorChar: ',', hasHeader: true); Visit this page to know more about data loading. Transform the data ‘Transformers’ in ML.NET pre-process your data and make it suitable for the ML model. // Convert sentiment text into numeric features IEstimator<ITransformer> dataTransformPipeline = mlContext.Transforms.Text.FeaturizeText("Features", "SentimentText"); Know more about data transformation here. Select the best-suited algorithm With ML.NET, you can use more than 30 ML algorithms (also known as ‘trainers’), some of which are as follows: Type of taskAvailable algorithmBinary classificationAveragedPerceptronTrainer, SdcaLogisticRegressionBinaryTrainerMulti-class classificationLightGbmMulticlassTrainer, OneVersusAllTrainerRegressionLbfgsPoissonRegressionTrainer, FastTreeRegressionTrainerClusteringKMeansTrainerAnomaly detectionRandomizedPcaTrainerRankingLightGbmRankingTrainer, FastTreeRankingTrainerRecommendationMatrixFactorizationTrainer IEstimator<ITransformer> trainer = mlContext.BinaryClassification.Trainers.AveragedPerceptron(labelColumnName: "Sentiment", featureColumnName: "Features")); IEstimator<ITransformer> trainingPipeline = dataTransformPipeline.Append(trainer); Refer to this link to get detailed information about the supported ML tasks and relevant algorithms. Training the model ML.NET uses a ‘lazy-loading’ approach. That is, data transformation and application of an algorithm done in the above steps occur only after you call the Fit() method to fit the model on your training data. ITransformer model = pipeline.Fit(train_Data); Go through this page to know more about model training. Model Evaluation ML.NET can evaluate your model based on several performance metrics such as: Accuracy, R-Squared, AUC and RMSE. // Make predictions on test data IDataView pred = model.Transform(testDataView); // Evaluate model and return metrics var metrics = mlContext.BinaryClassification.Evaluate(pred, labelColumnName: "Sentiment"); // Print out accuracy metric Console.WriteLine("Accuracy" + metrics.Accuracy); Explore more about evaluation metrics here. Deploy and consume the model Save your model as a binary file so that it can later be integrated into your .NET application my_context.Model.Save(model, train_Data, "my_model.zip"); Load the saved model into .NET applications MLContext my_context = new MLContext(); DataViewSchema predictionPipelineSchema; ITransformer trainedModel = mlContext.Model.Load("my_model.zip", out predictionPipelineSchema); Make predictions using the loaded model using one of the following ways: Prediction Engine (API for single predictions)Transform method (for batch predictions) var predEngine = mlContext.Model.CreatePredictionEngine(model); #Define the input SentimentInput ip = new SentimentInput{ SentimentText = "Analytics India Magazine”}; #Predict output for ip SentimentOutput result = predEngine.Predict(ip); #Print out the predicted output Console.WriteLine(result.Prediction); See how to use the trained model here. To ease the process of integrating ML tasks with .NET apps, ML.NET provides two major tools as follows. Both of them support AutoML, a process of automating selecting an optimized algorithm and parameters depending on the ML task to be accomplished. ML.NET CLI If you use ML.NET API without ML.NET CLI, you may at times spend a huge amount of time in choosing the best suitable combination of an optimal algorithm, set of hyperparameters and feature engineering steps. To avoid this overhead, ML.NET offers a .NET Core Tool called ML.NET CLI which currently supports ML tasks such as classification, regression and recommendation. Click here to know how to install the ML.NET CLI tool. After having successfully installed the tool, what you need to do is just give your ML task and training data to it. It automatically generated an appropriate optimal ML model and C# code to run that model in your .NET app. Refer to this page to learn more about ML.NET CLI. 2. ML.NET Model Builder It is a visual interface which .NET developers can use to build, train and deploy custom ML models in Visual Studio. It provides the following functionalities: Connect to data files and databases (Currently supported ones include .tsv files, .csv files and SQL Server databases) Model production and code generation : ML.NET stores models as a .zip file. Model Builder loads the suitable model and generates the code to use the model and the code to retrain it. It also adds a sample console app to run your model and see the results. Performs all tasks on your local machine without requiring cloud environment or any peripheral services. Click here to get detailed information about ML.NET Model Builder. Endnotes This article gives an overview of ML.NET platform, an efficient framework designed for .NET developers to get hands-on experience of Machine Learning tasks without requiring to have prior expertise in dealing with ML workflow. However, it must be noted that ML.NET is a project which is still in its developing phase. Future enhancements of the library will come up with additional capabilities such as support for programming languages other than C# and F#, compatibility with more file formats and databases and so on. In-depth knowledge about ML.NET can be gained by going through the following sources: Official DocumentationGetting started with ML.NET – a quick tutorial ML.NET versionsGitHub repository
ML.NET is an open-source Machine Learning framework developed by the .NET Foundation, an organization incorporated by the Microsoft Corporation. Basically, it is a robust library which .NET developers can use to implement Machine Learning projects coded in C# or F# language across several Microsoft products. When used with NimbusML, it also supports Python-coded models. The […]
ML.NET is an open-source Machine Learning framework for .NET developers to build, train and ship custom models using C# or F# languages.
[]
["machine learning", "TPU", "AI", "sentiment analysis", "ML", "Transformers", "RAG", "anomaly detection", "analytics", "LightGBM"]
["AI", "machine learning", "ML", "analytics", "LightGBM", "Transformers", "RAG", "anomaly detection", "sentiment analysis", "TPU"]
A zealous learner aspiring to advance in the domain of AI/ML. Eager to grasp emerging techniques to get insights from data and hence explore realistic Data Science applications as well.