【发布时间】:2021-12-09 05:06:18
【问题描述】:
我正在使用 ML.Net 模型,该模型在管道中使用 MapValueToKey 转换。训练模型后,我将其保存。
在运行时,我加载保存的模型并进行预测。这工作正常,但我只得到值(整数)。如何使用从存储文件加载的模型提取与预测值匹配的键(字符串值)(我知道映射包含在存储模型中,因为如果我打印混淆矩阵,我可以看到它)。
示例代码如下,显示了我的问题的主要元素:
//Note: this.AllData is preloaded: IDataView AllData
//Create the PipeLine - Mapping Treatment Name to a Label as needed by the Multiclass Classification model
var pipeline = mlContext.Transforms.Conversion.MapValueToKey(outputColumnName: "Label", nameof(TreatmentObservation.NextTreatmentName))
.Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "roadclass", inputColumnName: nameof(TreatmentObservation.RoadClass)))
.Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "surf_mat", inputColumnName: nameof(TreatmentObservation.SurfMaterial)))
.Append(mlContext.Transforms.Concatenate("Features", "roadclass", nameof(TreatmentObservation.Rut85th), nameof(TreatmentObservation.Naasra85th)));
//Create a LightGBM Trainer
IEstimator<ITransformer> trainer = mlContext.MulticlassClassification.Trainers.LightGbm();
var trainingPipeline = pipeline.Append(trainer);
var trainedModelWithPreproc = trainingPipeline.Fit(this.AllData); //Fit the pipleline on all the preloaded data
//Save the model
string modelSaveFilePath = "modelPipeline.zip";
mlContext.Model.Save(trainedModelWithPreproc, dataSplit.TrainSet.Schema, modelSaveFilePath);
//Now Load back the model and test it
// Define trained model schemas
DataViewSchema modelSchema;
ITransformer allInOneModel = mlContext.Model.Load(modelSaveFilePath, out modelSchema);
List<TreatmentObservation> testingData = null;
//Load testing data here...(code not included)
var observations = mlContext.Data.LoadFromEnumerable(testingData);
var engine = this.mlContext.Model.CreatePredictionEngine<TreatmentObservation, Prediction_MultiLabel>(allInOneModel); //Create the prediction engine
foreach (TreatmentObservation observation in testingData)
{
var predicted = engine.Predict(observation);
uint label_value = predicted.PredictedLabel;
string label_key;
//How to get back the Key from the predicted value?
}
这是上面示例中用于进行预测的类:
// Class used to capture predictions.
public class Prediction_MultiLabel
{
// Original label.
public uint Label { get; set; }
// Predicted label from the trainer.
public uint PredictedLabel { get; set; }
}
所以我的问题是:如何在使用存储和加载的模型进行预测时反转映射?
我看过像这样的例子:ML.NET example 但这些例子不使用存储模型。他们构建然后直接使用管道。我需要知道如何从存储的模型中反转映射。我不是 ML.NET 的专家,所以如果我的问题有一些无知,请原谅!
【问题讨论】: