-
Notifications
You must be signed in to change notification settings - Fork 1
/
fm.cpp
522 lines (422 loc) · 14.1 KB
/
fm.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
#pragma GCC diagnostic ignored "-Wunused-result"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <new>
#include <memory>
#include <random>
#include <stdexcept>
#include <string>
#include <cstring>
#include <vector>
#include <cassert>
#include <numeric>
#include <cmath>
#if defined USEOMP
#include <omp.h>
#endif
#include "fm.h"
#include "timer.h"
namespace fm {
namespace {
using namespace std;
fm_int const kCHUNK_SIZE = 10000000;
fm_int const kMaxLineSize = 100000;
fm_float uniform() {
return rand() / ((double)RAND_MAX + 1.0);
}
fm_float gaussian() {
fm_float u,v, x, y, Q;
do {
do {
u = uniform();
} while (u == 0.0);
v = 1.7156 * (uniform() - 0.5);
x = u - 0.449871;
y = fabs(v) + 0.386595;
Q = x * x + y * (0.19600 * y - 0.25472 * x);
} while (Q >= 0.27597 && (Q > 0.27846 || v * v > -4.0 * u * u * log(u)));
return v / u;
}
double gaussian(double mean, double stdev) {
if(0.0 == stdev) {
return mean;
} else {
return mean + stdev * gaussian();
}
}
inline fm_float wTx(
fm_node *begin,
fm_node *end,
fm_model &model,
fm_float kappa=0,
fm_float eta=0,
fm_float lambda=0,
bool do_update=false) {
fm_float res = 0;
if (do_update) {
// weight w0
fm_float w0_grad = kappa + lambda * model.w0;
model.w0_a += w0_grad * w0_grad;
model.w0 -= eta / sqrt(model.w0_a) * w0_grad;
for (fm_node *node1 = begin; node1 != end; node1++) { // one feature in a sample
fm_int idx1 = node1->idx;
fm_float value1 = node1->value;
if (idx1 >= model.n)
continue;
// weight wi
fm_weight_unit &unit1 = model.weight_map.find(idx1)->second; // weight unit of the feature
fm_float w_grad = kappa * value1 + lambda * unit1.w;
unit1.w_a += w_grad * w_grad;
unit1.w -= eta / sqrt(unit1.w_a) * w_grad;
// latent vector i
for (fm_int f = 0; f < model.k; f++) {
fm_float v_grad = 0;
for (fm_node *node2 = begin; node2 != end; node2++) {
fm_int idx2 = node2->idx;
fm_float value2 = node2->value;
fm_weight_unit &unit2 = model.weight_map.find(idx2)->second;
v_grad += unit2.v.at(f) * value2;
}
v_grad = value1 * v_grad - unit1.v.at(f) * value1 * value1;
v_grad = (kappa * v_grad + lambda * unit1.v.at(f));
unit1.v_a.at(f) += v_grad * v_grad;
unit1.v.at(f) -= eta / sqrt(unit1.v_a.at(f)) * v_grad;
}
}
} else {
// weight w0
res += model.w0;
// weight wi
for (fm_node *node = begin; node != end; node++) {
fm_float idx = node->idx;
fm_float value = node->value;
fm_weight_unit &unit = model.weight_map.find(idx)->second;
res += unit.w * value;
}
// latent vector i
fm_float latent_res = 0;
for (fm_int f = 0; f < model.k; f++) {
fm_float sum_square = 0;
fm_float square_sum = 0;
for (fm_node *node = begin; node != end; node++) {
fm_float idx = node->idx;
fm_float value = node->value;
fm_weight_unit &unit = model.weight_map.find(idx)->second;
sum_square += unit.v.at(f) * value;
square_sum += unit.v.at(f) * unit.v.at(f) * value * value;
}
latent_res += (sum_square * sum_square - square_sum);
}
res += (latent_res * 0.5);
}
return res;
}
fm_model init_model(fm_int n, fm_parameter param) {
fm_model model;
model.n = n;
model.k = param.k;
model.weight_map.clear();
model.w0 = 0;
model.w0_a = 1;
default_random_engine generator;
uniform_real_distribution<fm_float> distribution(0.0, 1.0);
for (fm_int i = 0; i < model.n; i++) {
fm_weight_unit unit;
unit.w = 0;
unit.w_a = 1;
for (fm_int j = 0; j < model.k; j++) {
fm_float v_temp = gaussian(0, param.stdev);
unit.v.push_back(v_temp);
unit.v_a.push_back(1);
}
model.weight_map.insert(make_pair(i, unit));
}
return model;
}
struct disk_problem_meta {
fm_int n = 0;
fm_int l = 0;
fm_int num_blocks = 0;
fm_long B_pos = 0;
uint64_t hash1;
uint64_t hash2;
};
struct problem_on_disk {
disk_problem_meta meta;
vector<fm_float> Y;
vector<fm_long> P;
vector<fm_node> X;
vector<fm_long> B;
problem_on_disk(string path) {
f.open(path, ios::in | ios::binary);
if (f.good()) {
f.read(reinterpret_cast<char*>(&meta), sizeof(disk_problem_meta));
f.seekg(meta.B_pos);
B.resize(meta.num_blocks);
f.read(reinterpret_cast<char*>(B.data()), sizeof(fm_long) * meta.num_blocks);
}
}
int load_block(int block_index) {
if(block_index >= meta.num_blocks)
assert(false);
f.seekg(B[block_index]);
fm_int l;
f.read(reinterpret_cast<char*>(&l), sizeof(fm_int));
Y.resize(l);
f.read(reinterpret_cast<char*>(Y.data()), sizeof(fm_float) * l);
P.resize(l+1);
f.read(reinterpret_cast<char*>(P.data()), sizeof(fm_long) * (l+1));
X.resize(P[l]);
f.read(reinterpret_cast<char*>(X.data()), sizeof(fm_node) * P[l]);
return l;
}
bool is_empty() {
return meta.l == 0;
}
private:
ifstream f;
};
uint64_t hashfile(string txt_path, bool one_block=false) {
ifstream f(txt_path, ios::ate | ios::binary);
if (f.bad())
return 0;
fm_long end = (fm_long) f.tellg();
f.seekg(0, ios::beg);
assert(static_cast<int>(f.tellg()) == 0);
uint64_t magic = 90359;
for (fm_long pos = 0; pos < end; ) {
fm_long next_pos = min(pos + kCHUNK_SIZE, end);
fm_long size = next_pos - pos;
vector<char> buffer(kCHUNK_SIZE);
f.read(buffer.data(), size);
fm_int i = 0;
while (i < size - 8) {
uint64_t x = *reinterpret_cast<uint64_t*>(buffer.data() + i);
magic = ( (magic + x) * (magic + x + 1) >> 1) + x;
i += 8;
}
for (; i < size; i++) {
char x = buffer[i];
magic = ( (magic + x) * (magic + x + 1) >> 1) + x;
}
pos = next_pos;
if (one_block)
break;
}
return magic;
}
void txt2bin(string txt_path, string bin_path) {
FILE *f_txt = fopen(txt_path.c_str(), "r");
if (f_txt == nullptr)
throw;
ofstream f_bin(bin_path, ios::out | ios::binary);
vector<char> line(kMaxLineSize);
fm_long p = 0;
disk_problem_meta meta;
vector<fm_float> Y;
vector<fm_long> P(1, 0);
vector<fm_node> X;
vector<fm_long> B;
auto write_chunk = [&] () {
B.push_back(f_bin.tellp());
fm_int l = Y.size();
fm_long nnz = P[l];
meta.l += l;
f_bin.write(reinterpret_cast<char*>(&l), sizeof(fm_int));
f_bin.write(reinterpret_cast<char*>(Y.data()), sizeof(fm_float) * l);
f_bin.write(reinterpret_cast<char*>(P.data()), sizeof(fm_long) * (l+1));
f_bin.write(reinterpret_cast<char*>(X.data()), sizeof(fm_node) * nnz);
Y.clear();
P.assign(1, 0);
X.clear();
p = 0;
meta.num_blocks++;
};
f_bin.write(reinterpret_cast<char*>(&meta), sizeof(disk_problem_meta));
while (fgets(line.data(), kMaxLineSize, f_txt)) {
char *y_char = strtok(line.data(), " \t");
fm_float y = (atoi(y_char)>0)? 1.0f : -1.0f;
for (; ; p++) {
char *idx_char = strtok(nullptr,":");
char *value_char = strtok(nullptr," \t");
if(idx_char == nullptr || *idx_char == '\n')
break;
fm_node N;
N.idx = atoi(idx_char);
N.value = atof(value_char);
X.push_back(N);
meta.n = max(meta.n, N.idx+1);
}
Y.push_back(y);
P.push_back(p);
if (X.size() > (size_t)kCHUNK_SIZE)
write_chunk();
}
write_chunk();
write_chunk(); // write a dummy empty chunk in order to know where the EOF is
assert(meta.num_blocks == (fm_int)B.size());
meta.B_pos = f_bin.tellp();
f_bin.write(reinterpret_cast<char*>(B.data()), sizeof(fm_long) * B.size());
fclose(f_txt);
meta.hash1 = hashfile(txt_path, true);
meta.hash2 = hashfile(txt_path, false);
f_bin.seekp(0, ios::beg);
f_bin.write(reinterpret_cast<char*>(&meta), sizeof(disk_problem_meta));
}
bool check_same_txt_bin(string txt_path, string bin_path) {
ifstream f_bin(bin_path, ios::binary | ios::ate);
if (f_bin.tellg() < (fm_long)sizeof(disk_problem_meta))
return false;
disk_problem_meta meta;
f_bin.seekg(0, ios::beg);
f_bin.read(reinterpret_cast<char*>(&meta), sizeof(disk_problem_meta));
if (meta.hash1 != hashfile(txt_path, true))
return false;
if (meta.hash2 != hashfile(txt_path, false))
return false;
return true;
}
} // unnamed namespace
fm_model::~fm_model() {
}
void fm_read_problem_to_disk(string txt_path, string bin_path) {
Timer timer;
cout << "First check if the text file has already been converted to binary format " << flush;
bool same_file = check_same_txt_bin(txt_path, bin_path);
cout << "(" << fixed << setprecision(1) << timer.toc() << " seconds)" << endl;
if(same_file) {
cout << "Binary file found. Skip converting text to binary" << endl;
} else {
cout << "Binary file NOT found. Convert text file to binary file " << flush;
txt2bin(txt_path, bin_path);
cout << "(" << fixed << setprecision(1) << timer.toc() << " seconds)" << endl;
}
}
fm_model fm_train_on_disk(string tr_path, string va_path, fm_parameter param, string model_path) {
problem_on_disk tr(tr_path);
problem_on_disk va(va_path);
fm_model model = init_model(tr.meta.n, param);
bool auto_stop = param.auto_stop && !va_path.empty();
fm_double best_va_loss = numeric_limits<fm_double>::max();
cout.width(4);
cout << "iter";
cout.width(13);
cout << "tr_logloss";
if (!va_path.empty()) {
cout.width(13);
cout << "va_logloss";
}
cout.width(13);
cout << "tr_time";
cout << endl;
Timer timer;
auto one_epoch = [&] (problem_on_disk &prob, bool do_update) {
fm_double loss = 0;
vector<fm_int> outer_order(prob.meta.num_blocks);
iota(outer_order.begin(), outer_order.end(), 0);
random_shuffle(outer_order.begin(), outer_order.end());
for (auto blk : outer_order) {
fm_int l = prob.load_block(blk);
vector<fm_int> inner_order(l);
iota(inner_order.begin(), inner_order.end(), 0);
random_shuffle(inner_order.begin(), inner_order.end());
#if defined USEOMP
#pragma omp parallel for schedule(static) reduction(+: loss)
#endif
for (fm_int ii = 0; ii < l; ii++) {
fm_int i = inner_order[ii];
fm_float y = prob.Y[i];
fm_node *begin = &prob.X[prob.P[i]];
fm_node *end = &prob.X[prob.P[i+1]];
fm_double t = wTx(begin, end, model);
fm_double expnyt = exp(-y*t);
loss += log1p(expnyt);
if(do_update) {
fm_float kappa = -y*expnyt/(1+expnyt);
wTx(begin, end, model, kappa, param.eta, param.lambda, true);
}
}
}
return loss / prob.meta.l;
};
for (fm_int iter = 1; iter <= param.nr_iters; iter++) {
timer.tic();
fm_double tr_loss = one_epoch(tr, true);
timer.toc();
cout.width(4);
cout << iter;
cout.width(13);
cout << fixed << setprecision(5) << tr_loss;
if (!va.is_empty()) {
fm_double va_loss = one_epoch(va, false);
cout.width(13);
cout << fixed << setprecision(5) << va_loss;
if (auto_stop) {
if(va_loss > best_va_loss) {
cout << endl << "Auto-stop. Use model at " << iter-1 << "th iteration." << endl;
break;
} else {
best_va_loss = va_loss;
}
}
}
cout.width(13);
cout << fixed << setprecision(1) << timer.get() << endl;
fm_save_model(model, model_path + "_" + std::to_string(iter));
}
return model;
}
fm_int fm_save_model(fm_model &model, string path) {
ofstream f_out(path);
if (!f_out.is_open())
return 1;
f_out << "n " << model.n << "\n";
f_out << "k " << model.k << "\n";
f_out << "bias " << model.w0 << "\n";
for (fm_int i = 0; i < model.n; i++) {
f_out << "w " << i << " " << model.weight_map.find(i)->second.w << "\n";
}
for (fm_int i = 0; i < model.n; i++) {
f_out << "v " << i;
fm_weight_unit &unit = model.weight_map.find(i)->second;
for (fm_int f = 0; f < model.k; f++) {
f_out << " " << unit.v.at(f);
}
f_out << "\n";
}
return 0;
}
fm_model fm_load_model(string path) {
ifstream f_in(path); // need check
string dummy;
fm_model model;
f_in >> dummy >> model.n
>> dummy >> model.k
>> dummy >> model.w0;
for (fm_int i = 0; i < model.n; i++) {
fm_weight_unit unit;
f_in >> dummy;
f_in >> dummy;
f_in >> unit.w;
model.weight_map.insert({i, unit});
}
for (fm_int i = 0; i < model.n; i++) {
fm_weight_unit &unit = model.weight_map.find(i)->second;
f_in >> dummy;
f_in >> dummy;
for (fm_int j = 0; j < model.k; j++) {
fm_float val = 0;
f_in >> val;
unit.v.push_back(val);
}
}
return model;
}
fm_float fm_predict(fm_node *begin, fm_node *end, fm_model &model) {
fm_float t = wTx(begin, end, model);
return 1 / (1 + exp(-t));
}
} // namespace fm