jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
classifier.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011,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 "classifier.hpp"
18 
19 #include <map>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "../classifier/classifier_factory.hpp"
25 #include "../classifier/classifier_base.hpp"
26 #include "../common/vector_util.hpp"
27 #include "../fv_converter/datum.hpp"
28 #include "../fv_converter/datum_to_fv_converter.hpp"
29 #include "../fv_converter/converter_config.hpp"
30 #include "../storage/storage_factory.hpp"
31 
32 using std::string;
33 using std::vector;
34 using jubatus::util::lang::shared_ptr;
37 
38 namespace jubatus {
39 namespace core {
40 namespace driver {
41 
43  shared_ptr<core::classifier::classifier_base> classifier_method,
44  shared_ptr<fv_converter::datum_to_fv_converter> converter)
45  : converter_(converter)
46  , classifier_(classifier_method)
47  , wm_(mixable_weight_manager::model_ptr(new weight_manager)) {
48  register_mixable(classifier_->get_mixable());
50 
51  converter_->set_weight_manager(wm_.get_model());
52 }
53 
55 }
56 
57 void classifier::train(const string& label, const fv_converter::datum& data) {
59  converter_->convert_and_update_weight(data, v);
61  classifier_->train(v, label);
62 }
63 
65  const fv_converter::datum& data) const {
67  converter_->convert(data, v);
68 
70  classifier_->classify_with_scores(v, scores);
71  return scores;
72 }
73 
74 void classifier::get_status(std::map<string, string>& status) const {
75  classifier_->get_status(status);
76 }
77 
78 bool classifier::delete_label(const std::string& label) {
79  return classifier_->delete_label(label);
80 }
81 
83  classifier_->clear();
84  converter_->clear_weights();
85 }
86 
87 std::vector<std::string> classifier::get_labels() const {
88  return classifier_->get_labels();
89 }
90 bool classifier::set_label(const std::string& label) {
91  return classifier_->set_label(label);
92 }
93 
95  pk.pack_array(2);
96  classifier_->pack(pk);
97  wm_.get_model()->pack(pk);
98 }
99 
100 void classifier::unpack(msgpack::object o) {
101  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
102  throw msgpack::type_error();
103  }
104 
105  // clear before load
106  classifier_->clear();
107  converter_->clear_weights();
108  classifier_->unpack(o.via.array.ptr[0]);
109  wm_.get_model()->unpack(o.via.array.ptr[1]);
110 }
111 
112 } // namespace driver
113 } // namespace core
114 } // namespace jubatus
std::vector< classify_result_elem > classify_result
classifier(jubatus::util::lang::shared_ptr< classifier_base > classifier_method, jubatus::util::lang::shared_ptr< fv_converter::datum_to_fv_converter > converter)
Definition: classifier.cpp:42
bool set_label(const std::string &label)
Definition: classifier.cpp:90
jubatus::util::lang::shared_ptr< fv_converter::datum_to_fv_converter > converter_
Definition: classifier.hpp:69
jubatus::core::classifier::classify_result classify(const fv_converter::datum &data) const
Definition: classifier.cpp:64
void train(const std::string &, const fv_converter::datum &)
Definition: classifier.cpp:57
void pack(framework::packer &pk) const
Definition: classifier.cpp:94
bool delete_label(const std::string &name)
Definition: classifier.cpp:78
framework::linear_mixable_helper< weight_manager, versioned_weight_diff > mixable_weight_manager
void unpack(msgpack::object o)
Definition: classifier.cpp:100
jubatus::util::lang::shared_ptr< classifier_base > classifier_
Definition: classifier.hpp:70
std::vector< T > v(size)
fv_converter::mixable_weight_manager wm_
Definition: classifier.hpp:71
std::vector< std::string > get_labels() const
Definition: classifier.cpp:87
void sort_and_merge(sfv_t &sfv)
Definition: vector_util.cpp:28
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
void get_status(std::map< std::string, std::string > &status) const
Definition: classifier.cpp:74
void register_mixable(framework::mixable *mixable)
Definition: driver.cpp:242
storage::mixable_lsh_index_storage::model_ptr model_ptr
Definition: euclid_lsh.cpp:90