Note: Currently, ML.NET CLI is in Preview and only supports the latest LTS version of the .NET SDK (.NET 6).
Once you've installed the .NET 6 SDK, open a new terminal and run the following command if you're on a x64 machine:
Command prompt
dotnet tool install -g mlnet-linux-x64
对于 ARM64 芯片体系结构,请改为运行以下命令:
Command prompt
dotnet tool install -g mlnet-linux-arm64
如果工具成功安装,应会看到以下输出消息,其中 [arch] 是芯片体系结构:
Command prompt
You can invoke the tool using the following command: mlnetTool 'mlnet-linux-[arch]' (version '16.13.9') was successfully installed.
Command prompt
dotnet tool install -g mlnet-osx-x64
对于 ARM64 芯片体系结构,请改为运行以下命令:
Command prompt
dotnet tool install -g mlnet-osx-arm64
If the tool installs successfully, you should see an output message where [arch] is the chip architecture similar to the following:
Command prompt
You can invoke the tool using the following command: mlnetTool 'mlnet-osx-[arch]' (version '16.14.3') was successfully installed.
Note: If you're using a console other than Bash (for example, zsh, which is the new default for macOS), then you'll need to give mlnet executable permissions and include mlnet to the system path. Instructions on how to do this should appear in the terminal when you install mlnet (or any global tool). In general, the following command should work for most systems: chmod +x [PATH-TO-MLNET-CLI-EXECUTABLE]
或者,可以尝试使用以下命令运行 mlnet 工具:
Command prompt
~/.dotnet/tools/mlnet
如果该命令仍显示错误,请使用下面的“我遇到了问题”按钮报告问题并获取解决问题的帮助。
创建应用
打开 Visual Studio 并新建 .NET 控制台应用:
从 Visual Studio 2022 开始窗口中选择 新建项目。
选择 C# 控制台应用 项目模板。
将项目名称更改为 myMLApp。
确保不选中将解决方案和项目置于同一目录中。
选择“下一步”按钮。
选择 .NET 7.0 (标准期限支持) 作为 Framework。
选择“创建”按钮。Visual Studio 将创建项目并加载 Program.cs 文件。
添加机器学习
右击 解决方案资源管理器 中的 myMLApp 项目,并选择 添加 > 机器学习模型。
在“添加新项目”对话框中,确保选中“机器学习模型(ML.NET)”。
将“名称”字段更改为 SentimentModel.mbconfig,然后选择“添加”按钮。
一个名为 SentimentModel.mbconfig 的新文件将添加到你的解决方案中,并且 Model Builder UI 将在 Visual Studio 的新停靠工具窗口中打开。mbconfig 文件只是一个 JSON 文件,用于跟踪 UI 的状态。
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 上使用自助学习模块继续学习,你将使用传感器数据来检测制造设备是否已损坏。