27 #include "jubatus/util/concurrent/lock.h"
28 #include "jubatus/util/lang/bind.h"
30 #include "../common/exception.hpp"
40 namespace classifier {
43 : storage_(storage), mixable_storage_(storage_) {
59 jubatus::util::lang::shared_ptr<unlearner::unlearner_base>
61 label_unlearner->set_callback(
62 jubatus::util::lang::bind(
63 delete_label_wrapper,
this, jubatus::util::lang::_1));
75 for (map_feature_val1_t::const_iterator it = ret.begin(); it != ret.end();
84 float max_score = -FLT_MAX;
86 for (vector<classify_result_elem>::const_iterator it = result.begin();
87 it != result.end(); ++it) {
88 if (it == result.begin() || it->score > max_score) {
89 max_score = it->score;
90 max_class = it->label;
110 bool result =
storage_->set_label(label);
120 status[
"storage"] =
storage_->type();
126 const string& pos_label,
127 const string& neg_label) {
128 storage_->bulk_update(sfv, step_width, pos_label, neg_label);
136 float max_score = -FLT_MAX;
138 for (vector<classify_result_elem>::const_iterator it = scores.begin();
139 it != scores.end(); ++it) {
140 if (it->label == label) {
143 if (it->score > max_score || it == scores.begin()) {
144 max_score = it->score;
145 max_class = it->label;
154 string& incorrect_label)
const {
157 float correct_score = 0.f;
158 float incorrect_score = 0.f;
159 for (vector<classify_result_elem>::const_iterator it = scores.begin();
160 it != scores.end(); ++it) {
161 if (it->label == label) {
162 correct_score = it->score;
163 }
else if (it->label == incorrect_label) {
164 incorrect_score = it->score;
167 return incorrect_score - correct_score;
173 string& incorrect_label,
175 float margin =
calc_margin(sfv, label, incorrect_label);
178 util::concurrent::scoped_lock lk(
storage_->get_lock());
179 for (
size_t i = 0; i < sfv.size(); ++i) {
180 const string& feature = sfv[i].first;
181 const float val = sfv[i].second;
183 storage_->get2_nolock(feature, weight_covars);
184 float label_covar = 1.f;
185 float incorrect_label_covar = 1.f;
186 for (
size_t j = 0; j < weight_covars.size(); ++j) {
187 if (weight_covars[j].first == label) {
188 label_covar = weight_covars[j].second.v2;
189 }
else if (weight_covars[j].first == incorrect_label) {
190 incorrect_label_covar = weight_covars[j].second.v2;
193 var += (label_covar + incorrect_label_covar) * val * val;
200 for (
size_t i = 0; i < fv.size(); ++i) {
201 ret += fv[i].second * fv[i].second;
228 "no more space available to add new label: " + label));
234 bool result =
storage_->delete_label(label);
251 return storage_->delete_label_nolock(label);
jubatus::util::lang::shared_ptr< jubatus::core::storage::storage_base > storage_ptr
std::vector< classify_result_elem > classify_result
bool set_label(const std::string &label)
static float squared_norm(const common::sfv_t &sfv)
bool unlearn_label(const std::string &label)
jubatus::util::data::unordered_map< std::string, val1_t > map_feature_val1_t
void set_label_unlearner(jubatus::util::lang::shared_ptr< unlearner::unlearner_base > label_unlearner)
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)
void unpack(msgpack::object o)
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
virtual ~linear_classifier()
#define JUBATUS_EXCEPTION(e)
void check_touchable(const std::string &label)
framework::mixable * get_mixable()
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > unlearner_
void pack(framework::packer &pk) const
std::string classify(const common::sfv_t &fv) const
std::vector< std::string > get_labels() const
framework::linear_function_mixer mixable_storage_
linear_classifier(storage_ptr storage)
std::vector< std::pair< std::string, float > > sfv_t
virtual void get_status(std::map< std::string, std::string > &status) const
void touch(const std::string &label)
std::string get_largest_incorrect_label(const common::sfv_t &sfv, const std::string &label, classify_result &scores) const
std::vector< std::pair< std::string, val2_t > > feature_val2_t
bool delete_label(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)