从c#

时间:2016-02-11 02:59:13

标签: c# .net matlab matlab-compiler

我正在使用Matlab 2015a并开发,训练和测试了分类集合(提升树)并保存了经过最​​佳训练的模型(.mat文件)。

由于我希望在.Net C#应用程序中使用此模型,我创建了一个.m文件来加载包含训练分类器的.mat文件,并使用它根据传入的功能预测结果(请参阅代码.m文件如下)。

function [ypredict score] = fnTrainedClassifer( input_args )
  load ('trainedClassifier.mat');
  [ypredict score] = predict(trainedClassifier,input_args);
end

然后我使用Matlab编译器创建.Net程序集,并确保在库中包含.mat文件以供运行库所需的文件。到目前为止一切都很好...然后我创建了一个快速的C#控制台应用程序来测试库(请参阅下面的应用程序代码)并运行它。

using System;
using MathWorks.MATLAB.NET.Arrays;
using TrainedClassifierComp;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            MLTestClass theModel = null;   /* Stores deployment class instance */
            MWStructArray inputs = null;   /* Sample input data */
            MWArray[] result = null;       /* Stores the result */
            MWNumericArray prediction = null;  /* Ouptut data extracted from result */
            MWNumericArray score = null;  /* Ouptut data extracted from result */

            /* Create the new deployment object */
            theModel = new MLTestClass();

            /* Create an MWStructArray */
            String[] myFieldNames = { "1", "2", "3", "4", "5", "6", "7", "8" };
            inputs = new MWStructArray(1, 8, myFieldNames);

            /* Populate struct with some sample inputs  */
            inputs["1", 1] = 1;
            inputs["2", 2] = 2;
            inputs["3", 3] = 3;
            inputs["4", 4] = 4;
            inputs["5", 5] = 5;
            inputs["6", 6] = 6;
            inputs["7", 7] = 7;
            inputs["8", 8] = 8;

            /* Show some of the sample data */
            Console.WriteLine("Inputs: ");
            Console.WriteLine(inputs.ToString());

            /* Pass it to a MATLAB function */
            result = theModel.fnTrainedClassifier(1, inputs);
            prediction = (MWNumericArray) result[0];
            score = (MWNumericArray)result[1];

            /* Show the results */
            Console.WriteLine("Prediction: ");
            Console.WriteLine(prediction.ToString());
            Console.WriteLine("Score: ");
            Console.WriteLine(prediction.ToString());
        }
    }
}

不幸的是,我提出了以下执行时间错误,在Google上搜索没有任何有用的结果:

  

警告:变量' trainingClassifier'最初保存为classreg.learning.classif.ClassificationEnsemble无法实例化为对象,将作为uint32读入。   在fnTrainedClassifier中(第4行)   使用预测时出错(第84行)   uint32类的系统不能与"预测"命令。首先将系统转换为标识的模型,例如使用" idss"命令。   fnTrainedClassifier出错(第5行)

如何解决有关如何实现类似问题的此问题信息的任何指导都将非常感谢。

附加信息:我尝试了其他几种AI系统,例如神经网络和我有同样的问题。在我看来,我没有正确地接近这个 - 是否有另一种在外部编译语言中使用Matlab函数的方法,如c#?

1 个答案:

答案 0 :(得分:1)

问题已经有一段时间了,但我遇到了同样的问题。我希望以下解决方案有助于节省一些时间。 因此,当从.mat文件加载类对象时,似乎Matlab编译器会读取类类型,但不会加载类本身。因此,我们需要在某种程度上添加实例化对象的类定义。

为了强制编译器加载所需的类,我创建了一个类的空对象,然后从.mat文件中加载了对象并将其分配给空对象。 您的垫码应该类似于以下内容;

function [ypredict score] = fnTrainedClassifer( input_args )
     trainedClassifier = classreg.learning.classif.ClassificationEnsemble.empty; % only this line is added.
     load ('trainedClassifier.mat');
     [ypredict score] = predict(trainedClassifier,input_args);
end