来自matlab编码器的神经网络c ++代码

时间:2016-06-10 17:14:32

标签: c++ matlab arduino neural-network matlab-compiler

我在Matlab"神经网络模式识别"中训练了一个神经网络。工具箱并从工具箱中生成.m函数。我还编写了自己的函数,它获取了一个包含带数据点作为输入的inf x 5矩阵的数据集,从工具箱中调用NN函数并返回一个具有识别模式和百分比值的向量。 (输入矩阵包含应检测的模式)

因为我需要在Arduino上运行代码,所以我尝试使用Matlab编码器生成C ++代码,但它不起作用。首先,我得到一个警告:arrayfun不支持代码生成(调用os arrayfun位于工具箱生成的函数中)。之后,我将函数的输入定义为"双倍(:7351 x 5)" (它达到了7351,因为我的训练数据集很大)。 现在编码器构建了一个MEX文件来检查运行时问题,并且发生了一些错误。

我不知道为什么目标构建日志是德语,因为Matlab的其余部分是英文的,但通常会丢失一些括号和分号,但为什么呢?如何在matrix.h或amlrt.h中删除括号???

  1   cl  /c /Zp8 /GR /W3 /EHs /nologo /MD /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0   /DMATLAB_MEX_FILE  /DMATLAB_MEX_FILE   /O2 /Oy- /DNDEBUG  /fp:strict  /I "C:\PROGRA~1\MATLAB\R2016a\simulink\include" /I "C:\PROGRA~1\MATLAB\R2016a\toolbox\shared\simtargets" /I "C:\Users\Kai\Desktop\YUN_SI~1" /I "C:\Users\Kai\Desktop\YUN_SI~1\codegen\mex\NeuralNetwork" /I ".\interface" /I "C:\PROGRA~1\MATLAB\R2016a\extern\include" /I "." "NeuralNetwork_data.c"
    2   NeuralNetwork_data.c
    3   c:\program files\matlab\r2016a\extern\include\matrix.h(260) : error C2061: Syntaxfehler: Bezeichner 'mxChar'
    4   c:\program files\matlab\r2016a\extern\include\matrix.h(260) : error C2059: Syntaxfehler: ';'
    5   c:\program files\matlab\r2016a\extern\include\matrix.h(605) : error C2143: Syntaxfehler: Es fehlt '{' vor '*'
    6   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(56) : error C2143: Syntaxfehler: Es fehlt ')' vor '*'
    7   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(56) : error C2143: Syntaxfehler: Es fehlt '{' vor '*'
    8   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(56) : error C2373: 'emlrtCTX': Neudefinition; unterschiedliche Modifizierer
    9           C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(50): Siehe Deklaration von 'emlrtCTX'
   10   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(56) : error C2146: Syntaxfehler: Fehlendes ';' vor Bezeichner 'aTLS'
   11   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(56) : error C2059: Syntaxfehler: ')'
   12   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(133) : error C2061: Syntaxfehler: Bezeichner 'EmlrtErrorFunction'
   13   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(139) : error C2059: Syntaxfehler: '}'
   14   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(194) : error C2061: Syntaxfehler: Bezeichner 'emlrtContextGlobal'
   15   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(194) : error C2059: Syntaxfehler: ';'
   16   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(808) : error C2143: Syntaxfehler: Es fehlt ')' vor '*'
   17   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(808) : error C2143: Syntaxfehler: Es fehlt '{' vor '*'
   18   C:\PROGRA~1\MATLAB\R2016a\extern\include\emlrt.h(808) : error C2059: Syntaxfehler: ')'
   19   c:\users\kai\desktop\yun_simulink\codegen\mex\neuralnetwork\NeuralNetwork_data.h(32) : error C2061: Syntaxfehler: Bezeichner 'emlrtContextGlobal'
   20   c:\users\kai\desktop\yun_simulink\codegen\mex\neuralnetwork\NeuralNetwork_data.h(32) : error C2059: Syntaxfehler: ';'
   21   NeuralNetwork_data.c(22) : error C2061: Syntaxfehler: Bezeichner 'emlrtContextGlobal'
   22   NeuralNetwork_data.c(22) : error C2059: Syntaxfehler: ';'
   23   NeuralNetwork_data.c(22) : error C2513: '/*global*/ ': Keine Variable vor '=' deklariert
   24   gmake: *** [NeuralNetwork_data.obj] Error 2

一般情况下,可以从NN工具箱创建C ++代码,但为什么它在我的情况下不起作用?

我想在c ++中翻译的函数是:

function [ activity, percent ] = NeuralNetwork( Data )
%NN for recogniton of activitys standing, sitting, walking from Accelerometer data, relative error
%is returned in percent
%   input Data contains 5 columns. 1. col -> time; 2-4.col ->
%   x,y,z-Acceleration; 5.col -> activity
%   VAR activity contains 1 for walking, 2 for sitting and 3 for standing 


    Acceleration_data = Data(:,2:4);  %store acceleration data in a matrix
    Acceleration_data = Acceleration_data';     %NN input expects transposed matrix

    targets = Data(:,5);    %activity that should be detected from the NN
    targets = targets';     %output of NN is also transposed

    NNfit = myNeuralNetworkFunction(Acceleration_data); %call the trained NN 
    NNfit = round(NNfit);                             %output can be e.g. 0.998 -> round to 1
    activity=zeros(1,length(NNfit));                   %allocate memory for cpp-code


for i=1:length(NNfit)               %NNfit is a 3D matrix, after round one
    [val pos] = max(NNfit(:,i));    %element is 1 the other are 0 the number of 
                                    %the row represents the activity
    activity(i) = pos;              %vektor containing the activity
end

cnt=0;              %calc percent of correct recognized data points
for i=1:length(NNfit)
   if activity(i) == targets(i)
       cnt = cnt+1;         %amound of correct recognized data points
   end
end

percent = (cnt/length(targets))*100;   %calc percent

end

编辑:

文件NeuralNetwork_data.c:

/* Include files */
#include "rt_nonfinite.h"
#include "NeuralNetwork.h"
#include "NeuralNetwork_data.h"
#include "blas.h"

/* Variable Definitions */
emlrtCTX emlrtRootTLSGlobal = NULL;
const volatile char_T *emlrtBreakCheckR2012bFlagVar = NULL;
covrtInstance emlrtCoverageInstance;
emlrtContext emlrtContextGlobal = { true, false, 131434U, NULL, "NeuralNetwork",
  NULL, false, { 2045744189U, 2170104910U, 2743257031U, 4284093946U }, NULL };

emlrtRSInfo f_emlrtRSI = { 68, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2016a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

emlrtRSInfo g_emlrtRSI = { 85, "xgemm",
  "C:\\Program Files\\MATLAB\\R2016a\\toolbox\\eml\\eml\\+coder\\+internal\\+blas\\xgemm.m"
};

emlrtRSInfo n_emlrtRSI = { 214, "minOrMax",
  "C:\\Program Files\\MATLAB\\R2016a\\toolbox\\eml\\eml\\+coder\\+internal\\minOrMax.m"
};

emlrtRSInfo o_emlrtRSI = { 375, "minOrMax",
  "C:\\Program Files\\MATLAB\\R2016a\\toolbox\\eml\\eml\\+coder\\+internal\\minOrMax.m"
};

emlrtRSInfo p_emlrtRSI = { 347, "minOrMax",
  "C:\\Program Files\\MATLAB\\R2016a\\toolbox\\eml\\eml\\+coder\\+internal\\minOrMax.m"
};

/* End of code generation (NeuralNetwork_data.c) */

0 个答案:

没有答案