我正在尝试使用新的Microsoft.ML 0.6.0进行预测功能
当我调用“ model.AsDynamic.MakePredictionFunction”时,我收到
“ System.ArgumentOutOfRangeException:'无法确定IDataView
成员特征的类型”。
码:
using System;
using Microsoft.ML;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Trainers;
using Microsoft.ML.StaticPipe;
namespace MachineLearning
{
class MLTest
{
public void Run()
{
var env = new LocalEnvironment();
var reader = TextLoader.CreateReader(env, ctx => (label: ctx.LoadBool(0), features: ctx.LoadFloat(1, 3)));
var traindata = reader.Read(new MultiFileSource("train.txt"));
var bctx = new BinaryClassificationContext(env);
var est = reader.MakeNewEstimator()
.Append(x => (x.label, prediction: bctx.Trainers.Sdca(x.label, x.features.Normalize())));
var model = est.Fit(traindata);
//FAILS: System.ArgumentOutOfRangeException: 'Could not determine an IDataView type for member features'
var predictionFunct = model.AsDynamic.MakePredictionFunction<Issue, Prediction>(env);
}
public class Issue
{
public float label;
public Vector<float> features; //what is wrong?
}
public class Prediction
{
[ColumnName("prediction.predictedLabel")]
public bool PredictionLabel;
[ColumnName("prediction.probability")]
public float Probability;
[ColumnName("prediction.score")]
public float Score;
}
}
}
文件train.txt包含:
1 0 0 0
1 0 1 0
1 0 0 1
1 0 1 1
0 1 1 1
0 1 0 1
0 1 1 0
0 1 0 0
看起来好像是“问题”类中的错误,但是究竟是什么错误呢?谢谢
最佳答案
您正在尝试schema comprehension to make a dataview and to read。通过使用基本数组,可以使用columntype设置数组的大小。
(这是针对ML.NET版本.10的)
public class Issue
{
public float label;
public float[] features; //change this
}
Using SchemaDefinition for run-time type mapping hints
var inputSchemaDefinition = SchemaDefinition.Create(typeof(Issue), SchemaDefinition.Direction.Both);
inputSchemaDefinition["features"].ColumnType = new VectorType(NumberType.R4, 4);
然后,您将创建引擎:
var predictionEngine = model.CreatePredictionEngine<InputSchema, Prediction>(model as IHostEnvironment, inputSchemaDefinition, outputSchemaDefinition);