jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
linear_classifier.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011 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_CLASSIFIER_LINEAR_CLASSIFIER_HPP_
18 #define JUBATUS_CORE_CLASSIFIER_LINEAR_CLASSIFIER_HPP_
19 
20 #include <stdint.h>
21 
22 #include <cmath>
23 #include <map>
24 #include <string>
25 #include <vector>
26 
27 #include "../common/type.hpp"
28 #include "../framework/linear_function_mixer.hpp"
29 #include "../storage/storage_base.hpp"
30 #include "../unlearner/unlearner_base.hpp"
31 #include "classifier_type.hpp"
32 #include "classifier_base.hpp"
33 
34 namespace jubatus {
35 namespace core {
36 namespace classifier {
37 
39  public:
40  explicit linear_classifier(storage_ptr storage);
41  virtual ~linear_classifier();
42  virtual void train(const common::sfv_t& fv, const std::string& label) = 0;
43 
45  jubatus::util::lang::shared_ptr<unlearner::unlearner_base>
47 
48  jubatus::util::lang::shared_ptr<unlearner::unlearner_base>
49  label_unlearner() const {
50  return unlearner_;
51  }
52 
53  std::string classify(const common::sfv_t& fv) const;
54  void classify_with_scores(const common::sfv_t& fv,
55  classify_result& scores) const;
56  bool delete_label(const std::string& label);
57  bool unlearn_label(const std::string& label);
58  void clear();
59 
60  std::vector<std::string> get_labels() const;
61  bool set_label(const std::string& label);
62 
63  virtual std::string name() const = 0;
64 
65  // TODO(beam2d): Think the objective of this function and where it should be
66  // defined. Algorithms have |get_status| tentatively to extract status from
67  // storages.
68  virtual void get_status(std::map<std::string, std::string>& status) const;
69 
70  void pack(framework::packer& pk) const;
71  void unpack(msgpack::object o);
72 
74 
75  protected:
76  void update_weight(
77  const common::sfv_t& sfv,
78  float step_weigth,
79  const std::string& pos_label,
80  const std::string& neg_class);
81  float calc_margin(
82  const common::sfv_t& sfv,
83  const std::string& label,
84  std::string& incorrect_label) const;
86  const common::sfv_t& sfv,
87  const std::string& label,
88  std::string& incorrect_label,
89  float& variance) const;
90  std::string get_largest_incorrect_label(
91  const common::sfv_t& sfv,
92  const std::string& label,
93  classify_result& scores) const;
94 
95  static float squared_norm(const common::sfv_t& sfv);
96  void check_touchable(const std::string& label);
97  void touch(const std::string& label);
98 
100  jubatus::util::lang::shared_ptr<unlearner::unlearner_base> unlearner_;
102 };
103 
104 } // namespace classifier
105 } // namespace core
106 } // namespace jubatus
107 
108 #endif // JUBATUS_CORE_CLASSIFIER_LINEAR_CLASSIFIER_HPP_
jubatus::util::lang::shared_ptr< jubatus::core::storage::storage_base > storage_ptr
std::vector< classify_result_elem > classify_result
virtual std::string name() const =0
static float squared_norm(const common::sfv_t &sfv)
bool unlearn_label(const std::string &label)
void classify_with_scores(const common::sfv_t &fv, classify_result &scores) const
void set_label_unlearner(jubatus::util::lang::shared_ptr< unlearner::unlearner_base > label_unlearner)
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > label_unlearner() const
float calc_margin_and_variance(const common::sfv_t &sfv, const std::string &label, std::string &incorrect_label, float &variance) const
void check_touchable(const std::string &label)
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > unlearner_
void pack(framework::packer &pk) const
virtual void train(const common::sfv_t &fv, const std::string &label)=0
std::string classify(const common::sfv_t &fv) const
std::vector< std::string > get_labels() const
framework::linear_function_mixer mixable_storage_
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
virtual void get_status(std::map< std::string, std::string > &status) const
std::string get_largest_incorrect_label(const common::sfv_t &sfv, const std::string &label, classify_result &scores) const
float calc_margin(const common::sfv_t &sfv, const std::string &label, std::string &incorrect_label) const
void update_weight(const common::sfv_t &sfv, float step_weigth, const std::string &pos_label, const std::string &neg_class)