ML.NET如何使输入模型通用?

时间:2020-05-13 00:21:52

标签: ml.net

我有3个用于多类分类的用例,它们的InputModels都不同,因为它们具有不同的列和数据结构。我该如何重构下面的方法,以便它可以预测任何种类的InputModels而无需复制和重复该方法3次,仅能满足3种不同的输入数据结构?

    private List<MulticlassClassificationPrediction> Predict(string modelName, string testDataPath)
    {
        PredictionEngine<InputModel, MulticlassClassificationPrediction> predEngine;

        predEngine = _predEnginePool.GetPredictionEngine(modelName: modelName);

        IDataView dataView = _mlContext.Data.LoadFromTextFile<InputModel>(
                            path: testDataPath,
                            hasHeader: true,
                            separatorChar: ',',
                            allowQuoting: true,
                            allowSparse: false);

        // Use first line of dataset as model input
        // You can replace this with new test data (hardcoded or from end-user application)
        List<InputModel> testDataList = _mlContext.Data.CreateEnumerable<InputModel>(dataView, false).ToList();

        List<MulticlassClassificationPrediction> predictionList = new List<MulticlassClassificationPrediction>();
        foreach (InputModel testData in testDataList)
        {

            MulticlassClassificationPrediction result = predEngine.Predict(testData);

            predictionList.Add(result);

        }

        return predictionList;
    }

1 个答案:

答案 0 :(得分:0)

如果我对您的问题理解正确,那么您是否有机会尝试过类似的事情?

> ..\make-4.1\WinRel\gnumake.exe -dr
GNU Make 4.1
Built for Windows32
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Reading makefiles...
Reading makefile 'Makefile'...
find_and_set_shell() path search set default_shell = C:/Program Files/PowerShell/7/pwsh.exe
Updating makefiles....
 Considering target file 'Makefile'.
  Looking for an implicit rule for 'Makefile'.
  No implicit rule found for 'Makefile'.
  Finished prerequisites of target file 'Makefile'.
 No need to remake target 'Makefile'.
Updating goal targets....
Considering target file 'get_var'.
 File 'get_var' does not exist.
 Finished prerequisites of target file 'get_var'.
Must remake target 'get_var'.
CreateProcess(NULL,C:/Program Files/PowerShell/7/pwsh.exe -c "&Write-Output asdf/asdf",...)
Putting child 00B86400 (get_var) PID 12170424 on the chain.
Live child 00B86400 (get_var) PID 12170424
Main thread handle = 0000016C
asdf/asdf
Reaping winning child 00B86400 PID 12170424
CreateProcess(NULL,C:/Program Files/PowerShell/7/pwsh.exe -c $PSVersionTable,...)
Live child 00B86400 (get_var) PID 12193536

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Reaping winning child 00B86400 PID 12193536
Removing child 00B86400 PID 12193536 from chain.
Successfully remade target file 'get_var'.