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);

 採用された回答

Greg Heath
Greg Heath 2014 年 5 月 24 日

1 投票

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 件のコメント

piyush dugar
piyush dugar 2014 年 5 月 24 日
編集済み: piyush dugar 2014 年 5 月 24 日
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 2014 年 5 月 26 日
Why should it?
Why use either?
cvFolds = 1+mod(randperm(N),k);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by