I am new to matlab. I have implemented a character recognition system using neural networks.Now, I am trying to do a 10 fold cross validation scheme for neural networks. I have done the following code.But i dont know if it is correct. Pls help me. close all clear all load inputdata load targetdata inputs = input; targets = target; % Create a Pattern Recognition Network
hiddenLayerSize = 30; net = patternnet(hiddenLayerSize); % Choose Input and Output Pre/Post-Processing Functions
% For a list of all processing functions type: help nnprocess
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'}; net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'}; k=10; groups=[1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 12 12 12 12 12 12 12 12 12 12 13 13 13 13 13 13 13 13 13 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13]; %target
cvFolds = crossvalind('Kfold', groups, k); %# get indices of 10-fold CV
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 = 'trainbr' 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) end accuracy=mean(test); % View the Network
view(net)
MATLAB: Neural Network cross validation
neural network
Related Question
- Problem with configuration of neural network
- Test, train and validation performance are so different from global performance in the neural network
- Neural network with multiple inputs and single output – how to change processing functions and how to plot results
- MAPE for out-of-sample in neural network
- MSE error
Best Answer