ML.Net 1


1. 定义数据结构
2. 读取训练数据
3. 选择向量
4. 训练模型
5. 预测

实现:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;namespace MLNetLab
{// 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 trainingpublic 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 operationspublic class IrisPrediction{[ColumnName("PredictedLabel")]public string PredictedLabels;}public class IrisRunner{private static string dataPath = ConfigurationManager.AppSettings["iris_file_name"];public static void Go(){// STEP 2: Create a pipeline and load your datavar pipeline = new LearningPipeline();pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(separator: ','));// STEP 3: Transform your data// Assign numeric values to text in the "Label" column, because only// numbers can be processed during model trainingpipeline.Add(new Dictionarizer("Label"));// Puts all features into a vectorpipeline.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 setvar model = pipeline.Train<IrisData, IrisPrediction>();// STEP 6: Use your model to make a prediction// You can change these numbers to test different predictionsvar 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}");}}
}


调用:

 static void Main(string[] args){SpeechSynthesizer synthesizer = new SpeechSynthesizer();synthesizer.Volume = 100;  // 0...100synthesizer.Rate = -3;     // -10...10// Synchronoussynthesizer.Speak("Hello , Microsoft");// Asynchronous//synthesizer.SpeakAsync("Hello World");Console.ReadLine();}