首页 > 解决方案 > Microsoft.ML 由于路径非法而无法保存模型?

问题描述

最近一直在玩机器学习,做了一个基本的机器学习算法。它工作得很好,然后我弄坏了一些东西,现在它拒绝保存模型。

这是代码:

        private static string MODEL_FILEPATH = @"MLModel.zip";
    // Create MLContext to be shared across the model creation workflow objects 
    // Set a random seed for repeatable/deterministic results across multiple trainings.
    private static MLContext mlContext = new MLContext(seed: 1);

    public static void CreateModel()
    {
        // Load Data
        IDataView trainingDataView = mlContext.Data.LoadFromTextFile<ModelInput>(
                                        path: TRAIN_DATA_FILEPATH,
                                        hasHeader: true,
                                        separatorChar: ',',
                                        allowQuoting: true,
                                        allowSparse: false);

        // Build training pipeline
        IEstimator<ITransformer> trainingPipeline = BuildTrainingPipeline(mlContext);

        // Evaluate quality of Model
        //Evaluate(mlContext, trainingDataView, trainingPipeline);

        // Train Model
        ITransformer mlModel = TrainModel(mlContext, trainingDataView, trainingPipeline);

        // Save model
        SaveModel(mlContext, mlModel, MODEL_FILEPATH, trainingDataView.Schema);
    }
    private static void SaveModel(MLContext mlContext, ITransformer mlModel, string 
modelRelativePath, DataViewSchema modelInputSchema)
    {

        try
        {
            mlContext.Model.Save(mlModel, null, (GetAbsolutePath(modelRelativePath)));
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString() + "\n" + e.Message + ": " + GetAbsolutePath(modelRelativePath));
        }

    }

此代码是自动生成的,我只是删除了注释,添加了 try catch 并指定了模型路径。

这是一个例外:

  > Exception thrown: 'System.ArgumentException' in mscorlib.dll
   System.ArgumentException: The path is not of a legal form.
   at System.IO.Path.NewNormalizePath(String path, Int32 maxPathLength, Boolean expandShortPaths)
   at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean 
   expandShortPaths)
   at System.IO.Path.GetFullPathInternal(String path)
   at System.IO.Path.GetFullPath(String path)
   at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String fileName)
   at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
   at Microsoft.ML.RepositoryWriter.CreateNew(Stream stream, IExceptionContext ectx, Boolean 
   useFileSystem)
   at Microsoft.ML.ModelOperationsCatalog.Save(ITransformer model, DataViewSchema inputSchema, Stream 
   stream)
   at Microsoft.ML.ModelOperationsCatalog.Save(ITransformer model, DataViewSchema inputSchema, String 
   filePath)
   at MachineLearningTest.ModelBuilder.SaveModel(MLContext mlContext, ITransformer mlModel, String 
   modelRelativePath, DataViewSchema modelInputSchema) in 
   C:\Users\Michael\Source\Repos\new\MachineLearingTest\ModelBuilder.cs:line 83
   The path is not of a legal form.: 
   C:\Users\Michael\Source\Repos\new\MachineLearingTest\bin\x64\Debug\MLModel.zip

这是应该包含模型的文件(它是空的)

标签: c#machine-learningmicrosoft.ml

解决方案


推荐阅读