升级到 ml.net v0.10 Fit() 后无法正常工作
After upgrading to ml.net v0.10 Fit() ist not working
我正在使用 .NET-Framework 4.6.1
升级 ML.NET 到 v0.10 后,我无法 运行 我的代码。
我构建了管道,然后在执行 Fit()-Method 时出错。
消息="Method not found: \"System.Collections.Generic.IEnumerable1<!!0> System.Linq.Enumerable.Append(System.Collections.Generic.IEnumerable
1,!!0)\"."
使用System.Collections.Generic;在我的指令中。
我是不是遗漏了什么,还是应该暂时坚持使用 v0.9?
谢谢
using System;
using System.IO;
using Microsoft.ML;
using Microsoft.ML.Core.Data;
using Microsoft.ML.Data;
using MulticlassClassification_Iris.DataStructures;
namespace MulticlassClassification_Iris
{
public static partial class Program
{
private static string AppPath => Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
private static string TrainDataPath = @"..\machinelearning-samples\samples\csharp\getting-started\MulticlassClassification_Iris\IrisClassification\Data\iris-train.txt";
private static string TestDataPath = @"..\machinelearning-samples\samples\csharp\getting-started\MulticlassClassification_Iris\IrisClassification\Data\iris-test.txt";
private static string ModelPath = @"C:\Users\waldemar\Documents\model.txt";
private static void Main(string[] args)
{
// Create MLContext to be shared across the model creation workflow objects
// Set a random seed for repeatable/deterministic results across multiple trainings.
var mlContext = new MLContext(seed: 0);
//1.
BuildTrainEvaluateAndSaveModel(mlContext);
//2.
TestSomePredictions(mlContext);
Console.WriteLine("=============== End of process, hit any key to finish ===============");
Console.ReadKey();
}
private static void BuildTrainEvaluateAndSaveModel(MLContext mlContext)
{
// STEP 1: Common data loading configuration
var trainingDataView = mlContext.Data.ReadFromTextFile<IrisData>(TrainDataPath, hasHeader: true);
var testDataView = mlContext.Data.ReadFromTextFile<IrisData>(TestDataPath, hasHeader: true);
// STEP 2: Common data process configuration with pipeline data transformations
var dataProcessPipeline = mlContext.Transforms.Concatenate("Features", "SepalLength",
"SepalWidth",
"PetalLength",
"PetalWidth").AppendCacheCheckpoint(mlContext);
// STEP 3: Set the training algorithm, then append the trainer to the pipeline
var trainer = mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumn: "Label", featureColumn: "Features");
var trainingPipeline = dataProcessPipeline.Append(trainer);
// STEP 4: Train the model fitting to the DataSet
//Measure training time
var watch = System.Diagnostics.Stopwatch.StartNew();
Console.WriteLine("=============== Training the model ===============");
ITransformer trainedModel = trainingPipeline.Fit(trainingDataView);
//Stop measuring time
watch.Stop();
long elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"***** Training time: {elapsedMs/1000} seconds *****");
// STEP 5: Evaluate the model and show accuracy stats
Console.WriteLine("===== Evaluating Model's accuracy with Test data =====");
var predictions = trainedModel.Transform(testDataView);
var metrics = mlContext.MulticlassClassification.Evaluate(predictions, "Label", "Score");
Common.ConsoleHelper.PrintMultiClassClassificationMetrics(trainer.ToString(), metrics);
// STEP 6: Save/persist the trained model to a .ZIP file
using (var fs = new FileStream(ModelPath, FileMode.Create, FileAccess.Write, FileShare.Write))
mlContext.Model.Save(trainedModel, fs);
Console.WriteLine("The model is saved to {0}", ModelPath);
}
private static void TestSomePredictions(MLContext mlContext)
{
//Test Classification Predictions with some hard-coded samples
ITransformer trainedModel;
using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
trainedModel = mlContext.Model.Load(stream);
}
// Create prediction engine related to the loaded trained model
var predEngine = trainedModel.CreatePredictionEngine<IrisData, IrisPrediction>(mlContext);
//Score sample 1
var resultprediction1 = predEngine.Predict(SampleIrisData.Iris1);
Console.WriteLine($"Actual: setosa. Predicted probability: setosa: {resultprediction1.Score[0]:0.####}");
Console.WriteLine($" versicolor: {resultprediction1.Score[1]:0.####}");
Console.WriteLine($" virginica: {resultprediction1.Score[2]:0.####}");
Console.WriteLine();
//Score sample 2
var resultprediction2 = predEngine.Predict(SampleIrisData.Iris2);
Console.WriteLine($"Actual: setosa. Predicted probability: setosa: {resultprediction2.Score[0]:0.####}");
Console.WriteLine($" versicolor: {resultprediction2.Score[1]:0.####}");
Console.WriteLine($" virginica: {resultprediction2.Score[2]:0.####}");
Console.WriteLine();
//Score sample 3
var resultprediction3 = predEngine.Predict(SampleIrisData.Iris3);
Console.WriteLine($"Actual: setosa. Predicted probability: setosa: {resultprediction3.Score[0]:0.####}");
Console.WriteLine($" versicolor: {resultprediction3.Score[1]:0.####}");
Console.WriteLine($" virginica: {resultprediction3.Score[2]:0.####}");
Console.WriteLine();
}
}
}
- The API in ML.NET v0.10 and moving to 0.11, is being changed so it is consistent across many different classes in the API.
您遇到的问题可能是因为在许多API 方法中我们更改了参数的顺序。因此,如果这些参数属于同一类型,它会编译但无法正常工作。
检查 ML.NET API 中使用的所有参数,以确保它们是正确的。
可能有帮助的是提供参数的名称,例如:
.method(param1:value1, param2:value2)
如果您希望从示例中获得更多帮助,请查看我们正在将示例迁移到 v0.10 的分支,好吗?
希望对您有所帮助。 :)
塞萨尔
ML.NET v0.10 正在运行。我必须将我的 .NET-Framework 更新到 4.7.1。
我正在使用 .NET-Framework 4.6.1
升级 ML.NET 到 v0.10 后,我无法 运行 我的代码。 我构建了管道,然后在执行 Fit()-Method 时出错。
消息="Method not found: \"System.Collections.Generic.IEnumerable1<!!0> System.Linq.Enumerable.Append(System.Collections.Generic.IEnumerable
1,!!0)\"."
使用System.Collections.Generic;在我的指令中。
我是不是遗漏了什么,还是应该暂时坚持使用 v0.9?
谢谢
using System;
using System.IO;
using Microsoft.ML;
using Microsoft.ML.Core.Data;
using Microsoft.ML.Data;
using MulticlassClassification_Iris.DataStructures;
namespace MulticlassClassification_Iris
{
public static partial class Program
{
private static string AppPath => Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
private static string TrainDataPath = @"..\machinelearning-samples\samples\csharp\getting-started\MulticlassClassification_Iris\IrisClassification\Data\iris-train.txt";
private static string TestDataPath = @"..\machinelearning-samples\samples\csharp\getting-started\MulticlassClassification_Iris\IrisClassification\Data\iris-test.txt";
private static string ModelPath = @"C:\Users\waldemar\Documents\model.txt";
private static void Main(string[] args)
{
// Create MLContext to be shared across the model creation workflow objects
// Set a random seed for repeatable/deterministic results across multiple trainings.
var mlContext = new MLContext(seed: 0);
//1.
BuildTrainEvaluateAndSaveModel(mlContext);
//2.
TestSomePredictions(mlContext);
Console.WriteLine("=============== End of process, hit any key to finish ===============");
Console.ReadKey();
}
private static void BuildTrainEvaluateAndSaveModel(MLContext mlContext)
{
// STEP 1: Common data loading configuration
var trainingDataView = mlContext.Data.ReadFromTextFile<IrisData>(TrainDataPath, hasHeader: true);
var testDataView = mlContext.Data.ReadFromTextFile<IrisData>(TestDataPath, hasHeader: true);
// STEP 2: Common data process configuration with pipeline data transformations
var dataProcessPipeline = mlContext.Transforms.Concatenate("Features", "SepalLength",
"SepalWidth",
"PetalLength",
"PetalWidth").AppendCacheCheckpoint(mlContext);
// STEP 3: Set the training algorithm, then append the trainer to the pipeline
var trainer = mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumn: "Label", featureColumn: "Features");
var trainingPipeline = dataProcessPipeline.Append(trainer);
// STEP 4: Train the model fitting to the DataSet
//Measure training time
var watch = System.Diagnostics.Stopwatch.StartNew();
Console.WriteLine("=============== Training the model ===============");
ITransformer trainedModel = trainingPipeline.Fit(trainingDataView);
//Stop measuring time
watch.Stop();
long elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"***** Training time: {elapsedMs/1000} seconds *****");
// STEP 5: Evaluate the model and show accuracy stats
Console.WriteLine("===== Evaluating Model's accuracy with Test data =====");
var predictions = trainedModel.Transform(testDataView);
var metrics = mlContext.MulticlassClassification.Evaluate(predictions, "Label", "Score");
Common.ConsoleHelper.PrintMultiClassClassificationMetrics(trainer.ToString(), metrics);
// STEP 6: Save/persist the trained model to a .ZIP file
using (var fs = new FileStream(ModelPath, FileMode.Create, FileAccess.Write, FileShare.Write))
mlContext.Model.Save(trainedModel, fs);
Console.WriteLine("The model is saved to {0}", ModelPath);
}
private static void TestSomePredictions(MLContext mlContext)
{
//Test Classification Predictions with some hard-coded samples
ITransformer trainedModel;
using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
trainedModel = mlContext.Model.Load(stream);
}
// Create prediction engine related to the loaded trained model
var predEngine = trainedModel.CreatePredictionEngine<IrisData, IrisPrediction>(mlContext);
//Score sample 1
var resultprediction1 = predEngine.Predict(SampleIrisData.Iris1);
Console.WriteLine($"Actual: setosa. Predicted probability: setosa: {resultprediction1.Score[0]:0.####}");
Console.WriteLine($" versicolor: {resultprediction1.Score[1]:0.####}");
Console.WriteLine($" virginica: {resultprediction1.Score[2]:0.####}");
Console.WriteLine();
//Score sample 2
var resultprediction2 = predEngine.Predict(SampleIrisData.Iris2);
Console.WriteLine($"Actual: setosa. Predicted probability: setosa: {resultprediction2.Score[0]:0.####}");
Console.WriteLine($" versicolor: {resultprediction2.Score[1]:0.####}");
Console.WriteLine($" virginica: {resultprediction2.Score[2]:0.####}");
Console.WriteLine();
//Score sample 3
var resultprediction3 = predEngine.Predict(SampleIrisData.Iris3);
Console.WriteLine($"Actual: setosa. Predicted probability: setosa: {resultprediction3.Score[0]:0.####}");
Console.WriteLine($" versicolor: {resultprediction3.Score[1]:0.####}");
Console.WriteLine($" virginica: {resultprediction3.Score[2]:0.####}");
Console.WriteLine();
}
}
}
- The API in ML.NET v0.10 and moving to 0.11, is being changed so it is consistent across many different classes in the API.
您遇到的问题可能是因为在许多API 方法中我们更改了参数的顺序。因此,如果这些参数属于同一类型,它会编译但无法正常工作。
检查 ML.NET API 中使用的所有参数,以确保它们是正确的。 可能有帮助的是提供参数的名称,例如:
.method(param1:value1, param2:value2)
如果您希望从示例中获得更多帮助,请查看我们正在将示例迁移到 v0.10 的分支,好吗?
希望对您有所帮助。 :)
塞萨尔
ML.NET v0.10 正在运行。我必须将我的 .NET-Framework 更新到 4.7.1。