升级到ml.net v0.10后,Fit()无法运行

时间:2019-02-06 10:31:58

标签: c# ml.net

我正在使用.NET-Framework 4.6.1

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

消息=“找不到方法:\“ System.Collections.Generic.IEnumerable 1<!!0> System.Linq.Enumerable.Append(System.Collections.Generic.IEnumerable 1,!! 0)\”。“

using 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();

    }
}

}

2 个答案:

答案 0 :(得分:0)

  
      
  • 正在更改ML.NET v0.10中的API,并将其移至0.11,以便在该API中的许多不同类之间保持一致。
  •   

您可能遇到的问题可能是由于许多API方法我们更改了参数的顺序引起的。因此,如果这些参数具有相同的类型,则可以编译但不能正常工作。

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

.method(param1:value1, param2:value2)

如果您正在从样本中寻求其他帮助,请检查此分支,我们将样本迁移到v0.10,好吗?

link

希望有帮助。 :)

C撒

答案 1 :(得分:0)

它正在运行ML.NET v0.10。我必须将.NET-Framework更新到4.7.1。