-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathva.cpp
601 lines (523 loc) · 15.3 KB
/
va.cpp
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#include "va.h"
#include <iostream>
#include <fstream>
#include <cmath>
#include <random>
struct IsoNode
{
int num;
double value;
struct IsoNode *next;
};
double IsotonicRegression(const struct Calibrator *cali, const double label, const double score) {
int num_ex = cali->num_ex + 1;
int i, j;
// create linked list
IsoNode *p, *h, *c;
h = new IsoNode;
h->next = NULL;
p = h;
for (i = 1; i < num_ex; ++i) {
c = new IsoNode;
p->next = c;
c->next = NULL;
p = c;
}
// insert label
i = 0;
p = h;
int flag = 0;
while (i < num_ex-1) {
if (score <= cali->scores[i] && !flag) {
p->num = 1;
p->value = label;
p = p->next;
j = i;
flag = 1;
}
p->num = 1;
p->value = cali->labels[i++];
p = p->next;
}
if (p != NULL) {
p->num = 1;
p->value = label;
j = num_ex-1;
}
// pava
p = h;
while (1) {
int inc = 0;
for (p = h; p->next != NULL; p = p->next) {
c = p->next;
if ((p->value) > (c->value)) {
double value = ((p->num * p->value) + (c->num * c->value)) / (p->num + c->num);
p->value = value;
p->num = p->num + c->num;
p->next = c->next;
inc = 1;
delete c;
break;
}
}
if (inc == 0) {
break;
}
}
// get g(s(x))
double value = 0;
for (p = h; p != NULL; p = p->next) {
j -= p->num;
if (j < 0) {
value = p->value;
break;
}
}
// free
for (p = h; p != NULL; ) {
c = p;
p = p->next;
delete c;
}
return value;
}
Model *TrainVA(const struct Problem *train, const struct Parameter *param) {
Model *model = new Model;
model->param = *param;
double ratio = param->ratio;
int num_classes;
int num_ex = train->num_ex;
int num_train = static_cast<int>(num_ex*ratio);
int num_cali = num_ex - num_train;
int *perm = new int[num_ex];
int *start = NULL;
int *label = NULL;
int *count = NULL;
GroupClasses(train, &num_classes, &label, &start, &count, perm);
if (num_classes == 1) {
std::cerr << "WARNING: training set only has one class. See README for details." << std::endl;
}
int *index;
int *selected = new int[num_ex];
clone(index, perm, num_ex);
std::random_device rd;
std::mt19937 g(rd());
for (int i = 0; i < num_classes; ++i) {
std::shuffle(index+start[i], index+start[i]+count[i], g);
}
for (int i = 0; i < num_ex; ++i) {
selected[i] = 0;
}
int fold_start = 0;
double curr_label = train->y[index[0]];
for (int i = 1; i < num_ex; ++i) {
double new_label = train->y[index[i]];
if (new_label != curr_label || i == num_ex-1) {
if (i == num_ex-1) {
++i;
}
int num_class = i - fold_start;
int num_selected = i*num_train/num_ex - fold_start*num_train/num_ex;
if (num_selected == 0) {
num_selected = 1;
std::cerr << "WARNING: at least one instance per class." << std::endl;
}
for (int j = 0; j < num_class; ++j) {
std::uniform_int_distribution<int> ui(0, num_class-j-1);
if (ui(g) < num_selected) {
selected[fold_start+j] = 1;
num_selected = num_selected - 1;
}
}
fold_start = i;
curr_label = new_label;
}
}
struct Problem proper;
proper.num_ex = num_train;
proper.x = new Node*[proper.num_ex];
proper.y = new double[proper.num_ex];
struct Problem calibrator;
calibrator.num_ex = num_cali;
calibrator.x = new Node*[calibrator.num_ex];
calibrator.y = new double[calibrator.num_ex];
int index1 = 0, index2 = 0;
for (int i = 0; i < num_ex; ++i) {
if (selected[i] == 1) {
proper.x[index1] = train->x[index[i]];
proper.y[index1] = train->y[index[i]];
++index1;
} else {
calibrator.x[index2] = train->x[index[i]];
calibrator.y[index2] = train->y[index[i]];
++index2;
}
}
model->svm_model = TrainSVM(&proper, param->svm_param);
Calibrator *cali = new Calibrator;
cali->num_ex = num_cali;
cali->scores = new double[num_cali];
cali->labels = new double[num_cali];
for(int i = 0; i < num_cali; ++i) {
double *decision_values = new double[num_classes*(num_classes-1)/2];
PredictSVMValues(model->svm_model, calibrator.x[i], decision_values);
cali->scores[i] = decision_values[0];
cali->labels[i] = (calibrator.y[i]==label[0]) ? 1 : 0;
delete[] decision_values;
}
QuickSortIndex(cali->scores, cali->labels, 0, static_cast<size_t>(num_cali)-1);
model->cali = cali;
model->num_classes = num_classes;
model->num_ex = num_ex;
clone(model->labels, model->svm_model->labels, num_classes);
delete[] selected;
delete[] index;
delete[] perm;
delete[] start;
delete[] label;
delete[] count;
delete[] proper.x;
delete[] proper.y;
delete[] calibrator.x;
delete[] calibrator.y;
return model;
}
double PredictVA(const struct Model *model, const struct Node *x, double &lower, double &upper, double **avg_prob) {
int num_classes = model->num_classes;
double *decision_values;
decision_values = new double[num_classes*(num_classes-1)/2];
double predicted_label = PredictSVMValues(model->svm_model, x, decision_values);
lower = IsotonicRegression(model->cali, 0, decision_values[0]);
upper = IsotonicRegression(model->cali, 1, decision_values[0]);
*avg_prob = new double[num_classes];
(*avg_prob)[0] = (lower + upper) / 2;
(*avg_prob)[1] = 1 - (*avg_prob)[0];
if (model->param.calibrated == 1) {
if ((*avg_prob)[0] > (*avg_prob)[1]) {
predicted_label = model->labels[0];
} else {
predicted_label = model->labels[1];
}
}
if (predicted_label == model->labels[1]) {
double tmp = lower;
lower = 1 - upper;
upper = 1 - tmp;
}
delete[] decision_values;
return predicted_label;
}
void CrossValidation(const struct Problem *prob, const struct Parameter *param,
double *predicted_labels, double *lower_bounds, double *upper_bounds,
double *brier, double *logloss) {
int num_folds = param->num_folds;
int num_ex = prob->num_ex;
int num_classes;
int *fold_start;
int *perm = new int[num_ex];
if (num_folds > num_ex) {
num_folds = num_ex;
std::cerr << "WARNING: number of folds > number of data. Will use number of folds = number of data instead (i.e., leave-one-out cross validation)" << std::endl;
}
fold_start = new int[num_folds+1];
if (num_folds < num_ex) {
int *start = NULL;
int *label = NULL;
int *count = NULL;
GroupClasses(prob, &num_classes, &label, &start, &count, perm);
int *fold_count = new int[num_folds];
int *index = new int[num_ex];
for (int i = 0; i < num_ex; ++i) {
index[i] = perm[i];
}
std::random_device rd;
std::mt19937 g(rd());
for (int i = 0; i < num_classes; ++i) {
std::shuffle(index+start[i], index+start[i]+count[i], g);
}
for (int i = 0; i < num_folds; ++i) {
fold_count[i] = 0;
for (int c = 0; c < num_classes; ++c) {
fold_count[i] += (i+1)*count[c]/num_folds - i*count[c]/num_folds;
}
}
fold_start[0] = 0;
for (int i = 1; i <= num_folds; ++i) {
fold_start[i] = fold_start[i-1] + fold_count[i-1];
}
for (int c = 0; c < num_classes; ++c) {
for (int i = 0; i < num_folds; ++i) {
int begin = start[c] + i*count[c]/num_folds;
int end = start[c] + (i+1)*count[c]/num_folds;
for (int j = begin; j < end; ++j) {
perm[fold_start[i]] = index[j];
fold_start[i]++;
}
}
}
fold_start[0] = 0;
for (int i = 1; i <= num_folds; ++i) {
fold_start[i] = fold_start[i-1] + fold_count[i-1];
}
delete[] start;
delete[] label;
delete[] count;
delete[] index;
delete[] fold_count;
} else {
for (int i = 0; i < num_ex; ++i) {
perm[i] = i;
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(perm, perm+num_ex, g);
fold_start[0] = 0;
for (int i = 1; i <= num_folds; ++i) {
fold_start[i] = fold_start[i-1] + (i+1)*num_ex/num_folds - i*num_ex/num_folds;
}
}
for (int i = 0; i < num_folds; ++i) {
int begin = fold_start[i];
int end = fold_start[i+1];
int k = 0;
struct Problem subprob;
subprob.num_ex = num_ex - (end-begin);
subprob.x = new Node*[subprob.num_ex];
subprob.y = new double[subprob.num_ex];
for (int j = 0; j < begin; ++j) {
subprob.x[k] = prob->x[perm[j]];
subprob.y[k] = prob->y[perm[j]];
++k;
}
for (int j = end; j < num_ex; ++j) {
subprob.x[k] = prob->x[perm[j]];
subprob.y[k] = prob->y[perm[j]];
++k;
}
struct Model *submodel = TrainVA(&subprob, param);
if (param->probability == 1) {
for (int j = 0; j < submodel->num_classes; ++j) {
std::cout << submodel->labels[j] << " ";
}
std::cout << '\n';
}
for (int j = begin; j < end; ++j) {
double *avg_prob = NULL;
brier[perm[j]] = 0;
predicted_labels[perm[j]] = PredictVA(submodel, prob->x[perm[j]], lower_bounds[perm[j]], upper_bounds[perm[j]], &avg_prob);
for (k = 0; k < submodel->num_classes; ++k) {
if (submodel->labels[k] == prob->y[perm[j]]) {
brier[perm[j]] += (1-avg_prob[k]) * (1-avg_prob[k]);
double tmp = std::fmax(std::fmin(avg_prob[k], 1-kEpsilon), kEpsilon);
logloss[perm[j]] = - std::log(tmp);
} else {
brier[perm[j]] += avg_prob[k] * avg_prob[k];
}
}
if (param->probability == 1) {
for (k = 0; k < submodel->num_classes; ++k) {
std::cout << avg_prob[k] << ' ';
}
std::cout << '\n';
}
delete[] avg_prob;
}
FreeModel(submodel);
delete[] subprob.x;
delete[] subprob.y;
}
delete[] fold_start;
delete[] perm;
return;
}
void OnlinePredict(const struct Problem *prob, const struct Parameter *param,
double *predicted_labels, int *indices,
double *lower_bounds, double *upper_bounds,
double *brier, double *logloss) {
int num_ex = prob->num_ex;
for (int i = 0; i < num_ex; ++i) {
indices[i] = i;
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(indices, indices+num_ex, g);
Problem subprob;
subprob.x = new Node*[num_ex];
subprob.y = new double[num_ex];
for (int i = 0; i < num_ex; ++i) {
subprob.x[i] = prob->x[indices[i]];
subprob.y[i] = prob->y[indices[i]];
}
for (int i = 4; i < num_ex; ++i) {
double *avg_prob = NULL;
brier[i] = 0;
subprob.num_ex = i;
Model *submodel = TrainVA(&subprob, param);
predicted_labels[i] = PredictVA(submodel, subprob.x[i], lower_bounds[i], upper_bounds[i], &avg_prob);
for (int j = 0; j < submodel->num_classes; ++j) {
if (submodel->labels[j] == subprob.y[i]) {
brier[i] += (1-avg_prob[j]) * (1-avg_prob[j]);
double tmp = std::fmax(std::fmin(avg_prob[j], 1-kEpsilon), kEpsilon);
logloss[i] = - std::log(tmp);
} else {
brier[i] += avg_prob[j] * avg_prob[j];
}
}
if (param->probability == 1) {
for (int j = 0; j < submodel->num_classes; ++j) {
std::cout << avg_prob[j] << ' ';
}
std::cout << '\n';
}
FreeModel(submodel);
delete[] avg_prob;
}
delete[] subprob.x;
delete[] subprob.y;
return;
}
int SaveModel(const char *model_file_name, const struct Model *model) {
std::ofstream model_file(model_file_name);
if (!model_file.is_open()) {
std::cerr << "Unable to open model file: " << model_file_name << std::endl;
return -1;
}
const Parameter ¶m = model->param;
model_file << "ratio " << param.ratio << '\n';
model_file << "probability " << param.probability << '\n';
model_file << "calibrated " << param.calibrated << '\n';
if (model->cali != NULL) {
model_file << "num_cali " << model->cali->num_ex << '\n';
}
if (model->svm_model != NULL) {
SaveSVMModel(model_file, model->svm_model);
}
if (model->cali != NULL) {
int num_cali = model->cali->num_ex;
model_file << "cali_scores\n";
for (int i = 0; i < num_cali; ++i) {
model_file << model->cali->scores[i] << ' ';
}
model_file << '\n';
model_file << "cali_labels\n";
for (int i = 0; i < num_cali; ++i) {
model_file << model->cali->labels[i] << ' ';
}
model_file << '\n';
}
if (model_file.bad() || model_file.fail()) {
model_file.close();
return -1;
}
model_file.close();
return 0;
}
Model *LoadModel(const char *model_file_name) {
std::ifstream model_file(model_file_name);
if (!model_file.is_open()) {
std::cerr << "Unable to open model file: " << model_file_name << std::endl;
return NULL;
}
Model *model = new Model;
Parameter ¶m = model->param;
param.load_model = 1;
model->labels = NULL;
model->cali = NULL;
char cmd[80];
while (1) {
model_file >> cmd;
if (std::strcmp(cmd, "ratio") == 0) {
model_file >> param.ratio;
} else
if (std::strcmp(cmd, "probability") == 0) {
model_file >> param.probability;
} else
if (std::strcmp(cmd, "calibrated") == 0) {
model_file >> param.calibrated;
} else
if (std::strcmp(cmd, "num_cali") == 0) {
model->cali = new Calibrator;
model_file >> model->cali->num_ex;
} else
if (std::strcmp(cmd, "svm_model") == 0) {
model->svm_model = LoadSVMModel(model_file);
if (model->svm_model == NULL) {
FreeModel(model);
delete model;
model_file.close();
return NULL;
}
model->num_ex = model->svm_model->num_ex;
model->num_classes = model->svm_model->num_classes;
clone(model->labels, model->svm_model->labels, model->num_classes);
model->param.svm_param = &model->svm_model->param;
} else
if (std::strcmp(cmd, "cali_scores") == 0) {
int num_cali = model->cali->num_ex;
model->cali->scores = new double[num_cali];
for (int i = 0; i < num_cali; ++i) {
model_file >> model->cali->scores[i];
}
} else
if (std::strcmp(cmd, "cali_labels") == 0) {
int num_cali = model->cali->num_ex;
model->cali->labels = new double[num_cali];
for (int i = 0; i < num_cali; ++i) {
model_file >> model->cali->labels[i];
}
break;
} else {
std::cerr << "Unknown text in model file: " << cmd << std::endl;
FreeModel(model);
delete model;
model_file.close();
return NULL;
}
}
model_file.close();
return model;
}
void FreeModel(struct Model *model) {
if (model->svm_model != NULL) {
FreeSVMModel(&(model->svm_model));
delete model->svm_model;
model->svm_model = NULL;
}
if (model->cali != NULL) {
if (model->cali->scores != NULL) {
delete[] model->cali->scores;
}
if (model->cali->labels != NULL) {
delete[] model->cali->labels;
}
delete model->cali;
model->cali = NULL;
}
if (model->labels != NULL) {
delete[] model->labels;
model->labels = NULL;
}
delete model;
model = NULL;
return;
}
void FreeParam(struct Parameter *param) {
if (param->svm_param != NULL) {
FreeSVMParam(param->svm_param);
param->svm_param = NULL;
}
return;
}
const char *CheckParameter(const struct Parameter *param) {
if (param->save_model == 1 && param->load_model == 1) {
return "cannot save and load model at the same time";
}
if (param->ratio <= 0 ||
param->ratio >= 1) {
return "ratio should be > 0 and < 1";
}
if (param->svm_param == NULL) {
return "no svm parameter";
}
return CheckSVMParameter(param->svm_param);
}