Repository navigation
Expand file tree
/
Copy pathKOIL.h
More file actions
257 lines (230 loc) · 10.2 KB
/
Copy pathKOIL.h
File metadata and controls
257 lines (230 loc) · 10.2 KB
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
/**
* @brief Kernelized Online Imbalanced Learning with Fixed Buddget
* Implemented by Junjie Hu
* Contact: jjhu@cse.cuhk.edu.hk
*/
#ifndef _KOIL_H
#define _KOIL_H
#include <string>
#include <vector>
#include "svm.h"
#include <iostream>
using namespace std;
/**
* @brief struct: store the AUC, Accuracy, time, error number in n runs
*
*/
struct koil_result
{
int runs; //number of runs
double* auc; //AUC for each run
double* accuracy; //Accuracy for each run
double* precision;
double* recall;
double* time; //Time for each run
int* err_cnt; //Misclassified samples online
void initial_result(int n);
void free_result();
void save_result(string path, string method);
void load_result(string path);
};
/**
* @brief Kernelized Online Imbalanced Learning
*
*/
class KOIL
{
public:
svm_problem prob;
svm_model rs_model;
svm_model fifo_model;
koil_result rs_result;
koil_result fifo_result;
// save and load path or file name
string save_result_path;
string load_data_path;
string dataset_file;
string idx_asso_file;
string idx_cv_file;
string rs_model_file;
string fifo_model_file;
string rs_result_file;
string fifo_result_file;
string log_file;
//test
//ofstream fout;
// main functions for KOIL
/**
* @brief KOIL_RS++
*
* @param id_train the index of the training samples
* @param cnt_train the number of the training samples
* @param id_test the index of the testing samples
* @param cnt_test the number of the testing samples
* @return model the learned decision function
* @return AUC the AUC value on the testing samples
* @return Accuracy the Accuracy on the testing samples
* @return time the time used for training
* @return err_count the number of the misclassified samples online
*/
void rs_plus(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_model& model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
/**
* @brief KOIL_FIFO++
*
* @param id_train the index of the training samples
* @param cnt_train the number of the training samples
* @param id_test the index of the testing samples
* @param cnt_test the number of the testing samples
* @return model the learned decision function
* @return AUC the AUC value on the testing samples
* @return Accuracy the Accuracy on the testing samples
* @return time the time used for training
* @return err_count the number of the misclassified samples online
*/
void fifo_plus(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_model& model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
/**
* @brief KOIL_MKL_FIFO++
*
* @param id_train the index of the training samples
* @param cnt_train the number of the training samples
* @param id_test the index of the testing samples
* @param cnt_test the number of the testing samples
* @return model multiple kernels for the learned decision function
* @return AUC the AUC value on the testing samples
* @return Accuracy the Accuracy on the testing samples
* @return time the time used for training
* @return err_count the number of the misclassified samples online
*/
void mkl_fifo_plus(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_mkl &model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
/**
* @brief KOIL_MKL_RS++
*
* @param id_train the index of the training samples
* @param cnt_train the number of the training samples
* @param id_test the index of the testing samples
* @param cnt_test the number of the testing samples
* @return model multiple kernels for the learned decision function
* @return AUC the AUC value on the testing samples
* @return Accuracy the Accuracy on the testing samples
* @return time the time used for training
* @return err_count the number of the misclassified samples online
*/
void mkl_rs_plus(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_mkl &model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
/**
* @brief KOIL_MKL_FIFO++
*
* @param id_train the index of the training samples
* @param cnt_train the number of the training samples
* @param id_test the index of the testing samples
* @param cnt_test the number of the testing samples
* @return model multiple kernels for the learned decision function
* @return AUC the AUC value on the testing samples
* @return Accuracy the Accuracy on the testing samples
* @return time the time used for training
* @return err_count the number of the misclassified samples online
*/
void mkl_fifo_plus_m(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_mkl &model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
/**
* @brief KOIL_MKL_RS++
*
* @param id_train the index of the training samples
* @param cnt_train the number of the training samples
* @param id_test the index of the testing samples
* @param cnt_test the number of the testing samples
* @return model multiple kernels for the learned decision function
* @return AUC the AUC value on the testing samples
* @return Accuracy the Accuracy on the testing samples
* @return time the time used for training
* @return err_count the number of the misclassified samples online
*/
void mkl_rs_plus_m(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_mkl &model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
void mkl_fifo_plus_m2(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_mkl &model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
void mkl_rs_plus_m2(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_mkl &model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
void mkl_fifo_plus_m3(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_mkl &model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
void mkl_rs_plus_m3(int* id_train,int cnt_train, int* id_test, int cnt_test, string losstype,
svm_mkl &model,double& AUC,double& Accuracy, double &Precision, double &Recall, double& time, int& err_count);
// helper functions
/**
* @brief KOIL_RS++: update budget
*
* @param xt xt the t-th sample xt
* @param at the weight of xt
* @param max_n the maximun number for the buffer
* @param t the current iteration
* @param model the current decision function f
* @param alpha the weights of SVs, which have the same label with xt
* @param SV the SV, which have the same label with xt
* @param cur_n current number of SVs in the buffer
* @param flag indicate whether xt is put in the buffer or not
* @param ridx the replaced index for xt if xt is put in the buffer
*/
void rs_update_budget(svm_node* xt,double at,int max_n,int t,
svm_model& model,double* &alpha,svm_node** &SV,int& cur_n,bool& flag, int& ridx);
/**
* @brief KOIL_FIFO++: update budget
*
* @param xt xt the t-th sample xt
* @param at the weight of xt
* @param max_n the maximun number for the buffer
* @param model the current decision function f
* @param fidx the index of the first SV in the buffer (FIFO)
* @param alpha the weights of SVs, which have the same label with xt
* @param SV the SV, which have the same label with xt
* @param cur_n current number of SVs in the buffer
* @param flag indicate whether xt is put in the buffer or not
* @param ridx the replaced index for xt if xt is put in the buffer
*/
void fifo_update_budget(svm_node* xt,double at,int max_n,
svm_model& model,int& fidx,double* &alpha,svm_node** &SV,int& cur_n,bool& flag, int& ridx);
/**
* @brief KOIL: update threshold of decision function
*
* @param model the current decision function
*/
void update_b(svm_model& model);
/**
* @brief update the weight for SV
*
* @param xt the t-th sample xt
* @param yt the label of xt
* @param model the current decision function f
* @param regularization a flag to determine whether regularization is used
* @return at return the weight of xt
* @return ploss pairwise loss value
* @param losstype indicate l1 or l2 loss, default = "l1"
*/
void update_kernel_l1(svm_node* xt,double yt, svm_model& model, double& at, double& ploss, bool regularization);
/**
* @brief update the weight for SV
*
* @param xt the t-th sample xt
* @param yt the label of xt
* @param model the current decision function f
* @param regularization a flag to determine whether regularization is used
* @return at return the weight of xt
* @return ploss pairwise loss value
* @param losstype indicate l1 or l2 loss, default = "l1"
*/
void update_kernel_l2(svm_node* xt,double yt, svm_model& model, double& at, double& ploss, bool regularization);
/**
* @brief the calculate the AUC and Accuracy between f and y
*
* @param f 1xn vector, the predicted label by the model
* @param y 1xn vector, the true label
* @param n the number of the label
* @return AUC AUC value
* @return Accuracy Accuracy for the correct prediction
*/
void evaluate_AUC(double* f, double* y, int n,
double& AUC, double& Accuracy, double &Precision, double &Recall);
};
#endif