问题
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