我正在尝试将MATLAB 2010b与Visual Studio 2008 Professional集成。
我有以下MATLAB方法。
function varargout = perform( func, varargin )
%% // Set default values
workspaceDirectory = ['Results/MatlabWorkspace_' datestr(now, 'yyyy-mm-dd_HH-MM-SS')];
clear args
args = struct('workspacePath', [ workspaceDirectory '/workspace.mat'], ...
'testArray', [], ...
'k', 50, ...
'rate', 0.0001, ...
'trainingDataPath', 'P2AT_LaserImageVectorList.csv', ...
'network', 'feedforwardnet', ...
'initialWeights', [], ...
'divideFcn', 'dividerand', ...
'trainRatio', 70/100, ...
'testRatio', 15/100, ...
'valRatio', 15/100, ...
'trainInd', [], ...
'testInd', [], ...
'valInd', [], ...
'trainFcn', 'trainlm', ...
'performFcn', 'mse', ...
'biasConnect', [0; 0; 0], ...
'layerSize', [9; 4; 1], ...
'layerTransferFcn', ['tansig '; 'tansig '; 'purelin'], ...
'max_fail', 10, ...
'mu_dec', 0.04, ...
'useInitialWeights', false, ...
'saveResults', true);
% // Returns a modified properties structure
args = getopt(args,varargin);
args.layerTransferFcn = cellstr(args.layerTransferFcn);
if args.saveResults && ~strcmpi(func,'test')
if (exist(workspaceDirectory, 'dir') ~= 7)
mkdir(workspaceDirectory);
end
end
if (strcmpi(func,'test'))
try
load(args.workspacePath, '-regexp', '.*');
catch err
Warning(err.message);
varargin{1,1} = null;
return;
end
data_inputAngle = args.testArray(2501);
data_inputPCA = args.testArray(1:2500);
if size(data_inputPCA,1) == 1
data_inputPCA = data_inputPCA';
end
switch(featureExtractionMethod)
case {'gha','apex'}
% // [W, errvals] = gha(data_inputPCA, k, varargin{1,3});
data_PCs = W' * data_inputPCA;
data_inputNN = [data_PCs; data_inputAngle];
case 'nnmf'
% // [W,H,D] = nnmf(data_inputPCA',k);
data_PCs = H * data_inputPCA;
data_inputNN = [data_PCs; data_inputAngle];
case 'pcaProcess'
otherwise
warning = 'ERROR: no feature extraction method has been defined.';
Warning('ERROR: no feature extraction method has been defined.');
varargout{1,1} = null;
return;
end
% // Just to test to see if it recognizes 'feedforwardnet'.
testnet = feedforwardnet; % // <------------------------------- LINE 81
% // Saving all the workspace variables to see if they are all correctly processed.
save('all');
varargout{1,1} = net(data_inputNN); %// <------------------------- LINE 86
end
end
这就是我在Visual Studio中创建要导入的DLL文件的方法:
%%// Determine path names
workdir = pwd();
outdir = fullfile(workdir, 'Output');
dnetdir = fullfile(workdir, 'dotnet');
%%// Determine file names
mfile = fullfile(workdir, 'perform.m');
dnetdll = fullfile(dnetdir, 'dotnet.dll');
%%// Build .NET Assembly
eval(['mcc -N -d ''' dnetdir ''' -W ''dotnet:dotnet,' ...
'dotnetclass,0.0,private'' -T link:lib ''' mfile '''']);
所以当我使用MATLAB Engine's COM接口在C#中运行MATLAB中的例程时,一切都运行得很好:
/*
* This function calls the routine inside
* MATLAB using the MATLAB Engine's COM interface
*/
static private float MatlabTestDebug(float[] testData, Double targetAngle)
{
Array X = new double[testData.Length + 1];
testData.CopyTo(X, 0);
X.SetValue((double)targetAngle, testData.Length);
Array zerosX = new double[X.GetLength(0)];
MLApp.MLAppClass matlab = new MLApp.MLAppClass();
matlab.PutFullMatrix("testArray", "base", X, zerosX);
matlab.PutWorkspaceData("workspacePath", "base", "workspace.mat");
// Using Engine Interface, execute the ML command
// contained in quotes.
matlab.Execute("cd 'c:\\Users\\H\\Documents\\Project\\Source Code\\MatlabFiles';");
matlab.Execute("open perform.m");
matlab.Execute("dbstop in perform.m");
matlab.Execute("result = perform('test', 'workspacePath', 'workspace.mat', 'testArray', testArray);");
matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll");
return (double)matlab.GetVariable("result", "base");
}
但是当我使用.NET程序集时,它无法识别'feedforwardnet'。我曾经在第86行(net(data_inputNN)
)上得到错误。所以我添加了一行来测试它是否至少识别出'feedforwardnet',但它没有。
注意:我正在从包含“net”的文件中加载一些变量,这是一个神经网络(load(args.workspacePath, '-regexp', '.*');
)
同样在MATLAB方法中,如果我从文件加载已保存的“网络”然后将其保存以查看它如何处理网络,它将把它保存为“结构”而不是“网络”。
我假设它正在加载它作为结构开始。
我在MATLAB 2009b中也遇到过这个问题。这就是我现在使用MATLAB 2010b的原因,因为显然MATLAB 2009b没有这个特殊的神经网络工具箱。
以下是使用.NET程序集的C#代码。
/*
* Calls the method from inside a .NET assembly created with MATLAB
* using Builder for .NET.
*/
private float MatlabTest(float[] testData, Double targetAngle)
{
if (testData != null)
{
dotnetclass AClass = new dotnetclass();
Array X = new double[testData.Length + 1];
testData.CopyTo(X, 0);
X.SetValue((double)targetAngle, testData.Length);
MWNumericArray XNumericArray = new MWNumericArray(X);
MWArray[] RetVal = AClass.perform(1, "test",
"workspacePath", "workspace.mat",
"testArray", XNumericArray);
Array result = ((MWNumericArray)RetVal[0]).ToVector(MWArrayComponent.Real);
return (float)result.GetValue(0);
}
else
{
return 0f;
}
}
我在Visual Studio中收到此错误:
... MWMCR::EvaluateFunction error ...
Undefined function or variable 'feedforwardnet'.
Error in => perform.m at line 81.
注意:我的编译器和软件的版本:
编译器:C:\Program Files (x86)\Microsoft Visual Studio 9.0
MATLAB:R2010b(64位)
Visual Studio:MVS 2008专业人员(.NET Framework 3.5 SP1)
Microsoft Windows SDK 6.1
最近更新:
我在mcc中添加了神经网络工具箱的路径。
eval(['mcc -N -p ''C:\Program Files\MATLAB\R2010b\toolbox\nnet'' -d ''' dnetdir ''' -W ''dotnet:dotnet,' ...
'dotnetclass,0.0,private'' -T link:lib -v ''' mfile '''']);
现在我在mccExcludeFiles.log
中收到这些消息:
C:\ Program Files \ MATLAB \ R2010b \ toolbox \ nnet \ nnet \ @network \ network.m
由C:\ Program Files \ MATLAB \ R2010b \ toolbox \ nnet \ nnet \ nnnetwork \ cascadeforwardnet.m调用 (因为工具箱可编辑性规则)
C:\ Program Files \ MATLAB \ R2010b \ toolbox \ nnet \ nnet \ @network \ network.m
由C:\ Program Files \ MATLAB \ R2010b \ toolbox \ nnet \ nnet \ nnnetwork \ feedforwardnet.m调用 (因为工具箱可编辑性规则)
答案 0 :(得分:1)
我能提出的唯一答案(不是问题的解决方案)来自 Creating standalone application that contains neural network toolbox functions ,说明:
您将无法编译任何内容 训练网络的功能 (像ADAPT)。虽然链接没有 明确列出这些功能(如 ADAPT),他们属于'All Other 命令行功能'。
但是,您可以部署M功能 使用预先训练的代码 网络。我相信SIM功能 将部署好。
我看到的工作流程是:
- 在MATLAB中,使用测试输入/输出训练您的网络
- 保存网络(MAT文件?)
- 创建一个可部署的函数,然后使用预训练的网络 新数据。网络本身不会 在这个功能中改变/适应/训练
- 编译并部署上述功能
醇>