save a training code using ANN in script and use it in a function in simulink Matlab.

Hello,
so i have a code for prediction of the output power of solar pv panel in a script, i want to save the training model of the script, then i want to use this training model in a function block using simulink Matlab, but the problem is i don't know how to save the "net" of the training model, and after saving it, how to use it in the function?
%code for prediction
load Nndat.mat
% This script assumes these variables are defined:
% datas21 - input data.
% Solarenergy - target data.
x = datas21';
t = Solarenergy';
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
% Create a Fitting Network
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize,trainFcn);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t);
Error using matlab.internal.lang.capability.Capability.require
This functionality is not available on remote platforms.

Error in matlab.ui.internal.uifigureImpl (line 32)
Capability.require(Capability.WebWindow);

Error in uifigure (line 34)
window = matlab.ui.internal.uifigureImpl(varargin{:});

Error in nnet.guis.StandaloneTrainToolView (line 115)
this.Figure = uifigure('Visible', 'off',...

Error in nnet.guis.NNTrainToolFactory/createStandaloneView (line 12)
view = nnet.guis.StandaloneTrainToolView(this);

Error in nnet.guis.StandaloneTrainToolPresenter (line 32)
this.StandaloneTrainToolView = this.TrainToolFactory.createStandaloneView();

Error in nnet.guis.NNTrainToolFactory/createStandalonePresenter (line 8)
presenter = nnet.guis.StandaloneTrainToolPresenter(this);

Error in nnet.train.TrainToolFeedback/startImpl (line 70)
this.TrainToolPresenter = this.TrainToolFactory.createStandalonePresenter();

Error in nnet.train.FeedbackHandler/start (line 18)
this.startImpl(useSPMD,data,net,tr,options,status);

Error in nnet.train.MultiFeedback/startImpl (line 29)
this.Handlers{i}.start(useSPMD,data,net,tr,options,status);

Error in nnet.train.FeedbackHandler/start (line 18)
this.startImpl(useSPMD,data,net,tr,options,status);

Error in nnet.train.trainNetwork>trainNetworkInMainThread (line 42)
feedback.start(false,rawData,archNet,worker.tr,calcLib.options,worker.status);

Error in nnet.train.trainNetwork (line 27)
[archNet,tr] = trainNetworkInMainThread(archNet,rawData,calcLib,calcNet,tr,feedback,localFcns);

Error in trainlm>train_network (line 160)
[archNet,tr] = nnet.train.trainNetwork(archNet,rawData,calcLib,calcNet,tr,localfunctions);

Error in trainlm (line 59)
[out1,out2] = train_network(varargin{2:end});

Error in network/train (line 374)
[net,tr] = feval(trainFcn,'apply',net,data,calcLib,calcNet,tr);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)
% View the Network
view(net)
% Plots
% Uncomment these lines to enable various plots.
plot(x(1,:),y,'o')
%function using simulink matlab:
function outputPower = predictPVOutput(inputData)
% Predict using the ANN
outputPower = net(inputData);
end

 採用された回答

Ganesh
Ganesh 2023 年 12 月 18 日
編集済み: Ganesh 2023 年 12 月 18 日
I understand that you are trying to save your ANN so as to use the model at a later point. You can achieve this by using the save and load commands.
In your case, the implementation will be as follows:
[net,tr] = train(net,x,t); % Training the model
save('net')
function outputPower = predictPVOutput(inputData)
load('net')
outputPower = net(inputData);
end
Please refer to the following documentation for more info on save() function. You may use it to name your saved file accordingly.
Thanks,
Ganesh

3 件のコメント

Mounira
Mounira 2024 年 1 月 10 日
hey, thanks a lot for your answer, i will check it and get back to you on that
Mounira
Mounira 2024 年 1 月 10 日
編集済み: Mounira 2024 年 1 月 19 日
so i just tried your code, but when i call the function like this: predictPVOutput(4,40,277,7,0,0,0.16,-30,3.2,0,0,96.2,1020,271,4.1)
it gives this error:
Error using predictPVOutput
Too many input arguments.
but why? i have 15 inputs for the ANN, why the function is not the same?
Ganesh
Ganesh 2024 年 1 月 11 日
編集済み: Ganesh 2024 年 1 月 11 日
Hi Kawsar,
You encounter this issue as you are calling predictPVOutput() with multiple inputs, wheras the function takes only one input - inputData.
You need to pass in the arguments as an array so that the input is treated as single argument.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Deep Learning Toolbox についてさらに検索

製品

リリース

R2023b

質問済み:

2023 年 12 月 13 日

編集済み:

2024 年 1 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by