How to store different NN topologies using For loop?

10 views (last 30 days)
Mohamed Nedal
Mohamed Nedal on 27 Apr 2019
Hello everyone,
This is a feedforward backbropagation neural network model for predicting the parameter (t). Basically, the code runs over different number of hidden nodes in two hidden layers and calculate the absolute error for each NN topologty. For each NN topology, the training process runs three times to get the average error for that topology.
Here, the for-loop of (k) is to run the same NN topology 3 times to take the average error.
The for-loop of (m) is the number of hidden nodes in the second hidden layer.
And the for-loop of (n) is the number of hidden nodes in the first hidden layer.
I would like to store the "net" data structure with its properties, the NN topology, and the associated error in a single matrix or array to find which NN topology has the least error. The problem is this code doesn't store all the possible combinations for the NN topologies.
Could you please tell me what should I change in the code to achieve that?
Thanks in advance!
Here is the code (you may try the concept with any random data):
close all; clear; clc
opengl('save', 'software')
% Define inputs & outputs
% define inputs & target
train_data = xlsread('trainDataset.xlsx', 1); % any random data is fine
x1 = train_data(:,1);
x2 = train_data(:,2);
x3 = train_data(:,3);
x4 = train_data(:,4);
t = train_data(:,5);
in = [x2 x1 x3 x4];
% Create and train the Multilayer FFNN
AbsErr = zeros(size(t));
meanAbsErr = zeros(size(t));
Topology = cell(64,1);
Performance = cell(64,1);
for n = 4:8
for m = 4:8
for k = 1:3
net = feedforwardnet([n m], 'trainlm');
net = configure(net, in', t');
net.layers{1}.transferFcn = 'logsig';
net.layers{2}.transferFcn = 'logsig';
net.divideParam.trainRatio = 0.7; % training set [%]
net.divideParam.valRatio = 0.3; % validation set [%]
net.divideParam.testRatio = 0; % test set [%]
% Train the NN
[net,tr,Y,E] = train(net, in', t');
% Calculate Error
AbsErr(k) = abs((Y(k) - t(k)) / t(k)) * 100;
meanAbsErr(m) = mean(AbsErr);
Topology{m} = sprintf('[%d %d]', n, m);
Performance{m} = {Topology(m), meanAbsErr(m)};

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by