jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
passive_aggressive.cpp
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 #include "passive_aggressive.hpp"
18 
19 #include <string>
20 
21 using std::string;
22 
23 namespace jubatus {
24 namespace core {
25 namespace classifier {
26 
28  : linear_classifier(storage) {
29 }
30 
31 void passive_aggressive::train(const common::sfv_t& sfv, const string& label) {
32  check_touchable(label);
33 
34  string incorrect_label;
35  float margin = calc_margin(sfv, label, incorrect_label);
36  float loss = 1.f + margin;
37  if (loss < 0.f) {
38  return;
39  }
40  float sfv_norm = squared_norm(sfv);
41  if (sfv_norm == 0.f) {
42  storage_->register_label(label);
43  return;
44  }
45  update_weight(sfv, loss / (2 * sfv_norm), label, incorrect_label);
46  touch(label);
47 }
48 
49 string passive_aggressive::name() const {
50  return string("passive_aggressive");
51 }
52 
53 } // namespace classifier
54 } // namespace core
55 } // namespace jubatus
jubatus::util::lang::shared_ptr< jubatus::core::storage::storage_base > storage_ptr
static float squared_norm(const common::sfv_t &sfv)
void train(const common::sfv_t &fv, const std::string &label)
void check_touchable(const std::string &label)
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
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)