forked from jegonzal/PowerGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl3als.hpp
394 lines (348 loc) · 12.4 KB
/
gl3als.hpp
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
/*
* Copyright (c) 2009 Carnegie Mellon University.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* For more about this software visit:
*
* http://www.graphlab.ml.cmu.edu
*
*/
#ifndef GL3_ALS_HPP
#define GL3_ALS_HPP
#include <Eigen/Dense>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <graphlab/util/stl_util.hpp>
#include "stats.hpp"
// This file defines the serialization code for the eigen types.
#include "eigen_serialization.hpp"
using namespace graphlab;
const int SAFE_NEG_OFFSET = 2; //add 2 to negative node id
//to prevent -0 and -1 which arenot allowed
double TOLERANCE = 1e-3;
double LAMBDA = 0.01;
size_t MAX_ITER= -1;
double MAXVAL = 1e+100;
double MINVAL = -1e+100;
int REGNORMAL = 0;
double TEST_PERCENT = 0.4;
bool INTERACTIVE = false;
/**
* \brief We use the eigen library's vector type to represent
* mathematical vectors.
*/
typedef Eigen::VectorXd vec_type;
/**
* \brief We use the eigen library's matrix type to represent
* matrices.
*/
typedef Eigen::MatrixXd mat_type;
/**
* \ingroup toolkit_matrix_factorization
*
* \brief the vertex data type which contains the latent factor.
*
* Each row and each column in the matrix corresponds to a different
* vertex in the ALS graph. Associated with each vertex is a factor
* (vector) of latent parameters that represent that vertex. The goal
* of the ALS algorithm is to find the values for these latent
* parameters such that the non-zero entries in the matrix can be
* predicted by taking the dot product of the row and column factors.
*/
struct vertex_data {
/**
* \brief A shared "constant" that specifies the number of latent
* values to use.
*/
static size_t NLATENT;
/** \brief The number of times this vertex has been updated. */
uint32_t nupdates;
/** \brief The most recent L1 change in the factor value */
float residual; //! how much the latent value has changed
/** \brief The mean raiting of the user or movie */
double bias;
/** \brief The latent factor for this vertex */
vec_type factor;
std::vector<std::pair<double, graphlab::vertex_id_type> > top_rated;
std::vector<std::pair<double, graphlab::vertex_id_type> > top_pred;
/** \brief the user specific item-item similarity matrix */
mat_type Wu;
// std::vector<std::vector<std::pair<double, graphlab::vertex_id_type> > > top_explain;
// std::vector<std::vector<std::pair<double, graphlab::vertex_id_type> > > top_explain2;
// std::vector<std::vector<std::pair<double, graphlab::vertex_id_type> > > top_explain3;
/**
* \brief Simple default constructor which randomizes the vertex
* data
*/
vertex_data() : nupdates(0), residual(1), bias(0) { randomize(); }
/** \brief Randomizes the latent factor */
void randomize() { factor.resize(NLATENT); factor.setRandom(); }
/** \brief Save the vertex data to a binary archive */
void save(oarchive& arc) const {
arc << nupdates << residual << bias << factor << top_rated << top_pred;
}
/** \brief Load the vertex data from a binary archive */
void load(iarchive& arc) {
arc >> nupdates >> residual >> bias >> factor >> top_rated >> top_pred;
}
}; // end of vertex data
size_t vertex_data::NLATENT = 20;
size_t hash_value(vertex_data const& v) {
return v.nupdates;
}
/**
* \brief The edge data stores the entry in the matrix.
*
* In addition the edge data also stores the most recent error estimate.
*/
struct edge_data : public IS_POD_TYPE {
/**
* \brief The type of data on the edge;
*
* \li *Train:* the observed value is correct and used in training
* \li *Validate:* the observed value is correct but not used in training
* \li *Predict:* The observed value is not correct and should not be
* used in training.
*/
enum data_role_type { TRAIN, VALIDATE, PREDICT };
/** \brief the observed value for the edge */
float obs;
/** \brief The train/validation/test designation of the edge */
data_role_type role;
/** \brief basic initialization */
edge_data(float obs = 0, data_role_type role = TRAIN) :
obs(obs), role(role) { }
}; // end of edge data
/**
* \brief The graph type is defined in terms of the vertex and edge
* data.
*/
typedef distributed_graph<vertex_data, edge_data> graph_type;
// Movie names
boost::unordered_map<graph_type::vertex_id_type, std::string> mlist;
stats_info count_edges(const graph_type::edge_type & edge){
stats_info ret;
if (edge.data().role == edge_data::TRAIN)
ret.training_edges = 1;
else if (edge.data().role == edge_data::VALIDATE)
ret.validation_edges = 1;
ret.max_user = (size_t)edge.source().id();
ret.max_item = (-edge.target().id()-SAFE_NEG_OFFSET);
return ret;
}
/////////////// Serialization /////////////////
#include "implicit.hpp"
/**
* \brief The graph loader function is a line parser used for
* distributed graph construction.
*/
inline bool graph_loader(graph_type& graph,
const std::string& filename,
const std::string& line) {
ASSERT_FALSE(line.empty());
if (boost::starts_with(line, "#") || boost::starts_with(line, "%")) {
logstream(LOG_INFO) << line << std::endl;
return true;
}
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
// Determine the role of the data
edge_data::data_role_type role = edge_data::TRAIN;
if(boost::ends_with(filename,".validate")) role = edge_data::VALIDATE;
else if(boost::ends_with(filename, ".predict")) role = edge_data::PREDICT;
// Parse the line
graph_type::vertex_id_type source_id(-1), target_id(-1);
float obs(0);
const bool success = qi::phrase_parse
(line.begin(), line.end(),
// Begin grammar
(
qi::ulong_[phoenix::ref(source_id) = qi::_1] >> -qi::char_(',') >>
qi::ulong_[phoenix::ref(target_id) = qi::_1] >>
-(-qi::char_(',') >> qi::float_[phoenix::ref(obs) = qi::_1])
)
,
// End grammar
ascii::space);
if(!success) return false;
if(role == edge_data::TRAIN || role == edge_data::VALIDATE){
if (obs < MINVAL || obs > MAXVAL)
logstream(LOG_FATAL)<<"Rating values should be between " << MINVAL << " and " << MAXVAL << ". Got value: " << obs << " [ user: " << source_id << " to item: " <<target_id << " ] " << std::endl;
}
// map target id into a separate number space
target_id = -(vertex_id_type(target_id + SAFE_NEG_OFFSET));
// Create an edge and add it to the graph
graph.add_edge(source_id, target_id, edge_data(obs, role));
return true; // successful load
} // end of graph_loader
/**
* \brief The prediction saver is used by the graph.save routine to
* output the final predictions back to the filesystem.
*/
struct prediction_saver {
typedef graph_type::vertex_type vertex_type;
typedef graph_type::edge_type edge_type;
std::string save_vertex(const vertex_type& vertex) const {
return ""; //nop
}
std::string save_edge(const edge_type& edge) const {
if(edge.data().role == edge_data::PREDICT) {
std::stringstream strm;
const double prediction =
edge.source().data().factor.dot(edge.target().data().factor);
strm << edge.source().id() << '\t';
strm << (-edge.target().id() - SAFE_NEG_OFFSET) << '\t';
strm << prediction << '\n';
return strm.str();
} else return "";
}
}; // end of prediction_saver
struct linear_model_saver_U {
typedef graph_type::vertex_type vertex_type;
typedef graph_type::edge_type edge_type;
/* save the linear model, using the format:
nodeid) factor1 factor2 ... factorNLATENT \n
*/
std::string save_vertex(const vertex_type& vertex) const {
if (vertex.num_out_edges() > 0){
std::string ret = boost::lexical_cast<std::string>(vertex.id()) + " ";
for (uint i=0; i< vertex_data::NLATENT; i++)
ret += boost::lexical_cast<std::string>(vertex.data().factor[i]) + " ";
ret += "\n";
return ret;
}
else return "";
}
std::string save_edge(const edge_type& edge) const {
return "";
}
};
struct linear_model_saver_V {
typedef graph_type::vertex_type vertex_type;
typedef graph_type::edge_type edge_type;
/* save the linear model, using the format:
nodeid) factor1 factor2 ... factorNLATENT \n
*/
std::string save_vertex(const vertex_type& vertex) const {
if (vertex.num_out_edges() == 0){
std::string ret = boost::lexical_cast<std::string>(-vertex.id()-SAFE_NEG_OFFSET) + " ";
for (uint i=0; i< vertex_data::NLATENT; i++)
ret += boost::lexical_cast<std::string>(vertex.data().factor[i]) + " ";
ret += "\n";
return ret;
}
else return "";
}
std::string save_edge(const edge_type& edge) const {
return "";
}
};
struct error_aggregator : public graphlab::IS_POD_TYPE {
double train;
double test;
size_t ntrain;
size_t ntest;
error_aggregator() : train(0), test(0), ntrain(0), ntest(0) {}
error_aggregator& operator+=(const error_aggregator& other) {
train += other.train;
test += other.test;
ntrain += other.ntrain;
ntest += other.ntest;
return *this;
}
};
/**
* \brief Given an edge compute the error associated with that edge
*/
error_aggregator extract_l2_error(const graph_type::edge_type & edge) {
double pred =
edge.source().data().factor.dot(edge.target().data().factor);
pred = std::min(MAXVAL, pred);
pred = std::max(MINVAL, pred);
double err = (edge.data().obs - pred) * (edge.data().obs - pred);
error_aggregator ret;
if (edge.data().role == edge_data::TRAIN) {
ret.train = err;
ret.ntrain = 1;
} else {
ret.test = err;
ret.ntest = 1;
}
return ret;
} // end of extract_l2_error
double extract_l2_norm(const graph_type::vertex_type& vertex) {
return vertex.data().factor.norm();
}
template<typename key_t, typename val_t>
struct map_join {
boost::unordered_map<key_t, val_t> data; // rating than movie
void save(graphlab::oarchive& oarc) const {
oarc << data;
}
void load(graphlab::iarchive& iarc) {
iarc >> data;
}
map_join& operator+=(const map_join& other) {
std::copy(other.data.begin(), other.data.end(),
std::inserter(data, data.end()));
return *this;
}
std::vector<std::pair<val_t, key_t> > get_top_k(size_t n) {
std::vector<std::pair<val_t, key_t> > ret;
typename boost::unordered_map<key_t, val_t>::const_iterator iter = data.begin();
std::vector<std::pair<val_t, key_t> > all_copy;
while (iter != data.end()) {
all_copy.push_back(std::make_pair(iter->second, iter->first));
++iter;
}
std::sort(all_copy.rbegin(), all_copy.rend());
size_t limit = all_copy.size() < n ? all_copy.size() : n;
std::copy(all_copy.begin(), all_copy.begin() + limit,
std::inserter(ret, ret.end()));
return ret;
}
std::vector<std::pair<val_t, key_t> > get_top_k_exclude(
size_t n, boost::unordered_map<key_t, val_t>& exclude) {
std::vector<std::pair<val_t, key_t> > ret;
typename boost::unordered_map<key_t, val_t>::const_iterator iter = data.begin();
std::vector<std::pair<val_t, key_t> > all_copy;
while (iter != data.end()) {
if (exclude.count(iter->first) == 0) {
all_copy.push_back(std::make_pair(iter->second, iter->first));
}
++iter;
}
std::sort(all_copy.rbegin(), all_copy.rend());
size_t limit = all_copy.size() < n ? all_copy.size() : n;
std::copy(all_copy.begin(), all_copy.begin() + limit,
std::inserter(ret, ret.end()));
return ret;
}
};
bool is_user(const graph_type::vertex_type& vertex) {
return vertex.num_out_edges() > 0;
}
bool is_movie(const graph_type::vertex_type& vertex) {
return !is_user(vertex);
}
inline int id2movieid(graphlab::vertex_id_type vid) {
return -vid - SAFE_NEG_OFFSET;
}
#endif