-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPureSlim.cpp
277 lines (224 loc) · 6.94 KB
/
PureSlim.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
#include "PureSlim.hpp"
PureSlim::PureSlim(int numItem, int numUser) {
nItem = numItem;
nUser = numUser;
}
void PureSlim::cleanUp() {
wtItem.clear();
wtItem.shrink_to_fit();
}
double PureSlim::ip_tp(const unordered_map<int, int>& reg, const unordered_map<int, int>& tar) {
double ans = 0.;
for (const auto &it : reg) {
if (tar.find(it.first) != tar.end()) {
ans += 1.;
}
}
return ans;
}
void PureSlim::add_hat_tp(const unordered_map<int, int>& rl, double weight, vector<double>& y_hat) {
if (weight > 0) {
for (const auto &it : rl) {
y_hat[it.first] += weight;
}
}
}
void PureSlim::subtract_hat_tp(const unordered_map<int, int>& rl, double weight, vector<double>& y_hat) {
if (weight > 0) {
for (const auto &it : rl) {
y_hat[it.first] -= weight;
}
}
}
double PureSlim::ip_faster_tp(const unordered_map<int, int>& rl, vector<double>& y_hat) {
double ans = 0.;
for (const auto &it : rl) {
ans += y_hat[it.first];
}
return ans;
}
unordered_map<int, double> PureSlim::norm_tp_x(const vector<unordered_map<int, int> >& R, vector<pair<int, double> >& active_items) {
int n = active_items.size();
unordered_map<int, double> norm;
norm.reserve(n);
for (const auto &it : active_items) {
norm[it.first] = (double)R[it.first].size();
}
return norm;
}
void PureSlim::train_slim(const vector<unordered_map<int, int> >& R, vector<double>& w, int y, double l1, double l2, double tol)
{
int T = 50;
int nCol = R.size();
int nRow = nUser;
/* precalculate the inner product of y and each x */
vector<pair<int, double> > active_items;
for (int i = 0; i < nCol; ++i) {
if (i != y) {
double ip = ip_tp(R[i], R[y]);
if (ip > 0.) {
active_items.push_back(make_pair(i, ip));
}
}
}
/* declare the weight, # of weight is equal to the # of non-zero active_items */
if (active_items.size() == 0) return;
/* precalculate the estimate of y using all x (all 0s by default) */
vector<double> y_hat(nRow, 0);
/* precalculate the norm of x's regard of y */
unordered_map<int, double> x_norm = norm_tp_x(R, active_items);
/* updating the weight */
for (int t = 0; t < T; ++t) {
double delta_weight = 0.;
random_shuffle(active_items.begin(), active_items.end());
for (const auto &it : active_items) {
int i = it.first;
double tp_weight = w[i];
subtract_hat_tp(R[i], tp_weight, y_hat);
double upper = it.second - ip_faster_tp(R[i], y_hat);
/* Eq(5) in Regularization Paths for Generalized Linear Models via Coordinate Descent */
w[i] = upper > l1 ? (upper - l1) / (x_norm[i] + l2) : 0.;
add_hat_tp(R[i], w[i], y_hat);
delta_weight += (w[i] - tp_weight) * (w[i] - tp_weight);
}
/* break if the norm of delta weight is less than tol */
if (delta_weight < tol) {
break;
}
}
}
void PureSlim::train(const vector<unordered_map<int, int> >& R, double l1, double l2, double tol, int n_threads) {
cout << "<----------ITEM MODEL---------->" << endl
<< "l1 = " << l1 << ", l2 = " << l2 << endl;
int nCol = R.size();
cout << nCol << endl;
if (nCol != nItem) {
cout << "Training dim not fit!" << endl;
exit(0);
}
wtItem.clear();
wtItem.resize(nItem);
vector<unordered_map<int, double> > wt(nCol);
clock_t start_train = clock();
#pragma omp parallel shared (R, l1, l2, tol, nCol) num_threads(n_threads)
{
#pragma omp for schedule(dynamic)
for (int i = 0; i < nCol; ++i) {
vector<double> weight(nCol, 0.);
train_slim(R, weight, i, l1, l2, tol);
for (int j = 0; j < nCol; ++j) {
if (weight[j] > 0.) {
wt[i][j] = weight[j];
}
}
}
}
for (int i = 0; i < nCol; ++i) {
for (const auto &it : wt[i]) {
wtItem[it.first][i] = it.second;
}
}
clock_t end_train = clock();
cout << "Training Finished!" << endl;
cout << "Training time = " << (double)(end_train - start_train) / CLOCKS_PER_SEC << endl;
}
vector<vector<int> > PureSlim::predict(vector<unordered_map<int, int> >& R_test, int n, int n_threads) {
if (wtItem.size() == 0) {
cout << "Item model not exist!" << endl;
exit(0);
}
cout << "<--------Normal Result!-------->" << endl;
clock_t start_test = clock();
int nTest = R_test.size();
vector<vector<int> > rec_list(nTest, vector<int>(n, 0));
#pragma omp parallel shared (R_test, rec_list) num_threads(n_threads)
{
#pragma omp for schedule(dynamic)
for (int i = 0; i < nTest; ++i) {
if (R_test[i].size() > 0) {
vector<int> tp_rec = predict_list(R_test[i], n);
for (int j = 0; j < n; ++j) {
rec_list[i][j] = tp_rec[j];
}
}
else {
rec_list[i][0] = -1;
}
}
}
clock_t end_test = clock();
cout << "Test time = " << (double)(end_test - start_test) / CLOCKS_PER_SEC << endl;
return rec_list;
}
vector<int> PureSlim::predict_list(const unordered_map<int, int>& seed, int n) {
vector<double> sc = predict_score(seed);
vector<pair<double, int> > score(nItem);
vector<int> rec(n);
for (int i = 0; i < nItem; ++i) {
score[i].second = i;
score[i].first = sc[i];
}
sort(score.begin(), score.end());
for (int i = nItem - 1, count = 0; i >= 0; i--) {
if (seed.find(score[i].second) == seed.end()) {
rec[count++] = score[i].second;
if (count == n) break;
}
}
return rec;
}
vector<double> PureSlim::predict_score(const unordered_map<int, int>& R_test) {
vector<double> score(nItem, 0.);
for (const auto &it : R_test) {
for (const auto &inner_it : wtItem[it.first]) {
score[inner_it.first] += inner_it.second;
}
}
return score;
}
void PureSlim::load_weight(const char* filename) {
cout << "Number of Item is: " << nItem << endl;
wtItem.clear();
wtItem.resize(nItem);
// read in reading list with temporal information
ifstream infile;
infile.open(filename);
if (!infile) {
printf("File Does Not Exist!!!");
}
int u_id;
int s_id;
double weight;
// read from .csv file
string delimiter = ",";
string value;
while (getline(infile, value)) {
int pos = 0;
string token;
pos = value.find(delimiter);
token = value.substr(0, pos);
u_id = (int)stoi(token);
value.erase(0, pos + delimiter.length());
pos = value.find(delimiter);
token = value.substr(0, pos);
s_id = (int)stoi(token);
value.erase(0, pos + delimiter.length());
weight = (double)stof(value);
if (s_id >= nItem || u_id >= nItem) {
cout << "Dim not fit!!" << endl;
exit(0);
}
wtItem[s_id][u_id] = weight;
}
infile.close();
}
void PureSlim::write_weight(const char* filenameItem) {
ofstream myfile(filenameItem);
for (int i = 0; i < nItem; ++i) {
for (const auto &it : wtItem[i]) {
myfile << to_string(i) << "," << to_string(it.first) << ","
<< to_string(it.second) << "\n";
}
}
myfile.close();
}