Check my following code, if there is any error (may be logical) let me know. Thank You.
clc;clear all; close all;
x=dlmread('train_data.txt');
y=dlmread('targets_train.txt');
test_data=dlmread('test_data.txt');
test_target=dlmread('test_target.txt');
best_acc=0;
nr_fold=5;
hiddenLayerSize=10:1:20;
for i=1:length(hiddenLayerSize)
net=patternnet(hiddenLayerSize(i));
net.divideFcn='';
acc=get_cv_ac(y,x,nr_fold,net);
if acc>best_acc
best_acc=acc; best_hiddenLayer=hiddenLayerSize(i);
end
fprintf('%g %g (best acc=%g, best hidden Layer=%g)\n)', acc,hiddenLayerSize(i), best_acc, best_hiddenLayer);
end
end
net=patternnet(best_hiddenLayer);
net.divideFcn='';
best_model=ovrtrain(net,x,y);
[pred,pred_ind,actual_ind,acc]=ovrpredict(best_model,test_data,test_target);
fprintf('Accuracy on test dataset=%g\n',acc)
Now get_cv_ac function code
function [ac]=get_cv_ac(y,x,nr_fold,net)
len=size(y,2);
ac=0;
rand_ind=randperm(len);
for i=1:nr_fold
val_ind=rand_ind([floor((i-1)*len/nr_fold)+1:floor(i*len/nr_fold)])';
train_ind=rand_ind[1:len]';
train_ind(test_ind)=[];
[model,tr]=ovrtrain(net,x(:,train_ind),y(:,train_ind));
[pred,pred_ind,actual_ind,acc]=ovrpredict(model,x(:,val_ind),y(:,val_ind));
ac=ac+sum(pred_ind==actual_ind);
end
ac=ac/len*100;
fprintf('cross-val accuracy=%g\n',ac);
end
Now ovrtrain function is written below
function [model,tr] = ovrtrain(net,x,y)
net=configure(net,x,y);
[model,tr]=train(net,x,y);
end
Now ovrpredict function is written below
function [pred,pred_ind,actual_ind,acc]=ovrpredict(model,x,test_ind)
pred=model(x);
[values,pred_ind]=max(pred,[],1);
[~,actual_ind]=max(test_ind,[],1);
acc=(sum(pred_ind==actual_ind)/size(x,2))*100
end