0

我正在使用 .NET-Framework 4.6.1

将 ML.NET 升级到 v0.10 后,我无法运行我的代码。我构建了我的管道,然后在执行 Fit()-Method 时出现错误。

Message = "找不到方法:\"System.Collections.Generic.IEnumerable 1<!!0> System.Linq.Enumerable.Append(System.Collections.Generic.IEnumerable1, !!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();

    }
}

}

4

2 回答 2

0
  • ML.NET v0.10 和移动到 0.11 中的 API 正在更改,因此它在 API 中的许多不同类中是一致的。

您面临的问题可能是因为在许多 API 方法中我们更改了参数的顺序。因此,如果这些参数的类型相同,它会编译但不能正常工作。

检查您在 ML.NET API 中使用的所有参数,以确保它们是正确的。可能有帮助的是提供参数的名称,例如:

.method(param1:value1, param2:value2)

如果您正在从示例中寻求更多帮助,请查看我们将示例迁移到 v0.10 的分支,好吗?

关联

希望能帮助到你。:)

塞萨尔

于 2019-02-06T19:42:27.110 回答
0

它的 ML.NET v0.10 正在运行。我不得不将我的 .NET-Framework 更新到 4.7.1。

于 2019-02-08T07:53:40.620 回答