jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
passive_aggressive_1.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_1.hpp"
18 
19 #include <algorithm>
20 #include <string>
21 
22 #include "../common/exception.hpp"
23 
24 using std::string;
25 using std::min;
26 
27 namespace jubatus {
28 namespace core {
29 namespace classifier {
30 
32  : linear_classifier(storage) {
33 }
34 
36  const classifier_config& config,
37  storage_ptr storage)
38  : linear_classifier(storage),
39  config_(config) {
40 
41  if (!(0.f < config.regularization_weight)) {
42  throw JUBATUS_EXCEPTION(
43  common::invalid_parameter("0.0 < regularization_weight"));
44  }
45 }
46 
48  const string& label) {
49  check_touchable(label);
50 
51  string incorrect_label;
52  float margin = calc_margin(sfv, label, incorrect_label);
53  float loss = 1.f + margin;
54  if (loss < 0.f) {
55  storage_->register_label(label);
56  return;
57  }
58  float sfv_norm = squared_norm(sfv);
59  if (sfv_norm == 0.f) {
60  storage_->register_label(label);
61  return;
62  }
63 
65  sfv,
66  min(config_.regularization_weight, loss / (2 * sfv_norm)),
67  label,
68  incorrect_label);
69  touch(label);
70 }
71 
73  return string("passive_aggressive_1");
74 }
75 
76 } // namespace classifier
77 } // namespace core
78 } // namespace jubatus
jubatus::util::lang::shared_ptr< jubatus::core::storage::storage_base > storage_ptr
static float squared_norm(const common::sfv_t &sfv)
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
void check_touchable(const std::string &label)
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
void train(const common::sfv_t &fv, const std::string &label)
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)