c# - 调用 MakePredictionFunction 时“无法确定成员功能的 IDataView 类型”
问题描述
我正在尝试使用新的 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
看起来错误在“问题”类中,但究竟是什么错误?谢谢
解决方案
您正在尝试模式理解来制作数据视图并阅读. 通过使用原始数组,您可以使用 columntype 来设置数组的大小。
(这是针对 ML.NET 的 .10 版本)
public class Issue
{
public float label;
public float[] features; //change this
}
将 SchemaDefinition 用于运行时类型映射提示
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);
推荐阅读
- sql - 如何获取每月计费客户的数量 SQL
- graphql - VertX HTTP GraphQL 请求响应“缺少查询”
- javascript - Applink 在 Safari 浏览器的 iframe 中不起作用
- node.js - 在 Nodejs 中将“/”与 require 一起使用
- postgresql - 如何将完成的课程计入1并计算所有完成培训的人
- javascript - 如果基于行内两个单元格的内容,jQuery将删除隐藏表行
- r - 如何使用数据框中心的分组符号旋转更长的时间?
- r - 使用 CalendR 使用颜色渐变来可视化基于 df 的选定日期“排名”
- android-studio - 如何确定哪个文件对应于 Android 应用程序上的屏幕?
- r - 在 testthat 中抛出警告而不是错误