使用多类分类算法在 ML.NET 中进行批量预测
Batch Predictions in ML.NET with a MultiClass Classification Algorithm
我正在尝试应用 Binary Classification, example to a Multi Class Classification
中所示的批量预测
google 和 Microsoft 工作中演示的许多示例都显示了一个单一的预测来演示训练模型的能力。
但是,我希望通过批量预测 a) 多手动输入和 b) 使用没有标签的文件来证明训练模型的有效性。
我遵循了 Binary Classification example and trying to map it across to the Multi Class Classification 示例,但是,预测没有显示。
以下是多 Class Classification 示例中所示的单个预测
// Single Prediction
ITransformer loadedModel = _mlContext.Model.Load(_modelPath, out var modelInputSchema);
GitHubIssue singleIssue = new GitHubIssue() { Title = "Entity Framework crashes", Description = "When connecting to the database, EF is crashing" }; // Our single issue
_predEngine = _mlContext.Model.CreatePredictionEngine<GitHubIssue, IssuePrediction>(loadedModel);
var singleprediction = _predEngine.Predict(singleIssue);
Console.WriteLine($"=============== Single Prediction - Result: {singleprediction.Area} ===============");
以下是 Binary Classification 示例中所示的批量预测,但是在该场景中不起作用。
// Batch Predictions from Enumerable
ITransformer loadedModel = _mlContext.Model.Load(_modelPath, out var modelInputSchema);
IDataView batchIssues = _mlContext.Data.LoadFromEnumerable(issues);
IDataView predictions = loadedModel.Transform(batchIssues);
IEnumerable<GitHubIssue> predictedResults = _mlContext.Data.CreateEnumerable<GitHubIssue>(predictions, reuseRowObject: false);
foreach (GitHubIssue prediction in predictedResults)
{
Console.WriteLine($"Title: {prediction.Title} | Prediction: {prediction.Area}");
}
根据单个预测,我得到以下结果:
area-System.Data
虽然批处理不预测输出,但我不认为它在预测,但是,阅读 Microsofts ML.NET 网站上的 material 它说转换将使用模型来对这批数据进行预测。
"Use the model to predict the comment data sentiment using the Transform() method"
问题 1) 我不确定,我在批量预测中遗漏了什么可枚举以获得 prediction.area 的预测,如单个值所示。
问题 2) 我如何调整可枚举以加载未标记信息的文件以对其进行预测。
请注意:其中的所有代码都放在多类分类示例的 PredictIssues 方法中。
问题 1 的答案
调整代码以利用 predEngine,然后对 foreach 中的单个预测使用预测函数。
为此需要进行两项更改:
var batchPrediction = _predEngine;
Console.WriteLine($"Prediction: {batchPrediction.Predict(prediction).Area}");
此外,我删除了以下行:
IDataView predictions = loadedModel.Transform(batchIssues);
删除此功能对预测结果没有影响。
似乎有效的完整代码如下:
IEnumerable<GitHubIssue> issues = new[]
{
new GitHubIssue
{
Title = "Entity Framework crashes",
Description = "When connecting to the database, EF is crashing"
},
new GitHubIssue
{
Title = "Github Down",
Description = "When going to the website, github says it is down"
}
};
var batchPrediction = _predEngine;
// Batch Predictions from Enumerable
IDataView batchIssues = _mlContext.Data.LoadFromEnumerable(issues);
IEnumerable<GitHubIssue> predictedResults = _mlContext.Data.CreateEnumerable<GitHubIssue>(batchIssues, reuseRowObject: false);
foreach (GitHubIssue prediction in predictedResults)
{
Console.WriteLine($"Title: {prediction.Title} | Prediction: {batchPrediction.Predict(prediction).Area}");
}
问题 2 的答案
我创建了一个包含 ID、区域(留空)、标题和描述的新文件,并镜像了测试和训练数据文件。
我将两个变量添加到全局范围(就在命名空间下面),如下所示:
private static string _myTestDataPath => Path.Combine(_appPath, "..", "..", "..", "Data", "myTestData.tsv");
private static IDataView _myTestDataView;
我没有创建IEnumerable,而是直接传入了文件,如下:
_myTestDataView = _mlContext.Data.LoadFromTextFile<GitHubIssue>(_myTestDataPath, hasHeader: true);
以下是该方法的完整示例:
ITransformer loadedModel = _mlContext.Model.Load(_modelPath, out var modelInputSchema);
_predEngine = _mlContext.Model.CreatePredictionEngine<GitHubIssue, IssuePrediction>(loadedModel);
_myTestDataView = _mlContext.Data.LoadFromTextFile<GitHubIssue>(_myTestDataPath, hasHeader: true);
IDataView predictions = loadedModel.Transform(_myTestDataView);
var batchPrediction = _predEngine;
IEnumerable<GitHubIssue> predictedResults =
_mlContext.Data.CreateEnumerable<GitHubIssue>(predictions, reuseRowObject: false);
foreach (GitHubIssue prediction in predictedResults)
{
Console.WriteLine($"Title: {prediction.Title} | Prediction: {batchPrediction.Predict(prediction).Area}");
}
作为旁注,将 batchPredictions 与 singlePredictions 进行比较,您必须将 .Area 添加到输出的末尾,如最后一行所示
// Manual Batch Predictions
Console.WriteLine($"Title: {prediction.Title} | Prediction: {batchPrediction.Predict(prediction).Area}");
// File-based Batch Predictions
Console.WriteLine($"Title: {prediction.Title} | Prediction: {batchPrediction.Predict(prediction).Area}");
我正在尝试应用 Binary Classification, example to a Multi Class Classification
中所示的批量预测google 和 Microsoft 工作中演示的许多示例都显示了一个单一的预测来演示训练模型的能力。
但是,我希望通过批量预测 a) 多手动输入和 b) 使用没有标签的文件来证明训练模型的有效性。
我遵循了 Binary Classification example and trying to map it across to the Multi Class Classification 示例,但是,预测没有显示。
以下是多 Class Classification 示例中所示的单个预测
// Single Prediction
ITransformer loadedModel = _mlContext.Model.Load(_modelPath, out var modelInputSchema);
GitHubIssue singleIssue = new GitHubIssue() { Title = "Entity Framework crashes", Description = "When connecting to the database, EF is crashing" }; // Our single issue
_predEngine = _mlContext.Model.CreatePredictionEngine<GitHubIssue, IssuePrediction>(loadedModel);
var singleprediction = _predEngine.Predict(singleIssue);
Console.WriteLine($"=============== Single Prediction - Result: {singleprediction.Area} ===============");
以下是 Binary Classification 示例中所示的批量预测,但是在该场景中不起作用。
// Batch Predictions from Enumerable
ITransformer loadedModel = _mlContext.Model.Load(_modelPath, out var modelInputSchema);
IDataView batchIssues = _mlContext.Data.LoadFromEnumerable(issues);
IDataView predictions = loadedModel.Transform(batchIssues);
IEnumerable<GitHubIssue> predictedResults = _mlContext.Data.CreateEnumerable<GitHubIssue>(predictions, reuseRowObject: false);
foreach (GitHubIssue prediction in predictedResults)
{
Console.WriteLine($"Title: {prediction.Title} | Prediction: {prediction.Area}");
}
根据单个预测,我得到以下结果:
area-System.Data
虽然批处理不预测输出,但我不认为它在预测,但是,阅读 Microsofts ML.NET 网站上的 material 它说转换将使用模型来对这批数据进行预测。
"Use the model to predict the comment data sentiment using the Transform() method"
问题 1) 我不确定,我在批量预测中遗漏了什么可枚举以获得 prediction.area 的预测,如单个值所示。
问题 2) 我如何调整可枚举以加载未标记信息的文件以对其进行预测。
请注意:其中的所有代码都放在多类分类示例的 PredictIssues 方法中。
问题 1 的答案
调整代码以利用 predEngine,然后对 foreach 中的单个预测使用预测函数。 为此需要进行两项更改:
var batchPrediction = _predEngine;
Console.WriteLine($"Prediction: {batchPrediction.Predict(prediction).Area}");
此外,我删除了以下行:
IDataView predictions = loadedModel.Transform(batchIssues);
删除此功能对预测结果没有影响。 似乎有效的完整代码如下:
IEnumerable<GitHubIssue> issues = new[]
{
new GitHubIssue
{
Title = "Entity Framework crashes",
Description = "When connecting to the database, EF is crashing"
},
new GitHubIssue
{
Title = "Github Down",
Description = "When going to the website, github says it is down"
}
};
var batchPrediction = _predEngine;
// Batch Predictions from Enumerable
IDataView batchIssues = _mlContext.Data.LoadFromEnumerable(issues);
IEnumerable<GitHubIssue> predictedResults = _mlContext.Data.CreateEnumerable<GitHubIssue>(batchIssues, reuseRowObject: false);
foreach (GitHubIssue prediction in predictedResults)
{
Console.WriteLine($"Title: {prediction.Title} | Prediction: {batchPrediction.Predict(prediction).Area}");
}
问题 2 的答案
我创建了一个包含 ID、区域(留空)、标题和描述的新文件,并镜像了测试和训练数据文件。
我将两个变量添加到全局范围(就在命名空间下面),如下所示:
private static string _myTestDataPath => Path.Combine(_appPath, "..", "..", "..", "Data", "myTestData.tsv");
private static IDataView _myTestDataView;
我没有创建IEnumerable,而是直接传入了文件,如下:
_myTestDataView = _mlContext.Data.LoadFromTextFile<GitHubIssue>(_myTestDataPath, hasHeader: true);
以下是该方法的完整示例:
ITransformer loadedModel = _mlContext.Model.Load(_modelPath, out var modelInputSchema);
_predEngine = _mlContext.Model.CreatePredictionEngine<GitHubIssue, IssuePrediction>(loadedModel);
_myTestDataView = _mlContext.Data.LoadFromTextFile<GitHubIssue>(_myTestDataPath, hasHeader: true);
IDataView predictions = loadedModel.Transform(_myTestDataView);
var batchPrediction = _predEngine;
IEnumerable<GitHubIssue> predictedResults =
_mlContext.Data.CreateEnumerable<GitHubIssue>(predictions, reuseRowObject: false);
foreach (GitHubIssue prediction in predictedResults)
{
Console.WriteLine($"Title: {prediction.Title} | Prediction: {batchPrediction.Predict(prediction).Area}");
}
作为旁注,将 batchPredictions 与 singlePredictions 进行比较,您必须将 .Area 添加到输出的末尾,如最后一行所示
// Manual Batch Predictions
Console.WriteLine($"Title: {prediction.Title} | Prediction: {batchPrediction.Predict(prediction).Area}");
// File-based Batch Predictions
Console.WriteLine($"Title: {prediction.Title} | Prediction: {batchPrediction.Predict(prediction).Area}");