/
reject.m
458 lines (383 loc) · 13.8 KB
/
reject.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
%% Reject main code file
%
% Developed by (alphabethic order):
% - Jaime S. Cardoso
% Professor at FEUP and Researcher at INESC Porto
% - Beatriz Barbero
% Researcher at INESC Porto
% - Ricardo Sousa
% PhD. Student and Researcher at INESC Porto
%
% Code License (to be defined)
function reject(general_opt)
if nargin ~= 1
% argument sanity check
usage();
return;
end
% max num of threads
% MaxNumCompThreads(1);
warning off all;
% debug info will be written to this file
global filename
datasetID = general_opt.datasetID;
method = general_opt.method;
% ----------------------------------------------------------------------------------
% CONFIGURATION SECTION
% method name
% method = 'threshold';
file = ['log_',method,'_',datasetID,'.log'];
delete( file )
filename = fopen( file, 'w' );
if ~isfield( general_opt, 'normalize' )
normalize = false;
else
normalize = general_opt.normalize;
end
% C-values
if ~isfield( general_opt, 'C' )
Cvalue = -5:2:3; %-5:2:10; %-5:2:15;
Cvalue = 2.^Cvalue;
else
Cvalue = general_opt.C;
end
% Gamma values
if ~isfield ( general_opt, 'gamma' )
gamma = -3:2:-1;
gamma = 2.^gamma;
else
gamma = general_opt.gamma;
end
% rejosvm specific configuration values
if ~isfield ( general_opt, 'h' )
h = 1:4; %3:7 - syntheticI
else
h = general_opt.h;
end
if ~isfield ( general_opt, 's' )
s = [2,4];
else
s = general_opt.s;
end
% weights
wr = 0.04:0.2:.48; % test values
% wr = 0.04:0.04:0.48;
% wr = 0.04:0.1:0.48;
% wr = [ 0.0800 0.1200 0.1600 0.2000 0.2800 0.3200 0.3600 0.4000 0.4800];
% precision
epsilon = 1e-5;
% kernel degree
degree = general_opt.degree;
% folds
foldsx = .05:.05:.8;
folds = [foldsx' 1-foldsx'];
nfolds = 5;
% neural network options
% number of neurons
if ~isfield ( general_opt, 'nneurons' )
nneurons = 5:5:10
else
nneurons = general_opt.nneurons;
end
% number of layers
if ~isfield ( general_opt, 'nlayers' )
nlayers = 2; %1:4; %2
else
nlayers = general_opt.nlayers;
end
% number of layers
if ~isfield ( general_opt, 'trainepochs' )
trainepochs = 15; %1:4; %2
else
trainepochs = general_opt.trainepochs;
end
if ~isfield ( general_opt, 'nensembleAll' )
nensembleAll = 1;
else
nensembleAll = general_opt.nensembleAll;
end
if ~isfield ( general_opt, 'topologyType' )
topologyType = 'square';
else
topologyType = general_opt.SOMtopologyType;
end
if ~isfield ( general_opt, 'SOMcfgcv' )
somcfgcv = true;
else
somcfgcv = general_opt.SOMcfgcv;
end
if ~isfield ( general_opt, 'lattice' )
lattice = 'hextop';
else
lattice = general_opt.lattice;
end
if ~isfield ( general_opt, 'distthresh' )
distthresh = 2;
else
distthresh = general_opt.distthresh;
end
if ~isfield ( general_opt, 'k' )
k = 3;
else
k = general_opt.k;
end
% learning rate
lr = general_opt.learningrate; %0.05;
entropy = general_opt.entropy;
paramRange = struct('neurons',nneurons,'layers',nlayers,'h',h,'s',s,'C',Cvalue,'gamma',gamma,'SOMcfgcv',somcfgcv)
pause
parameters = struct();
parameters.k = k;
parameters.Cvalue = Cvalue;
parameters.gamma = gamma;
parameters.h = h;
parameters.s = s;
parameters.nneurons = nneurons;
parameters.nlayers = nlayers;
parameters.distthresh = distthresh;
parameters.nensembleAll = nensembleAll;
% number of rounds
nrounds = 50; % 100;
% kernel type
kerneltype = general_opt.kernel; %'linear'; 'semipolynomial';
%'polynomial'; 'rbf'; 'sigmoid'
switch (method)
case {'rejoSVM','MLP_threshold','MLP_weights','rejoNN','fumera', ...
'MLP_frankhall_threshold','MLP_frankhall', 'frankhall', 'MLP_threshold_ensemble'}
kernel = kerneltype;
case {'threshold','threshold_ensemble','weights','weights_ensemble','frankhall_threshold'}
switch ( kerneltype )
case 'linear'
kernel = 0;
case 'polynomial'
kernel = 1;
case 'rbf'
kernel = 2;
case 'sigmoid'
kernel = 3;
end
case {'SOM_threshold','SOM_threshold_supervised','SOM_weights','SOM_weights_supervised','rejoSOM','knn'}
kernel = '';
otherwise
fprintf(1,'error: method ''%s'' unknown\n',method);
usage();
return
end
if ( strcmp(method,'threshold') == 1 || ...
strcmp(method,'MLP_threshold') == 1 || ...
strcmp(method,'frankhall_threshold') == 1 || ...
strcmp(method,'MLP_frankhall_threshold') == 1 || ...
strcmp(method,'threshold_ensemble') == 1 )
probability = 1;
else
probability = 0;
end
% -------------------------------------------------------
% lets combine all them
combinations = combine_parameters( general_opt, parameters);
method_parameter = 1;
% ----------------------------------------------------------------------------------
% specific options for the my_svm_dual_train
options = struct('trial',general_opt.trial,'epsilon',epsilon,'method',method,'method_parameter',method_parameter);
options.project_lib_path = 'libraries/';
options.fumera_path = fullfile('Fumera','fumera_code');
options.workmem = 1024;
%options.test = false;
options.normalize = normalize;
% SVM Specific Options
options.coef = 1;
options.kernel = kernel;
options.degree = degree;
options.weights = 1;
options.wr = wr;
options.folds = folds;
options.nfolds = nfolds;
options.nrounds = nrounds;
options.probability = probability;
options.maxiter = 7000;
options.optimization = 'cplex';
% Neural Network Specific Options
options.learningrate = lr;
options.trainfnt = 'trainrp'; %trainrp
options.errorfnt = 'mse';
options.trainepochs = trainepochs;
options.transferFcn = general_opt.transferFcn; %tansig
options.adaptfcn = 'trains'; %'trainb';
options.learnFcn = 'learngdm'; %'learngdm';
options.wbInitFcn = ''; % rands, initzero
options.layerInitFcn = 'initnw'; %'initnw'
options.performRatio = ''; %0.5;
options.outputsize = 1; % don't touch
% stuff for ensemble learning
options.nensembleAll = nensembleAll;
% stuff for SOM
options.SOMtoolbox = general_opt.SOMtoolbox;
options.SOMconfig = [];
options.SOMtopologyType = topologyType;
options.entropy = entropy;
options.lattice = lattice;
options.rejectSOMmethod = general_opt.rejectSOMmethod;
options.SOMcfgcv = somcfgcv;
% Stuff to save for the models
options.C = [];
options.gamma = [];
options.nneurons = [];
options.nlayers = [];
if ( method_parameter == 0 && ...
( strcmp(options.method,'threshold') == 1||...
strcmp(options.method,'SOM_threshold') == 1||...
strcmp(options.method,'SOM_threshold_supervised') == 1) )
options.nbins = 1;
end
options
pause
%message = 'Dataset division is *NOT* done equally through all classes. Dataset with 600 points.';
%fprintf(1,'\n\n****** %s ******\n\n',upper(message));
fprintf(1,'Using dataset ''%s'' on method ''%s'' \n', datasetID, method );
% ----------------------------------------------------------------------------------
% Thinner grid search
for j = 1 %1:2
best_options = reject_run( options, combinations, datasetID);
break;
switch( method )
case {'MLP_threshold','MLP_weights','rejoNN'}
break;
end
combinations = thinner_grid( best_options, filename );
if j == 1
fprintf(filename,'I will start doing a thinner grid search\n');
fprintf(filename,'Combinations size: %d\n',size(combinations,2));
end
end
fclose( filename );
return
%%
%
function usage
fprintf(1,'You must identify the method and datasetID\n\n');
fprintf(1,'Usage: ./reject method datasetID\n');
fprintf(1,'method: \n');
fprintf(1,['\t- threshold\n'...
'\t- rejoSVM\n'...
'\t- weights\n'...
'\t- MLP_threshold\n'...
'\t- MLP_weights\n'...
'\t- rejoNN\n'...
'\t- frankhall\n'...
'\t- threshold_ensemble\n'...
'\t- weights_ensemble\n'...
'\t- SOM_threshold\n'...
'\t- SOM_weights\n'...
'\t- rejoSOM\n'...
'\n']);
fprintf(1,'datasetID: \n');
fprintf(1,['\t- syntheticI\n'...
'\t- syntheticII'...
'\n\n']);
return
%%
% thinner grid parameters definition
function combinations = thinner_grid( best_options, filename )
mmeanC = 0;
mmeanGamma = 0;
mmeanh = 0;
mmeans = 0;
size(best_options)
nmodels = size(best_options,2);
for i=1:nmodels
mmeanC = mmeanC + best_options{i}.C;
mmeanGamma = mmeanGamma + best_options{i}.gamma;
switch( best_options{i}.method )
case {'rejoSVM','rejoNN'}
mmeanh = mmeanh + best_options{i}.h;
mmeans = mmeans + best_options{i}.s;
end
end
mmeanC = mmeanC/nmodels;
mmeanGamma = mmeanGamma/nmodels;
switch( best_options{i}.method )
case {'rejoSVM','rejoNN'}
mmeanh = mmeanh/nmodels;
mmeans = mmeans/nmodels;
end
fprintf(filename,'C values\n');
%Cvalue = checkinterval(mminC,mmaxC);
Cvalue = checkinterval(mmeanC,mmeanC);
Cvalue = 2.^Cvalue;
fprintf(filename,'Gamma values\n');
%gamma = checkinterval(mminGamma,mmaxGamma);
gamma = checkinterval(mmeanGamma,mmeanGamma);
gamma = 2.^gamma;
h = checkinterval(mmeanh,mmeanh);
s = checkinterval(mmeans,mmeans);
combinations = combvec( Cvalue, gamma, h, s );
return
%%
%
function combinations = combine_parameters( options, parameters)
switch( options.method )
case {'SOM_threshold', 'SOM_threshold_supervised', 'SOM_weights', ...
'SOM_weights_supervised', 'rejoSOM'}
if options.SOMcfgcv
n = length( parameters.nneurons );
if strcmp( options.SOMtopologyType, 'rectangle' ) == 1
v = triu(ones(n,n));
elseif strcmp( options.SOMtopologyType, 'square' ) == 1
v = eye(n);
else
error('SOM topology type unknown.');
end
[I J] = find( v == 1 );
parameters.nneurons = [parameters.nneurons(I); parameters.nneurons(J)];
else
parameters.nneurons = [];
end
end
switch( options.method )
case 'knn'
combinations = combvec( parameters.k );
case 'rejoSVM'
switch( options.kernel )
case 'linear'
combinations = combvec( parameters.Cvalue, parameters.h, parameters.s );
otherwise
combinations = combvec( parameters.Cvalue, parameters.gamma, parameters.h, parameters.s );
end
case {'threshold','threshold_ensemble', 'weights', 'weights_ensemble', 'frankhall','frankhall_threshold'}
switch( options.kernel )
case 'linear'
combinations = parameters.Cvalue;
otherwise
combinations = combvec( parameters.Cvalue, parameters.gamma );
end
case 'fumera'
combinations = parameters.Cvalue;
case {'MLP_threshold','MLP_threshold_ensemble','MLP_weights','MLP_frankhall_threshold','MLP_frankhall'}
combinations = combvec( parameters.nneurons, parameters.nlayers );
case 'rejoNN'
combinations = combvec( parameters.nneurons, parameters.nlayers, parameters.h, parameters.s );
case {'SOM_threshold','SOM_threshold_supervised'}
% , parameters.distthresh
switch( options.rejectSOMmethod )
case {'weightscosts', 'somtoolboxprob', 'exprule'}
combinations = combvec( parameters.nneurons, [1,5,10] );
case 'parzen'
combinations = combvec( parameters.nneurons, [1,5,10], parameters.gamma );
otherwise
error('*** combine_parameters *** ');
end
case {'SOM_weights','SOM_weights_supervised'}
switch( options.rejectSOMmethod )
case {'weightscosts', 'somtoolboxprob', 'exprule'}
combinations = combvec( parameters.nneurons );
case 'parzen'
combinations = combvec( parameters.nneurons, parameters.gamma );
otherwise
error('*** combine_parameters *** ');
end
case 'rejoSOM'
combinations = combvec( parameters.SOMcfg, parameters.h, parameters.s );
otherwise
str = sprintf('Method ''%s'' unknown.\n',options.method);
error(str);
end