Neural Network k fold cross validation

I've got the code for Neural network k fold cross validation,but it isnt performing well. Do I need to initialize the network in every iteration?
Is it wrong?
please help!!
clear all;
close all;
clc;
%xlrange=input('Enter the range of data for input','s');
wineInputs=xlsread('wine.xlsx',1);
%xlrange=input('Enter the range of data for target','s');
wineTargets=xlsread('wine.xlsx',2);
%clear xlrange;
inputs = wineInputs;
targets = wineTargets;
k=10;
cvFolds = crossvalind('Kfold', size(targets,2), k);
net = patternnet(10);
for i = 1:k %# for each fold
testIdx = (cvFolds == i); %# get indices of test instances
trainIdx = ~testIdx ; %# get indices training instances
trInd = find(trainIdx);
tstInd = find(testIdx);
net.trainFcn = 'trainscg' ;
net.trainParam.epochs = 100;
net.divideFcn = 'divideind';
net.divideParam.trainInd=trInd;
net.divideParam.testInd=tstInd;
% Choose a Performance Function
net.performFcn = 'mse'; % Mean squared error
% Train the Network
[net,tr] = train(net,inputs,targets);
%# test using test instances
outputs = net(inputs);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs);
trainTargets = targets .* tr.trainMask{1};
testTargets = targets .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,outputs);
testPerformance = perform(net,testTargets,outputs);
test(k)=testPerformance;
%save net
figure, plotconfusion(targets,outputs);
disp('reached here');
end
accuracy=mean(test)
% View the Network
view(net);

 Respuesta aceptada

Greg Heath
Greg Heath el 24 de Mayo de 2014
1. Yes, the net needs to be reconfigured at the top of the for loop.
2. There is no contingency for obtaining a poor design due to an unfortunate assignment of random initial weights. Two ideas
a. For each i of i =1:k design multiple nets differing by the assignment of random initial weights. Discard those with poor performance and average the performance of the rest.
b. For each k design, keep designing, evaluating and discarding nets until one satisfies an "acceptable" criterion.
Hope this helps.
Thank you for formally accepting my answer
Greg

2 comentarios

piyush dugar
piyush dugar el 24 de Mayo de 2014
Editada: piyush dugar el 24 de Mayo de 2014
Thanks
I did the changes as mentioned by you. I want to know whether the performance will change if I use cvpartition instead of crossvalind.
Greg Heath
Greg Heath el 26 de Mayo de 2014
Why should it?
Why use either?
cvFolds = 1+mod(randperm(N),k);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Deep Learning Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 22 de Mayo de 2014

Comentada:

el 26 de Mayo de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by