Clear Filters
Clear Filters

Finding min. MSE

3 views (last 30 days)
Seray Mirasci
Seray Mirasci on 17 Oct 2022
Answered: Akshat on 20 Sep 2023
Hello All,
I am using nftool for find the best hidden layer size.And i want to make a table coloumn as;
  • Hidden layer number
  • MSE Value
  • R Value
Problem is, i want to make a table with every hidden layer size between 10-12 layers and see the details as hidden layer number,mse value and r value for understand which one have better values. But i couldn't make this table.
Here is my code;
% Solve an Input-Output Fitting problem with a Neural Network
% Script generated by Neural Fitting app
% Created 17-Oct-2022 21:54:46
%
% This script assumes these variables are defined:
%
% input - input data.
% output - target data.
x = input';
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
t = output';
% 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
for i = 10:12;
hiddenLayerSize = i
net = fitnet(i,trainFcn);
% Choose Input and Output Pre/Post-Processing Functions
% For a list of all processing functions type: help nnprocess
net.input.processFcns = {'removeconstantrows','mapminmax'};
net.output.processFcns = {'removeconstantrows','mapminmax'};
% Setup Division of Data for Training, Validation, Testing
% For a list of all data division functions type: help nndivision
net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'sample'; % Divide up every sample
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse'; % Mean Squared Error
% Choose Plot Functions
% For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
'plotregression', 'plotfit'};
% Train the Network
[net,tr] = train(net,x,t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)
% Recalculate Training, Validation and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,y)
valPerformance = perform(net,valTargets,y)
testPerformance = perform(net,testTargets,y)
% View the Network
view(net)
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotfit(net,x,t)
% Deployment
% Change the (false) values to (true) to enable the following code blocks.
% See the help for each generation function for more information.
if (false)
% Generate MATLAB function for neural network for application
% deployment in MATLAB scripts or with MATLAB Compiler and Builder
% tools, or simply to examine the calculations your trained neural
% network performs.
genFunction(net,'myNeuralNetworkFunction');
y = myNeuralNetworkFunction(x);
end
if (false)
% Generate a matrix-only MATLAB function for neural network code
% generation with MATLAB Coder tools.
genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');
y = myNeuralNetworkFunction(x);
end
if (false)
% Generate a Simulink diagram for simulation or deployment with.
% Simulink Coder tools.
gensim(net);
end
end

Answers (1)

Akshat
Akshat on 20 Sep 2023
Hi Seray,
As per my understanding of the question, I see that you have the boilerplate code to train and evaluate the neural network you’re using for 10 to 12 hidden layer sizes but are struggling to tabulate the outputs you’re getting.
In that case, I think adding these lines would help:
resultsTable = table('Size', [3, 3], 'VariableTypes', {'double', 'double', 'double'}, 'VariableNames', {'HiddenLayerNumber', 'MSEValue', 'RValue'});
This is to store the data in the table, and hence will be written before the loop is initiated.
resultsTable(i-9, :) = [hiddenLayerSize, performance, corr(t', y')];
This line indicates that we are adding the hiddenLayerSize (between 10 to 12), the performance metric calculated by you in the code (The MSE) and the correlation metric (the R Value) and adding them to the table.
Hence, this will be added inside the loop, and will be the last line when the MSE is already calculated using the call to the “perform” function.
I have attached the updated .m file as well, you can refer to that as well.
Hope this helps!

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!