jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
lsh_util.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #include "lsh_util.hpp"
18 
19 #include <cmath>
20 #include <string>
21 #include <vector>
22 #include "jubatus/util/math/random.h"
23 #include "../common/assert.hpp"
24 #include "../storage/bit_vector.hpp"
25 
26 namespace jubatus {
27 namespace core {
28 namespace recommender {
29 
30 using std::string;
31 using std::vector;
33 using jubatus::util::data::unordered_map;
34 
35 void generate_random_vector(size_t dim, uint32_t seed, vector<float>& ret) {
36  jubatus::util::math::random::mtrand rnd(seed);
37  vector<float> v(dim);
38  for (uint64_t i = 0; i < dim; ++i) {
39  v[i] = rnd.next_gaussian();
40  }
41  ret.swap(v);
42 }
43 
44 void set_bit_vector(const std::vector<float>& vec, bit_vector& bit_vec) {
45  bit_vector bv;
46  bv.resize_and_clear(vec.size());
47  for (size_t i = 0; i < vec.size(); ++i) {
48  if (vec[i] >= 0.f) {
49  bv.set_bit(i);
50  }
51  }
52  bit_vec.swap(bv);
53 }
54 
56  const unordered_map<string, vector<float> >& matrix,
57  const common::sfv_t& vec,
58  size_t dim,
59  vector<float>& ret) {
60  vector<float> r(dim);
61  for (size_t i = 0; i < vec.size(); ++i) {
62  const string& column = vec[i].first;
63  float val = vec[i].second;
64  unordered_map<string, vector<float> >::const_iterator it =
65  matrix.find(column);
66  if (it == matrix.end()) {
67  continue;
68  }
69  const vector<float>& v = it->second;
70 
71  JUBATUS_ASSERT_EQ(v.size(), r.size(), "");
72  for (size_t j = 0; j < v.size(); ++j) {
73  r[j] += v[j] * val;
74  }
75  }
76 
77  ret.swap(r);
78 }
79 
80 } // namespace recommender
81 } // namespace core
82 } // namespace jubatus
#define JUBATUS_ASSERT_EQ(a, b, messages)
Definition: assert.hpp:63
void resize_and_clear(uint64_t bit_num)
Definition: bit_vector.hpp:174
void set_bit_vector(const std::vector< float > &vec, bit_vector &bit_vec)
Definition: lsh_util.cpp:44
bit_vector_base< uint64_t > bit_vector
void prod_invert_and_vector(const unordered_map< string, vector< float > > &matrix, const common::sfv_t &vec, size_t dim, vector< float > &ret)
Definition: lsh_util.cpp:55
std::vector< T > v(size)
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
void generate_random_vector(size_t dim, uint32_t seed, vector< float > &ret)
Definition: lsh_util.cpp:35