1 function [estimated, scores, estimated_training, scores_training] =
classif_module(xTrain, xVal, xTest, labels_train, labels_val, parameters)
2 %classification module,
3 %Mohammad Soleymani, 1 October 2012, m.soleymani@imperial.ac.uk
5 %xTrain = training samples,
if non sequnetional, each row is one sample, each
8 %labels_train = training ground truth labels_train
9 %prameters gridSearch 0 = no grid search
for svm , 1 = grid search
for svm
11 %estimated = estimated labels_train on the test set
12 %scores = confidence scores on the estimations
13 % estimated_training : classfied labels_train on training set
17 parameters.grid_search = 1;
18 parameters.priorP = ones(1,parameters.nbClasses)/parameters.nbClasses;
21 if ~isfield(parameters,
'priorP')
22 parameters.priorP = ones(1,parameters.nbClasses)/parameters.nbClasses;
25 labels_unique = unique(labels_train);
26 if ~isfield(parameters,'nbClasses')
27 parameters.nbClasses = length(labels_unique);
33 switch(parameters.classifier)
37 if parameters.grid_search
38 perf_metr_m = zeros(length(Cs),length(Gammas));
39 perf_metr_sd = zeros(length(Cs),length(Gammas));
40 parameters.nbFolds = 5;
41 folds =
kfold_gen(labels_train,parameters);
43 if length(labels_unique)==parameters.nbClasses
44 for k = 1:parameters.nbClasses
45 paramSV = [paramSV '-w' num2str(labels_unique(k)) ' ' num2str(length(labels_train)/sum(labels_train==labels_unique(k))) ' ' ];
48 warning('Training set labels do not contains all the labels')
51 for iGamma = 1:length(Gammas)
52 svm_prms = ['-s 0 -t 2 -d 3 -c ' num2str(Cs(iC)) ' -h 0 -b 1 -g ' num2str(Gammas(iGamma)) ' ' paramSV];
53 lbls_fld = ones(size(labels_train));
54 nbTrials = length(labels_train);
55 for k = 1:parameters.nbFolds
57 train_set = setdiff(1:nbTrials,test_set);
58 model = svmtrain(labels_train(train_set), xTrain(train_set,:), svm_prms);
59 [lbls_fld(test_set)] = svmpredict(labels_train(test_set), ...
60 xTrain(test_set,:), model);
63 perf_metr_m(iC,iGamma) = mean(f1s);
64 perf_metr_sd(iC,iGamma) = std(f1s);
67 f1_max = max(perf_metr_m(:));
68 iBest = find(perf_metr_m == f1_max);
70 temp = perf_metr_sd(:);
71 sd_min = min(temp(iBest));
73 sd_min = perf_metr_sd(iBest);
75 [iBestC iBestGamma] = find(perf_metr_m==f1_max & perf_metr_sd==sd_min,1,
'first');
76 disp([
'Best C = ' num2str(Cs(iBestC))
' Best Gamma = ' num2str(Gammas(iBestGamma))
', f1 = ' num2str(f1_max)]);
77 svm_prms = [
'-s 0 -t 2 -d 3 -c ' num2str(Cs(iBestC))
' -h 0 -b 1 -g ' num2str(Gammas(iBestGamma))
' ' paramSV];
79 svm_prms =
'-s 0 -t 2 -d 3 -c 1 -g 0.15 -h 0 -b 1 ';
81 model = svmtrain(labels_train, xTrain, svm_prms);
82 if parameters.is_fusion
83 [estimated_training, ~, post_p_tr] = svmpredict(labels_train, xTrain, model,
' -b 1');
84 scores_training(:,model.Label) = post_p_tr;
86 [estimated, ~, post_p] = ...
87 svmpredict(ones(size(xTest,1),1), xTest, model,
' -b 1');
88 scores(:,model.Label) = post_p;
90 b = ridge(labels_train,xTrain,10,0);
91 estimated= b(1)*ones(size(xTest,1),1);
92 estimated = estimated+ xTest*b(2:end);
93 estimated(estimated<parameters.lower_limit) = parameters.lower_limit;
94 estimated(estimated>parameters.upper_limit) = parameters.upper_limit;
95 scores = zeros(length(estimated),1);
97 xTrain = [ones(size(xTrain,1),1) xTrain];
98 b = regress(labels_train,xTrain);
99 estimated= b(1)*ones(size(xTest,1),1);
100 estimated = estimated+ xTest*b(2:end);
101 estimated(estimated<parameters.lower_limit) = parameters.lower_limit;
102 estimated(estimated>parameters.upper_limit) = parameters.upper_limit;
103 scores = zeros(length(estimated),1);
110 if size(y,1)<size(y,2)
116 if parameters.grid_search
117 perf_metr_m = zeros(length(Cs),1);
118 perf_metr_sd = zeros(length(Cs),1);
122 for iC = 1:length(Cs)
124 svm_prms = ['-s 11 -c ' num2str(Cs(iC))];
125 lbls_fld = ones(size(y));
126 nbTrials = length(y);
128 for k = 1:length(folds)
130 train_set = setdiff(1:nbTrials,test_set);
131 %model = svmtrain( y(train_set), x(train_set,:), svm_prms);
132 model = train(y(train_set), sparse(x(train_set,:)), svm_prms);
133 % [lbls_fld(test_set)] = svmpredict(y(test_set), ...
134 % x(test_set,:), model);
135 [lbls_fld(test_set)] = predict(y(test_set), ...
136 sparse(x(test_set,:)), model);
140 model = train(y, sparse(x), svm_prms);
141 % [lbls_fld(test_set)] = svmpredict(y(test_set), ...
142 % x(test_set,:), model);
143 lbls_est = predict(y_val, ...
144 sparse(x_val), model);
149 perf_metr_rho(iC) = evals.rho;
150 perf_metr_rmse(iC) = evals.RMSE;
152 inds = find(perf_metr_rmse==min(perf_metr_rmse));
155 iBestC = inds(perf_metr_rho(inds) == max(perf_metr_rho(inds)));
159 rmse_min = min(perf_metr_rmse(:));
161 disp([
'Best C = ' num2str(Cs(iBestC))
', RMSE = ' num2str(rmse_min)]);
163 svm_prms = [
'-s 11 -c ' num2str(Cs(iBestC)) ];
165 svm_prms =
'-s 11 -c 10 ';
169 model = train(y, sparse(x), svm_prms);
170 estimated = predict(ones(size(xTest,1),1), sparse(xTest), model);
171 estimated(estimated<parameters.lower_limit) = parameters.lower_limit;
172 estimated(estimated>parameters.upper_limit) = parameters.upper_limit;
175 scores = zeros(length(estimated),1);
177 addpath(
'../toolboxes/libsvm-3.19/matlab');
183 if size(y,1)<size(y,2)
187 Cs = 2.^[-3:3];%5 10 % instead of starting at -6
188 Gammas = 1/size(xTrain,2);%2.^[-15:2:-9];%instead of +9
191 if parameters.grid_search
192 perf_metr_m = zeros(length(Cs),length(Gammas));
193 perf_metr_sd = zeros(length(Cs),1);
194 parameters.nbFolds = 3;
197 for iC = 1:length(Cs)
198 for iGamma = 1:length(Gammas)
199 svm_prms = ['-s 4 -t 2 -d 3 -c ' num2str(Cs(iC)) ' -g ' num2str(Gammas(iGamma)) ' -h 0' ];
200 lbls_fld = ones(size(y));
201 nbTrials = length(y);
204 for k = 1:parameters.nbFolds
206 train_set = setdiff(1:nbTrials,test_set);
207 model = svmtrain(y(train_set), x(train_set,:), svm_prms);
208 [lbls_fld(test_set)] = svmpredict(y(test_set), ...
209 x(test_set,:), model);
213 model = svmtrain(y, x, svm_prms);
215 lbls_est = svmpredict(y_val, ...
220 perf_metr_rho(iC,iGamma) = evals.rho;
221 perf_metr_rmse(iC,iGamma) = evals.RMSE;
225 inds = find(perf_metr_rmse==min(perf_metr_rmse(:)));
228 iBests = inds(find(perf_metr_rho(inds) == max(perf_metr_rho(inds)),1,
'first'));
234 [iBestC iBestGamma] = find(perf_metr_rmse==perf_metr_rmse(iBests) ...
235 & perf_metr_rho==perf_metr_rho(iBests),1,
'first');
236 disp([
'Best C = ' num2str(Cs(iBestC))
' Best Gamma = ' num2str(Gammas(iBestGamma))
', rho = ' num2str(max(perf_metr_rho(:)))]);
238 rho_max = max(perf_metr_rho(:));
239 svm_prms = [
'-s 4 -t 2 -d 3 -c ' num2str(Cs(iBestC))
' -g ' num2str(Gammas(iBestGamma))
' -h 0' ];
241 svm_prms =
'-s 4 -t 2 -d 3 -c 10 -g 0.01 -h 0 ';
246 model = svmtrain(y, x, svm_prms);
247 estimated = svmpredict(ones(size(xTest,1),1), xTest, model);
248 estimated(estimated<parameters.lower_limit) = parameters.lower_limit;
249 estimated(estimated>parameters.upper_limit) = parameters.upper_limit;
250 scores = zeros(length(estimated),1);
251 case {
'diaglinear' 'linear' 'quadratic' 'diagquadratic'}
253 [estimated,~,scores] = classify(xTest, xTrain,labels_train,parameters.classifier);
255 disp(
'pooled variance problem - passing')
256 estimated = ones(size(xTest,1));
257 scores = zeros(size(xTest,1),parameters.nbClasses);
260 [estimated_training, ~, scores_training] = classify(xTrain, xTrain, labels_train,parameters.classifier);
262 disp('pooled variance problem for training - passing')
263 estimated_training = ones(size(xTrain,1));
264 scores_training = zeros(size(xTrain,1),parameters.nbClasses);