c# - 调用 MakePredictionFunction 时“无法确定成员特征的 IDataView 类型”

标签 c# machine-learning ml.net

我正在尝试使用新的 Microsoft.ML 0.6.0 进行预测功能

当我调用“model.AsDynamic.MakePredictionFunction”时,我收到了

"System.ArgumentOutOfRangeException: 'Could not determine an IDataView type for member features'".

代码:

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

看起来错误在类“Issue”中,但到底错在哪里?谢谢

最佳答案

您正在尝试 schema comprehension to make a dataview and to read .通过使用原始数组,您可以使用列类型来设置数组的大小。

(这是 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);

关于c# - 调用 MakePredictionFunction 时“无法确定成员特征的 IDataView 类型”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52794804/

相关文章:

c# - 周期性任务无效跨线程访问异常

c# - C# 删除立即窗口中的变量

c# - 如何使用 iTextSharp 从 PDF 中正确提取下标/上标?

machine-learning - 使用 Microsoft ML.net 0.2(机器学习)进行图像识别/分类

c# - 将 UICulture 设置为 Html.EditorFor 方法

python - 在 sklearn 机器学习工具链中寻找最佳算法组合

python - 现实与预测之间的延迟差距

python - 名称错误 : name 'plot_confusion_matrix' is not defined

c# - 无法在 Windows 2012 R2 服务器上加载 DLL 'tensorflow'

speech-recognition - 可以在 ML.NET 中进行语音识别吗?