My goal is to optimize my NN for a fiting problem. So I want to test several number of neurons in the hidden layer and repeat the simulation with a new initialization using initwn function several times, finnally i'll choose the best architecture.
run('Data.m') % downloading data
Nmax=13; % maximum Number of neurones in the hidden layer
s2=10; % maximum Number of initialization
for i=1:Nmax
com=0;
while true
com=com+1;
inputs = In'; % In dimension 4*576
targets = Out'; % Out dimension 2*576
hiddenLayerSize =i; net = feedforwardnet(hiddenLayerSize); net.layers{1}.transferFcn = 'tansig'; %
net.layers{2}.transferFcn = 'tansig'; % I choosed tansig because I want to use 'trainbr' and 'initnw'
net.initFcn = 'initlay'; net.layers{1}.initFcn = 'initnw'; net.layers{2}.initFcn = 'initnw'; net = init(net); net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'}; net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};
net.divideFcn='dividerand'; net.divideParam.trainRatio = 75/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 10/100;
net.trainFcn = 'trainbr'; % Bayesian Regularization backpropagation
net.performFcn = 'mse'; % Mean squared error
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', … 'plotregression', 'plotfit'};
[net,tr] = train(net,inputs,targets);
outputs = net(inputs); errors = gsubtract(targets,outputs); performance = perform(net,targets,outputs);
% What is the importance of the folowing lines?
trainTargets = targets .* tr.trainMask{1}; valTargets = targets .* tr.valMask{1}; trainPerformance = perform(net,trainTargets,outputs) valPerformance = perform(net,valTargets,outputs)
save(sprintf('Network_%d',com), 'net') % I save networks
if com>s2 break; end end % end of while
end
Unfortunately I got an error in this program when I run it. It seems that the the probleme occur in trainTargets = targets .* tr.trainMask{1}; valTargets = targets .* tr.valMask{1};
Could someone help me with this issue?
I want to know if this strategy to find the best NN for my problem is good too. I want to find the optimal Neurons in the hidden layer and the good weight initialization to find a global minimum with a good generalization at the same time.
Best Answer