1 % Solve an Input-Output Fitting problem with a Neural Network
2 % Script generated by Neural Fitting app
3 % Created 20-Jun-2022 16:02:39
5 % This script assumes these variables are defined:
13 % Choose a Training Function
14 % For a list of all training functions type: help nntrain
15 % 'trainlm' is usually fastest.
16 % 'trainbr' takes longer but may be better for challenging problems.
17 % 'trainscg' uses less memory. Suitable in low memory situations.
18 trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
20 % Create a Fitting Network
22 net = fitnet(hiddenLayerSize,trainFcn);
24 % Choose Input and Output Pre/Post-Processing Functions
25 % For a list of all processing functions type: help nnprocess
26 net.input.processFcns = {'removeconstantrows','mapminmax'};
27 net.output.processFcns = {'removeconstantrows','mapminmax'};
29 % Setup Division of Data for Training, Validation, Testing
30 % For a list of all data division functions type: help nndivision
31 net.divideFcn = 'dividerand'; % Divide data randomly
32 net.divideMode = 'sample'; % Divide up every sample
33 net.divideParam.trainRatio = 70/100;
34 net.divideParam.valRatio = 15/100;
35 net.divideParam.testRatio = 15/100;
37 % Choose a Performance Function
38 % For a list of all performance functions type: help nnperformance
39 net.performFcn = 'mse'; % Mean Squared Error
41 % Choose Plot Functions
42 % For a list of all plot functions type: help nnplot
43 net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
44 'plotregression', 'plotfit'};
47 [net,tr] = train(net,x,t);
52 performance = perform(net,t,y)
54 % Recalculate Training, Validation and Test Performance
55 trainTargets = t .* tr.trainMask{1};
56 valTargets = t .* tr.valMask{1};
57 testTargets = t .* tr.testMask{1};
58 trainPerformance = perform(net,trainTargets,y)
59 valPerformance = perform(net,valTargets,y)
60 testPerformance = perform(net,testTargets,y)
66 % Uncomment these lines to enable various plots.
67 %figure, plotperform(tr)
68 %figure, plottrainstate(tr)
69 %figure, ploterrhist(e)
70 %figure, plotregression(t,y)
71 %figure, plotfit(net,x,t)
74 % Change the (false) values to (true) to enable the following code blocks.
75 % See the help for each generation function for more information.
77 % Generate MATLAB function for neural network for application
78 % deployment in MATLAB scripts or with MATLAB Compiler and Builder
79 % tools, or simply to examine the calculations your trained neural
81 genFunction(net,'myNeuralNetworkFunction');
82 y = myNeuralNetworkFunction(x);
85 % Generate a matrix-only MATLAB function for neural network code
86 % generation with MATLAB Coder tools.
87 genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');
88 y = myNeuralNetworkFunction(x);
91 % Generate a Simulink diagram for simulation or deployment with.
92 % Simulink Coder tools.