forked from alexeygrigorev/libffm-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffm.h
94 lines (64 loc) · 1.99 KB
/
ffm.h
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
#ifndef _LIBFFM_H
#define _LIBFFM_H
#include <string>
extern "C" {
namespace ffm {
using namespace std;
typedef float ffm_float;
typedef double ffm_double;
typedef int ffm_int;
typedef long long ffm_long;
struct ffm_node {
ffm_int f; // field index
ffm_int j; // feature index
ffm_float v; // value
};
struct ffm_model {
ffm_int n; // number of features
ffm_int m; // number of fields
ffm_int k; // number of latent factors
ffm_float *W = nullptr;
bool normalization;
~ffm_model();
};
struct ffm_parameter {
ffm_float eta = 0.2; // learning rate
ffm_float lambda = 0.00002; // regularization parameter
ffm_int nr_iters = 15;
ffm_int k = 4; // number of latent factors
bool normalization = true;
bool auto_stop = false;
};
void ffm_read_problem_to_disk(string txt_path, string bin_path);
void ffm_save_model(ffm_model &model, string path);
ffm_model ffm_load_model(string path);
ffm_model ffm_train_on_disk(string Tr_path, string Va_path, ffm_parameter param);
ffm_float ffm_predict(ffm_node *begin, ffm_node *end, ffm_model &model);
// new structs and methods for the wrapper
struct ffm_line {
ffm_node* data;
ffm_float label;
ffm_int size;
};
struct ffm_problem {
ffm_int size = 0;
ffm_long num_nodes = 0;
ffm_node* data;
ffm_long* pos;
ffm_float* labels;
ffm_float* scales;
ffm_int n = 0;
ffm_int m = 0;
};
ffm_model ffm_load_model_c_string(char *path);
void ffm_save_model_c_string(ffm_model &model, char *path);
ffm_problem ffm_convert_data(ffm_line *data, ffm_int num_lines);
void ffm_cleanup_data(ffm_problem *p);
ffm_model ffm_init_model(ffm_problem &data, ffm_parameter params);
ffm_float ffm_train_iteration(ffm_problem &data, ffm_model &model, ffm_parameter params);
ffm_float ffm_predict_array(ffm_node *nodes, int len, ffm_model &model);
ffm_float* ffm_predict_batch(ffm_problem &data, ffm_model &model);
void ffm_cleanup_prediction(ffm_float* f);
} // namespace ffm
#endif // _LIBFFM_H
}