ml.net train model input from string instead of a file

◇◆丶佛笑我妖孽 提交于 2019-12-02 05:36:11

问题


I've been using the following line of code to load text data:

pipeline.Add(new TextLoader(dataPath).CreateFrom<SentimentData>(separator: ','));

But is there a way to inject a string as data? Let's say we want to grab the model from a database, I don't have to save the string to a file first, or do I?

The documentation at this date is really poor, but it's also a shiny new tool Microsoft gave us.

Thanks


回答1:


You'll want to use a CollectionDataSource which was introduced in v0.2 of ML.NET. You can either grab fresh github bits or the nuget, and then you can use CollectionDataSource on top of an enumerable.

You can find a full example in its tests: https://github.com/dotnet/machinelearning/blob/6d5a41d39face9e98c242d3db3ff10ea8e233cc1/test/Microsoft.ML.Tests/CollectionDataSourceTests.cs

One example on Iris data:

var data = new List<IrisData>() {
    new IrisData { SepalLength = 1f, SepalWidth = 1f ,PetalLength=0.3f, PetalWidth=5.1f, Label=1},
    new IrisData { SepalLength = 1f, SepalWidth = 1f ,PetalLength=0.3f, PetalWidth=5.1f, Label=1},
    new IrisData { SepalLength = 1.2f, SepalWidth = 0.5f ,PetalLength=0.3f, PetalWidth=5.1f, Label=0}
};
var collection = CollectionDataSource.Create(data);

pipeline.Add(collection);
pipeline.Add(new ColumnConcatenator(outputColumn: "Features",
    "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"));
pipeline.Add(new StochasticDualCoordinateAscentClassifier());
PredictionModel<IrisData, IrisPrediction> model = pipeline.Train<IrisData, IrisPrediction>();

IrisPrediction prediction = model.Predict(new IrisData()
{
    SepalLength = 3.3f,
    SepalWidth = 1.6f,
    PetalLength = 0.2f,
    PetalWidth = 5.1f,
});

pipeline = new LearningPipeline();
collection = CollectionDataSource.Create(data.AsEnumerable());
pipeline.Add(collection);
pipeline.Add(new ColumnConcatenator(outputColumn: "Features",
    "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"));
pipeline.Add(new StochasticDualCoordinateAscentClassifier());
model = pipeline.Train<IrisData, IrisPrediction>();


来源:https://stackoverflow.com/questions/50793621/ml-net-train-model-input-from-string-instead-of-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!