forked from CastellanZhang/alphaFM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fm_predict.cpp
118 lines (104 loc) · 2.81 KB
/
fm_predict.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
#include <iostream>
#include <map>
#include <fstream>
#include "src/Frame/pc_frame.h"
#include "src/FTRL/ftrl_predictor.h"
using namespace std;
struct Option
{
Option() : dim(8), threads_num(1), space_size(2 ^ 28) {}
string model_path, predict_path;
int threads_num, dim, space_size;
};
string predict_help()
{
return string(
"\nusage: cat sample | ./fm_predict [<options>]"
"\n"
"\n"
"options:\n"
"-m <model_path>: set the model path\n"
"-s <model_path>: set space size\n"
"-dim <factor_num>: dim of 2-way interactions\tdefault:8\n"
"-core <threads_num>: set the number of threads\tdefault:1\n"
"-out <predict_path>: set the predict path\n");
}
vector<string> argv_to_args(int argc, char* argv[])
{
vector<string> args;
for(int i = 1; i < argc; ++i)
{
args.push_back(string(argv[i]));
}
return args;
}
Option parse_option(const vector<string>& args)
{
int argc = args.size();
if(0 == argc)
throw invalid_argument("invalid command\n");
Option opt;
for(int i = 0; i < argc; ++i)
{
if(args[i].compare("-m") == 0)
{
if(i == argc - 1)
throw invalid_argument("invalid command\n");
opt.model_path = args[++i];
}
else if(args[i].compare("-dim") == 0)
{
if(i == argc - 1)
throw invalid_argument("invalid command\n");
opt.dim = stoi(args[++i]);
}
else if (args[i].compare("-s") == 0)
{
if (i == argc - 1)
throw invalid_argument("invalid command\n");
opt.space_size = stoi(args[++i]);
}
else if(args[i].compare("-core") == 0)
{
if(i == argc - 1)
throw invalid_argument("invalid command\n");
opt.threads_num = stoi(args[++i]);
}
else if(args[i].compare("-out") == 0)
{
if(i == argc - 1)
throw invalid_argument("invalid command\n");
opt.predict_path = args[++i];
}
else
{
break;
}
}
return opt;
}
int main(int argc, char* argv[])
{
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
Option opt;
try
{
opt = parse_option(argv_to_args(argc, argv));
}
catch(const invalid_argument& e)
{
cout << e.what() << endl;
cout << predict_help() << endl;
return EXIT_FAILURE;
}
ifstream f_model(opt.model_path.c_str());
ofstream f_predict(opt.predict_path.c_str(), ofstream::out);
ftrl_predictor predictor(opt.dim, opt.space_size, f_model, f_predict);
pc_frame frame;
frame.init(predictor, opt.threads_num);
frame.run();
f_model.close();
f_predict.close();
return 0;
}