-
Notifications
You must be signed in to change notification settings - Fork 36
/
RunSVMClassifierGAK.m
221 lines (131 loc) · 8.8 KB
/
RunSVMClassifierGAK.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
function RunSVMClassifierGAK(DataSetStartIndex, DataSetEndIndex)
% first 2 values are '.' and '..' - UCR Archive 2018 version has 128 datasets
dir_struct = dir('/rigel/dsi/users/ikp2103/VLDBGRAIL/UCR2018/');
Datasets = {dir_struct(3:130).name};
% Sort Datasets
[Datasets, DSOrder] = sort(Datasets);
addpath(genpath('SVMMatlab/.'));
rng(ceil(DataSetStartIndex*100))
pause(300*rand);
poolobj = gcp('nocreate');
delete(poolobj);
parpool(22);
rng('default');
for i = 1:length(Datasets)
if (i>=DataSetStartIndex && i<=DataSetEndIndex)
Results = zeros(length(Datasets),13);
disp(['Dataset being processed: ', char(Datasets(i))]);
DS = LoadUCRdataset(char(Datasets(i)));
%TrainInstancesCount = 1000;
%TrainClassLabels = DS.TrainClassLabels(1:1000);
TrainInstancesCount = DS.TrainInstancesCount;
TrainClassLabels = DS.TrainClassLabels;
[Thebestgamma1,Thebestcost1,Thebestacc1,Thebestiming1] = GridSearchSVM1(-10,0.1,20,TrainInstancesCount,TrainClassLabels,Datasets,i);
[Thebestgamma2,Thebestcost2,Thebestacc2,Thebestiming2] = GridSearchSVM2(Thebestcost1-2,0.01,Thebestcost1+2,TrainInstancesCount,TrainClassLabels,Datasets,i,Thebestgamma1);
[Thebestgamma3,Thebestcost3,Thebestacc3,Thebestiming3] = GridSearchSVM2(Thebestcost2-0.2,0.001,Thebestcost2+0.2,TrainInstancesCount,TrainClassLabels,Datasets,i,Thebestgamma1);
KM = dlmread( strcat( 'KernelMatricesGAK/',char(Datasets(i)),'/', char(Datasets(i)), '_GAK_Sigma_', num2str(Thebestgamma3) ,'_TRAIN.kernelmatrix') );
KMTrain = KM(1:DS.TrainInstancesCount,1:DS.TrainInstancesCount);
tic;
cmd = ['-q -m 500 -t 4 -e 0.001 -c ', num2str(2^Thebestcost3)];
model_precomputed = svmtrain(DS.TrainClassLabels, [(1:DS.TrainInstancesCount)', KMTrain], cmd);
ModelTrainingRuntime = toc;
KM = dlmread( strcat( 'KernelMatricesGAK/',char(Datasets(i)),'/', char(Datasets(i)), '_GAK_Sigma_', num2str(Thebestgamma3) ,'_TESTTOTRAIN.kernelmatrix') );
tic;
%[predict_label_P, accuracy_P, dec_values_P] = svmpredict(DS.TestClassLabels(1:1000), [(1:1000)', KM], model_precomputed);
[predict_label_P, accuracy_P, dec_values_P] = svmpredict(DS.TestClassLabels, [(1:DS.TestInstancesCount)', KM], model_precomputed);
PredictionRuntime = toc;
Results(i,1) = Thebestgamma1;
Results(i,2) = Thebestgamma2;
Results(i,3) = Thebestgamma3;
Results(i,4) = Thebestcost1;
Results(i,5) = Thebestcost2;
Results(i,6) = Thebestcost3;
Results(i,7) = Thebestacc1*0.01;
Results(i,8) = Thebestacc2*0.01;
Results(i,9) = Thebestacc3*0.01;
Results(i,10) = Thebestiming1+Thebestiming2+Thebestiming3;
Results(i,11) = accuracy_P(1)*0.01;
Results(i,12) = ModelTrainingRuntime;
Results(i,13) = PredictionRuntime;
dlmwrite( strcat('RunSVMClassifierGAK/','RunSVMClassifierGAK_', num2str(i) ), Results, 'delimiter', '\t');
end
end
poolobj = gcp('nocreate');
delete(poolobj);
end
function [Thebestgamma,Thebestcost,Thebestacc,Thebestiming] = GridSearchSVM1(GridStart,GridStep,GridEnd,TrainInstancesCount,TrainClassLabels,Datasets,DatasetsNumber)
previousMaxbestacc = 0;
Thebestgamma = 0;
Thebestcost = 0;
Thebestacc = 0;
Thebestiming = 0;
WarpValues = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
% Tuning Parameters
for gamma=1:20
gamma
log2cTmp = GridStart:GridStep:GridEnd;
bestacc = zeros(1,length(log2cTmp));
bestgamma = zeros(1,length(log2cTmp));
bestcost = zeros(1,length(log2cTmp));
besttiming = zeros(1,length(log2cTmp));
KM = dlmread( strcat( 'KernelMatricesGAK/',char(Datasets(DatasetsNumber)),'/', char(Datasets(DatasetsNumber)), '_GAK_Sigma_', num2str(WarpValues(gamma)) ,'_TRAIN.kernelmatrix') );
KMTrain = KM(1:TrainInstancesCount,1:TrainInstancesCount);
% grid search
parfor log2cNEW = 1:length(log2cTmp)
log2cNEW
tic;
log2c = log2cTmp(log2cNEW);
cmd = ['-q -m 500 -t 4 -e 0.001 -v ' num2str(10) ' -c ', num2str(2^log2c)];
cv = svmtrain(TrainClassLabels, [(1:TrainInstancesCount)', KMTrain], cmd);
bestacc(log2cNEW) = cv;
bestcost(log2cNEW) = log2c;
bestgamma(log2cNEW) = WarpValues(gamma);
besttiming(log2cNEW) = toc;
end
[Maxbestacc,~] = max(bestacc);
Posbestacc = find(bestacc==Maxbestacc,1,'last');
Thebestiming = Thebestiming+sum(besttiming);
if Maxbestacc>previousMaxbestacc
Thebestgamma = bestgamma(Posbestacc);
Thebestcost = bestcost(Posbestacc);
Thebestacc = Maxbestacc;
previousMaxbestacc = Maxbestacc;
end
end
end
function [Thebestgamma,Thebestcost,Thebestacc,Thebestiming] = GridSearchSVM2(GridStart,GridStep,GridEnd,TrainInstancesCount,TrainClassLabels,Datasets,DatasetsNumber,gamma)
previousMaxbestacc = 0;
Thebestgamma = 0;
Thebestcost = 0;
Thebestacc = 0;
Thebestiming = 0;
% Tuning Parameters
log2cTmp = GridStart:GridStep:GridEnd;
bestacc = zeros(1,length(log2cTmp));
bestgamma = zeros(1,length(log2cTmp));
bestcost = zeros(1,length(log2cTmp));
besttiming = zeros(1,length(log2cTmp));
KM = dlmread( strcat( 'KernelMatricesGAK/',char(Datasets(DatasetsNumber)),'/', char(Datasets(DatasetsNumber)), '_GAK_Sigma_', num2str(gamma) ,'_TRAIN.kernelmatrix') );
KMTrain = KM(1:TrainInstancesCount,1:TrainInstancesCount);
% grid search
parfor log2cNEW = 1:length(log2cTmp)
log2cNEW
tic;
log2c = log2cTmp(log2cNEW);
cmd = ['-q -m 500 -t 4 -e 0.001 -v ' num2str(10) ' -c ', num2str(2^log2c)];
cv = svmtrain(TrainClassLabels, [(1:TrainInstancesCount)', KMTrain], cmd);
bestacc(log2cNEW) = cv;
bestcost(log2cNEW) = log2c;
bestgamma(log2cNEW) = gamma;
besttiming(log2cNEW) = toc;
end
[Maxbestacc,~] = max(bestacc);
Posbestacc = find(bestacc==Maxbestacc,1,'last');
Thebestiming = Thebestiming+sum(besttiming);
if Maxbestacc>previousMaxbestacc
Thebestgamma = bestgamma(Posbestacc);
Thebestcost = bestcost(Posbestacc);
Thebestacc = Maxbestacc;
previousMaxbestacc = Maxbestacc;
end
end