jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
passive_aggressive_2.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_2.hpp"
18 
19 #include <algorithm>
20 #include <string>
21 
22 #include "../common/exception.hpp"
23 
24 using std::string;
25 
26 namespace jubatus {
27 namespace core {
28 namespace classifier {
29 
31  : linear_classifier(storage) {
32 }
33 
35  const classifier_config& config,
36  storage_ptr storage)
37  : linear_classifier(storage),
38  config_(config) {
39 
40  if (!(0.f < config.regularization_weight)) {
41  throw JUBATUS_EXCEPTION(
42  common::invalid_parameter("0.0 < regularization_weight"));
43  }
44 }
45 
47  const string& label) {
48  check_touchable(label);
49 
50  string incorrect_label;
51  float margin = calc_margin(sfv, label, incorrect_label);
52  float loss = 1.f + margin;
53 
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  }
64  sfv,
65  loss / (2 * sfv_norm + 1 / (2 * config_.regularization_weight)),
66  label,
67  incorrect_label);
68  touch(label);
69 }
70 
72  return string("passive_aggressive_2");
73 }
74 
75 } // namespace classifier
76 } // namespace core
77 } // 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 &sfv, const std::string &label)
#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
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)