-
Notifications
You must be signed in to change notification settings - Fork 2
/
Node.cpp
584 lines (558 loc) · 21.6 KB
/
Node.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
//
// Created by squall on 18-6-11.
//
#include <iostream>
#include <set>
#include "Node.h"
#include<numeric>
#include <chrono>
int Node::calc_node_score(const Matrix &gradients, float lambda) {
float sum_g = 0.0f;
float sum_h = 0.0f;
for (int i = 0; i < this->sample_index.size(); ++i) {
sum_g += gradients[this->sample_index[i]][0];
sum_h += gradients[this->sample_index[i]][1];
}
this->node_score = (sum_g * sum_g) / (sum_h + lambda);
return SUCCESS;
}
// calculate structure score of each task
int Node::calc_node_scores(Dataset const &data, float lambda) {
vector<vector<float>> used_data;
vector<float> used_label;
vector<int> used_task;
Matrix node_gradients;
data.get_sample_by_index(this->sample_index, used_data, used_label, used_task, node_gradients);
vector<float> sum_g(data.get_task_num() + 1, 0.0f);
vector<float> sum_h(data.get_task_num() + 1, 0.0f);
for (int i = 0; i < this->sample_index.size(); ++i) {
sum_g[used_task[i]] += node_gradients[i][0];
sum_h[used_task[i]] += node_gradients[i][1];
}
this->node_scores.push_back(0.0f);
for (int i = 1; i <= data.get_task_num(); ++i) {
this->node_scores.push_back((sum_g[i] * sum_g[i]) / (sum_h[i] + lambda));
}
return SUCCESS;
}
int Node::calc_node_weight(const Matrix &gradients, float lambda) {
float sum_g = 0.0f;
float sum_h = 0.0f;
for (int i = 0; i < this->sample_index.size(); ++i) {
sum_g += gradients[this->sample_index[i]][0];
sum_h += gradients[this->sample_index[i]][1];
}
// cout<<"This is : " << sum_g<<", this is sum_h: "<<sum_h<<endl;
this->weight = -sum_g / (sum_h + lambda);
// cout<<"leaf weight: "<<this->weight<<endl;
return SUCCESS;
};
int Node::find_split_point(Dataset const &data, float lambda) {
vector<vector<float>> used_data;
vector<float> used_label;
vector<int> used_task;
Matrix node_gradients;
data.get_sample_by_index(this->sample_index, used_data, used_label, used_task, node_gradients);
int best_feature = -1;
float best_cut_point = -1.0f;
float best_score = -1.0f;
// std::cout<<this->node_score<<endl;
for (int i = 0; i < data.get_feature_size(); ++i) {
float cut_point, score;
ERROR_CODE_CHECK(this->find_split_point_single_feature(used_data[i],
used_label,
node_gradients,
cut_point,
score,
lambda));
if (score > best_score) {
best_score = score;
best_cut_point = cut_point;
best_feature = i;
}
}
// cout<<best_score<<endl;
this->feature_index = best_feature;
this->split_point = best_cut_point;
// this->node_score = best_score;
if (this->feature_index == -1) {
this->right = NULL;
this->left = NULL;
} else {
// generate the corresponding child node.
this->generate_node(used_data[best_feature], best_cut_point);
}
return 0;
}
int Node::find_split_point_single_feature(const vector<float> &feature,
const vector<float> &label,
const Matrix &gradients,
float &cut_point,
float &score,
float lambda) {
if (feature.empty()) {
return NODE_SAMPLE_EMPTY;
}
set<float> unique_num;
for (int i = 0; i < feature.size(); i++) {
unique_num.insert(feature[i]);
}
set<float> candidate_cut_points;
if (unique_num.size() > 100) {
this->find_candidate_split_feature_value(feature, gradients, candidate_cut_points);
} else {
candidate_cut_points = unique_num;
}
// std::cout<<unique_num.size()<<" "<<feature.size()<<endl;
float best_score = -1.0f;
float best_cut_point = 0;
for (set<float>::iterator it = candidate_cut_points.begin(); it != candidate_cut_points.end(); ++it) {
float tmp = this->score_obj->get_score(feature, gradients,
this->sample_index,
*it, lambda
);
// current cut point score.
tmp = tmp - this->node_score;
if (tmp > best_score) {
best_score = tmp;
best_cut_point = *it;
}
}
// std::cout<<best_score<<endl;
score = best_score;
cut_point = best_cut_point;
return 0;
}
int Node::find_split_point_common(Dataset const &data, float lambda, float beta, string regularization) {
vector<vector<float>> used_data;
vector<float> used_label;
vector<int> used_task;
Matrix node_gradients;
data.get_sample_by_index(this->sample_index, used_data, used_label, used_task, node_gradients);
vector<int> used_data_sizes(data.get_task_num() + 1, 0);
for (int i = 0; i < used_task.size(); ++i) {
used_data_sizes[used_task[i]] += 1;
}
int best_feature = -1;
float best_cut_point = 0.0f;
float best_score = -1000.0f;
// std::cout<<this->node_score<<endl;
for (int i = 0; i < data.get_feature_size(); ++i) {
float cut_point, score;
ERROR_CODE_CHECK(this->find_split_point_single_feature_common(used_data[i],
used_label,
used_task,
used_data_sizes,
data.get_task_num(),
node_gradients,
cut_point,
score,
lambda,
beta,
regularization));
if (score > best_score) {
best_score = score;
best_cut_point = cut_point;
best_feature = i;
}
}
// cout<<best_feature<<" "<<best_score<<" "<<best_cut_point<<endl;
this->feature_index = best_feature;
this->split_point = best_cut_point;
// this->node_score = best_score;
if (this->feature_index == -1) {
this->right = NULL;
this->left = NULL;
} else {
// generate the corresponding child node.
this->generate_node(used_data[best_feature], best_cut_point);
}
return 0;
}
int Node::find_split_point_single_feature_common(const vector<float> &feature,
const vector<float> &label,
const vector<int> &task,
const vector<int> &data_sizes,
const int &task_num,
const Matrix &gradients,
float &cut_point,
float &score,
float lambda,
float beta,
string regularization) {
if (feature.empty()) {
return NODE_SAMPLE_EMPTY;
}
cout << "go here" << endl;
set<float> unique_num;
for (int i = 0; i < feature.size(); i++) {
unique_num.insert(feature[i]);
}
set<float> candidate_cut_points;
cout << " outof the count " << unique_num.size() << endl;
if (unique_num.size() > 100) {
this->find_candidate_split_feature_value(feature, gradients, candidate_cut_points);
} else {
candidate_cut_points = unique_num;
}
cout << unique_num.size() << endl;
float best_score = -1000.0f;
float best_cut_point = 0;
for (set<float>::iterator it = candidate_cut_points.begin(); it != candidate_cut_points.end(); ++it) {
vector<float> tmp_scores = this->score_obj->get_scores(feature, gradients, task, task_num,
this->sample_index,
*it, lambda
);
float tmp_score = this->score_obj->get_score(feature, gradients, this->sample_index, *it, lambda);
float raw_gain_score = (tmp_score - this->node_score);
if (raw_gain_score <= 0.0f) {
continue;
}
// gain rate for each task
vector<float> tmp_gains(task_num, 0.0f);
for (int i = 1; i <= task_num; ++i) {
//tmp_gains[i] can be equal to 0
tmp_gains[i - 1] = tmp_scores[i] - this->node_scores[i];
}
// current cut point score.
float reg = 0.0f;
float gain_score = 0.0f;
if (regularization == "variance") {
stddev_regularization(tmp_gains, reg);
gain_score = raw_gain_score - beta * reg;
} else if (regularization == "entropy") {
entropy_regularization(tmp_gains, reg);
gain_score = reg * raw_gain_score;
} else if (regularization == "weight_entropy") {
entropy_regularization(tmp_gains, reg);
gain_score = (1 + beta * reg) * gain_score;
} else {
gain_score = raw_gain_score;
}
if (gain_score > best_score) {
best_score = gain_score;
best_cut_point = *it;
}
}
// std::cout<<best_score<<endl;
score = best_score;
cut_point = best_cut_point;
return 0;
}
int Node::find_split_point_thread(Dataset const &data, float lambda) {
vector<vector<float>> used_data;
vector<float> used_label;
vector<int> used_task;
Matrix node_gradients;
data.get_sample_by_index(this->sample_index, used_data, used_label, used_task, node_gradients);
int best_feature = -1;
float best_cut_point = -1.0f;
float best_score = -1.0f;
ThreadPool pool(THREAD_NUM);
std::vector<std::future<int> > results;
auto *cut_point = new float[data.get_feature_size()]();
auto *score = new float[data.get_feature_size()]();
for (int i = 0; i < data.get_feature_size(); ++i) {
results.emplace_back(pool.enqueue(Node::find_split_point_single_feature_static,
used_data[i],
used_label,
node_gradients,
this->sample_index,
this->score_obj,
this->node_score,
lambda,
i,
cut_point,
score));
}
for (auto &&x: results) {
if (x.get() != SUCCESS) {
return NODE_SPLIT_ERROR;
}
}
for (int i = 0; i < data.get_feature_size(); ++i) {
if (score[i] > best_score) {
best_score = score[i];
best_feature = i;
best_cut_point = cut_point[i];
}
}
delete[] score;
delete[] cut_point;
// cout<<best_score<<endl;
this->feature_index = best_feature;
this->split_point = best_cut_point;
// this->node_score = best_score;
if (this->feature_index == -1) {
this->right = NULL;
this->left = NULL;
} else {
// generate the corresponding child node.
this->generate_node(used_data[best_feature], best_cut_point);
}
return SUCCESS;
}
int Node::find_split_point_single_feature_static(const vector<float> &feature,
const vector<float> &label,
const Matrix &gradients,
const vector<int> &sample_index,
Updater *score_obj,
float node_score,
float lambda,
int feature_index,
float *cut_point,
float *score) {
if (feature.empty()) {
return NODE_SAMPLE_EMPTY;
}
set<float> unique_num;
for (int i = 0; i < feature.size(); i++) {
unique_num.insert(feature[i]);
}
set<float> candidate_cut_points;
if (unique_num.size() > 100) {
find_candidate_split_feature_value(feature, gradients, candidate_cut_points);
} else {
candidate_cut_points = unique_num;
}
// std::cout<<unique_num.size()<<" "<<feature.size()<<endl;
float best_score = -1.0f;
float best_cut_point = 0;
for (set<float>::iterator it = candidate_cut_points.begin(); it != candidate_cut_points.end(); ++it) {
float tmp = score_obj->get_score(feature, gradients,
sample_index,
*it, lambda
);
// current cut point score.
tmp = tmp - node_score;
if (tmp > best_score) {
best_score = tmp;
best_cut_point = *it;
}
}
// std::cout<<best_score<<endl;
score[feature_index] = best_score;
cut_point[feature_index] = best_cut_point;
return SUCCESS;
}
int Node::find_split_point_common_thread(Dataset const &data, float lambda, float beta, string regularization) {
vector<vector<float>> used_data;
vector<float> used_label;
vector<int> used_task;
Matrix node_gradients;
data.get_sample_by_index(this->sample_index, used_data, used_label, used_task, node_gradients);
vector<int> used_data_sizes(data.get_task_num() + 1, 0);
for (int i = 0; i < used_task.size(); ++i) {
used_data_sizes[used_task[i]] += 1;
}
int best_feature = -1;
float best_cut_point = -1.0f;
float best_score = -1000.0f;
ThreadPool pool(THREAD_NUM);
std::vector<std::future<int> > results;
auto *cut_point = new float[data.get_feature_size()]();
auto *score = new float[data.get_feature_size()]();
for (int i = 0; i < data.get_feature_size(); ++i) {
results.emplace_back(pool.enqueue(Node::find_split_point_single_feature_common_static,
used_data[i],
used_label,
used_task,
used_data_sizes,
data.get_task_num(),
node_gradients,
this->sample_index,
this->score_obj,
this->node_score,
this->node_scores,
lambda,
beta,
i,
cut_point,
score,
regularization));
}
for (auto &&x: results) {
if (x.get() != SUCCESS) {
return NODE_SPLIT_ERROR;
}
}
for (int i = 0; i < data.get_feature_size(); ++i) {
if (score[i] > best_score) {
best_score = score[i];
best_feature = i;
best_cut_point = cut_point[i];
}
}
delete[] score;
delete[] cut_point;
this->feature_index = best_feature;
this->split_point = best_cut_point;
// this->node_score = best_score;
if (this->feature_index == -1) {
this->right = NULL;
this->left = NULL;
} else {
// cout<<"best_cut_point: "<<best_cut_point<<endl;
// generate the corresponding child node.
this->generate_node(used_data[best_feature], best_cut_point);
}
return SUCCESS;
}
int Node::find_split_point_single_feature_common_static(const vector<float> &feature,
const vector<float> &label,
const vector<int> &task,
const vector<int> &data_sizes,
const int &task_num,
const Matrix &gradients,
const vector<int> &sample_index,
Updater *score_obj,
const float &node_score,
const vector<float> &node_scores,
const float &lambda,
const float &beta,
const int &feature_index,
float *cut_point,
float *score,
string regularization) {
if (feature.empty()) {
return NODE_SAMPLE_EMPTY;
}
set<float> unique_num;
for (int i = 0; i < feature.size(); i++) {
unique_num.insert(feature[i]);
}
set<float> candidate_cut_points;
if (unique_num.size() > 100) {
find_candidate_split_feature_value(feature, gradients, candidate_cut_points);
} else {
candidate_cut_points = unique_num;
}
float best_score = -1000.0f;
float best_cut_point = 0;
for (set<float>::iterator it = candidate_cut_points.begin(); it != candidate_cut_points.end(); ++it) {
vector<float> tmp_scores = score_obj->get_scores(feature, gradients, task, task_num,
sample_index,
*it, lambda
);
float tmp_score = score_obj->get_score(feature, gradients, sample_index, *it, lambda);
float raw_gain_score = (tmp_score - node_score);
if (raw_gain_score <= -1.0f) {
continue;
}
// gain for each task
vector<float> tmp_gains(task_num, 0.0f);
for (int i = 1; i <= task_num; ++i) {
tmp_gains[i - 1] = tmp_scores[i] - node_scores[i];
}
// current cut point score.
float reg = 0.0f;
float gain_score = 0.0f;
if (regularization == "variance") {
stddev_regularization(tmp_gains, reg);
gain_score = raw_gain_score - beta * reg;
} else if (regularization == "entropy") {
entropy_regularization(tmp_gains, reg);
gain_score = reg * raw_gain_score;
} else if (regularization == "weight_entropy") {
entropy_regularization(tmp_gains, reg);
gain_score = (1 + beta * reg) * gain_score;
} else {
gain_score = raw_gain_score;
}
if (gain_score > best_score) {
best_score = gain_score;
best_cut_point = *it;
}
}
// std::cout<<best_score<<endl;
score[feature_index] = best_score;
cut_point[feature_index] = best_cut_point;
return SUCCESS;
}
int Node::stddev_regularization(const vector<float> &task_gains, float ®) {
// calculate mean and variance
float sum = std::accumulate(std::begin(task_gains), std::end(task_gains), 0.0f);
float mean = sum / task_gains.size();
float variance = 0.0f;
for (vector<float>::const_iterator it = task_gains.begin(); it != task_gains.end(); ++it) {
variance += (*it - mean) * (*it - mean);
}
reg = variance / (task_gains.size() - 1);
}
int Node::entropy_regularization(const vector<float> &task_gains, float ®) {
float z = 0.0f;
vector<float> c;
float gain_sum = 0.0f;
for (vector<float>::const_iterator it = task_gains.begin(); it != task_gains.end(); ++it) {
float task_gain = *it <= 0 ? 0 : *it;
gain_sum += task_gain;
c.push_back(task_gain);
}
for (vector<float>::const_iterator it = c.begin(); it != c.end(); ++it) {
if (*it == 0) continue;
z -= (*it / gain_sum) * std::log(*it / gain_sum);
}
reg = z;
}
int Node::generate_node(const vector<float> &feature, float cut_points) {
vector<int> left_node_sample;
vector<int> right_node_sample;
if (feature.size() != this->sample_index.size() || feature.size() == 0) {
return NODE_SAMPLE_EMPTY;
}
for (int i = 0; i < feature.size(); ++i) {
int index = this->sample_index[i];
feature[i] >= cut_points ? right_node_sample.push_back(index) : left_node_sample.push_back(index);
}
Node *left_node = NULL;
Node *right_node = NULL;
if (left_node_sample.size() != 0 && right_node_sample.size() != 0) {
// 没问题,只有当两个叶子结点都样本的时候才能进行分裂,否则就不分裂了。
left_node = new Node(left_node_sample, this, this->score_obj, this->min_sample_num);
right_node = new Node(right_node_sample, this, this->score_obj, this->min_sample_num);
}
// cout<<left_node_sample.size()<< " " <<right_node_sample.size()<<endl;
this->right = right_node;
this->left = left_node;
}
int Node::get_sample_size() const {
return this->sample_index.size();
}
int Node::find_candidate_split_feature_value(const vector<float> &feature,
const Matrix &gradients,
set<float> &candidate_cut_points) {
cout << "go here" << endl;
vector<pair<float, float>> d;
for (int i = 0; i < feature.size(); ++i) {
d.emplace_back(make_pair(feature[i], gradients[i][1]));
}
// sort by feature value
sort(d.begin(), d.end(), cmp);
cout << "go here 1" << endl;
float sum_h = 0.0f;
for (int i = 0; i < d.size(); ++i) {
sum_h += d[i].second;
}
// init candidate_cut_points
candidate_cut_points.insert(d[0].first);
// 上一个特征值的rank
float pre_rank = 0.0f;
// 当前二阶导数和
float cur_sum_h = d[0].second;
// 当前符合x<z的二阶导数和
float cur_real_sum_h = 0.0f;
float pre_feature_value = d[0].first;
for (int i = 1; i < d.size(); ++i) {
if (d[i].first > pre_feature_value) {
cur_real_sum_h = cur_sum_h;
}
float cur_rank = cur_real_sum_h / sum_h;
if (cur_rank - pre_rank >= 0.01) {
candidate_cut_points.insert(d[i].first);
pre_rank = cur_rank;
}
pre_feature_value = d[i].first;
cur_sum_h += d[i].second;
}
return 0;
}