我构建了一个这样的管道:
// PurchaseData.TrainingInputColumnNames is string[] containing the input column names
var predictColumn = nameof(PurchaseData.Brand);
var dataProcessPipeline = mlContext.Transforms.Categorical.OneHotEncoding(nameof(PurchaseData.CurrentBrand))
.Append(mlContext.Transforms.Categorical.OneHotEncoding(nameof(PurchaseData.Gender)))
.Append(mlContext.Transforms.Concatenate(DefaultColumnNames.Features, PurchaseData.TrainingInputColumnNames))
.Append(mlContext.Transforms.Conversion.MapValueToKey(outputColumnName: DefaultColumnNames.Label, inputColumnName: predictColumn))
.Append(mlContext.Transforms.Normalize())
.Append(mlContext.Transforms.Conversion.MapKeyToValue(("PredictedLabel", DefaultColumnNames.Label)))
.AppendCacheCheckpoint(mlContext)
IEstimator<ITransformer> trainer = null;
trainer = mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent
(
featureColumn: DefaultColumnNames.Features,
l2Const: 0.0001f,
l1Threshold: null,
maxIterations: 200
);
var trainingPipeline = dataProcessPipeline.Append(trainer);
var trainedModel = trainingPipeline.Fit(trainingDataView);
还有一个预测课
public class PurchaseDataPrediction
{
public float[] Score;
public string PredictedLabel;
}
当我尝试使用时解码标签
// https://github.com/dotnet/machinelearning/blob/master/test/Microsoft.ML.Tests/Scenarios/Api/Estimators/PredictAndMetadata.cs
VBuffer<ReadOnlyMemory<char>> keys = default;
predictionEngine.OutputSchema[nameof(PurchaseDataPrediction.PredictedLabel)].GetKeyValues(ref keys);
我得到了例外:
'无法将类型为'Key'的IDataView列'PredictedLabel'绑定到'System.String'类型的字段或属性'PredictedLabel'。
我究竟做错了什么?
以下是如何获取预测标签的示例(作为字符串)
// Create Estimator
var pipe = mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
.Append(mlContext.Transforms.Normalize("Features"))
.Append(mlContext.Transforms.Conversion.MapValueToKey("Label", "IrisPlantType"), TransformerScope.TrainTest)
.AppendCacheCheckpoint(mlContext)
.Append(mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent())
.Append(mlContext.Transforms.Conversion.MapKeyToValue(("PredictPlant", "PredictedLabel")));
// Train the pipeline
var trainedModel = pipe.Fit(trainData);
// Make predictions
var predictFunction = trainedModel.CreatePredictionEngine<IrisDataWithStringLabel, IrisPredictionWithStringLabel>(mlContext);
IrisPredictionWithStringLabel prediction = predictFunction.Predict(new IrisDataWithStringLabel()
{
SepalLength = 5.1f,
SepalWidth = 3.3f,
PetalLength = 1.6f,
PetalWidth = 0.2f,
});
// Outputs string : "Iris-setosa" as the prediction
Console.WriteLine(prediction.PredictPlant);
请注意管道中指定培训师的位置。此外,MapKeyToValue中指定的位置和参数
使用的预测类与上面示例中的类似:
private class IrisPredictionWithStringLabel
{
[ColumnName("Score")]
public float[] PredictedScores { get; set; }
public string PredictPlant { get; set; }
}
希望能帮助到你!
那个PredictAndMetadata是在你的管道中写下了你有多类训练器的想法,它会产生“PredictedLabel”列,类型为“Label”列。我没有看到训练师在你的管道中我会认为它根本不存在。
你这样做:
.Append(mlContext.Transforms.Conversion.MapKeyToValue(("PredictedLabel", DefaultColumnNames.Label)))
您获取string类型的“Label”并将其转换为类型为Key的“PredictedLabel”列。 (Key基本上是使用uint进行枚举的)。
public class PurchaseDataPrediction
{
public float[] Score;
public string PredictedLabel;
}
但是您的结果类型定义具有PredictedLabel的字符串类型。在DataView中,您有Key(uint)。
这正是异常所说的:
Can't bind the IDataView column 'PredictedLabel' of type 'Key' to field or property 'PredictedLabel' of type 'System.String'.
在目前的时刻,我不确定你想用这个代码实现什么,如果你能描述你想要解决的任务,我可以帮助你。
我认为你的预测课有string PredictedLabel
而我相信GetKeyValues
期待一个关键专栏。
有关键到值和值到键转换的更多信息,请参阅以下示例:https://github.com/dotnet/machinelearning/blob/master/docs/samples/Microsoft.ML.Samples/Dynamic/KeyToValueValueToKey.cs