ML.net负载来自无法枚举的问题

问题描述

我一直在努力为ML.net创建合适的数据结构并将其加载到我的应用程序中。本质上,我有一个应用程序,其中的训练数据将是动态的,并且在运行时之前不会知道类型和/或大小。另外,我必须从非标准原始类型(即App_Bool或App_Number ...而不是简单地使用bool或double等)转换训练数据,因此,事实证明这是一个问题,因为我尝试将训练数据转换为通用数据类型,然后可以使用 LoadFromEnumerable 函数从内存中加载该数据类型。 我有四个基本的数据类型类:

public class MLIntData
{
    public MLIntData(string label,List<object> l)
    {
        Label = label;
        foreach (App_Integer element in l)
            Features.Add((int)element.Value);
    }

    public List<int> Features { get; set; } = new List<int>();
    public string Label { get; set; } = "";
}

public class MLNumberData 
{
    public MLNumberData(string label,List<object> l)
    {
        Label = label;
        foreach (App_Number element in l)
            Features.Add((double)element.Value);
    }

    public List<double> Features { get; set; } = new List<double>();
    public string Label { get; set; } = "";
}

public class MLBoolData 
{
    public MLBoolData(string label,List<object> l)
    {
        Label = label;
        foreach (App_Boolean element in l)
            Features.Add((bool)element.Value);
    }

    public List<bool> Features { get; set; } = new List<bool>();

    public string Label { get; set; } = "";
}

public class MLTextData
{
    public MLTextData(string label,List<object> l)
    {
        Label = label;
        foreach (App_String element in l)
            Features.Add(element.Value.ToString());
    }

    public List<string> Features { get; set; } = new List<string>();
    public string Label { get; set; } = "";
}

因此,每个基类将包含一个数据标签,然后是一个特征列表,这些特征类型将为 bool double int 或 string

现在,我正在ML.net代码中尝试加载训练数据,然后创建数据的 IDataView 对象。首先,我遍历输入数据(最初是通用类型 object ),然后创建新的数据类。

List<object> data = new List<object>();

for(int i = 0; i < input.Count; i++)
{
    MLCodifiedData codifiedData = input[i].Value as MLCodifiedData;
    Type dataType = codifiedData.Features[0].GetType();

    if (dataType == typeof(App_Boolean))
    {
        data.Add(new MLBoolData(codifiedData.Label,codifiedData.Features));                       
    }
    else if (dataType == typeof(App_Number))
    {
        data.Add(new MLNumberData(codifiedData.Label,codifiedData.Features));
    }
    else if (dataType == typeof(App_Integer))
    {
        data.Add(new MLIntData(codifiedData.Label,codifiedData.Features));
    }
    if (dataType == typeof(App_String))
    {
        data.Add(new MLTextData(codifiedData.Label,codifiedData.Features));
    }
}

IDataView TrainingData = mlContext.Data.LoadFromEnumerable<object>(data);

我尝试创建一个 schema definition (可以将其作为 LoadFromEnumerable 方法中的第二个参数传递,但是我似乎无法理解我也曾尝试使用 schema builder 创建架构来创建架构,但这似乎也不起作用。现在,我正在使用其中之一datasets that is included in one of the sample files。是的,为了避免问题,我知道我可以简单地将数据加载为文件并以这种方式读取...但是,在我的应用中,我需要先将CSV读入内存,然后创建数据结构,这样我就不能真正使用许多通过 LoadFromTextFile 方法读取CSV文件的示例。任何人都可以提供有关如何设置动态输入的支持。内存集合并将其转换为IDataView对象?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)