ml.net 从字符串而不是文件训练模型输入
ml.net train model input from string instead of a file
我一直在使用以下代码行来加载文本数据:
pipeline.Add(new TextLoader(dataPath).CreateFrom<SentimentData>(separator: ','));
但是有没有办法将字符串作为数据注入?假设我们想从数据库中获取模型,我不必先将字符串保存到文件中,还是这样?
这个日期的文档真的很差,但它也是微软给我们的一个闪亮的新工具。
谢谢
您需要使用在 ML.NET 的 v0.2 中引入的 CollectionDataSource。您可以获取新的 github 位或 nuget,然后您可以在可枚举的基础上使用 CollectionDataSource。
您可以在其测试中找到完整示例:
https://github.com/dotnet/machinelearning/blob/6d5a41d39face9e98c242d3db3ff10ea8e233cc1/test/Microsoft.ML.Tests/CollectionDataSourceTests.cs
虹膜数据的一个例子:
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>();
我一直在使用以下代码行来加载文本数据:
pipeline.Add(new TextLoader(dataPath).CreateFrom<SentimentData>(separator: ','));
但是有没有办法将字符串作为数据注入?假设我们想从数据库中获取模型,我不必先将字符串保存到文件中,还是这样?
这个日期的文档真的很差,但它也是微软给我们的一个闪亮的新工具。
谢谢
您需要使用在 ML.NET 的 v0.2 中引入的 CollectionDataSource。您可以获取新的 github 位或 nuget,然后您可以在可枚举的基础上使用 CollectionDataSource。
您可以在其测试中找到完整示例: https://github.com/dotnet/machinelearning/blob/6d5a41d39face9e98c242d3db3ff10ea8e233cc1/test/Microsoft.ML.Tests/CollectionDataSourceTests.cs
虹膜数据的一个例子:
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>();