-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
116 lines (108 loc) · 2.41 KB
/
test.cpp
File metadata and controls
116 lines (108 loc) · 2.41 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
#pragma warning(disable:4996)
#include <iostream>
#include <fstream>
#include<vector>
#include <string>
#include <string.h>
#include <map>
#include"FM.h"
#include "FFM.h"
using namespace std;
vector<string> split(const string& str, const string& delim) {
vector<string> res;
if ("" == str) return res;
char * strs = new char[str.length() + 1];
strcpy(strs, str.c_str());
char * d = new char[delim.length() + 1];
strcpy(d, delim.c_str());
char *p = strtok(strs, d);
while (p) {
string s = p;
res.push_back(s);
p = strtok(NULL, d);
}
return res;
}
double logloss(double p, int label) {
if (p<0.000000001)
p = 0.000000001;
if (label == 1) {
p = -log(p);
}
else {
p = -log(1.0 - p);
}
return p;
}
template <typename Model>
void oneEpoch(bool istrain, string file, Model & mymodel, int & rights, int & count, double& loss);
template<typename Model>
void oneEpoch(bool istrain,string file,Model & mymodel,int & rights,int & count,double& loss)
{
//train
count = 0;
rights = 0;
loss = 0;
string tmp;
ifstream trfile(file);
map<int, double> m;
while (getline(trfile, tmp))
{
count++;
vector<string> fs = split(tmp, " ");
int label = stoi(fs[0]);
for (int i = 1; i < fs.size(); i++)
{
vector<string> indexs = split(fs[i], ":");
int findex = stoi(indexs[0]);
double value = stod(indexs[1]);
if (findex < 128)
m.insert(pair<int, double>(findex, value));
}
double p = mymodel.predict(m);
loss += logloss(p, label);
if(istrain)
mymodel.update(m, label, p);
m.clear();
if ((p > 0.5) == (label == 1))
rights += 1;
}
trfile.close();
}
template<typename Model>
void testfm();
template<typename Model>
void testfm()
{
int epoch = 30;
string trpath = "D:\\xgboost\\demo\\data\\agaricus.txt.train";
string tepath = "D:\\xgboost\\demo\\data\\agaricus.txt.test";
Model mymodel(128);
double loss = 0.0;
int count = 0;
int rights = 0;
try {
for (int epo = 0; epo<epoch; epo++)
{ // train
oneEpoch(true, trpath, mymodel, rights, count, loss);
if (epo % 5 == 3) {
cout << "train-" << epo<<":"<<((double)rights) / count << endl;
}
//test
oneEpoch(false, tepath, mymodel, rights, count, loss);
if (epo % 10 == 2) {
cout << "test-" << epo << ":" << ((double)rights) / count << endl;
}
}
int noop;
cin >> noop;
}
catch (exception& e)
{
cout << e.what();
}
}
int main(int, char *[])
{
testfm<FFM>();
}