Neural Network k fold cross validation
Show older comments
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
More Answers (0)
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!