Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
KUMAZAKI Hiroki committed Mar 23, 2015
1 parent bc14cd1 commit 9771cc0
Show file tree
Hide file tree
Showing 27 changed files with 259 additions and 194 deletions.
8 changes: 5 additions & 3 deletions jubatus/core/anomaly/anomaly_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "../common/exception.hpp"
#include "../common/jsonconfig.hpp"
#include "../nearest_neighbor/nearest_neighbor_factory.hpp"
#include "../unlearner/unlearner_config.hpp"
#include "../unlearner/unlearner_factory.hpp"
#include "../storage/column_table.hpp"
#include "../recommender/recommender_factory.hpp"
Expand Down Expand Up @@ -100,10 +101,11 @@ shared_ptr<anomaly_base> anomaly_factory::create_anomaly(
<< common::exception::error_message(
"unlearner is set but unlearner_parameter is not found"));
}
shared_ptr<unlearner::unlearner_config_base> unl_conf(
unlearner::create_unlearner_config(*conf.unlearner,
*conf.unlearner_parameter));
jubatus::util::lang::shared_ptr<unlearner::unlearner_base> unlearner(
unlearner::create_unlearner(
*conf.unlearner,
*conf.unlearner_parameter));
unlearner::create_unlearner(unl_conf));
return shared_ptr<anomaly_base>(
new light_lof(conf, id, nearest_neighbor_engine, unlearner));
}
Expand Down
14 changes: 5 additions & 9 deletions jubatus/core/classifier/arow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@

#include "classifier_util.hpp"
#include "../common/exception.hpp"
#include "../storage/storage_base.hpp"

using std::string;
using jubatus::core::storage_ptr;

namespace jubatus {
namespace core {
namespace classifier {

arow::arow()
: linear_classifier(storage) {
}

arow::arow(
const classifier_parameter& config,
storage_ptr storage)
: linear_classifier(storage),
config_(config) {
arow::arow(const classifier_parameter& config)
: linear_classifier(),
config_(config) {

if (!(0.f < config.regularization_weight)) {
throw JUBATUS_EXCEPTION(
Expand Down
1 change: 1 addition & 0 deletions jubatus/core/classifier/arow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace jubatus {
namespace core {
namespace classifier {
struct classifier_parameter;

class arow : public linear_classifier {
public:
Expand Down
119 changes: 119 additions & 0 deletions jubatus/core/classifier/classifier_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2012 Preferred Networks and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include "classifier_config.hpp"

#include "jubatus/util/data/serialization.h"
#include "jubatus/util/data/optional.h"
#include "jubatus/util/lang/shared_ptr.h"
#include "../common/jsonconfig.hpp"
#include "../unlearner/unlearner_config.hpp"
#include "../storage/column_table.hpp"
#include "../nearest_neighbor/nearest_neighbor_base.hpp"

using jubatus::util::lang::shared_ptr;

namespace jubatus {
namespace core {
namespace classifier {
classifier_config::classifier_config(const std::string& method,
const common::jsonconfig::config& param) {
typedef util::lang::shared_ptr<detail::classifier_config_base> conf_ptr;

if (method == "perceptron" ||
method == "PA" || method == "passive_aggressive") {
// perceptron passive_aggressive doesn't have parameter
conf_ = conf_ptr(new classifier_parameter(
common::config::config_cast_check<unlearning_classifier_config>(param)));
} else if (name == "PA1" || name == "passive_aggressive_1") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}

unlearning_classifier_conf_ conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new passive_aggressive_1(conf, storage));
} else if (name == "PA2" || name == "passive_aggressive_2") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
unlearning_classifier_config conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new passive_aggressive_2(conf, storage));
} else if (name == "CW" || name == "confidence_weighted") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
unlearning_classifier_config conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new confidence_weighted(conf, storage));
} else if (name == "AROW" || name == "arow") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
unlearning_classifier_config conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new arow(conf, storage));
} else if (name == "NHERD" || name == "normal_herd") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));

}
unlearning_classifier_config conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new normal_herd(conf, storage));
} else if (name == "NN" || name == "nearest_neighbor") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
nearest_neighbor_classifier_config conf
= config_cast_check<nearest_neighbor_classifier_config>(param);
unlearner = create_unlearner(conf);
shared_ptr<storage::column_table> table(new storage::column_table);
shared_ptr<nearest_neighbor::nearest_neighbor_base>
nearest_neighbor_engine(nearest_neighbor::create_nearest_neighbor(
conf.method, conf.parameter, table, ""));
res.reset(
new nearest_neighbor_classifier(nearest_neighbor_engine,
conf.nearest_neighbor_num,
conf.local_sensitivity));
} else {
throw JUBATUS_EXCEPTION(
common::unsupported_method("classifier(" + name + ")"));
}
}
};

} // namespace classifier
} // namespace core
} // namespace jubatus
152 changes: 43 additions & 109 deletions jubatus/core/classifier/classifier_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,157 +19,91 @@

#include "jubatus/util/data/serialization.h"
#include "jubatus/util/data/optional.h"
#include "jubatus/util/lang/shared_ptr.h"
#include "../unlearner/unlearner_config.hpp"

namespace jubatus {
namespace core {
namespace classifier {
namespace detail {
struct classifier_parameter {
classifier_parameter()
: regularization_weight(1.0f) {
}

float regularization_weight;
struct classifier_config_base {
std::string method;
virtual ~classifier_config_base() {}

template<typename Ar>
void serialize(Ar& ar) {
ar & JUBA_NAMED_MEMBER("regularization_weight", regularization_weight);
ar & JUBA_MEMBER(method);
}
};

struct unlearner_config {
jubatus::util::data::optional<std::string> unlearner;
jubatus::util::data::optional<unlearner::unlearner_config_base>
unlearner_parameter;
struct classifier_parameter : public classifier_config_base {
classifier_parameter()
: regularization_weight(1.0f) {
}
float regularization_weight;

template<typename Ar>
void serialize(Ar& ar) {
ar & JUBA_MEMBER(unlearner) & JUBA_MEMBER(unlearner_parameter);
classifier_config_base::serialize(ar);
ar & JUBA_MEMBER(regularization_weight);
}
};

struct unlearning_classifier_config
: public classifier_parameter, unlearner_config {
float get_reguralization_weight(const classifier_config_base& conf) {
const classifier_parameter* c =
dynamic_cast<const classifier_parameter*>(&conf);
if (!c) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"invalid classifier parameter"));
}
return c->regularization_weight;
}

struct unlearning_classifier_config : public classifier_config_base {
template<typename Ar>
void serialize(Ar& ar) {
classifier_parameter::serialize(ar);
unlearner_config::serialize(ar);
classifier_config_base::serialize(ar);
unlearner_config_->serialize(ar);
}
unlearning_classifier_config(const std::string& method,
const common::jsonconfig::config& param)
: classifier_config_base(method) {
param[""]
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
}
util::lang::shared_ptr<unlearner::unlearner_config_base> unlearner_config_;
};

struct nearest_neighbor_classifier_config
: public unlearner_config {
struct nearest_neighbor_classifier_config : public classifier_config_base {
std::string method;
classifier_parameter parameter;
int nearest_neighbor_num;
float local_sensitivity;
util::lang::shared_ptr<unlearner::unlearner_config_base> unlearner_config_;

template<typename Ar>
void serialize(Ar& ar) {
classifier_config_base::serialize(ar);
ar & JUBA_MEMBER(method)
& JUBA_MEMBER(parameter)
& JUBA_MEMBER(nearest_neighbor_num)
& JUBA_MEMBER(local_sensitivity);
unlearner_config::serialize(ar);
unlearner_config_->serialize(ar);
}
};

} // namespace detail

struct classifier_config {
std::string method_;
util::data::optional<detail::unlearner_config> unlearner_conf_;
util::data::optional<unlearning_classifier_config> unlerner_classifier_conf_;
util::data::optional<nearest_neighbor_classifier_config>
nearest_neighbor_conf_;
util::lang::shared_ptr<detail::classifier_config_base> conf_;
classifier_config(const std::string& method,
const common::jsonconfig::config& param)
: method_(method) {
if (method_ == "perceptron") {
// perceptron doesn't have parameter
if (param.type() != jubatus::util::text::json::json::Null) {
unlerner_conf_ = config_cast_check<classifier_config>(param);
}
} else if (name == "PA" || name == "passive_aggressive") {
// passive_aggressive doesn't have parameter
if (param.type() != jubatus::util::text::json::json::Null) {
unlearner_conf = config_cast_check<classifier_config>(param);
}
} else if (name == "PA1" || name == "passive_aggressive_1") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
unlearning_classifier_conf_ conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new passive_aggressive_1(conf, storage));
} else if (name == "PA2" || name == "passive_aggressive_2") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
unlearning_classifier_config conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new passive_aggressive_2(conf, storage));
} else if (name == "CW" || name == "confidence_weighted") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
unlearning_classifier_config conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new confidence_weighted(conf, storage));
} else if (name == "AROW" || name == "arow") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
unlearning_classifier_config conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new arow(conf, storage));
} else if (name == "NHERD" || name == "normal_herd") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
unlearning_classifier_config conf
= config_cast_check<unlearning_classifier_config>(param);
unlearner = create_unlearner(conf);
res.reset(new normal_herd(conf, storage));
} else if (name == "NN" || name == "nearest_neighbor") {
if (param.type() == jubatus::util::text::json::json::Null) {
throw JUBATUS_EXCEPTION(
common::config_exception() << common::exception::error_message(
"parameter block is not specified in config"));
}
nearest_neighbor_classifier_config conf
= config_cast_check<nearest_neighbor_classifier_config>(param);
unlearner = create_unlearner(conf);
shared_ptr<storage::column_table> table(new storage::column_table);
shared_ptr<nearest_neighbor::nearest_neighbor_base>
nearest_neighbor_engine(nearest_neighbor::create_nearest_neighbor(
conf.method, conf.parameter, table, ""));
res.reset(
new nearest_neighbor_classifier(nearest_neighbor_engine,
conf.nearest_neighbor_num,
conf.local_sensitivity));
} else {
throw JUBATUS_EXCEPTION(
common::unsupported_method("classifier(" + name + ")"));
}
}

model
const common::jsonconfig::config& param);
};

} // namespace classifier
Expand Down
Loading

0 comments on commit 9771cc0

Please sign in to comment.