MATLAB Answers

Neural Network k fold cross validation

89 views (last 30 days)
piyush dugar
piyush dugar on 22 May 2014
Commented: Greg Heath on 26 May 2014
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);

Accepted Answer

Greg Heath
Greg Heath on 24 May 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 Comments
Greg Heath
Greg Heath on 26 May 2014
Why should it?
Why use either?
cvFolds = 1+mod(randperm(N),k);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!