-
Notifications
You must be signed in to change notification settings - Fork 84
/
test_logistic_regression.m
387 lines (292 loc) · 12.9 KB
/
test_logistic_regression.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
function test_logistic_regression()
clc;
clear;
close all;
%% Set algorithms
if 0
algorithms = sgd_solver_list('ALL');
else
algorithms = {'SGD','SVRG','SVRG-LBFGS','IQN'};
end
%% prepare dataset
if 1
% generate synthtic data
d = 10;
n = 100;
data = logistic_regression_data_generator(n, d);
x_train = data.x_train;
y_train = data.y_train;
x_test = data.x_test;
y_test = data.y_test;
d = size(x_train,1);
w_opt = data.w_opt;
lambda = 0.1;
elseif 1
% load pre-created synthetic data
data = importdata('../data/logistic_regression/data_100d_10000.mat');
x_train = data.x_train;
y_train = data.y_train;
x_test = data.x_test;
y_test = data.y_test;
d = size(x_train,1);
n = length(y_train);
w_opt = data.w_opt;
lambda = data.lambda;
else
% load real-world data
data = importdata('../data/mushroom/mushroom.mat');
x_in = data.X';
y_in = data.y';
d = size(x_in,1);
n = length(y_in);
n_train = floor(n/2);
% split data into train and test data
x_train = x_in(:,1:n_train);
y_train = y_in(1:n_train);
x_test = x_in(:,n_train+1:end);
y_test = y_in(n_train+1:end);
w_opt = zeros(d,1);
lambda = 0.1;
end
% set plot_flag
if d > 4
plot_flag = false; % too high dimension
else
plot_flag = true;
end
%% define problem definitions
problem = logistic_regression(x_train, y_train, x_test, y_test, lambda);
%% initialize
w_init = randn(d,1);
batch_size = 10;
w_list = cell(length(algorithms),1);
info_list = cell(length(algorithms),1);
%% calculate solution
if norm(w_opt)
else
% calculate solution
w_opt = problem.calc_solution(1000);
end
f_opt = problem.cost(w_opt);
fprintf('f_opt: %.24e\n', f_opt);
%% perform algorithms
for alg_idx=1:length(algorithms)
fprintf('\n\n### [%02d] %s ###\n\n', alg_idx, algorithms{alg_idx});
clear options;
% general options for optimization algorithms
options.w_init = w_init;
options.tol_optgap = 10^-36;
options.max_epoch = 100;
options.verbose = true;
options.lambda = lambda;
options.permute_on = 1;
options.f_opt = f_opt;
switch algorithms{alg_idx}
case {'SD'}
options.step_init = 0.05;
options.max_iter = options.max_epoch;
[w_list{alg_idx}, info_list{alg_idx}] = sd(problem, options);
case {'SGD'}
options.batch_size = batch_size;
options.step_init = 0.0001 * options.batch_size;
%options.step_alg = 'decay';
options.step_alg = 'fix';
[w_list{alg_idx}, info_list{alg_idx}] = sgd(problem, options);
% Variance reduction (VR) varitns
case {'SVRG'}
options.batch_size = batch_size;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
[w_list{alg_idx}, info_list{alg_idx}] = svrg(problem, options);
case {'SAG'}
options.batch_size = batch_size;
%options.step_init = 0.00005 * options.batch_size;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'SAG';
[w_list{alg_idx}, info_list{alg_idx}] = sag(problem, options);
case {'SAGA'}
options.batch_size = batch_size;
%options.step_init = 0.00005 * options.batch_size;
options.step_init = 0.000001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'SAGA';
[w_list{alg_idx}, info_list{alg_idx}] = sag(problem, options);
% AdaGrad variants
case {'AdaGrad'}
options.batch_size = batch_size;
options.step_init = 0.001 * options.batch_size;
options.step_alg = 'fix';
options.epsilon = 0.00001;
options.sub_mode = 'AdaGrad';
[w_list{alg_idx}, info_list{alg_idx}] = adagrad(problem, options);
case {'RMSProp'}
options.batch_size = batch_size;
options.step_init = 0.00001 * options.batch_size;
options.step_alg = 'fix';
options.epsilon = 0.00001;
options.sub_mode = 'RMSProp';
options.beta = 0.9;
[w_list{alg_idx}, info_list{alg_idx}] = adagrad(problem, options);
case {'AdaDelta'}
options.batch_size = batch_size;
options.step_init = 0.01 * options.batch_size;
options.step_alg = 'fix';
options.epsilon = 0.00001;
options.sub_mode = 'AdaDelta';
options.beta = 0.9;
[w_list{alg_idx}, info_list{alg_idx}] = adagrad(problem, options);
case {'Adam'}
options.batch_size = batch_size;
options.step_init = 0.00001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'Adam';
options.beta1 = 0.8;
options.beta2 = 0.999;
options.epsilon = 0.00001;
[w_list{alg_idx}, info_list{alg_idx}] = adam(problem, options);
case {'AdaMax'}
options.batch_size = batch_size;
options.step_init = 0.00001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'AdaMax';
options.beta1 = 0.8;
options.beta2 = 0.999;
options.epsilon = 0.00001;
[w_list{alg_idx}, info_list{alg_idx}] = adam(problem, options);
% Stochastic Quasi-Newton variants
case {'SQN'}
options.batch_size = batch_size;
options.batch_hess_size = batch_size * 20;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'SQN';
options.L = 20;
options.r = 20;
[w_list{alg_idx}, info_list{alg_idx}] = slbfgs(problem, options);
case {'SVRG-SQN'}
options.batch_size = batch_size;
options.batch_hess_size = batch_size * 20;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'SVRG-SQN';
options.L = 20;
options.r = 20;
[w_list{alg_idx}, info_list{alg_idx}] = slbfgs(problem, options);
case {'SVRG-LBFGS'}
options.batch_size = batch_size;
options.batch_hess_size = batch_size * 20;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'SVRG-LBFGS';
options.mem_size = 20;
[w_list{alg_idx}, info_list{alg_idx}] = slbfgs(problem, options);
case {'SS-SVRG'}
options.batch_size = batch_size;
options.batch_hess_size = batch_size * 20;
options.step_init = 0.0005 * options.batch_size;
options.step_alg = 'fix';
r = d-1;
if r < 1
r = 1;
end
options.r = r;
[w_list{alg_idx}, info_list{alg_idx}] = subsamp_svrg(problem, options);
case {'oBFGS-Inf'}
options.batch_size = batch_size;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'Inf-mem';
options.regularized = false;
[w_list{alg_idx}, info_list{alg_idx}] = obfgs(problem, options);
case {'oBFGS-Lim'}
options.batch_size = batch_size;
options.step_init = 0.00001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'Lim-mem';
options.r = 20;
options.regularized = false;
[w_list{alg_idx}, info_list{alg_idx}] = obfgs(problem, options);
case {'Reg-oBFGS-Inf'}
options.batch_size = batch_size;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'Inf-mem';
options.regularized = true;
options.delta = 0.1;
[w_list{alg_idx}, info_list{alg_idx}] = obfgs(problem, options);
case {'Reg-oBFGS-Lim'}
options.batch_size = batch_size;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'Lim-mem';
options.r = 20;
options.regularized = true;
options.delta = 0.1;
[w_list{alg_idx}, info_list{alg_idx}] = obfgs(problem, options);
case {'Damp-oBFGS-Inf'}
options.batch_size = batch_size;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'Inf-mem';
options.regularized = false;
options.delta = 0.1;
options.damped = true;
[w_list{alg_idx}, info_list{alg_idx}] = obfgs(problem, options);
case {'Damp-oBFGS-Lim'}
options.batch_size = batch_size;
options.step_init = 0.0001 * options.batch_size;
options.step_alg = 'fix';
options.sub_mode = 'Lim-mem';
options.regularized = false;
options.delta = 0.1;
options.damped = true;
[w_list{alg_idx}, info_list{alg_idx}] = obfgs(problem, options);
case {'IQN'}
options.w_init = w_init;
options.step_init = 1;
options.step_alg = 'fix';
[w_list{alg_idx}, info_list{alg_idx}] = iqn(problem, options);
otherwise
warn_str = [algorithms{alg_idx}, ' is not supported.'];
warning(warn_str);
w_list{alg_idx} = '';
info_list{alg_idx} = '';
end
end
fprintf('\n\n');
%% plot all
close all;
% display cost vs grads
display_graph('grad_calc_count','cost', algorithms, w_list, info_list);
% display optimality gap vs grads
if options.f_opt ~= -Inf
display_graph('grad_calc_count','optimality_gap', algorithms, w_list, info_list);
end
% display classification results
y_pred_list = cell(length(algorithms),1);
accuracy_list = cell(length(algorithms),1);
for alg_idx=1:length(algorithms)
if ~isempty(w_list{alg_idx})
p = problem.prediction(w_list{alg_idx});
% calculate accuracy
accuracy_list{alg_idx} = problem.accuracy(p);
fprintf('Classificaiton accuracy: %s: %.4f\n', algorithms{alg_idx}, problem.accuracy(p));
% convert from {1,-1} to {1,2}
p(p==-1) = 2;
p(p==1) = 1;
% predict class
y_pred_list{alg_idx} = p;
else
fprintf('Classificaiton accuracy: %s: Not supported\n', algorithms{alg_idx});
end
end
% convert from {1,-1} to {1,2}
y_train(y_train==-1) = 2;
y_train(y_train==1) = 1;
y_test(y_test==-1) = 2;
y_test(y_test==1) = 1;
if plot_flag
display_classification_result(problem, algorithms, w_list, y_pred_list, accuracy_list, x_train, y_train, x_test, y_test);
end
end