-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
197 lines (180 loc) · 6.41 KB
/
main.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
#include <iostream>
#include <fstream>
#include <set>
#include <queue>
#include <ctime>
#include <iomanip>
#include "utils.h"
#include "tree.h"
#include "LinearLoss.h"
#include "SingleTaskUpdater.h"
#include "MultiTaskUpdater.h"
#include "Booster.h"
#include "Booster.cpp"
#include "Random.h"
#include "ThreadPool.h"
using namespace std;
Dataset load_dataset(string path, const int &feature_size, const int &task_num) {
Dataset dataset(feature_size);
dataset.set_task_num(task_num);
cout << "begin load train data" << endl;
ERROR_CODE_CHECK(dataset.load_data_from_file(
path,
","));
cout << "load train data successful" << endl;
return dataset;
}
// single experiment
int boost(int max_num_round,
int common_num_round,
float beta,
int early_stopping_rounds,
string eval_metric,
string dataset_name,
string log_path,
string path,
int feature_size,
int task_num,
float learning_rate,
string regularization) {
clock_t start = clock();
int n_splits = 10;
Booster<LinearLoss, MultiTaskUpdater>
booster(max_num_round, common_num_round, 5, 0.1, beta, 10, learning_rate, regularization);
// vector<pair<Dataset, Dataset>> datasets = data.shuffle_split(n_splits, 0.25, 33);
Matrix scores;
vector<float> mean_scores;
for (int i = 0; i < n_splits; ++i) {
Dataset train_data = load_dataset(path + "train_re" + to_string(i+1) + ".csv", feature_size, task_num);
Dataset test_data = load_dataset(path + "test_re" + to_string(i+1) + ".csv", feature_size, task_num);
booster.train(train_data, train_data, eval_metric, early_stopping_rounds, false);
vector<float> score;
booster.predict(test_data, score, log_path);
float mean_score = 0;
for(int j=0;j<task_num;++j){
mean_score+=score[j];
}
mean_scores.push_back(mean_score/task_num);
scores.push_back(score);
}
float final_mean_score = 0;
float final_var = 0.0f;
float t_sum = std::accumulate(std::begin(mean_scores), std::end(mean_scores), 0.0f);
final_mean_score = t_sum / mean_scores.size();
for (vector<float>::const_iterator it = mean_scores.begin(); it != mean_scores.end(); ++it) {
final_var += (*it - final_mean_score) * (*it - final_mean_score);
}
final_var = final_var / (mean_scores.size() - 1);
//calculate each task mean score
Matrix scores_tran = transpose(scores);
ofstream ofile(log_path + "log_score", ios::app);
ofile << "dataset_name: " << dataset_name << endl;
ofile << "Parameter:" << endl;
ofile << "max_num_round = " << max_num_round << ", common_num_round = " << common_num_round << ", regularization = "
<< regularization;
if (regularization == "variance" or regularization == "weight_entropy") {
ofile << ", beta = " << beta;
}
ofile << ", early_stopping_rounds = " << early_stopping_rounds << ", eval_metric = " << eval_metric << ", n_splits = "
<< n_splits << endl;
// the mean score of all tasks
float avg_score = 0;
for (int i = 0; i < scores_tran.size(); ++i) {
float sum = std::accumulate(std::begin(scores_tran[i]), std::end(scores_tran[i]), 0.0f);
float mean = sum / scores_tran[i].size();
avg_score += mean;
float stddev = 0.0f;
for (vector<float>::const_iterator it = scores_tran[i].begin(); it != scores_tran[i].end(); ++it) {
stddev += (*it - mean) * (*it - mean);
}
stddev = std::sqrt(stddev / (scores_tran[i].size() - 1));
time_t now = time(NULL);
cout << asctime(localtime(&now)) << "The " << i + 1 << "th task " << eval_metric << " score: " << mean << ", +/- "
<< stddev << endl;
// ofile << asctime(localtime(&now)) << "The " << i + 1 << "th task " << eval_metric << " score: " << mean
// << ", +/- "
// << stddev << endl;
ofile << std::setprecision(4) << mean << "\t";
}
ofile << endl;
time_t now = time(NULL);
cout << asctime(localtime(&now)) << eval_metric << " score: " << final_mean_score << "+/-" << final_var <<endl;
ofile << asctime(localtime(&now)) << eval_metric << " score: " << final_mean_score << "+/-" << final_var <<endl;
clock_t finish = clock();
cout << "program cost time: " << (double)(finish - start)/CLOCKS_PER_SEC <<"seconds" << endl;
ofile << "program cost time: " << (double)(finish - start)/CLOCKS_PER_SEC <<"seconds" << endl;
ofile << "------------------------------------------------" << endl;
ofile.close();
return SUCCESS;
}
int single_sarcos_boost() {
string eval_metric = "nrmse";
string dataset_name = "sarcos";
int feature_size = 21;
int task_num = 7;
string log_path = "D:\\C++\\ClionProject\\multi-task-gradient-boosting\\data\\sarcos\\";
string path = "D:\\C++\\ClionProject\\multi-task-gradient-boosting\\data\\sarcos\\sub_sarcos.csv";
int max_num_round = 100;
int common_num_round = 50;
float beta = 0;
int early_stopping_round = 10;
float learning_rate = 0.05;
string regularization = "entropy";
boost(max_num_round,
common_num_round,
beta,
early_stopping_round,
eval_metric,
dataset_name,
log_path,
path,
feature_size,
task_num,
learning_rate,
regularization
);
return SUCCESS;
}
int single_school_boost() {
string eval_metric = "rmse";
string dataset_name = "school";
int feature_size = 28;
int task_num = 139;
string log_path = "D:\\C++\\ClionProject\\multi-task-gradient-boosting\\data\\school\\";
string path = "D:\\C++\\ClionProject\\multi-task-gradient-boosting\\data\\school\\school_";
int max_num_round = 10;
int common_num_round = 5;
float beta = 0.01;
int early_stopping_round = 10;
float learning_rate = 0.5;
string regularization = "entropy";
boost(max_num_round,
common_num_round,
beta,
early_stopping_round,
eval_metric,
dataset_name,
log_path,
path,
feature_size,
task_num,
learning_rate,
regularization
);
return SUCCESS;
}
int test_class_boost() {
vector<int> common_num_rounds{0};
vector<float> betas{0, 0.001, 1.0};
vector<int> early_stopping_rounds{0};
float learning_rate = 0.1;
Booster<LogisticLoss, MultiTaskUpdater>
booster(20, 10, 5, 0.1, betas[0], 10, learning_rate, "variance");
Dataset data = load_dataset("/Users/squall/work/tree/data/xijue_data.txt", 263, 4);
booster.train(data, data, "auc", 4, false);
return SUCCESS;
}
int main() {
single_school_boost();
return 0;
}