Neural network error: 'model' parameter must be a string when calling sim(net,data)

Hello,
I have seen other people have had this problem too - http://www.mathworks.de/matlabcentral/answers/1925-model-parameter-must-be-a-string-error but am unsure how to correct so I apologize in advance only this is acting weird.
I want to save the network to a file so I can (hopefully) use it in a aspx.NET application. When the I call sim(net,data) without saving to a file then it works, when I save it to a file I get the error:
Error using nn1 (line 34)
'model' parameter must be a string
I am using R2012a and am unsure what to do. Following the advice of others in previous versions of this problem I have found the following *when loading from the file*:
>> which sim
built-in (C:\Program Files\MATLAB\R2012a\toolbox\simulink\simulink\sim)
when I *don't* load from the file I get:
K>> which sim
built-in (C:\Program Files\MATLAB\R2012a\toolbox\simulink\simulink\sim)
any help will be gratefully appreciated
Mark
clearvars -global;
a = xlsread('c:\\CS Data\\ScoringValidation1.xls');
a(isnan(a)) =0;
data = a(:, 2:size(a,2)-2);
data = data';
targets = a(:, size(a,2)-1:size(a,2));
t = targets';
net = patternnet(10)
net = train(net,data,t);
view(net)
y = net(data);
perf = perform(net,t,y)
classes = vec2ind(y)
save 'c:\\nn\\net' net
net = load('c:\\nn\\net'); % ***when I use this I get the error when calling sim(net,data1)
% test to see if it works...
data1 = [9,1];
data1(1) = 75;
data1(2) = 75;
data1(3) = 0.06;
data1(4) = 0;
data1(5) = 1;
data1(6) = 0;
data1(7) = 1;
data1(8) = 0;
data1(9) = 8;
data1 = data1'
sim(net,data1)

 采纳的回答

Use the extended version of "which" to provide an argument:
which sim(net)
The question becomes what class "net" is when it is created, and which class it is after the load operation.

12 个评论

Hi,
Sorry for the delay in replying, when it works, I get this:
K>> which sim(net)
C:\Program Files\MATLAB\R2012a\toolbox\nnet\nnet\@network\sim.m % network method
when it doesn't I get:
>> which sim(net)
built-in (C:\Program Files\MATLAB\R2012a\toolbox\simulink\simulink\sim)
You could also try:
>> which -all sim
And make sure that something under $matlabroot/toolbox/nnet shows up.
when it does work:
K>> which -all sim
built-in (C:\Program Files\MATLAB\R2012a\toolbox\simulink\simulink\sim)
C:\Program Files\MATLAB\R2012a\toolbox\ident\ident\@iddata\sim.m % iddata method
C:\Program Files\MATLAB\R2012a\toolbox\ident\ident\@idParametric\sim.m % idParametric method
C:\Program Files\MATLAB\R2012a\toolbox\ident\nlident\@idnlhw\sim.m % idnlhw method
C:\Program Files\MATLAB\R2012a\toolbox\ident\nlident\@idnlgrey\sim.m % idnlgrey method
C:\Program Files\MATLAB\R2012a\toolbox\ident\nlident\@idnlarx\sim.m % idnlarx method
C:\Program Files\MATLAB\R2012a\toolbox\mpc\mpc\@mpc\sim.m % mpc method
C:\Program Files\MATLAB\R2012a\toolbox\nnet\nnet\@network\sim.m % network method
when it doesn't:
K>> which -all sim
built-in (C:\Program Files\MATLAB\R2012a\toolbox\simulink\simulink\sim)
C:\Program Files\MATLAB\R2012a\toolbox\ident\ident\@iddata\sim.m % iddata method
C:\Program Files\MATLAB\R2012a\toolbox\ident\ident\@idParametric\sim.m % idParametric method
C:\Program Files\MATLAB\R2012a\toolbox\ident\nlident\@idnlhw\sim.m % idnlhw method
C:\Program Files\MATLAB\R2012a\toolbox\ident\nlident\@idnlgrey\sim.m % idnlgrey method
C:\Program Files\MATLAB\R2012a\toolbox\ident\nlident\@idnlarx\sim.m % idnlarx method
C:\Program Files\MATLAB\R2012a\toolbox\mpc\mpc\@mpc\sim.m % mpc method
C:\Program Files\MATLAB\R2012a\toolbox\nnet\nnet\@network\sim.m
it shows up but how do I make it go to the right place?
thanks for your help,
Mark
doesn't work:
K>> class(net)
ans =
struct
does work:
K>> class(net)
ans =
network
Ah, I suspected something like that would be the case. Either the net is not being stored as a network object or else it is not being loaded as a network object. You should be able to tell which of those two cases it is by using "whos" with the -file option to have whos look into the .mat file and report what is there.
doesn't work:
K>> who('-file','c:\\nn\\net')
Your variables are:
net
and when I do class(net) I get
K>> class(net)
ans =
struct
how do I read the file into a network object? I have tried:
network net = load('c:\\nn\\net')
and get the errors:
Error using false
Inputs must be numeric.
Error in network>new_network (line 82)
if nargin < 4, inputConnect = false(numLayers,numInputs); end
Error in network (line 72)
net = new_network(varargin{:});
Error in nn1 (line 17)
network net = load('c:\\nn\\net')
Please try with whos instead of who as whos reports the variable class along with other information.
Hi,
I managed to get this working with the following code, using the solutions found here:
so many thanks to these people, including you Walter, you guys are honestly super!!
The final code for anyone else that's interested is(the parameters are to predict survival rates of an individual(male obviously!) after standing on a landmine so edit as you see fit!):
function [output] = runNet(ISS, NISS, TRISS, RTS, pelvic, penis, teste, scrotum, rectum)
%#function network
thedata = load ('c:\\nn\\net');
variablenames = fieldnames(thedata);
for k = 1:numel(variablenames)
y = thedata.(variablenames{k});
if isa(y, 'network')
neti = y;
break;
end
end
data = [9,1];
data(1) = ISS;
data(2) = NISS;
data(3) = TRISS;
data(4) = RTS;
data(5) = pelvic;
data(6) = penis;
data(7) = teste;
data(8) = scrotum;
data(9) = rectum;
data = data'
output = sim(neti,data)
end
for any newbies out there believe me the
%#function network
does work, I was skeptical thinking that matlab would ignore it because of the '%', but it doesn't!! Honestly try it!
I tested it using:
ISS =21
NISS = 21
TRISS = 54.09
RTS=4.09
pelvic=0
penis=3
teste=5
scrotum=0
rectum=0
result=runNet(ISS, NISS, TRISS, RTS, pelvic, penis, teste, scrotum, rectum)
and then compiled it to a dll using the 'deploytool' command to a .net assembly. The C# code that was used was (I deployed to a dll called 'neural':
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using neural;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MWArray ISS, NISS, TRISS, RTS, pelvic, penis, teste, scrotum, rectum;
MWArray result = null;
ISS =21;
NISS = 21;
TRISS = 54.09;
RTS=4.09;
pelvic=0;
penis=3;
teste=5;
scrotum=0;
rectum = 0;
neural.predict np = new predict();
result = np.runNet(ISS, NISS, TRISS, RTS, pelvic, penis, teste, scrotum, rectum);
}
}
}
I hope that this helps somebody so they don't get as frustrated as I have been!
Thanks again Walter for your help.
I'm sure that in the constructor you could probably pass the path of the neural network as a parameter, rather than have it hard coded like I have, but at the moment with 2 weeks left before I have to demonstrate this I probably won't update it(since its now working as I require), but it would be useful. I assume that this would be possible?
Thanks again people for your expertise, it is appreciated.
Oh forgot to mention for the newbies out there that I used matlab 2012a edition and was running in a .net 4.0 environment.
Good luck
:-)
Ah... I didn't realize you were deploying this. If I had, I would have suggested the %#function approach much earlier. Oh well.

请先登录,再进行评论。

更多回答(1 个)

My answer may not be related to this specific question, but it may help anyone searching for this particular error message when attempting to use the function sim() after having saved and loaded a trained neural network.
The trained network is saved as a .mat file. Eg:
save([path 'net.mat'], 'net');
If for example, this network is then loaded in another script:
net = load([path 'net.mat']);
and then one attempts the follwing code:
results = net(testdata); or results = sim(net, testdata) ;
Then MATLAB outputs the following error (respective to the function used above): Subscript indices must either be real positive integers or logicals or 'model' parameter must be a string
The reason for the error in this particular scenario is because the network has been saved in a struct array. To access the network for simulation you must write: importvariablename.savedvariablename . For example, if using the variable names from above the network must be properly accessed by writing:
results = net.net(testdata); or results = sim(net.net, testdata);

类别

帮助中心File Exchange 中查找有关 Deep Learning Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by