我建立了这样的管道:
// 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”。
我究竟做错了什么?