/
mf.cpp
411 lines (345 loc) · 11 KB
/
mf.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
/*
This file implements PMF in C++. In this implementation, batch gradient descent is used to minimize loss function:
F = acc_error + doc_reg + word_reg
= sum_{(doc_id, word_id, cnt):train_vec} (cnt - D(doc_id)*W(word_id))^2 + 0.5*lambda*(|D|^2 + |W|^2)
The program should take two data files i.e. training data and testing data.
The input data is in the format of
n1 w_a(1,1):cnt_a(1,1) w_a(1,2):cnt_a(1,2) ... w_a(1,n1):cnt_a(1,n1)
n2 w_a(2,1):cnt_a(2,1) w_a(2,2):cnt_a(2,2) ... w_a(2,n2):cnt_a(2,n2)
...
nm w_a(m,1):cnt_a(m,1) w_a(m,2):cnt_a(m,2) ... w_a(m,nm):cnt_a(m,nm)
The program will print to the screen the training loss and accuracy on testing data in each epoch and batch.
Author: Xi Yi (yixi1992@gmail.com)
Date: 11/05/2015
*/
#include <stdio.h> // fscanf
#include <stdlib.h> // std::rand, std::srand
#include <string.h> // strlen, strcmp
#include <algorithm> // std::random_shuffle, min
#include <vector> // std::vector
#include <stdexcept> // throw
#include <sstream> // ostringstream
#include <math.h> //sqrt
using namespace std;
#define RANDOM "random"
/*
Triplet structure where each triplet is {doc_id, word_id, cnt}
*/
struct Triplet{
int doc_id, word_id;
double cnt;
Triplet(int dd, int ww, double cc):doc_id(dd), word_id(ww), cnt(cc){}
};
/*
Matrix structure.
*/
class Mat{
private:
int n, m;
double **arr;
/*
Generator who generates initial values for Mat elements.
*/
double ValueGenerator(string default_val){
double val = 0;
if (default_val==RANDOM){ // if default_val is random, draw a random number.
val = 0.1 * ((double)rand()) / RAND_MAX;
} else { // otherwise, default_val is a double value.
val = std::strtod(default_val.c_str(), NULL);
}
return val;
}
/*
Convert double to string.
*/
string ToString(double d){
ostringstream strs;
strs << d;
return strs.str();
}
/*
Until c++11, constructor cannot call another constructor so I created this InitMat function to do initialization.
*/
void InitMat(int nn, int mm, string default_val=RANDOM) {
n = nn; m = mm;
arr = new double*[n];
for (int i=0; i<n; i++) {
arr[i] = new double[m];
for (int j=0; j<m; j++)
arr[i][j] = ValueGenerator(default_val);
}
}
public:
/*
Constructor for constructing a nn*mm matrix with default_val (double).
*/
Mat(int nn, int mm, double default_val){
InitMat(nn, mm, ToString(default_val));
}
/*
Constructor for constructing a nn*mm matrix with default_val.
default_val: a string indicating default values of the matrix which is either RANDOM or any double value, e.g. "0.5"
*/
Mat(int nn=0, int mm=0, string default_val = RANDOM) {
InitMat(nn, mm, default_val);
}
/*
Copy constructor. Deep copy.
*/
Mat(const Mat &cp){
InitMat(cp.get_n(),cp.get_m());
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
arr[i][j] = cp.get(i,j);
}
int get_n() const {
return n;
}
int get_m() const {
return m;
}
double get(int i, int j) const {
if (i>=n || i<0 || j<0 || j>=m) {
throw invalid_argument("Index out of Bound when using get(i,j) for Mat object.");
}
return arr[i][j];
}
/*
This class (Proxy) is to enable client use two dimensional subscripts. i.e. Mat a=Mat(1,1,0); a[0][0]+=2;
*/
class Proxy {
public:
Proxy(double* _array) : _array(_array) { }
double & operator[](int index) {
return _array[index];
}
private:
double* _array;
};
Proxy & operator[](int i){
if (i>=n || i<0) {
throw invalid_argument("Index out of Bound when using [] for Mat object.");
}
return *(new Proxy(arr[i]));
}
/*
Add two matrix elementwise. Throw exception if their dimensions don't match.
*/
Mat operator +(const Mat& rhs){
if (rhs.get_n()!=n || rhs.get_m()!=m){
throw invalid_argument("Matrix dimensions are not consistent when trying '+' two matrices");
return *this;
}
Mat res = Mat(*this);
for (int i=0; i<res.get_n(); i++){
for (int j=0; j<res.get_m(); j++)
res[i][j] += rhs.get(i,j);
}
return res;
}
/*
Substraction. Throw exception if their dimensions don't match.
*/
Mat operator -(const Mat& rhs){
if (rhs.get_n()!=n || rhs.get_m()!=m){
throw invalid_argument("Matrix dimensions are not consistent when trying '+' two matrices");
return Mat();
}
Mat res = Mat(*this);
for (int i=0; i<res.get_n(); i++){
for (int j=0; j<res.get_m(); j++)
res[i][j] -= rhs.get(i, j);
}
return res;
}
/*
Multiply by a number.
*/
Mat operator *(const double& mult){
Mat res = Mat(*this);
for (int i=0; i<res.get_n(); i++){
for (int j=0; j<res.get_m(); j++)
res[i][j] *= mult;
}
return res;
}
/*
Return a matrix which is the elementwise square.
*/
Mat Sqr(){
Mat res = Mat(*this);
for (int i=0; i<res.get_n(); i++)
for (int j=0; j<res.get_m(); j++)
res[i][j] = res[i][j]*res[i][j];
return res;
}
/*
Return the sum of all elements in the matrix.
*/
double Sum(){
double s;
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
s += arr[i][j];
return s;
}
};
vector<Triplet> train_vec, probe_vec;
vector<double> err_train, err_valid;
/*
Load the data from data_file and save in data_vec as triplet. Indices start from 0 (NOT from 1).
data_file: input file name
data_vec: vector of Triplets read from the input file
*/
void load_data(string data_file, vector<Triplet> &data_vec, int &num_d, int &num_w){
FILE *pFile;
pFile = fopen(data_file.c_str(), "r");
int doc_id = 0, n;
while (fscanf(pFile, "%d", &n)>0){
for (int i=0; i<n; i++){
int word_id;
double cnt;
fscanf(pFile, "%d:%lf", &word_id, &cnt);
word_id--;
if (word_id + 1 > num_w) num_w = word_id + 1;
data_vec.push_back(Triplet(doc_id, word_id, cnt));
}
doc_id ++;
}
num_d = doc_id;
fclose(pFile);
}
/*
Sum up the 'cnt' value in a vector of Triplet.
data_vec: the vector of Triplets to be summed up.
*/
double sum_cnt(vector<Triplet> &data_vec){
double sum = 0;
for (int i=0; i<data_vec.size(); i++){
sum += data_vec[i].cnt;
}
return sum;
}
/*
Return the square of x.
*/
template <typename Type>
Type sqr(Type x)
{
return x * x;
}
/*
Objective function value calculator:
Takes Doc feature matrix and Word feature matrix, as well as ground truth counts,
return objective function value and a vector of error value for each case.
The objective function is
F = acc_error + doc_reg + word_reg
= sum_{(doc_id, word_id, cnt):train_vec} (cnt - D(doc_id)*W(word_id))^2 + 0.5*lambda*(|D|^2 + |W|^2)
*/
double CalcObj(const Mat &D, const Mat &W, const vector<Triplet> &train_vec, const int &begin_idx, const int &end_idx, const double &lambda, const double &mean_cnt, Mat &error, Mat &pred_out){
double acc_error = 0.0, doc_reg = 0.0, word_reg = 0.0;
error = Mat(end_idx-begin_idx+1, 1, 0); pred_out = Mat(end_idx-begin_idx+1, 1, 0);
for (int i = begin_idx; i <= end_idx; i++){
int doc_id = train_vec[i].doc_id, word_id = train_vec[i].word_id;
double cnt = train_vec[i].cnt;
// calculate the prediction: inner product of D(doc_id), W(word_id)
double predict = 0.0;
for (int j=0; j < D.get_m(); j++){
double dd = D.get(doc_id, j), ww = W.get(word_id, j);
predict += (dd*ww);
doc_reg += sqr(dd);
word_reg += sqr(ww);
}
pred_out[i-begin_idx][0] = predict + mean_cnt;
error[i - begin_idx ][0] = pred_out[i-begin_idx][0] - train_vec[i].cnt;
// calculate the error between predict and ground truth cnt
acc_error += sqr(pred_out[i-begin_idx][0] - train_vec[i].cnt);
}
// Objective function value
double F = acc_error + 0.5*lambda*(doc_reg + word_reg);
return F;
}
/*
Parse arguments passed to main function, return success if train_file and probe_file are found.
*/
bool ParseMainArgs(int argc, char **argv, string &train_file, string &probe_file){
if (argc<3){
printf("Please provide train and probe file name e.g. mf train.txt probe\n");
return false;
}
train_file = string(argv[1]);
probe_file = string(argv[2]);
return true;
}
/*
main
*/
int main(int argc, char **argv){
string train_file = "";
string probe_file = "";
if (!ParseMainArgs(argc, argv, train_file, probe_file)) return 0;
double epsilon = 50.0; // Learning rate
double lambda = 0.01; // Regularization parameter
double momentum = 0.8;
int epoch = 0;
int maxepoch = 50;
int num_d = 1; // Number of docs
int num_w = 2; // Number of words
int num_feat = 1; // Rank 10 decomposition
int tmp_num_d, tmp_num_w;
load_data(train_file, train_vec, num_d, num_w); // Triplets: {doc_id, word_id, cnt}
load_data(probe_file, probe_vec, tmp_num_d, tmp_num_w);
int pairs_tr = train_vec.size(); // training data
int pairs_pr = probe_vec.size(); // validation data
double mean_cnt = sum_cnt(train_vec)/pairs_tr;
int numbatches = 1; // Number of batches
int N = (pairs_tr-1)/numbatches+1; // number training triplets per batch
Mat D = Mat(num_d, num_feat); // Doc feature vectors
Mat W = Mat(num_w, num_feat); // Word feature vecators
Mat D_inc = Mat(num_d, num_feat, 0);
Mat W_inc = Mat(num_w, num_feat, 0);
Mat error, pred_out;
double F = 0.0;
for (; epoch < maxepoch; epoch++){
// Random permute training data.
std::random_shuffle ( train_vec.begin(), train_vec.end() );
for (int batch = 1; batch <= numbatches; batch++ ){
int begin_idx = (batch-1)*N, end_idx = min(batch*N-1, pairs_tr-1), N = end_idx - begin_idx +1;
//%%%%%%%%%%%%%% Compute Predictions %%%%%%%%%%%%%%%%%
F = CalcObj(D, W, train_vec, begin_idx, end_idx, lambda, mean_cnt, error, pred_out);
//%%%%%%%%%%%%%% Compute Gradients %%%%%%%%%%%%%%%%%%%
Mat Ix_D = Mat(N, num_feat, 0), Ix_W = Mat(N, num_feat, 0);
for (int i = begin_idx; i <= end_idx; i++){
int doc_id = train_vec[i].doc_id, word_id = train_vec[i].word_id;
for (int j=0; j < num_feat; j++){
double dd = D[doc_id][j], ww = W[word_id][j];
Ix_D[i - begin_idx][j] = error[i-begin_idx][0] *2* ww + lambda*dd;
Ix_W[i - begin_idx][j] = error[i-begin_idx][0] *2* dd + lambda*ww;
}
}
Mat d_D = Mat(num_d, num_feat, 0);
Mat d_W = Mat(num_w, num_feat, 0);
for (int i = begin_idx; i <= end_idx; i++){
int doc_id = train_vec[i].doc_id, word_id = train_vec[i].word_id;
for (int j=0; j<num_feat; j++){
d_D[doc_id][j] += Ix_D[i-begin_idx][j];
d_W[word_id][j] += Ix_W[i-begin_idx][j];
}
}
//%%%% Update doc and word features %%%%%%%%%%%
D_inc = D_inc*momentum + d_D*(epsilon/N);
D = D - D_inc;
W_inc = W_inc*momentum + d_W*(epsilon/N);
W = W - W_inc;
//%%%%%%%%%%%%%% Compute Predictions after Parameter Updates %%%%%%%%%%%%%%%%%
F = CalcObj(D, W, train_vec, begin_idx, end_idx, lambda, mean_cnt, error, pred_out);
err_train.push_back(sqrt(F/N));
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%% Compute predictions on the validation set %%%%%%%%%%%%%%%%%%%%%%
F = CalcObj(D, W, probe_vec, 0, pairs_pr-1, lambda, mean_cnt, error, pred_out);
err_valid.push_back(sqrt(error.Sqr().Sum())/pairs_pr);
printf("epoch %4i batch %4i Training RMSE %6.4f Test RMSE %6.4f \n", epoch, batch, err_train[epoch], err_valid[epoch]);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
}
}