jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
nearest_neighbor.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 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 "nearest_neighbor.hpp"
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 #include "../storage/row_deleter.hpp"
23 #include "../fv_converter/datum_to_fv_converter.hpp"
24 #include "../fv_converter/weight_manager.hpp"
25 #include "../fv_converter/mixable_weight_manager.hpp"
26 #include "../nearest_neighbor/nearest_neighbor_base.hpp"
27 
28 namespace jubatus {
29 namespace core {
30 namespace driver {
31 
32 using jubatus::util::lang::shared_ptr;
34 using fv_converter::weight_manager;
35 
37  shared_ptr<core::nearest_neighbor::nearest_neighbor_base> nn,
38  shared_ptr<fv_converter::datum_to_fv_converter> converter)
39  : converter_(converter),
40  nn_(nn),
41  wm_(mixable_weight_manager::model_ptr(new weight_manager)) {
42  register_mixable(nn_->get_mixable());
43  register_mixable(&wm_);
44 
45  converter_->set_weight_manager(wm_.get_model());
46 }
47 
49  shared_ptr<core::nearest_neighbor::nearest_neighbor_base> nn,
50  shared_ptr<fv_converter::datum_to_fv_converter> converter,
51  shared_ptr<unlearner::unlearner_base> unlearner)
52  : converter_(converter),
53  nn_(nn),
54  unlearner_(unlearner),
55  wm_(mixable_weight_manager::model_ptr(new weight_manager)) {
56  register_mixable(nn_->get_mixable());
57  register_mixable(&wm_);
58 
59  converter_->set_weight_manager(wm_.get_model());
60  unlearner->set_callback(storage::row_deleter(nn_->get_table()));
61 }
62 
63 shared_ptr<storage::column_table> nearest_neighbor::get_table() {
64  return nn_->get_table();
65 }
66 
67 shared_ptr<const storage::column_table>
69  return nn_->get_const_table();
70 }
71 
73  const std::string& id,
74  const fv_converter::datum& datum) {
75  if (unlearner_) {
76  unlearner_->touch(id);
77  }
79  converter_->convert_and_update_weight(datum, v);
80  nn_->set_row(id, v);
81 }
82 
83 std::vector<std::pair<std::string, float> >
84 nearest_neighbor::neighbor_row_from_id(const std::string& id, size_t size) {
85  std::vector<std::pair<std::string, float> > ret;
86  nn_->neighbor_row(id, ret, size);
87  return ret;
88 }
89 
90 std::vector<std::pair<std::string, float> >
92  const fv_converter::datum& datum,
93  size_t size) {
95  converter_->convert(datum, v);
96  std::vector<std::pair<std::string, float> > ret;
97  nn_->neighbor_row(v, ret, size);
98  return ret;
99 }
100 
101 std::vector<std::pair<std::string, float> >
102 nearest_neighbor::similar_row(const std::string& id, size_t ret_num) {
103  std::vector<std::pair<std::string, float> > ret;
104  nn_->similar_row(id, ret, ret_num);
105  return ret;
106 }
107 
108 std::vector<std::pair<std::string, float> >
110  const core::fv_converter::datum& datum,
111  size_t ret_num) {
113  converter_->convert(datum, v);
114  std::vector<std::pair<std::string, float> > ret;
115  nn_->similar_row(v, ret, ret_num);
116  return ret;
117 }
118 
119 std::vector<std::string> nearest_neighbor::get_all_rows() {
120  std::vector<std::string> ret;
121  nn_->get_all_row_ids(ret);
122  return ret;
123 }
124 
126  converter_->clear_weights();
127  nn_->clear();
128  if (unlearner_) {
129  unlearner_->clear();
130  }
131 }
132 
134  pk.pack_array(2);
135  nn_->pack(pk);
136  wm_.get_model()->pack(pk);
137 }
138 
139 void nearest_neighbor::unpack(msgpack::object o) {
140  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
141  throw msgpack::type_error();
142  }
143 
144  // clear
145  nn_->clear();
146  converter_->clear_weights();
147 
148  // load
149  nn_->unpack(o.via.array.ptr[0]);
150  wm_.get_model()->unpack(o.via.array.ptr[1]);
151 }
152 
153 } // namespace driver
154 } // namespace core
155 } // namespace jubatus
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > unlearner_
fv_converter::mixable_weight_manager wm_
void set_row(const std::string &id, const fv_converter::datum &datum)
jubatus::util::lang::shared_ptr< core::nearest_neighbor::nearest_neighbor_base > nn_
jubatus::util::data::optional< std::string > unlearner
std::vector< std::pair< std::string, float > > neighbor_row_from_datum(const fv_converter::datum &datum, size_t size)
framework::linear_mixable_helper< weight_manager, versioned_weight_diff > mixable_weight_manager
jubatus::util::lang::shared_ptr< fv_converter::datum_to_fv_converter > converter_
std::vector< std::pair< std::string, float > > similar_row(const std::string &id, size_t ret_num)
nearest_neighbor(jubatus::util::lang::shared_ptr< core::nearest_neighbor::nearest_neighbor_base > nn, jubatus::util::lang::shared_ptr< fv_converter::datum_to_fv_converter > converter)
std::vector< T > v(size)
jubatus::util::lang::shared_ptr< const storage::column_table > get_const_table() const
jubatus::util::lang::shared_ptr< storage::column_table > get_table()
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
std::vector< std::string > get_all_rows()
std::vector< std::pair< std::string, float > > neighbor_row_from_id(const std::string &id, size_t size)
void pack(framework::packer &pk) const
void register_mixable(framework::mixable *mixable)
Definition: driver.cpp:242
storage::mixable_lsh_index_storage::model_ptr model_ptr
Definition: euclid_lsh.cpp:90