K-cross validation ANN

2 visualizaciones (últimos 30 días)
Mallikarjun Yelameli
Mallikarjun Yelameli el 9 de Mayo de 2017
Comentada: Greg Heath el 9 de Mayo de 2017
Check my following code, if there is any error (may be logical) let me know. Thank You.
clc;clear all; close all;
%training data of size (1024x3600) It has 1024 features and 3600 samples
x=dlmread('train_data.txt');
%targets data labels of size (15x3600) The data has 15 classes.
y=dlmread('targets_train.txt');
%test data has 1024 features and 900 samples.
test_data=dlmread('test_data.txt');
%target test data is of size (15x900);
test_target=dlmread('test_target.txt');
% Take note that, the train and test data is already divided. These two datasets are independant.
best_acc=0; %this is the best cross-validation accuracy, initialized as zero.
nr_fold=5; % number of cross-validation required, in my case, I took number 5
%number hidden layers iterates over the loop, to calculate the best best hidden layer.
hiddenLayerSize=10:1:20;
for i=1:length(hiddenLayerSize)
net=patternnet(hiddenLayerSize(i));
net.divideFcn='';
acc=get_cv_ac(y,x,nr_fold,net); %get_cv_ac function is written below.
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
%now retrain the whole training data with best hiddenLayer number.
net=patternnet(best_hiddenLayer);
net.divideFcn='';
best_model=ovrtrain(net,x,y); %ovrtrain function is written below.
%now check the performance of test dataset (this independant) on this best_model
[pred,pred_ind,actual_ind,acc]=ovrpredict(best_model,test_data,test_target); %ovrpredict function is written below.
fprintf('Accuracy on test dataset=%g\n',acc)
Now get_cv_ac function code
function [ac]=get_cv_ac(y,x,nr_fold,net)
%y is train label
%x is train data
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)); %ovrtrain function is written below
[pred,pred_ind,actual_ind,acc]=ovrpredict(model,x(:,val_ind),y(:,val_ind)); %ovrpredict function is written below
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
  1 comentario
Greg Heath
Greg Heath el 9 de Mayo de 2017
Run your code. If there are any errors, let us know.
Greg

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Sequence and Numeric Feature Data Workflows en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by