ML.NET 專門為.NET開發者提供了一套跨平台的開源的機器學習框架。
ML.NET支持.NET開發者不需要過度專業的機器學習開發經驗,就能輕松地訓練自己的模型,並且嵌入到自己的應用中。一切盡在.NET之中。ML.NET早期是由Microsoft Research開發,近十年來逐步集成到一個大體系中被眾多Microsoft產品使用,如大家熟知的Windows、Bing、PowerPoint、Excel之類。
ML.NET的第一個預覽版提供了分類器(如文本分類、情感分析)和回歸(如價格預測)等實用的機器學習模型。第一版發布后在既有功能之上又新增了關於訓練模型的.NET API,使用這些模型進行預測,就像框架中算法、轉換、數據結構一類核心組件一樣的開發體驗。
接下來用個示例,一起進入快速上手的實踐中來。
-
安裝.NET SDK
為了創建一個.NET應用,首先下載 .NET SDK。
-
創建應用
使用如下命令初始化項目,創建一個控制台應用程序,目標為myApp:
dotnet new console -o myApp cd myApp
-
安裝ML.NET包
使用如下命令安裝Microsoft.ML包:
dotnet add package Microsoft.ML
-
下載數據集
假設我們使用機器學習來預測鳶尾花的類型,比如有setosa、versicolor、virginica三種,基於特征有四種:花瓣長度、花瓣寬度, 萼片長度、萼片寬度。
去UCI Machine Learning Repository: Iris Data Set下載一個現成的數據集,復制粘貼其中的數據到任何一個文本編輯器中,然后保存命名為iris-data.txt到myApp目錄中。
粘貼完文本內容應該是如下格式,每一行表示不同鳶尾花的樣本,數值的部分從左到右依次是萼片長度、萼片寬度、花瓣長度、花瓣寬度,最后是鳶尾花的類型。
5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setosa ...
如果是使用了Visual Studio,將iris-data.txt添加至項目中,需要進行如下配置確保運行時數據集文件在輸出的目錄中。
-
編寫代碼
打開Program.cs文件,輸入以下代碼:
using Microsoft.ML; using Microsoft.ML.Runtime.Api; using Microsoft.ML.Trainers; using Microsoft.ML.Transforms; using System; namespace myApp { class Program { // STEP 1: Define your data structures // IrisData is used to provide training data, and as // input for prediction operations // - First 4 properties are inputs/features used to predict the label // - Label is what you are predicting, and is only set when training public class IrisData { [Column("0")] public float SepalLength; [Column("1")] public float SepalWidth; [Column("2")] public float PetalLength; [Column("3")] public float PetalWidth; [Column("4")] [ColumnName("Label")] public string Label; } // IrisPrediction is the result returned from prediction operations public class IrisPrediction { [ColumnName("PredictedLabel")] public string PredictedLabels; } static void Main(string[] args) { // STEP 2: Create a pipeline and load your data var pipeline = new LearningPipeline(); // If working in Visual Studio, make sure the 'Copy to Output Directory' // property of iris-data.txt is set to 'Copy always' string dataPath = "iris-data.txt"; pipeline.Add(new TextLoader<IrisData>(dataPath, separator: ",")); // STEP 3: Transform your data // Assign numeric values to text in the "Label" column, because only // numbers can be processed during model training pipeline.Add(new Dictionarizer("Label")); // Puts all features into a vector pipeline.Add(new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")); // STEP 4: Add learner // Add a learning algorithm to the pipeline. // This is a classification scenario (What type of iris is this?) pipeline.Add(new StochasticDualCoordinateAscentClassifier()); // Convert the Label back into original text (after converting to number in step 3) pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" }); // STEP 5: Train your model based on the data set var model = pipeline.Train<IrisData, IrisPrediction>(); // STEP 6: Use your model to make a prediction // You can change these numbers to test different predictions var prediction = model.Predict(new IrisData() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }); Console.WriteLine($"Predicted flower type is: {prediction.PredictedLabels}"); } } }
-
運行應用
使用如下命令行運行程序:
dotnet run
在最后一行將輸出對花的預測結果,你可以修改傳給Predict函數各種鳶尾花的特征值看看有什么不同的結果。
恭喜,你已經跨入使用ML.NET進行機器學習的門檻了!