-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxclimf.cpp
307 lines (251 loc) · 11 KB
/
xclimf.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
/*
The MIT License (MIT)
Copyright (c) 2014 Gabriel Poesia <gabriel.poesia at gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "xclimf.h"
#include <algorithm>
#include <cmath>
#include <ctime>
#include <limits>
#include <random>
#include <utility>
#include <vector>
using std::default_random_engine;
using std::exp;
using std::fill;
using std::log;
using std::make_pair;
using std::max;
using std::min;
using std::numeric_limits;
using std::pair;
using std::pow;
using std::random_device;
using std::uniform_real_distribution;
using std::vector;
namespace {
double sigmoid(double x) {
return 1 / (1 + exp(-x));
}
double sigmoid_prime(double x) {
double s = sigmoid(x);
return s * (1 - s);
}
// Evaluate xCLiMF's loss function (an approximation of the ERR)
double compute_loss(
const vector<vector<double> > &users_features,
const vector<vector<double> > &items_features,
const vector<vector<pair<int, double>>> &ratings_matrix,
double lambda,
unsigned int latent_features,
const std::function<double(double)> &relevance_probability) {
int number_of_users = users_features.size();
int number_of_items = items_features.size();
double loss = 0;
for (unsigned i = 0; i < users_features.size(); i++) {
double user_err = 0;
for (unsigned j = 0; j < ratings_matrix[i].size(); j++) {
double user_j_err = log(sigmoid(xclimf::predict_score(
users_features[i],
items_features[ratings_matrix[i][j].first])));
for (unsigned k = 0; k < ratings_matrix[i].size(); k++) {
user_j_err +=
log(1 - relevance_probability(ratings_matrix[i][k].second) *
sigmoid(xclimf::predict_score(users_features[i],
items_features[ratings_matrix[i][k].first]) -
xclimf::predict_score(users_features[i],
items_features[ratings_matrix[i][j].first])));
}
user_err += relevance_probability(ratings_matrix[i][j].second)
* user_j_err;
}
loss += user_err;
}
double f_user_norm = 0;
double f_item_norm = 0;
for (int i = 0; i < number_of_users; i++)
for (unsigned j = 0; j < latent_features; j++)
f_user_norm += pow(users_features[i][j], 2);
for (int i = 0; i < number_of_items; i++)
for (unsigned j = 0; j < latent_features; j++)
f_item_norm += pow(items_features[i][j], 2);
return loss - lambda / 2 * (sqrt(f_user_norm) + sqrt(f_item_norm));
}
// Gradient of the loss function w.r.t one user's latent factors
void compute_gradient_um(
const vector<vector<double>> &users_features,
const vector<vector<double>> &items_features,
vector<vector<double>> &users_features_prime,
int user_id,
const vector<vector<pair<int, double>>> &ratings_matrix,
unsigned int latent_features,
double lambda,
const std::function<double(double)> &relevance_probability) {
fill(users_features_prime[user_id].begin(),
users_features_prime[user_id].end(), 0.);
for (unsigned i = 0; i < ratings_matrix[user_id].size(); i++) {
int i_id = ratings_matrix[user_id][i].first;
double predicted_i = xclimf::predict_score(users_features[user_id],
items_features[i_id]);
double relevant_p_i = relevance_probability(
ratings_matrix[user_id][i].second);
for (unsigned j = 0; j < latent_features; j++)
users_features_prime[user_id][j] += relevant_p_i *
sigmoid(-predicted_i) * items_features[i_id][j];
for (unsigned k = 0; k < ratings_matrix[user_id].size(); k++) {
int k_id = ratings_matrix[user_id][k].first;
double predicted_k = xclimf::predict_score(users_features[user_id],
items_features[k_id]);
double relevant_p_k = relevance_probability(
ratings_matrix[user_id][k].second);
for (unsigned j = 0; j < latent_features; j++) {
users_features_prime[user_id][j] += relevant_p_i *
(items_features[i_id][j] - items_features[k_id][j]) *
(relevant_p_k * sigmoid_prime(predicted_k - predicted_i)) /
(1 - relevant_p_k * sigmoid(predicted_k - predicted_i));
}
}
}
for (unsigned j = 0; j < users_features_prime[user_id].size(); j++)
users_features_prime[user_id][j] -= lambda * users_features[user_id][j];
}
// Gradient of the loss function w.r.t one user's latent factors
void compute_gradient_vj(
const vector<vector<double>> &users_features,
const vector<vector<double>> &items_features,
vector<vector<double>> &items_features_prime,
int user_id,
int item_index,
const vector<vector<pair<int, double>>> &ratings_matrix,
double lambda,
const std::function<double(double)> &relevance_probability) {
int item_id = ratings_matrix[user_id][item_index].first;
fill(items_features_prime[item_id].begin(),
items_features_prime[item_id].end(), 0.0);
double relevant_p = relevance_probability(
ratings_matrix[user_id][item_index].second);
double score = xclimf::predict_score(users_features[user_id],
items_features[item_id]);
for (unsigned j = 0; j < items_features_prime[item_id].size(); j++) {
items_features_prime[item_id][j] +=
relevant_p * sigmoid(-score) * users_features[user_id][j];
}
for (unsigned k = 0; k < ratings_matrix[user_id].size(); k++) {
int k_id = ratings_matrix[user_id][k].first;
double relevant_p_k =
relevance_probability(ratings_matrix[user_id][k].second);
double score_k =
xclimf::predict_score(users_features[user_id],
items_features[k_id]);
for (unsigned j = 0; j < items_features_prime[item_id].size(); j++) {
items_features_prime[item_id][j] += relevant_p * relevant_p_k *
sigmoid_prime(score - score_k) *
(1 / (1 - relevant_p_k * sigmoid(score_k - score)) -
1 / (1 - relevant_p * sigmoid(score - score_k))) *
users_features[user_id][j];
}
}
for (unsigned j = 0; j < items_features_prime[item_id][j]; j++)
items_features_prime[item_id][j] -= lambda * items_features[item_id][j];
}
void randomly_initialize(vector<vector<double>> &features) {
random_device rd;
default_random_engine re(rd());
// Best range found in experiments with the MovieLens datasets
uniform_real_distribution<> dis(0, 0.01);
for (auto &v : features) {
for (auto &f : v) {
f = dis(re);
}
}
}
} // namespace
namespace xclimf {
double predict_score(const vector<double> &user_features,
const vector<double> &item_features) {
double rating = 0;
for (unsigned i = 0; i < user_features.size(); i++)
rating += user_features[i] * item_features[i];
return rating;
}
void train(
const vector<vector<pair<int, double>>> &ratings_matrix,
vector<vector<double>> &users_features,
vector<vector<double>> &items_features,
unsigned int latent_features,
double learning_rate,
double lambda,
double eps,
unsigned int max_iterations,
bool initialize,
const std::function<double(double)> &relevance_probability) {
int number_of_items = 0;
int number_of_users = ratings_matrix.size();
for (unsigned i = 0; i < ratings_matrix.size(); i++)
for (unsigned j = 0; j < ratings_matrix[i].size(); j++)
number_of_items = max(number_of_items,
ratings_matrix[i][j].first + 1);
users_features.resize(number_of_users);
items_features.resize(number_of_items);
for (int i = 0; i < number_of_items; i++)
items_features[i].resize(latent_features);
for (int i = 0; i < number_of_users; i++)
users_features[i].resize(latent_features);
if (initialize) {
randomly_initialize(users_features);
randomly_initialize(items_features);
}
vector<vector<double>> users_features_prime(number_of_users,
vector<double>(latent_features));
vector<vector<double>> items_features_prime(number_of_items,
vector<double>(latent_features));
double last_loss = -numeric_limits<double>::infinity();
// Stochastic Gradient Descent iterations
for (unsigned it = 0; max_iterations == 0 || it < max_iterations; it++) {
double loss = compute_loss(users_features, items_features,
ratings_matrix, lambda, latent_features, relevance_probability);
if (loss < last_loss + eps)
break;
last_loss = loss;
for (int i = 0; i < number_of_users; i++) {
compute_gradient_um(users_features, items_features,
users_features_prime, i, ratings_matrix,
latent_features, lambda, relevance_probability);
for (unsigned j = 0; j < latent_features; j++) {
users_features[i][j] += learning_rate *
users_features_prime[i][j];
}
for (unsigned j = 0; j < ratings_matrix[i].size(); j++) {
compute_gradient_vj(users_features, items_features,
items_features_prime, i, j, ratings_matrix,
lambda, relevance_probability);
for (unsigned k = 0; k < latent_features; k++) {
items_features[ratings_matrix[i][j].first][k] +=
learning_rate *
items_features_prime[ratings_matrix[i][j].first][k];
}
}
}
}
}
// Relevance probability function used in the xCLiMF paper
// Assumes the maximum rating is 5.
double default_relevance_probability_function(double rating) {
return (pow(2, min(rating, 5.0)) - 1) / 32.0;
}
} // namespace xclimf