SentimentModel.zip: 该文件是经过训练的 ML.NET 模型,它是一个序列化的 zip 文件。
若要尝试该模型,可以运行控制台应用来使用模型预测单个语句的情绪。
使用模型
最后一步是在最终用户应用程序中使用经过训练的模型。
将 myMLApp 项目中的 Program.cs 代码替换为以下代码:
Program.cs
using MyMLApp;// Add input datavar sampleData = new SentimentModel.ModelInput()
{ Col0 = "This restaurant was wonderful."};// Load model and predict output of sample datavar result = SentimentModel.Predict(sampleData);// If Prediction is 1, sentiment is "Positive"; otherwise, sentiment is "Negative"var sentiment = result.PredictedLabel == 1 ? "Positive" : "Negative";Console.WriteLine($"Text: {sampleData.Col0}\nSentiment: {sentiment}");
using System;namespace SentimentModel.ConsoleApp{ class Program { static void Main(string[] args) { // Add input data SentimentModel.ModelInput sampleData = new SentimentModel.ModelInput() { Col0 = @"Wow... Loved this place." }; // Make a single prediction on the sample data and print results
var predictionResult = SentimentModel.Predict(sampleData); Console.WriteLine("Using model to make single prediction -- Comparing actual Col1 with predicted Col1 from sample data...\n\n"); Console.WriteLine($"Col0: @{"Wow... Loved this place."}"); Console.WriteLine($"Col1: {1F}"); Console.WriteLine($"\n\nPredicted Col1: {predictionResult.PredictedLabel}\n\n"); Console.WriteLine("=============== End of process, hit any key to finish ==============="); Console.ReadKey(); } }}
Using model to make single prediction -- Comparing actual Col1 with predicted Col1 from sample data...Col0: Wow... Loved this place.Col1: 1Predicted Col1: 1=============== End of process, hit any key to finish ===============
后续步骤
恭喜,你已使用 ML.NET Model Builder 构建了首个机器学习模型!
现在你已经掌握了基础知识,请在 Microsoft Learn 上使用自助学习模块继续学习,你将使用传感器数据来检测制造设备是否已损坏。