-
Notifications
You must be signed in to change notification settings - Fork 0
/
testCup.cpp
648 lines (551 loc) · 18.1 KB
/
testCup.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <functional>
#include <string>
#include <time.h>
#include <algorithm>
#include <fstream>
#include <thread>
#include <mutex>
#include <sstream>
#include <algorithm>
#include <random>
#include "MLP.h"
#include "Utilities.h"
using namespace std;
#define CUP_ROOT "/Users/phamgiang/Study/Master/ThirdSemeter/ML/MLProject/CUP/"
#define MODEL "/Users/phamgiang/Study/Master/ThirdSemeter/ML/MLProject/model-cup/final.model"
#define TRFILE "train.csv"
#define INTSFILE "ints.csv"
#define ALLTRFILE "ML-CUP20-TR.csv"
#define REALTEST "ML-CUP20-TS.csv"
// #define SAVE_SPLIT_FILE
vector<float> scaler {25.978398f, 78.83648f, -41.864859, -4.064733}; //vector contains min_out1, max_out1, min_out2, max_out2 for normalizaion reason
// #define TEST_UNIT
/**
* Function to read both the training and testing data
* + alltridx: list of training data indices
* + alltrdata: list of traning data
* + alltrtarget: list of traning targets
* */
void read_trdata(string trname, vector<int> &alltridx, vector<vector<float> > &alltrdata, vector<vector<float> > &alltrtarget)
{
//clear all the vectors first
alltridx.clear();
alltrdata.clear();
alltrtarget.clear();
//read training file
ifstream trfile(trname);
if(trfile.is_open())
{
string line;
//get rid of 7 first lines or the original file
if (trname == string(CUP_ROOT) + string(ALLTRFILE) ||
trname == string(CUP_ROOT) + string(REALTEST))
{
for(int i = 0; i < 7; i++)
{
getline(trfile, line);
}
}
while(getline(trfile, line))
{
stringstream ss;
ss << line;
int temidx;
ss >> temidx;
char c;
ss >> c;
vector<float> temin;
for(int i = 0; i < 10; i++) //get 10 input
{
float tematt;
ss >> tematt;
ss >> c;
// cout << tematt;
temin.push_back(tematt);
}
vector<float> temout;
for(int i = 0; i < 2; i++)
{
float tematt;
ss >> tematt;
// if (tematt < 0) {tematt = tematt*(-1);}
ss >> c;
temout.push_back(tematt);
}
alltridx.push_back(temidx);
alltrdata.push_back(temin);
alltrtarget.push_back(temout);
}
}
else
{
cerr << "Cannot open file " << trname << endl;
}
#ifdef TEST_UNIT
vector<int> tidx{34, 22, 53, 35, 89, (int)alltrdata.size()-1};
for (int j = 0; j < tidx.size(); j++)
{
cout << alltridx[tidx[j]] << " ";
for(int i = 0; i < 10; i++)
{
cout << alltrdata[tidx[j]][i] << " ";
}
for(int i = 0; i < 2; i++)
{
cout << alltrtarget[tidx[j]][i] << " ";
}
cout << endl;
}
#endif
}
/**
* Function to read the test file
* - tsidx: vector will contain the test index,
* - tsdata: vector will contain the test data
* */
void read_tsdata(vector<int> &tsidx, vector<vector<float> > &tsdata, string filename)
{
//clear all the vector first
tsidx.clear();
tsdata.clear();
string tsname= filename;
//read test file
ifstream tsfile(tsname);
if(tsfile.is_open())
{
string line;
//get rid of 7 first lines from the original data
// if (tsname == string(CUP_ROOT) + string(ALLTRFILE) ||
// tsname == string(CUP_ROOT) + string(REALTEST))
{
for(int i = 0; i < 7; i++)
{
getline(tsfile, line);
}
}
while(getline(tsfile, line))
{
stringstream ss;
ss << line;
int temidx;
char c;
ss >> temidx;
ss >> c;
vector<float> temin;
for(int i = 0; i < 10; i++) //get 10 input
{
float tematt;
ss >> tematt;
ss >> c;
temin.push_back(tematt);
}
tsidx.push_back(temidx);
tsdata.push_back(temin);
}
}
else
{
cerr << "Cannot open file " << tsname << endl;
}
#ifdef TEST_UNIT
vector<int> tidx{34, 22, 53, 35, 89, (int)tsdata.size()-1};
for (int j = 0; j < tidx.size(); j++)
{
cout << tsidx[tidx[j]] << " ";
for(int i = 0; i < 10; i++)
{
cout << tsdata[tidx[j]][i] << " ";
}
cout << endl;
}
#endif
}
/**
* Normalize the target of training data to range [0, 1]
* using (x - min)/(max - min)
* -alltrtarget: vector of training target
* */
void preprocess(vector<vector<float> > &alltrtarget)
{
for(int i = 0; i < alltrtarget.size(); i++)
{
alltrtarget[i][0] = (alltrtarget[i][0] - scaler[0])/(scaler[1] - scaler[0]);
alltrtarget[i][1] = (alltrtarget[i][1] - scaler[2])/(scaler[3] - scaler[2]);
}
}
/**
* Function to split training set into internal test set + training set (will be used in kfold),
* run only once to save sets to file, next time load the saved files
* - ratio: ratio of the internal test set, in range [0,1].
* - alltridx: input vector contains all the training idx
* - alltrdata: input vector contains all the training data
* - alltrtarget: input vector contains all the training target
*
* - tridx: output vector contains the training idx
* - trdata: output vector contains the training data
* - trtarget: ouput vector contains the training target
*
* - intsidx: output vector contains the internal test idx
* - intsdata: output vector contains the internal test data
* - intstarget: output vector contains the internal test target
* */
void split_sets(float ratio, vector<int> alltridx, vector<vector<float> > alltrdata, vector<vector<float> > alltrtarget,
vector<int>& tridx, vector<vector<float> >& trdata, vector<vector<float> >& trtarget,
vector<int>& intsidx, vector<vector<float> >& intsdata, vector<vector<float> >& intstarget)
{
//clear all the output vector first
trdata.clear(); trtarget.clear();
intsdata.clear(); intstarget.clear();
//shuffle all the training data
std::random_device rd;
std::mt19937 g(rd());
shuffle(alltridx.begin(), alltridx.end(), g);
//get the number of internal test set
int intssize = (int)alltridx.size()*ratio;
#ifdef SAVE_SPLIT_FILE
ofstream ftrain (string(CUP_ROOT) + string(TRFILE));
ofstream fintest (string(CUP_ROOT) + string(INTSFILE));
#endif
for(int i = 0; i < alltridx.size(); i++)
{
if (i < intssize) //put this sample into the internal test set
{
stringstream testss;
testss.clear();
intsidx.push_back(alltridx[i]);
testss << alltridx[i] << ",";
intsdata.push_back(alltrdata[alltridx[i] - 1]); // because index of the file start from 1
for(int j = 0; j < 10; j++)
{
testss << alltrdata[alltridx[i] - 1][j] << ",";
}
intstarget.push_back(alltrtarget[alltridx[i] - 1]); // becaue index of the file start from 1
for (int j = 0; j < 2; j++)
{
testss << alltrtarget[alltridx[i]-1][j];
if(j == 0)
{
testss << ",";
}
}
testss << endl;
#ifdef SAVE_SPLIT_FILE
fintest << testss.str();
#endif
}
else // put the remaining samples into the training set
{
stringstream trainss;
tridx.push_back(alltridx[i]);
trainss << alltridx[i] << ",";
trdata.push_back(alltrdata[alltridx[i] - 1]); //because index of the file start from 1
for(int j = 0; j < 10; j++)
{
trainss << alltrdata[alltridx[i] -1][j] << ",";
}
trtarget.push_back(alltrtarget[alltridx[i] - 1]); //because index of the file start from 1
for(int j = 0; j < 2; j++)
{
trainss << alltrtarget[alltridx[i]-1][j];
if (j == 0)
{
trainss << ",";
}
}
trainss << endl;
#ifdef SAVE_SPLIT_FILE
ftrain << trainss.str();
#endif
}
}
#ifdef SAVE_SPLIT_FILE
ftrain.close();
fintest.close();
#endif
#ifdef TEST_UNIT
for(int i = 0; i < tridx.size(); i++)
{
cout << tridx[i] << " ";
for (int j = 0; j < 10; j++)
{
cout << trdata[i][j] << " ";
}
for(int j = 0; j < 2; j++)
{
cout << trtarget[i][j] << " ";
}
cout << endl;
}
for(int i = 0; i < intsidx.size(); i++)
{
cout << intsidx[i] << " ";
for (int j = 0; j < 10; j++)
{
cout << intsdata[i][j] << " ";
}
for(int j = 0; j < 2; j++)
{
cout << intstarget[i][j] << " ";
}
cout << endl;
}
#endif
}
/**
* Function to find the normalizing factor for the training data (min and max of each attribute)
* - normalizer: the result found
* - data: data
* */
void find_normalizer(vector<vector<float> >& normalizer, vector<vector<float> > data)
{
// normalizer.clear();
//find min and max of each attribute
for(int i = 0; i < data[0].size(); i++)
{
float min = 100.0f;
float max = -100.0f;
// cout << 1 << endl;
vector<float> oneatt {min, max};
normalizer.push_back(oneatt);
}
for(int i = 0; i < data.size(); i++)
{
for(int j = 0; j < data[i].size(); j++)
{
if (data[i][j] < normalizer[j][0])
{
normalizer[j][0] = data[i][j];
}
if(data[i][j] > normalizer[j][1])
{
normalizer[j][1] = data[i][j];
}
}
}
}
/**
* Function to normalize the data
* */
vector<vector<float> > normalize_data(vector<vector<float> > data, vector<vector<float> > normalizer)
{
vector<vector<float> > ndata;
for(int i = 0; i < data.size(); i++)
{
vector<float> onendata;
for(int j = 0; j < data[i].size(); j++)
{
float natt = (data[i][j] - normalizer[j][0])/(normalizer[j][1] - normalizer[j][0]);
onendata.push_back(natt);
}
ndata.push_back(onendata);
}
return ndata;
}
/*
void k_fold_cup()
{
vector<int> tridx;
vector<vector<float> > trdata;
vector<vector<float> > trtarget;
read_trdata(string(CUP_ROOT) + string(TRFILE), tridx, trdata, trtarget);
//preprocessing
preprocess(trtarget);
//create a network
int n_in = 11; // 10 attributes + 1 bias term
int n_out = 2; // 2 outputs
vector<int> hid{20, 10, 4};
MLP* mlp = new MLP(n_in, n_out, hid.size(), hid, "gauss", "sigmoid");
float res = kfold_validation(mlp, 2, 2, trdata, trtarget, 5, 2000, 0.01f, 0.3f, 0.0001f, 0.0001f, scaler);
cout << "K fold res = " << res << endl;
//reset and retrain with all the training data
mlp->reset_mlp();
mlp->train_batch(trdata, trtarget, 0.01f, 2000, 0.3f, 0.0001f, 0.0001f);
//read test data and run the trained model with the test
vector<int> intsidx;
vector<vector<float> > intsdata;
vector<vector<float> > intstarget;
read_trdata(string(CUP_ROOT) + string(INTSFILE), intsidx, intsdata, intstarget);
float intsres = regression_evaluate(mlp, intsdata, intstarget, scaler);
cout << "Internal test res = " << intsres << endl;
delete mlp;
}
*/
/**
* Just a function to get some strange result from strange models
* */
void screen_demo(float eta, float alfa, float lambda)
{
vector<int> tridx;
vector<vector<float> > trdata;
vector<vector<float> > trtarget;
vector<int> intsidx;
vector<vector<float> > intsdata;
vector<vector<float> > intstarget;
read_trdata(string(CUP_ROOT)+string(TRFILE), tridx, trdata, trtarget);
read_trdata(string(CUP_ROOT)+string(INTSFILE), intsidx, intsdata, intstarget);
// normalize all the target to the range [0, 1]
preprocess(trtarget);
preprocess(intstarget);
//create a network
int n_in = 11; // 10 attributes + 1 bias term
int n_out = 2; // 2 outputs
vector<int> hid{16, 8, 4, 2};
MLP* mlp = new MLP(n_in, n_out, hid.size(), hid, "gauss", "linear");
float trmse, tsmse, trmee, tsmee;
int iter = 0;
do
{
iter++;
mlp->train_batch(trdata, trtarget, 0.001f, 1, eta, alfa, lambda);
// mlp->train_stochastic(trdata, trtarget, 0.001f, 1, eta, alfa, lambda);
trmse = mlp->compute_sum_square_multithreads(trdata, trtarget);
tsmse = mlp->compute_sum_square_multithreads(intsdata, intstarget);
trmee = regression_evaluate(mlp, trdata, trtarget, scaler);
tsmee = regression_evaluate(mlp, intsdata, intstarget, scaler);
if (iter%10 == 0)
{
cout << iter << "\t" << trmse << "\t" << tsmse << "\t" << trmee << "\t" << tsmee << endl;
}
} while (iter < 5000);
cout << regression_evaluate(mlp, intsdata, intstarget, scaler);
regression_to_file(mlp, "res.csv", intsdata, scaler);
delete mlp;
}
/**
* Function to do the grid search
* the searching range for each hyper parameters is declare inside the code
* also the model is inside the code
* */
vector<float> grid_search_cup ()
{
//save result to file
ofstream file ("grid_search.csv");
//load the training set
vector<int> tridx;
vector<vector<float> > trdata;
vector<vector<float> > trtarget;
read_trdata(string(CUP_ROOT) + string(TRFILE), tridx, trdata, trtarget);
//preprocessing
preprocess(trtarget);
//create a network
int n_in = 11; // 10 attributes + 1 bias term
int n_out = 2; // 2 outputs
vector<int> hid{16, 12, 12, 8, 4};
MLP* mlp = new MLP(n_in, n_out, hid.size(), hid, "gauss", "linear");
vector<float> etas {0.41f};
vector<float> alfas {0.015};
vector<float> lambdas {9.5e-5};
float besres = 10000.0f;
vector<float> besparas;
for(int e = 0; e < etas.size(); e++)
{
for(int a = 0; a < alfas.size(); a++)
{
for(int l = 0; l < lambdas.size(); l++)
{
float res = kfold_validation(mlp, 2, 2, trdata, trtarget, 5, 5000, 0.0001f, etas[e], alfas[a], lambdas[l], scaler);
stringstream ss;
ss << "e=" <<etas[e] << "-a=" << alfas[a] << "-l=" << lambdas[l] << "\t" << res << endl;
file << ss.str();
cout << ss.str();
if(res < besres)
{
besres = res;
besparas.clear();
besparas.push_back(etas[e]);
besparas.push_back(alfas[a]);
besparas.push_back(lambdas[l]);
besparas.push_back(besres);
}
}
}
}
delete mlp;
return besparas;
}
/**
* Just to separated sets
* */
void run_just_once()
{
vector<int> alltridx;
vector<vector<float> > alltrdata;
vector<vector<float> > alltrtarget;
read_trdata(string(CUP_ROOT)+ string(ALLTRFILE), alltridx, alltrdata, alltrtarget);
vector<int> tridx;
vector<vector<float> > trdata;
vector<vector<float> > trtarget;
vector<int> intsidx;
vector<vector<float> > intsdata;
vector<vector<float> > intstarget;
split_sets(0.2f, alltridx, alltrdata, alltrtarget,
tridx, trdata, trtarget, intsidx, intsdata, intstarget);
}
/**
* Function to train the final model the test the trained model with internal test set
* */
void final_train()
{
//load the training set
vector<int> tridx;
vector<vector<float> > trdata;
vector<vector<float> > trtarget;
read_trdata(string(CUP_ROOT) + string(TRFILE), tridx, trdata, trtarget);
//preprocess the target
preprocess(trtarget);
//create a network
int n_in = 11; // 10 attributes + 1 bias term
int n_out = 2; // 2 outputs
vector<int> hid{16, 12, 8, 8, 4};
MLP* mlp = new MLP(n_in, n_out, hid.size(), hid, "gauss", "linear");
train_with_early_stopping(mlp, 2, 0.2, 0.00001f, trdata, trtarget, 0.16, 0.00097, 0.00011, "tem.model", scaler, "tem.csv");
//test the internal test set to get the final result
vector<int> intsidx;
vector<vector<float> > intsdata;
vector<vector<float> > intstarget;
read_trdata(string(CUP_ROOT)+string(INTSFILE), intsidx, intsdata, intstarget);
//preprocess
preprocess(intstarget);
float res = regression_evaluate(mlp, intsdata, intstarget, scaler);
cout << "Internal test result: " << res << endl;
delete mlp;
}
/**
* Function to load trained model and test with the test set
* - modelname: name of the model
* - testname: name of the test
* - outname: name of the output file
* */
void load_model_and_test(string modelname, string testname, string outname)
{
//load the model
MLP * mlp = new MLP(modelname);
vector<int> intsidx;
vector<vector<float> > intsdata;
vector<int> tridx;
vector<vector<float> > trdata;
vector<vector<float> > trtarget;
read_trdata(string(CUP_ROOT) +string(TRFILE), tridx, trdata, trtarget);
preprocess(trtarget);
float alltrres = regression_evaluate(mlp, trdata, trtarget, scaler);
cout << "All train MEE: " << alltrres << endl;
read_tsdata(intsidx, intsdata, testname);
regression_to_file(mlp, outname, intsdata, scaler);
}
int main(int argc, char** argv)
{
// test_unit();
srand(time(NULL));
//train and then test with internal test set.
// final_train();
// grid_search_cup();
string testfile = "ML-CUP20-TS.csv";
string testname = string (CUP_ROOT) + string(testfile);
string outname = string (CUP_ROOT) + string("noob.csv");
load_model_and_test(MODEL, testname, outname);
return 0;
}