gpt4 book ai didi

c# - 使用 ML.NET 的动态训练/测试类

转载 作者:太空宇宙 更新时间:2023-11-03 20:53:35 51 4
gpt4 key购买 nike

这是这里问题的跟进 Dynamic classes/objects ML.net's PredictionMoadel<TInput, TOutput> Train()

我的系统无法在编译时使用预定义的类,因此我尝试将动态类提供给 ML.NET,如下所示

    // field data type
public class Field
{
public string FieldName { get; set; }
public Type FieldType { get; set; }
}

// dynamic class helper
public class DynamicClass : DynamicObject
{
private readonly Dictionary<string, KeyValuePair<Type, object>> _fields;

public DynamicClass(List<Field> fields)
{
_fields = new Dictionary<string, KeyValuePair<Type, object>>();
fields.ForEach(x => _fields.Add(x.FieldName,
new KeyValuePair<Type, object>(x.FieldType, null)));
}

public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (_fields.ContainsKey(binder.Name))
{
var type = _fields[binder.Name].Key;
if (value.GetType() == type)
{
_fields[binder.Name] = new KeyValuePair<Type, object>(type, value);
return true;
}
else throw new Exception("Value " + value + " is not of type " + type.Name);
}
return false;
}

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _fields[binder.Name].Value;
return true;
}
}

private static void Main(string[] args)
{
var fields = new List<Field>
{
new Field {FieldName = "Name", FieldType = typeof(string)},
new Field {FieldName = "Income", FieldType = typeof(float)}
};

dynamic obj1 = new DynamicClass(fields);
obj1.Name = "John";
obj1.Income = 100f;

dynamic obj2 = new DynamicClass(fields);
obj2.Name = "Alice";
obj2.Income = 200f;

var trainingData = new List<dynamic> {obj1, obj2};

var env = new LocalEnvironment();
var schemaDef = SchemaDefinition.Create(typeof(DynamicClass));
schemaDef.Add(new SchemaDefinition.Column(null, "Name", TextType.Instance));
schemaDef.Add(new SchemaDefinition.Column(null, "Income", NumberType.R4));
var trainDataView = env.CreateStreamingDataView(trainingData, schemaDef);

var pipeline = new CategoricalEstimator(env, "Name")
.Append(new ConcatEstimator(env, "Features", "Name"))
.Append(new FastTreeRegressionTrainer(env, "Income", "Features"));

var model = pipeline.Fit(trainDataView);
}

并收到错误:“在类型‘System.Object’中找不到名称为‘Name’的字段或属性”。我尝试使用反射生成类,但遇到了同样的问题。

有解决办法吗?谢谢

最佳答案

对于那些尝试这样做的人,我有一个可以创建模式并可用于动态训练数据的工作解决方案。

首先,从我的另一个答案中获取 DynamicTypeProperty 和 DynamicType 的代码 here .

以下代码将动态创建一个模式:

var properties = new List<DynamicTypeProperty>()
{
new DynamicTypeProperty("SepalLength", typeof(float)),
new DynamicTypeProperty("SepalWidth", typeof(float)),
new DynamicTypeProperty("PetalLength", typeof(float)),
new DynamicTypeProperty("PetalWidth", typeof(float)),
};

// create the new type
var dynamicType = DynamicType.CreateDynamicType(properties);
var schema = SchemaDefinition.Create(dynamicType);

然后您需要创建包含所需数据的列表。这是按如下方式完成的:

var dynamicList = DynamicType.CreateDynamicList(dynamicType);

// get an action that will add to the list
var addAction = DynamicType.GetAddAction(dynamicList);

// call the action, with an object[] containing parameters in exact order added
addAction.Invoke(new object[] {1.1, 2.2, 3.3, 4.4});
// call add action again for each row.

然后您需要使用数据创建一个 IDataView,这需要使用反射,否则训练器将无法推断出正确的类型。

            var mlContext = new MLContext();
var dataType = mlContext.Data.GetType();
var loadMethodGeneric = dataType.GetMethods().First(method => method.Name =="LoadFromEnumerable" && method.IsGenericMethod);
var loadMethod = loadMethodGeneric.MakeGenericMethod(dynamicType);
var trainData = (IDataView) loadMethod.Invoke(mlContext.Data, new[] {dynamicList, schema});

然后,您应该能够通过管道运行 trainData

祝你好运。

关于c# - 使用 ML.NET 的动态训练/测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52822696/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com