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) 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 "passive_aggressive.hpp"
18 
19 #include <algorithm>
20 #include <cmath>
21 #include <iostream>
22 
23 namespace jubatus {
24 namespace core {
25 namespace regression {
26 
28  const config& config,
29  storage_ptr storage)
30  : regression_base(storage),
31  config_(config),
32  sum_(0.f),
33  sq_sum_(0.f),
34  count_(0.f) {
35 
36  if (!(0.f < config.regularization_weight)) {
37  throw JUBATUS_EXCEPTION(
38  common::invalid_parameter("0.0 < regularization_weight"));
39  }
40 
41  if (!(0.f <= config.sensitivity)) {
42  throw JUBATUS_EXCEPTION(
43  common::invalid_parameter("0.0 <= sensitivity"));
44  }
45 }
46 
48  : regression_base(storage),
49  sum_(0.f),
50  sq_sum_(0.f),
51  count_(0.f) {
52 }
53 
54 static float squared_norm(const common::sfv_t& fv) {
55  float norm = 0.f;
56  for (size_t i = 0; i < fv.size(); ++i) {
57  norm += fv[i].second * fv[i].second;
58  }
59  return norm;
60 }
61 
62 void passive_aggressive::train(const common::sfv_t& fv, float value) {
63  sum_ += value;
64  sq_sum_ += value * value;
65  count_ += 1;
66  float avg = sum_ / count_;
67  float std_dev = std::sqrt(sq_sum_ / count_ - avg * avg);
68 
69  float predict = estimate(fv);
70  float error = value - predict;
71  float sign_error = error > 0.f ? 1.0f : -1.0f;
72  float loss = sign_error * error - config_.sensitivity * std_dev;
73 
74  if (loss > 0.f) {
76  float coeff = sign_error * std::min(C, loss) / squared_norm(fv);
77  if (!std::isinf(coeff)) {
78  update(fv, coeff);
79  }
80  }
81 }
82 
85  sum_ = 0.f;
86  sq_sum_ = 0.f;
87  count_ = 0.f;
88 }
89 
90 } // namespace regression
91 } // namespace core
92 } // namespace jubatus
jubatus::util::lang::shared_ptr< jubatus::core::storage::storage_base > storage_ptr
passive_aggressive(const config &config, storage_ptr storage)
static float squared_norm(const common::sfv_t &fv)
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
float estimate(const common::sfv_t &fv) const
void update(const common::sfv_t &fv, float coeff)
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
void train(const common::sfv_t &fv, float value)