jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
weight_manager.hpp
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 #ifndef JUBATUS_CORE_FV_CONVERTER_WEIGHT_MANAGER_HPP_
18 #define JUBATUS_CORE_FV_CONVERTER_WEIGHT_MANAGER_HPP_
19 
20 #include <istream>
21 #include <ostream>
22 #include <sstream>
23 #include <string>
24 #include <msgpack.hpp>
25 #include "jubatus/util/data/unordered_map.h"
26 #include "jubatus/util/concurrent/mutex.h"
27 #include "jubatus/util/concurrent/lock.h"
28 #include "../framework/model.hpp"
29 #include "../common/type.hpp"
30 #include "../common/version.hpp"
31 #include "keyword_weights.hpp"
32 
33 namespace jubatus {
34 namespace core {
35 namespace fv_converter {
36 
41  const storage::version& v);
43 
45 
48 };
49 
51  public:
53 
54  void update_weight(const common::sfv_t& fv);
55  void get_weight(common::sfv_t& fv) const;
56 
57  void add_weight(const std::string& key, float weight);
58 
59  void get_diff(versioned_weight_diff& diff) const {
60  util::concurrent::scoped_lock lk(mutex_);
62  }
63 
64  bool put_diff(const versioned_weight_diff& diff) {
65  util::concurrent::scoped_lock lk(mutex_);
66  if (diff.version_ == version_) {
70  return true;
71  } else {
72  return false;
73  }
74  }
75 
76  void mix(
77  const versioned_weight_diff& lhs,
78  versioned_weight_diff& acc) const {
79  if (lhs.version_ == acc.version_) {
80  acc.weights_.merge(lhs.weights_);
81  } else if (lhs.version_ > acc.version_) {
82  acc = lhs;
83  }
84  }
85 
86  void clear() {
87  util::concurrent::scoped_lock lk(mutex_);
90  }
91 
93  return version_;
94  }
95 
97 
98  void pack(framework::packer& pk) const {
99  util::concurrent::scoped_lock lk(mutex_);
100  pk.pack(*this);
101  }
102 
103  void unpack(msgpack::object o) {
104  util::concurrent::scoped_lock lk(mutex_);
105  o.convert(this);
106  }
107 
108  std::string to_string() const {
109  util::concurrent::scoped_lock lk(mutex_);
110  std::stringstream ss;
111  ss << "version:" << version_
112  << " diff_weights:" << diff_weights_.to_string()
113  << " master_weights:" << master_weights_.to_string();
114  return ss.str();
115  }
116 
117  private:
118  uint64_t get_document_count() const {
121  }
122 
123  size_t get_document_frequency(const std::string& key) const {
126  }
127 
128  double get_user_weight(const std::string& key) const {
129  return diff_weights_.get_user_weight(key) +
131  }
132 
133  double get_global_weight(const std::string& key) const;
134 
135  mutable util::concurrent::mutex mutex_;
139 };
140 
141 } // namespace fv_converter
142 } // namespace core
143 } // namespace jubatus
144 
145 #endif // JUBATUS_CORE_FV_CONVERTER_WEIGHT_MANAGER_HPP_
size_t get_document_frequency(const std::string &key) const
double get_user_weight(const std::string &key) const
void get_weight(common::sfv_t &fv) const
void get_diff(versioned_weight_diff &diff) const
versioned_weight_diff & merge(const versioned_weight_diff &target)
float get_user_weight(const std::string &key) const
void pack(framework::packer &pk) const
void mix(const versioned_weight_diff &lhs, versioned_weight_diff &acc) const
double get_global_weight(const std::string &key) const
std::vector< T > v(size)
void add_weight(const std::string &key, float weight)
void update_weight(const common::sfv_t &fv)
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
MSGPACK_DEFINE(version_, diff_weights_, master_weights_)
bool put_diff(const versioned_weight_diff &diff)
size_t get_document_frequency(const std::string &key) const